JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
changed licence to lgpl, metaform includes wfpl in tarbal
[wfpl.git] / tar.php
1 <?php
2
3 #  Copyright (C) 2006 Jason Woofenden
4 #
5 #  This file is part of wfpl.
6 #
7 #  wfpl is free software; you can redistribute it and/or modify it under the
8 #  terms of the GNU Lesser General Public License as published by the Free
9 #  Software Foundation; either version 2.1 of the License, or (at your option)
10 #  any later version.
11 #
12 #  wfpl is distributed in the hope that it will be useful, but WITHOUT ANY
13 #  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 #  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
15 #  more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with wfpl; if not, write to the Free Software Foundation, Inc., 51
19 #  Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
21
22 # This file is for making a tar archive out of some strings you pass. See
23 # make_tar()
24
25
26 # create a file (including contents)
27 function write_file($name, $data) {
28         $fd = fopen($name, 'w');
29         if($fd === false) {
30                 die("Failed to open file: '$name' for writing");
31         }
32         $temp = fwrite($fd, $data);
33         fclose($fd);
34         return $temp;
35 }
36
37
38 # create (and output) a tar archive. Don't put in any symbols in filenames.
39 #
40 # parameters:
41 #    $dirname: the name of the tar file (sans "tgz"). Also the name of the directory within.
42 #    $files: a hash. The keys are the filenames, the values the file data
43 #    $extra: (optional) a function to be called right before tar-ing.
44 function make_tar($dirname, $files, $extra = '') {
45         $tmpdir = '/tmp/make_tar';
46         $dirname = ereg_replace('[^a-z0-9_-]', '', $dirname);
47         if($dirname == '') $dirname = 'foo';
48         if(!file_exists($tmpdir)) {
49                 mkdir($tmpdir);
50         }
51         mkdir("$tmpdir/$dirname");
52         foreach($files as $filename => $file_data) {
53                 $filename_fixed = ereg_replace('[^a-zA-Z0-9_.-]', '', $filename);
54                 if($filename != $filename_fixed) {
55                         die("Invalid filename for tar archive");
56                 }
57                 write_file("$tmpdir/$dirname/$filename", $file_data);
58         }
59
60         if(function_exists($extra)) {
61                 $extra("$tmpdir/$dirname");
62         }
63
64         header("Content-type: application/x-gzip");
65         passthru("tar -C $tmpdir -czf - $dirname/");
66         system("/bin/rm -rf '$tmpdir/$dirname'");
67 }
68
69 # like make_tar above, except it includes a copy of code/wfpl
70 function make_wfpl_tar($dirname, $files) {
71         make_tar($dirname, $files, 'add_wfpl_dir');
72 }
73
74 function add_wfpl_dir($dir) {
75         mkdir("$dir/code");
76         system("/bin/cp -HRp 'code/wfpl' '$dir/code/'", $return_code);
77         if($return_code != 0) {
78                 die("ERROR: while trying to copy wfpl into archive: cp returned $return_code");
79         }
80         system("/bin/rm -rf '$dir/code/wfpl/.git'");
81 }