<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
# file: record_to_tarball.pm	G. Moody         16 March 2009
#				Last revised:	  25 May 2016
# _____________________________________________________________________________
# record_to_tarball module for PhysioBank's Automated Teller Machine
# Copyright (C) 2009-2010 George B. Moody

use File::Basename;

sub mod_record_to_tarball {
    # Set the path, URL, and final output filenames for this request.
    $ubase = "/atm/$database/$record/export";
    $pbase = "/ptmp$ubase";
    $orec = basename($record);
    $ptar = "$pbase/$orec.tar.gz";
    $ptoc = "$ptar.toc";
    $utar = "$ubase/$orec.tar.gz";

    # If results for this request are already in the cache, return them.
    if (-s $ptoc &amp;&amp; -s $ptar) {
	mod_record_to_tarball_out();
	return;
    }

    # Make sure the output has a place to go.
    mkpath($pbase);
    $ttar = "$ptar.$$";
    $ttoc = "$ptoc.$$";

    unless (fork) {
	open(STDOUT, "&gt;$ttoc");
	chdir("/home/physionet/html/physiobank/database/$database");
	exec "$TAR cfvz $ttar $record*";
    }

    # Wait until tar is finished.
    wait;

    if (-s $ptoc) {  # there was a race, and this process lost!
	unlink($ttar);
	unlink($ttoc);
    }
    else {
	rename($ttar, $ptar);
	rename($ttoc, $ptoc);
    }
    mod_record_to_tarball_out();
}

sub mod_record_to_tarball_out {
    if (-s $ptoc) {
	$tsize = -s $ptar;
	print "Download &lt;a href=$utar&gt;&lt;tt&gt;$orec.tar.gz&lt;/tt&gt;&lt;/a&gt;",
	      " ($tsize bytes).  Once you",
	      " have completed the download, unpack the tarball by running the",
	      " command:&lt;pre&gt;tar xfvz $orec.tar.gz&lt;/pre&gt;",
	      " This command unpacks the contents of the tarball into the",
	      " current directory.",
	      "&lt;p&gt;The contents of &lt;tt&gt;$orec.tar.gz&lt;/tt&gt; are listed below:",
	      "&lt;hr&gt;&lt;pre&gt;";
	show_file($ptoc);
	print "&lt;/pre&gt;";
    }
    else {
	print "A tarball of $database/$record could not be made.  Please",
              " go &lt;a href=/physiobank/database/$database&gt;here&lt;/a&gt; to download",
	      " the individual files of record $record.";
    }
}

1;
</pre></body></html>