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 $filename_fixed = ereg_replace('[^a-zA-Z0-9_.-]', '', $filename);
54 if($filename != $filename_fixed) {
55 die("Invalid filename for tar archive");
57 write_file("$tmpdir/$dirname/$filename", $file_data);
60 if(function_exists($extra)) {
61 $extra("$tmpdir/$dirname");
64 header("Content-type: application/x-gzip");
65 passthru("tar -C $tmpdir -czf - $dirname/");
66 system("/bin/rm -rf '$tmpdir/$dirname'");
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');
74 function add_wfpl_dir($dir) {
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");
80 system("/bin/rm -rf '$dir/code/wfpl/.git'");