3 # Copyright (C) 2006 Jason Woofenden
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # This file is for making a tar archive out of some strings you pass. See
23 # create a file (including contents)
24 function write_file($name, $data) {
25 $fd = fopen($name, 'w');
27 die("Failed to open file: '$name' for writing");
29 $temp = fwrite($fd, $data);
35 # create (and output) a tar archive. Don't put in any symbols in filenames.
38 # $dirname: the name of the tar file (sans "tgz"). Also the name of the directory within.
39 # $files: a hash. The keys are the filenames, the values the file data
40 # $extra: (optional) a function to be called right before tar-ing.
41 function make_tar($dirname, $files, $extra = '') {
42 $tmpdir = '/tmp/make_tar';
43 $dirname = ereg_replace('[^a-z0-9_-]', '', $dirname);
44 if($dirname == '') $dirname = 'foo';
45 if(!file_exists($tmpdir)) {
48 mkdir("$tmpdir/$dirname");
49 foreach($files as $filename => $file_data) {
50 if(substr($filename, -3) == ' ->') {
51 $filename = substr($filename, 0, -3);
56 $filename_fixed = ereg_replace('[^a-zA-Z0-9_.-]', '', $filename);
57 if($filename != $filename_fixed) {
58 die("Invalid filename for tar archive");
61 $target = ereg_replace('[^a-zA-Z0-9_./-]', '', $file_data);
62 system("/bin/ln -s $file_data \"$tmpdir/$dirname/$filename\"");
64 write_file("$tmpdir/$dirname/$filename", $file_data);
68 if(function_exists($extra)) {
69 $extra("$tmpdir/$dirname");
72 header("Content-type: application/x-gzip");
73 passthru("tar -C $tmpdir -czf - $dirname/");
74 system("/bin/rm -rf '$tmpdir/$dirname'");
77 # like make_tar above, except it includes a copy of code/wfpl
78 function make_wfpl_tar($dirname, $files) {
79 make_tar($dirname, $files, 'add_wfpl_dir');
82 function add_wfpl_dir($dir) {
84 system("rsync -plr --exclude=\".git\" --exclude=\"*.swp\" 'code/wfpl/' '$dir/code/wfpl/'", $return_code);
85 if($return_code != 0) {
86 die("ERROR: while trying to copy wfpl into archive: rsync returned $return_code");