summaryrefslogtreecommitdiffstats
path: root/private/posix/programs/pax/fileio.c
blob: f71de99cdfa8566cf2b7e07b0d39b2ee165313b1 (plain) (blame)
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* $Source: /u/mark/src/pax/RCS/fileio.c,v $
 *
 * $Revision: 1.2 $
 *
 * fileio.c - file I/O functions for all archive interfaces
 *
 * DESCRIPTION
 *
 *	These function all do I/O of some form or another.  They are
 *	grouped here mainly for convienence.
 *
 * AUTHOR
 *
 *	Mark H. Colburn, NAPS International (mark@jhereg.mn.org)
 *
 * Sponsored by The USENIX Association for public distribution. 
 *
 * Copyright (c) 1989 Mark H. Colburn.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice is duplicated in all such 
 * forms and that any documentation, advertising materials, and other 
 * materials related to such distribution and use acknowledge that the 
 * software was developed * by Mark H. Colburn and sponsored by The 
 * USENIX Association. 
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * $Log:	fileio.c,v $
 * Revision 1.2  89/02/12  10:04:31  mark
 * 1.2 release fixes
 * 
 * Revision 1.1  88/12/23  18:02:09  mark
 * Initial revision
 * 
 */

#ifndef lint
static char *ident = "$Id: fileio.c,v 1.2 89/02/12 10:04:31 mark Exp $";
static char *copyright = "Copyright (c) 1989 Mark H. Colburn.\nAll rights reserved.\n";
#endif /* ! lint */


/* Headers */

#include "pax.h"


/* open_archive -  open an archive file.  
 *
 * DESCRIPTION
 *
 *	Open_archive will open an archive file for reading or writing,
 *	setting the proper file mode, depending on the "mode" passed to
 *	it.  All buffer pointers are reset according to the mode
 *	specified.
 *
 * PARAMETERS
 *
 * 	int	mode 	- specifies whether we are reading or writing.   
 *
 * RETURNS
 *
 *	Returns a zero if successfull, or -1 if an error occured during 
 *	the open.
 */

#ifdef __STDC__
    
int open_archive(int mode)

#else
    
int open_archive(mode)
int             mode;

#endif
{
#ifdef DF_TRACE_DEBUG
printf("DF_TRACE_DEBUG: int open_archive() in fileio.c\n");
#endif
    if (ar_file[0] == '-' && ar_file[1] == '\0') {
	if (mode == AR_READ) {
	    archivefd = STDIN;
	    bufend = bufidx = bufstart;
	} else {
	    archivefd = STDOUT;
	}
    } else if (mode == AR_READ) {
	archivefd = open(ar_file, O_RDONLY | O_BINARY);
	bufend = bufidx = bufstart;	/* set up for initial read */
    } else if (mode == AR_WRITE) {
	archivefd = open(ar_file, O_WRONLY|O_TRUNC|O_CREAT|O_BINARY,  /* Xn */
	    S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);  /* Xn */
    } else if (mode == AR_APPEND) {
	archivefd = open(ar_file, O_RDWR | O_BINARY,  /* Xn */
	    S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);  /* Xn */
	bufend = bufidx = bufstart;	/* set up for initial read */
    }

    if (archivefd < 0) {
	warnarch(strerror(errno), (OFFSET) 0);  /* Xn */
	return (-1);
    }
    ++arvolume;
    return (0);
}


/* close_archive - close the archive file
 *
 * DESCRIPTION
 *
 *	Closes the current archive and resets the archive end of file
 *	marker.
 */

#ifdef __STDC__

void close_archive(void)

#else
    
void close_archive()

#endif
{
#ifdef DF_TRACE_DEBUG
printf("DF_TRACE_DEBUG: void close_archive() in fileio.c\n");
#endif
    if (archivefd != STDIN && archivefd != STDOUT) {
	close(archivefd);
    }
    areof = 0;
}


/* openout - open an output file
 *
 * DESCRIPTION
 *
 *	Openo opens the named file for output.  The file mode and type are
 *	set based on the values stored in the stat structure for the file.
 *	If the file is a special file, then no data will be written, the
 *	file/directory/Fifo, etc., will just be created.  Appropriate
 *	permission may be required to create special files.
 *
 * PARAMETERS
 *
 *	char 	*name		- The name of the file to create
 *	Stat	*asb		- Stat structure for the file
 *	Link	*linkp;		- pointer to link chain for this file
 *	int	 ispass		- true if we are operating in "pass" mode
 *
 * RETURNS
 *
 * 	Returns the output file descriptor, 0 if no data is required or -1 
 *	if unsuccessful. Note that UNIX open() will never return 0 because 
 *	the standard input is in use. 
 */

#ifdef __STDC__

int openout(char *name, Stat *asb, Link *linkp, int ispass)

#else
    
int openout(name, asb, linkp, ispass)
char           *name;
Stat           *asb;
Link           *linkp;
int             ispass;

#endif
{
    int             exists;
    int             fd;
    ushort          perm;
    ushort          operm = 0;
    Stat            osb;
#ifdef	S_IFLNK
    int             ssize;
    char            sname[PATH_MAX + 1];
#endif	/* S_IFLNK */

#ifdef DF_TRACE_DEBUG
printf("DF_TRACE_DEBUG: int openout() in fileio.c\n");
#endif
    if (exists = (LSTAT(name, &osb) == 0)) {
	if (ispass && osb.sb_ino == asb->sb_ino && osb.sb_dev == asb->sb_dev) {
	    warn(name, "Same file");
	    return (-1);
	} else if ((osb.sb_mode & S_IFMT) == (asb->sb_mode & S_IFMT)) {
	    operm = osb.sb_mode & S_IPERM;
	} else if (REMOVE(name, &osb) < 0) {
	    warn(name, strerror(errno));  /* Xn */
	    return (-1);
	} else {
	    exists = 0;
	}
    }
    if (linkp) {
	if (exists) {
	    if (asb->sb_ino == osb.sb_ino && asb->sb_dev == osb.sb_dev) {
		return (0);
	    } else if (unlink(name) < 0) {
		warn(name, strerror(errno));  /* Xn */
		return (-1);
	    } else {
		exists = 0;
	    }
	}
	if (link(linkp->l_name, name) != 0) {
	    if (errno == ENOENT) {
		if (f_dir_create) {
		    if (dirneed(name) != 0 ||
			    link(linkp->l_name, name) != 0) {
			    warn(name, strerror(errno));  /* Xn */
			return (-1);
		    }
		} else {
		    warn(name, 
			     "Directories are not being created (-d option)");
		}
		return(0);
	    } else if (errno != EXDEV) {
		warn(name, strerror(errno));  /* Xn */
		return (-1);
	    }
	} else {
	    return(0);
	} 
    }
    perm = asb->sb_mode & S_IPERM;
    switch (asb->sb_mode & S_IFMT) {
    case S_IFBLK:
    case S_IFCHR:
	fd = 0;
	if (exists) {
#ifndef _POSIX_SOURCE
	    if (asb->sb_rdev == osb.sb_rdev) {
		if (perm != operm && chmod(name, (int) perm) < 0) {
		    warn(name, strerror(errno));  /* Xn */
		    return (-1);
		} else {
		    break;
		}
	    } else if (REMOVE(name, &osb) < 0) {
		warn(name, strerror(errno));  /* Xn */
		return (-1);
	    } else {
		exists = 0;
	    }
#else
	    if (REMOVE(name, &osb) < 0) {
		warn(name, strerror(errno));  /* Xn */
		return (-1);
	    } else {
		exists = 0;
	    }
#endif /* _POSIX_SOURCE */
	}
#ifdef _POSIX_SOURCE
	if (mknod(name, (int) asb->sb_mode, 0))
#else
	if (mknod(name, (int) asb->sb_mode, (int) asb->sb_rdev) < 0)
#endif
	{
	    if (errno == ENOENT) {
		if (f_dir_create) {
		    if (dirneed(name) < 0 || mknod(name, (int) asb->sb_mode, 
#ifdef _POSIX_SOURCE
			   0))
#else
			   (int) asb->sb_rdev) < 0)
#endif
		    {
			warn(name, strerror(errno));  /* Xn */
			return (-1);
		    }
		} else {
		    warn(name, "Directories are not being created (-d option)");
		}
	    } else {
		warn(name, strerror(errno));  /* Xn */
		return (-1);
	    }
	}
	return(0);
	break;
    case S_IFDIR:
	if (exists) {
	    if (perm != operm && chmod(name, (int) perm) < 0) {
		warn(name, strerror(errno));  /* Xn */
		return (-1);
	    }
	} else if (f_dir_create) {
	    if (dirmake(name, asb) < 0 || dirneed(name) < 0) {
		warn(name, strerror(errno));  /* Xn */
		return (-1);
	    }
	} else {
	    warn(name, "Directories are not being created (-d option)");
	}
	return (0);
#ifdef	S_IFIFO
    case S_IFIFO:
	fd = 0;
	if (exists) {
	    if (perm != operm && chmod(name, (int) perm) < 0) {
		warn(name, strerror(errno));  /* Xn */
		return (-1);
	    }
	}
# ifdef	_POSIX_SOURCE  /* Xn */
	else if (mkfifo(name, asb->sb_mode) < 0)  /* Xn */
# else  /* Xn */
	else if (mknod(name, (int) asb->sb_mode, 0) < 0)
# endif  /* Xn */
	{
	    if (errno == ENOENT) {
		if (f_dir_create) {
		    if (dirneed(name) < 0
# ifdef	_POSIX_SOURCE  /* Xn */
		       || mkfifo(name, asb->sb_mode) < 0)  /* Xn */
# else  /* Xn */
		       || mknod(name, (int) asb->sb_mode, 0) < 0)
# endif	/* _POSIX_SOURCE */  /* Xn */
		    {
			warn(name, strerror(errno));  /* Xn */
			return (-1);
		    }
		} else {
		    warn(name, "Directories are not being created (-d option)");
		}
	    } else {
		warn(name, strerror(errno));  /* Xn */
		return (-1);
	    }
	}
	return(0);
	break;
#endif				/* S_IFIFO */
#ifdef	S_IFLNK
    case S_IFLNK:
	if (exists) {
	    if ((ssize = readlink(name, sname, sizeof(sname))) < 0) {
		warn(name, strerror(errno));  /* Xn */
		return (-1);
	    } else if (strncmp(sname, asb->sb_link, ssize) == 0) {
		return (0);
	    } else if (REMOVE(name, &osb) < 0) {
		warn(name, strerror(errno));  /* Xn */
		return (-1);
	    } else {
		exists = 0;
	    }
	}
	if (symlink(asb->sb_link, name) < 0) {
	    if (errno == ENOENT) {
		if (f_dir_create) {
		    if (dirneed(name) < 0 || symlink(asb->sb_link, name) < 0) {
			warn(name, strerror(errno));  /* Xn */
			return (-1);
		    }
		} else {
		    warn(name, "Directories are not being created (-d option)");
		}
	    } else {
		warn(name, strerror(errno));  /* Xn */
		return (-1);
	    }
	}
	return (0);		/* Can't chown()/chmod() a symbolic link */
#endif				/* S_IFLNK */
    case S_IFREG:
	if (exists) {
	    if (!f_unconditional && osb.sb_mtime > asb->sb_mtime) {
		warn(name, "Newer file exists");
		return (-1);
	    } else if (unlink(name) < 0) {
		warn(name, strerror(errno));  /* Xn */
		return (-1);
	    } else {
		exists = 0;
	    }
	}
	if ((fd = creat(name, (int) perm)) < 0) {
	    if (errno == ENOENT) {
		if (f_dir_create) {
		    if (dirneed(name) < 0 || 
			    (fd = creat(name, (int) perm)) < 0) {
			warn(name, strerror(errno));  /* Xn */
			return (-1);
		    }
		} else {
		    /* 
		     * the file requires a directory which does not exist
		     * and which the user does not want created, so skip
		     * the file...
		     */
		    warn(name, "Directories are not being created (-d option)");
		    return(0);
		}
	    } else {
		warn(name, strerror(errno));  /* Xn */
		return (-1);
	    }
	}
	break;
    default:
	warn(name, "Unknown filetype");
	return (-1);
    }
    if (f_owner) {
	if (!exists || asb->sb_uid != osb.sb_uid || asb->sb_gid != osb.sb_gid) {
	    chown(name, (int) asb->sb_uid, (int) asb->sb_gid);
	}
    }
    return (fd);
}


/* openin - open the next input file
 *
 * DESCRIPTION
 *
 *	Openi will attempt to open the next file for input.  If the file is
 *	a special file, such as a directory, FIFO, link, character- or
 *	block-special file, then the file size field of the stat structure
 *	is zeroed to make sure that no data is written out for the file.
 *	If the file is a special file, then a file descriptor of 0 is
 *	returned to the caller, which is handled specially.  If the file
 *	is a regular file, then the file is opened and a file descriptor
 *	to the open file is returned to the caller.
 *
 * PARAMETERS
 *
 *	char   *name	- pointer to the name of the file to open
 *	Stat   *asb	- pointer to the stat block for the file to open
 *
 * RETURNS
 *
 * 	Returns a file descriptor, 0 if no data exists, or -1 at EOF. This 
 *	kludge works because standard input is in use, preventing open() from 
 *	returning zero. 
 */

#ifdef __STDC__

int openin(char *name, Stat *asb)

#else
    
int openin(name, asb)
char           *name;		/* name of file to open */
Stat           *asb;		/* pointer to stat structure for file */

#endif
{
    int             fd;

#ifdef DF_TRACE_DEBUG
printf("DF_TRACE_DEBUG: int openin() in fileio.c\n");
#endif
    switch (asb->sb_mode & S_IFMT) {
    case S_IFDIR:
#if 0  /* NIST-PCTS */
	asb->sb_nlink = 1;
#endif  /* NIST-PCTS */
	asb->sb_size = 0;
	return (0);
#ifdef	S_IFLNK
    case S_IFLNK:
	if ((asb->sb_size = readlink(name,
			     asb->sb_link, sizeof(asb->sb_link) - 1)) < 0) {
	    warn(name, strerror(errno));  /* Xn */
	    return(0);
	}
	asb->sb_link[asb->sb_size] = '\0';
	return (0);
#endif				/* S_IFLNK */
    case S_IFREG:
	if (asb->sb_size == 0) {
	    return (0);
	}
	if ((fd = open(name, O_RDONLY | O_BINARY)) < 0) {
	    warn(name, strerror(errno));  /* Xn */
	}
	return (fd);
    default:
	asb->sb_size = 0;
	return (0);
    }
}