summaryrefslogtreecommitdiff
path: root/genisoimage/files.c
blob: d17a2c7ae9effc5cd1a753f13d712f65a35379ca (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * 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.
 *
 */

/* @(#)files.c	1.12 04/03/04 joerg */
/*
 * File files.c - Handle ADD_FILES related stuff.
 *
 * Written by Eric Youngdale (1993).
 *
 * Copyright 1993 Yggdrasil Computing, Incorporated
 *
 * 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; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* ADD_FILES changes made by Ross Biro biro@yggdrasil.com 2/23/95 */

#include <mconfig.h>
#include "genisoimage.h"
#include <errno.h>
#include <schily.h>
#include <ctype.h>

#ifdef ADD_FILES

void	add_one_file(char *addpath, char *path);
void	add_file_list(int argc, char **argv, int ind);
void	add_file(char *filename);
char *look_up_addition(char **newpath, char *path, struct dirent **de);
void	nuke_duplicates(char *path, struct dirent **de);
struct dirent  *readdir_add_files(char **pathp, char *path, DIR *dir);

struct file_adds {
	char			*name;
	struct file_adds	*child;
	struct file_adds	*next;
	int			add_count;
	int			used;
	union diru {
	/*
	 * XXX Struct dirent is not guaranteed to be any size on a POSIX
	 * XXX compliant system.
	 * XXX We need to allocate enough space here, to allow the hacky
	 * XXX code in tree.c made by Ross Biro biro@yggdrasil.com
	 * XXX to work on operating systems other than Linux :-(
	 * XXX Changes made by Joerg Schilling
	 * XXX joerg@schily.isdn.cs.tu-berlin.de
	 * XXX to prevent core dumps on Solaris.
	 * XXX Space allocated:
	 * XXX		1024 bytes == NAME_MAX
	 * XXX	+	2   bytes for directory record length
	 * XXX	+	2*8 bytes for inode number & offset (64 for future exp)
	 */
		struct dirent	de;
		char		dspace[NAME_MAX + 2 + 2 * 8];
	} du;
	struct {
		char		*path;
		char		*name;
	} *adds;
};
extern struct file_adds *root_file_adds;

/*
 * FIXME(eric) - the file adding code really doesn't work very well
 * at all.  We should differentiate between adding directories, and adding
 * single files, as adding a full directory affects how we should be
 * searching for things.  Ideally what we should do is make two passes
 * through the local filesystem - one to figure out what trees we need
 * to scan (and merge in any additions at that point), and the second to
 * actually fill out each structure with the appropriate contents.
 */

struct file_adds *root_file_adds = NULL;

void
add_one_file(char *addpath, char *path)
{
	char			*cp;
	char			*name;
	struct file_adds	*f;
	struct file_adds	*tmp;

	f = root_file_adds;
	tmp = NULL;

	name = strrchr(addpath, PATH_SEPARATOR);
	if (name == NULL) {
		name = addpath;
	} else {
		name++;
	}

	cp = strtok(addpath, SPATH_SEPARATOR);

	while (cp != NULL && strcmp(name, cp)) {
		if (f == NULL) {
			root_file_adds = e_malloc(sizeof (*root_file_adds));
			f = root_file_adds;
			f->name = NULL;
			f->child = NULL;
			f->next = NULL;
			f->add_count = 0;
			f->adds = NULL;
			f->used = 0;
		}
		if (f->child) {
			for (tmp = f->child; tmp->next != NULL;
							tmp = tmp->next) {
				if (strcmp(tmp->name, cp) == 0) {
					f = tmp;
					goto next;
				}
			}
			if (strcmp(tmp->name, cp) == 0) {
				f = tmp;
				goto next;
			}
			/* add a new node. */
			tmp->next = e_malloc(sizeof (*tmp->next));
			f = tmp->next;
			f->name = strdup(cp);
			f->child = NULL;
			f->next = NULL;
			f->add_count = 0;
			f->adds = NULL;
			f->used = 0;
		} else {
			/* no children. */
			f->child = e_malloc(sizeof (*f->child));
			f = f->child;
			f->name = strdup(cp);
			f->child = NULL;
			f->next = NULL;
			f->add_count = 0;
			f->adds = NULL;
			f->used = 0;

		}
next:
		cp = strtok(NULL, SPATH_SEPARATOR);
	}
	/* Now f if non-null points to where we should add things */
	if (f == NULL) {
		root_file_adds = e_malloc(sizeof (*root_file_adds));
		f = root_file_adds;
		f->name = NULL;
		f->child = NULL;
		f->next = NULL;
		f->add_count = 0;
		f->adds = NULL;
	}
	/* Now f really points to where we should add this name. */
	f->add_count++;
	f->adds = realloc(f->adds, sizeof (*f->adds) * f->add_count);
	f->adds[f->add_count - 1].path = strdup(path);
	f->adds[f->add_count - 1].name = strdup(name);
}

/*
 * Function:	add_file_list
 *
 * Purpose:	Register an add-in file.
 *
 * Arguments:
 */
void
add_file_list(int argc, char **argv, int ind)
{
	char	*ptr;
	char	*dup_arg;

	while (ind < argc) {
		dup_arg = strdup(argv[ind]);
		ptr = strchr(dup_arg, '=');
		if (ptr == NULL) {
			free(dup_arg);
			return;
		}
		*ptr = 0;
		ptr++;
		add_one_file(dup_arg, ptr);
		free(dup_arg);
		ind++;
	}
}

void
add_file(char *filename)
{
	char	buff[PATH_MAX];
	FILE	*f;
	char	*ptr;
	char	*p2;
	int	count = 0;

	if (strcmp(filename, "-") == 0) {
		f = stdin;
	} else {
		f = fopen(filename, "r");
		if (f == NULL) {
#ifdef	USE_LIBSCHILY
			comerr("Cannot open '%s'.\n", filename);
#else
			perror("fopen");
			exit(1);
#endif
		}
	}
	while (fgets(buff, sizeof (buff), f)) {
		count++;
		ptr = buff;
		while (isspace(*ptr))
			ptr++;
		if (*ptr == 0)
			continue;
		if (*ptr == '#')
			continue;

		if (ptr[strlen(ptr) - 1] == '\n')
			ptr[strlen(ptr) - 1] = 0;
		p2 = strchr(ptr, '=');
		if (p2 == NULL) {
#ifdef	USE_LIBSCHILY
			comerrno(EX_BAD, "Error in file '%s' line %d: %s\n",
						filename, count, buff);
#else
			fprintf(stderr, "Error in file '%s' line %d: %s\n",
						filename, count, buff);
			exit(1);
#endif
		}
		*p2 = 0;
		p2++;
		add_one_file(ptr, p2);
	}
	if (f != stdin)
		fclose(f);
}

/* This function looks up additions. */
char *
look_up_addition(char **newpath, char *path, struct dirent **de)
{
	char			*dup_path;
	char			*cp;
	struct file_adds	*f;
	struct file_adds	*tmp = NULL;

	f = root_file_adds;
	if (!f)
		return (NULL);

	/* I don't trust strtok */
	dup_path = strdup(path);

	cp = strtok(dup_path, SPATH_SEPARATOR);
	while (cp != NULL) {
		for (tmp = f->child; tmp != NULL; tmp = tmp->next) {
			if (strcmp(tmp->name, cp) == 0)
				break;
		}
		if (tmp == NULL) {
			/* no match */
			free(dup_path);
			return (NULL);
		}
		f = tmp;
		cp = strtok(NULL, SPATH_SEPARATOR);
	}
	free(dup_path);

	/* If nothing, then return. */
	if (tmp == NULL) {
	/* no match */
		return (NULL);
	}
	/* looks like we found something. */
	if (tmp->used >= tmp->add_count)
		return (NULL);

	*newpath = tmp->adds[tmp->used].path;
	tmp->used++;
	*de = &(tmp->du.de);
	return (tmp->adds[tmp->used - 1].name);

}

/* This function looks up additions. */
void
nuke_duplicates(char *path, struct dirent **de)
{
	char			*dup_path;
	char			*cp;
	struct file_adds	*f;
	struct file_adds	*tmp;

	f = root_file_adds;
	if (!f)
		return;

	/* I don't trust strtok */
	dup_path = strdup(path);

	cp = strtok(dup_path, SPATH_SEPARATOR);
	while (cp != NULL) {
		for (tmp = f->child; tmp != NULL; tmp = tmp->next) {
			if (strcmp(tmp->name, cp) == 0)
				break;
		}
		if (tmp == NULL) {
			/* no match */
			free(dup_path);
			return;
		}
		f = tmp;
		cp = strtok(NULL, SPATH_SEPARATOR);
	}
	free(dup_path);

#if 0
	/* looks like we found something. */
	if (tmp->used >= tmp->add_count)
		return;

	*newpath = tmp->adds[tmp->used].path;
	tmp->used++;
	*de = &(tmp->du.de);
	return (tmp->adds[tmp->used - 1].name);
#endif
}

/*
 * This function lets us add files from outside the standard file tree.
 * It is useful if we want to duplicate a cd, but add/replace things.
 * We should note that the real path will be used for exclusions.
 */

struct dirent  *
readdir_add_files(char **pathp, char **path, DIR *dir)
{
	struct dirent  *de;

	char	*addpath;
	char	*name;

	de = readdir(dir);
	if (de) {
		nuke_duplicates(path, &de);
		return (de);
	}
	name = look_up_addition(&addpath, path, &de);

	if (!name) {
		return (NULL);
	}
	*pathp = addpath;

	/*
	 * Now we must create the directory entry.
	 * fortuneately only the name seems to matter.
	 */
/*	de->d_ino = -1; de->d_off = 0; de->d_reclen = strlen (name); */
	strncpy(de->d_name, name, NAME_MAX);
	de->d_name[NAME_MAX] = 0;
	nuke_duplicates(path, &de);
	return (de);

}

#else
struct dirent  *
readdir_add_files(char **pathp, char *path, DIR *dir)
{
	return (readdir(dir));
}

#endif