summaryrefslogtreecommitdiff
path: root/3rd-party/zisofs_tools/walk.c
blob: 3b62f73a7d63563dc4ed5362d8ce0ccc1524dbe7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * This file has been modified for the cdrkit suite.
 *
 * The behaviour and appearence of the program code below can differ to a major
 * extent from the version distributed by the original author(s).
 *
 * For details, see Changelog file distributed with the cdrkit package. If you
 * received this file from another source then ask the distributing person for
 * a log of modifications.
 *
 */

#ident "$Id: walk.c,v 1.7 2006/07/04 04:57:42 hpa Exp $"
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2001-2006 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
 *   USA; either version 2 of the License, or (at your option) any later
 *   version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * walk.c
 *
 * Functions to walk the file tree
 */

#include "mkzftree.h"		/* Must be included first! */

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "iso9660.h"

static int munge_file(const char *inpath, const char *outpath,
		      const char *cribpath, struct stat *st)
{
  FILE *in, *out;
  int err = 0;

  if ( cribpath ) {
    struct stat cst;
    struct compressed_file_header cfh;

    /* Compare as much as we realistically can */
    if ( !stat(cribpath, &cst) &&
	 st->st_mode == cst.st_mode &&
	 st->st_uid == cst.st_uid &&
	 st->st_gid == cst.st_gid &&
	 st->st_mtime == cst.st_mtime ) {
      if ( (in = fopen(cribpath, "rb")) ) {
	int e = fread(&cfh, 1, sizeof cfh, in);
	fclose(in);
	/* Attempt to restore the atime */
	copytime(cribpath, &cst);

	if ( (e == sizeof cfh &&
	      !memcmp(cfh.magic, zisofs_magic, sizeof zisofs_magic) &&
	      (off_t)get_731(cfh.uncompressed_len) == st->st_size) ||
	     (st->st_size == cst.st_size &&
	      (e < (int)(sizeof zisofs_magic) ||
	       memcmp(cfh.magic, zisofs_magic, sizeof zisofs_magic))) ) {
	  /* File is cribbable.  Steal it. */
	  if ( !link(cribpath, outpath) ) {
	    message(vl_crib, "crib: %s -> %s\n", cribpath, outpath);
	    copytime(outpath, st);	/* Set the the atime */
	    return 0;
	  }
	}
      }
    }
  }

  in = fopen(inpath, "rb");
  if ( !in )
    return EX_NOINPUT;
  out = fopen(outpath, "wb");
  if ( !out ) {
    fclose(in);
    return EX_CANTCREAT;
  }

  if ( spawn_worker() ) {
    err = opt.munger(in, out, st->st_size);
    fclose(in);
    fclose(out);
    
    chown(outpath, st->st_uid, st->st_gid);
    chmod(outpath, st->st_mode);
    copytime(outpath, st);
    
    end_worker(err);
  } else {
    fclose(in);
    fclose(out);
  }

  return err;
}

int munge_tree(const char *intree, const char *outtree, const char *cribtree)
{
  char *in_path, *out_path, *crib_path;
  char *in_file, *out_file, *crib_file;
  DIR *thisdir;
  struct dirent *dirent;
  struct stat dirst;
  int err = 0;
  
  /* Construct buffers with the common filename prefix, and point to the end */

  in_path = xmalloc(strlen(intree) + NAME_MAX + 2);
  strcpy(in_path, intree);
  in_file = strchr(in_path, '\0');
  *in_file++ = '/';

  out_path = xmalloc(strlen(outtree) + NAME_MAX + 2);
  strcpy(out_path, outtree);
  out_file = strchr(out_path, '\0');
  *out_file++ = '/';

  if ( cribtree ) {
    crib_path = xmalloc(strlen(cribtree) + NAME_MAX + 2);
    strcpy(crib_path, cribtree);
    crib_file = strchr(crib_path, '\0');
    *crib_file++ = '/';
  } else {
    crib_path = crib_file = NULL;
  }

  /* Get directory information */
  if ( stat(intree, &dirst) ) {
    message(vl_error, "%s: Failed to stat directory %s: %s\n",
	    program, intree, strerror(errno));
    return EX_NOINPUT;
  }

  /* Open the directory */
  thisdir = opendir(intree);
  if ( !thisdir ) {
    message(vl_error, "%s: Failed to open directory %s: %s\n",
	    program, intree, strerror(errno));
    return EX_NOINPUT;
  }

  /* Create output directory */
  if ( mkdir(outtree, 0700) ) {
    message(vl_error, "%s: Cannot create output directory %s: %s\n",
	    program, outtree, strerror(errno));
    return EX_CANTCREAT;
  }

  while ( (dirent = readdir(thisdir)) != NULL ) {
    if ( !strcmp(dirent->d_name, ".") ||
	 !strcmp(dirent->d_name, "..") )
      continue;			/* Ignore . and .. */
    
    strcpy(in_file, dirent->d_name);
    strcpy(out_file, dirent->d_name);
    if ( crib_file )
      strcpy(crib_file, dirent->d_name);
    
    err = munge_entry(in_path, out_path, crib_path, &dirst);
    if ( err )
      break;
  }
  closedir(thisdir);
  
  free(in_path);
  free(out_path);
  
  return err;
}


int munge_entry(const char *in_path, const char *out_path,
		const char *crib_path, const struct stat *dirst)
{
  struct stat st;
  int err = 0;

  message(vl_filename, "%s -> %s\n", in_path, out_path);

  if ( lstat(in_path, &st) ) {
    message(vl_error, "%s: Failed to stat file %s: %s\n",
	    program, in_path, strerror(errno));
    return EX_NOINPUT;
  }
  
  if ( S_ISREG(st.st_mode) ) {
    if ( st.st_nlink > 1 ) {
      /* Hard link. */
      const char *linkname;
      
      if ( (linkname = hash_find_file(&st)) != NULL ) {
	/* We've seen it before, hard link it */
	
	if ( link(linkname, out_path) ) {
	  message(vl_error, "%s: hard link %s -> %s failed: %s\n",
		  program, out_path, linkname, strerror(errno));
	  return EX_CANTCREAT;
	}
      } else {
	/* First encounter, compress and enter into hash */
	if ( (err = munge_file(in_path, out_path, crib_path, &st)) != 0 ) {
	  message(vl_error, "%s: %s: %s", program, in_path, strerror(errno));
	  return err;
	}
	hash_insert_file(&st, out_path);
      }
    } else {
      /* Singleton file; no funnies */
      if ( (err = munge_file(in_path, out_path, crib_path, &st)) != 0 ) {
	message(vl_error, "%s: %s: %s", program, in_path, strerror(errno));
	return err;
      }
    }
  } else if ( S_ISDIR(st.st_mode) ) {
    /* Recursion: see recursion */
    if ( !opt.onedir &&
	 (!opt.onefs || (dirst && dirst->st_dev == st.st_dev)) ) {
      if ( (err = munge_tree(in_path, out_path, crib_path)) != 0 )
	return err;
    } else if ( opt.do_mkdir ) {
      /* Create stub directories */
      if ( mkdir(out_path, st.st_mode) ) {
	message(vl_error, "%s: %s: %s", program, out_path, strerror(errno));
	return EX_CANTCREAT;
      }
    }
  } else if ( S_ISLNK(st.st_mode) ) {
    int chars;
#ifdef PATH_MAX
#define BUFFER_SLACK PATH_MAX
#else
#define BUFFER_SLACK BUFSIZ
#endif
    int buffer_len = st.st_size + BUFFER_SLACK + 1;
    char *buffer = xmalloc(buffer_len);
    if ( (chars = readlink(in_path, buffer, buffer_len)) < 0 ) {
      message(vl_error, "%s: readlink failed for %s: %s\n",
	      program, in_path, strerror(errno));
      return EX_NOINPUT;
    }
    buffer[chars] = '\0';
    if ( symlink(buffer, out_path) ) {
      message(vl_error, "%s: symlink %s -> %s failed: %s\n",
	      program, out_path, buffer, strerror(errno));
      return EX_CANTCREAT;
    }
    free(buffer);
  } else {
    if ( st.st_nlink > 1 ) {
      /* Hard link. */
      const char *linkname;
      
      if ( (linkname = hash_find_file(&st)) != NULL ) {
	/* We've seen it before, hard link it */
	
	if ( link(linkname, out_path) ) {
	  message(vl_error, "%s: hard link %s -> %s failed: %s\n",
		  program, out_path, linkname, strerror(errno));
	  return EX_CANTCREAT;
	}
      } else {
	/* First encounter, create and enter into hash */
	if ( mknod(out_path, st.st_mode, st.st_rdev) ) {
	  message(vl_error, "%s: mknod failed for %s: %s\n",
		  program, out_path, strerror(errno));
	  return EX_CANTCREAT;
	}
	hash_insert_file(&st, out_path);
      }
    } else {
      /* Singleton node; no funnies */
      if ( mknod(out_path, st.st_mode, st.st_rdev) ) {
	message(vl_error, "%s: mknod failed for %s: %s\n",
		program, out_path, strerror(errno));
	return EX_CANTCREAT;
      }
    }
  }
  
  /* This is done by munge_file() for files */
  if ( !S_ISREG(st.st_mode) ) {
#ifdef HAVE_LCHOWN
    if ( lchown(out_path, st.st_uid, st.st_gid) && opt.sloppy && !err ) {
      message(vl_error, "%s: %s: %s", program, out_path, strerror(errno));
      err = EX_CANTCREAT;
    }
#endif
    if ( !S_ISLNK(st.st_mode) ) {
#ifndef HAVE_LCHOWN
      if ( chown(out_path, st.st_uid, st.st_gid) && !opt.sloppy && !err ) {
	message(vl_error, "%s: %s: %s", program, out_path, strerror(errno));
	err = EX_CANTCREAT;
      }
#endif
      if ( chmod(out_path, st.st_mode) && !opt.sloppy && !err ) {
	message(vl_error, "%s: %s: %s", program, out_path, strerror(errno));
	err = EX_CANTCREAT;
      }
      if ( copytime(out_path, &st) && !opt.sloppy && !err ) {
	message(vl_error, "%s: %s: %s", program, out_path, strerror(errno));
	err = EX_CANTCREAT;
      }
    }
  }

  return err;
}