3 # Copyright (C) 2006 Jason Woofenden
5 # This file is part of wfpl.
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)
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
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
22 # This file is for making a tar archive out of some strings you pass. See
26 # create a file (including contents)
27 function write_file($name, $data) {
28 $fd = fopen($name, 'w');
30 die("Failed to open file: '$name' for writing");
32 $temp = fwrite($fd, $data);
38 # create (and output) a tar archive. Don't put in any symbols in filenames.
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)) {
51 mkdir("$tmpdir/$dirname");
52 foreach($files as $filename => $file_data) {
53 if(substr($filename, -3) == ' ->') {
54 $filename = substr($filename, 0, -3);
59 $filename_fixed = ereg_replace('[^a-zA-Z0-9_.-]', '', $filename);
60 if($filename != $filename_fixed) {
61 die("Invalid filename for tar archive");
64 $target = ereg_replace('[^a-zA-Z0-9_./-]', '', $file_data);
65 system("/bin/ln -s $file_data \"$tmpdir/$dirname/$filename\"");
67 write_file("$tmpdir/$dirname/$filename", $file_data);
71 if(function_exists($extra)) {
72 $extra("$tmpdir/$dirname");
75 header("Content-type: application/x-gzip");
76 passthru("tar -C $tmpdir -czf - $dirname/");
77 system("/bin/rm -rf '$tmpdir/$dirname'");
80 # like make_tar above, except it includes a copy of code/wfpl
81 function make_wfpl_tar($dirname, $files) {
82 make_tar($dirname, $files, 'add_wfpl_dir');
85 function add_wfpl_dir($dir) {
87 system("rsync -plr --exclude=\".git\" --exclude=\"*.swp\" 'code/wfpl/' '$dir/code/wfpl/'", $return_code);
88 if($return_code != 0) {
89 die("ERROR: while trying to copy wfpl into archive: rsync returned $return_code");