2 * This file has been modified for the cdrkit suite.
4 * The behaviour and appearence of the program code below can differ to a major
5 * extent from the version distributed by the original author(s).
7 * For details, see Changelog file distributed with the cdrkit package. If you
8 * received this file from another source then ask the distributing person for
9 * a log of modifications.
13 #ident "$Id: walk.c,v 1.7 2006/07/04 04:57:42 hpa Exp $"
14 /* ----------------------------------------------------------------------- *
16 * Copyright 2001-2006 H. Peter Anvin - All Rights Reserved
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
21 * USA; either version 2 of the License, or (at your option) any later
22 * version; incorporated herein by reference.
24 * ----------------------------------------------------------------------- */
29 * Functions to walk the file tree
32 #include "mkzftree.h" /* Must be included first! */
42 #include <sys/types.h>
46 static int munge_file(const char *inpath, const char *outpath,
47 const char *cribpath, struct stat *st)
54 struct compressed_file_header cfh;
56 /* Compare as much as we realistically can */
57 if ( !stat(cribpath, &cst) &&
58 st->st_mode == cst.st_mode &&
59 st->st_uid == cst.st_uid &&
60 st->st_gid == cst.st_gid &&
61 st->st_mtime == cst.st_mtime ) {
62 if ( (in = fopen(cribpath, "rb")) ) {
63 int e = fread(&cfh, 1, sizeof cfh, in);
65 /* Attempt to restore the atime */
66 copytime(cribpath, &cst);
68 if ( (e == sizeof cfh &&
69 !memcmp(cfh.magic, zisofs_magic, sizeof zisofs_magic) &&
70 (off_t)get_731(cfh.uncompressed_len) == st->st_size) ||
71 (st->st_size == cst.st_size &&
72 (e < (int)(sizeof zisofs_magic) ||
73 memcmp(cfh.magic, zisofs_magic, sizeof zisofs_magic))) ) {
74 /* File is cribbable. Steal it. */
75 if ( !link(cribpath, outpath) ) {
76 message(vl_crib, "crib: %s -> %s\n", cribpath, outpath);
77 copytime(outpath, st); /* Set the the atime */
85 in = fopen(inpath, "rb");
88 out = fopen(outpath, "wb");
94 if ( spawn_worker() ) {
95 err = opt.munger(in, out, st->st_size);
99 chown(outpath, st->st_uid, st->st_gid);
100 chmod(outpath, st->st_mode);
101 copytime(outpath, st);
112 int munge_tree(const char *intree, const char *outtree, const char *cribtree)
114 char *in_path, *out_path, *crib_path;
115 char *in_file, *out_file, *crib_file;
117 struct dirent *dirent;
121 /* Construct buffers with the common filename prefix, and point to the end */
123 in_path = xmalloc(strlen(intree) + NAME_MAX + 2);
124 strcpy(in_path, intree);
125 in_file = strchr(in_path, '\0');
128 out_path = xmalloc(strlen(outtree) + NAME_MAX + 2);
129 strcpy(out_path, outtree);
130 out_file = strchr(out_path, '\0');
134 crib_path = xmalloc(strlen(cribtree) + NAME_MAX + 2);
135 strcpy(crib_path, cribtree);
136 crib_file = strchr(crib_path, '\0');
139 crib_path = crib_file = NULL;
142 /* Get directory information */
143 if ( stat(intree, &dirst) ) {
144 message(vl_error, "%s: Failed to stat directory %s: %s\n",
145 program, intree, strerror(errno));
149 /* Open the directory */
150 thisdir = opendir(intree);
152 message(vl_error, "%s: Failed to open directory %s: %s\n",
153 program, intree, strerror(errno));
157 /* Create output directory */
158 if ( mkdir(outtree, 0700) ) {
159 message(vl_error, "%s: Cannot create output directory %s: %s\n",
160 program, outtree, strerror(errno));
164 while ( (dirent = readdir(thisdir)) != NULL ) {
165 if ( !strcmp(dirent->d_name, ".") ||
166 !strcmp(dirent->d_name, "..") )
167 continue; /* Ignore . and .. */
169 strcpy(in_file, dirent->d_name);
170 strcpy(out_file, dirent->d_name);
172 strcpy(crib_file, dirent->d_name);
174 err = munge_entry(in_path, out_path, crib_path, &dirst);
187 int munge_entry(const char *in_path, const char *out_path,
188 const char *crib_path, const struct stat *dirst)
193 message(vl_filename, "%s -> %s\n", in_path, out_path);
195 if ( lstat(in_path, &st) ) {
196 message(vl_error, "%s: Failed to stat file %s: %s\n",
197 program, in_path, strerror(errno));
201 if ( S_ISREG(st.st_mode) ) {
202 if ( st.st_nlink > 1 ) {
204 const char *linkname;
206 if ( (linkname = hash_find_file(&st)) != NULL ) {
207 /* We've seen it before, hard link it */
209 if ( link(linkname, out_path) ) {
210 message(vl_error, "%s: hard link %s -> %s failed: %s\n",
211 program, out_path, linkname, strerror(errno));
215 /* First encounter, compress and enter into hash */
216 if ( (err = munge_file(in_path, out_path, crib_path, &st)) != 0 ) {
217 message(vl_error, "%s: %s: %s", program, in_path, strerror(errno));
220 hash_insert_file(&st, out_path);
223 /* Singleton file; no funnies */
224 if ( (err = munge_file(in_path, out_path, crib_path, &st)) != 0 ) {
225 message(vl_error, "%s: %s: %s", program, in_path, strerror(errno));
229 } else if ( S_ISDIR(st.st_mode) ) {
230 /* Recursion: see recursion */
232 (!opt.onefs || (dirst && dirst->st_dev == st.st_dev)) ) {
233 if ( (err = munge_tree(in_path, out_path, crib_path)) != 0 )
235 } else if ( opt.do_mkdir ) {
236 /* Create stub directories */
237 if ( mkdir(out_path, st.st_mode) ) {
238 message(vl_error, "%s: %s: %s", program, out_path, strerror(errno));
242 } else if ( S_ISLNK(st.st_mode) ) {
245 #define BUFFER_SLACK PATH_MAX
247 #define BUFFER_SLACK BUFSIZ
249 int buffer_len = st.st_size + BUFFER_SLACK + 1;
250 char *buffer = xmalloc(buffer_len);
251 if ( (chars = readlink(in_path, buffer, buffer_len)) < 0 ) {
252 message(vl_error, "%s: readlink failed for %s: %s\n",
253 program, in_path, strerror(errno));
256 buffer[chars] = '\0';
257 if ( symlink(buffer, out_path) ) {
258 message(vl_error, "%s: symlink %s -> %s failed: %s\n",
259 program, out_path, buffer, strerror(errno));
264 if ( st.st_nlink > 1 ) {
266 const char *linkname;
268 if ( (linkname = hash_find_file(&st)) != NULL ) {
269 /* We've seen it before, hard link it */
271 if ( link(linkname, out_path) ) {
272 message(vl_error, "%s: hard link %s -> %s failed: %s\n",
273 program, out_path, linkname, strerror(errno));
277 /* First encounter, create and enter into hash */
278 if ( mknod(out_path, st.st_mode, st.st_rdev) ) {
279 message(vl_error, "%s: mknod failed for %s: %s\n",
280 program, out_path, strerror(errno));
283 hash_insert_file(&st, out_path);
286 /* Singleton node; no funnies */
287 if ( mknod(out_path, st.st_mode, st.st_rdev) ) {
288 message(vl_error, "%s: mknod failed for %s: %s\n",
289 program, out_path, strerror(errno));
295 /* This is done by munge_file() for files */
296 if ( !S_ISREG(st.st_mode) ) {
298 if ( lchown(out_path, st.st_uid, st.st_gid) && opt.sloppy && !err ) {
299 message(vl_error, "%s: %s: %s", program, out_path, strerror(errno));
303 if ( !S_ISLNK(st.st_mode) ) {
305 if ( chown(out_path, st.st_uid, st.st_gid) && !opt.sloppy && !err ) {
306 message(vl_error, "%s: %s: %s", program, out_path, strerror(errno));
310 if ( chmod(out_path, st.st_mode) && !opt.sloppy && !err ) {
311 message(vl_error, "%s: %s: %s", program, out_path, strerror(errno));
314 if ( copytime(out_path, &st) && !opt.sloppy && !err ) {
315 message(vl_error, "%s: %s: %s", program, out_path, strerror(errno));