xref: /freebsd/bin/pax/file_subs.c (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
1 /*-
2  * Copyright (c) 1992 Keith Muller.
3  * Copyright (c) 1992, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Keith Muller of the University of California, San Diego.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)file_subs.c	8.1 (Berkeley) 5/31/93";
41 #endif
42 static const char rcsid[] =
43 	"$Id$";
44 #endif /* not lint */
45 
46 #include <sys/types.h>
47 #include <sys/time.h>
48 #include <sys/stat.h>
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <string.h>
52 #include <stdio.h>
53 #include <errno.h>
54 #include <sys/uio.h>
55 #include <stdlib.h>
56 #include "pax.h"
57 #include "extern.h"
58 
59 static int
60 mk_link __P((register char *,register struct stat *,register char *, int));
61 
62 /*
63  * routines that deal with file operations such as: creating, removing;
64  * and setting access modes, uid/gid and times of files
65  */
66 
67 #define FILEBITS		(S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
68 #define SETBITS			(S_ISUID | S_ISGID)
69 #define ABITS			(FILEBITS | SETBITS)
70 
71 /*
72  * file_creat()
73  *	Create and open a file.
74  * Return:
75  *	file descriptor or -1 for failure
76  */
77 
78 #if __STDC__
79 int
80 file_creat(register ARCHD *arcn)
81 #else
82 int
83 file_creat(arcn)
84 	register ARCHD *arcn;
85 #endif
86 {
87 	int fd = -1;
88 	mode_t file_mode;
89 	int oerrno;
90 
91 	/*
92 	 * assume file doesn't exist, so just try to create it, most times this
93 	 * works. We have to take special handling when the file does exist. To
94 	 * detect this, we use O_EXCL. For example when trying to create a
95 	 * file and a character device or fifo exists with the same name, we
96 	 * can accidently open the device by mistake (or block waiting to open)
97 	 * If we find that the open has failed, then figure spend the effore to
98 	 * figure out why. This strategy was found to have better average
99 	 * performance in common use than checking the file (and the path)
100 	 * first with lstat.
101 	 */
102 	file_mode = arcn->sb.st_mode & FILEBITS;
103 	if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
104 	    file_mode)) >= 0)
105 		return(fd);
106 
107 	/*
108 	 * the file seems to exist. First we try to get rid of it (found to be
109 	 * the second most common failure when traced). If this fails, only
110 	 * then we go to the expense to check and create the path to the file
111 	 */
112 	if (unlnk_exist(arcn->name, arcn->type) != 0)
113 		return(-1);
114 
115 	for (;;) {
116 		/*
117 		 * try to open it again, if this fails, check all the nodes in
118 		 * the path and give it a final try. if chk_path() finds that
119 		 * it cannot fix anything, we will skip the last attempt
120 		 */
121 		if ((fd = open(arcn->name, O_WRONLY | O_CREAT | O_TRUNC,
122 		    file_mode)) >= 0)
123 			break;
124 		oerrno = errno;
125 		if (chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) {
126 			sys_warn(1, oerrno, "Unable to create %s", arcn->name);
127 			return(-1);
128 		}
129 	}
130 	return(fd);
131 }
132 
133 /*
134  * file_close()
135  *	Close file descriptor to a file just created by pax. Sets modes,
136  *	ownership and times as required.
137  * Return:
138  *	0 for success, -1 for failure
139  */
140 
141 #if __STDC__
142 void
143 file_close(register ARCHD *arcn, int fd)
144 #else
145 void
146 file_close(arcn, fd)
147 	register ARCHD *arcn;
148 	int fd;
149 #endif
150 {
151 	int res = 0;
152 
153 	if (fd < 0)
154 		return;
155 	if (close(fd) < 0)
156 		sys_warn(0, errno, "Unable to close file descriptor on %s",
157 		    arcn->name);
158 
159 	/*
160 	 * set owner/groups first as this may strip off mode bits we want
161 	 * then set file permission modes. Then set file access and
162 	 * modification times.
163 	 */
164 	if (pids)
165 		res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid);
166 
167 	/*
168 	 * IMPORTANT SECURITY NOTE:
169 	 * if not preserving mode or we cannot set uid/gid, then PROHIBIT
170 	 * set uid/gid bits
171 	 */
172 	if (!pmode || res)
173 		arcn->sb.st_mode &= ~(SETBITS);
174 	if (pmode)
175 		set_pmode(arcn->name, arcn->sb.st_mode);
176 	if (patime || pmtime)
177 		set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0);
178 }
179 
180 /*
181  * lnk_creat()
182  *	Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name
183  *	must exist;
184  * Return:
185  *	0 if ok, -1 otherwise
186  */
187 
188 #if __STDC__
189 int
190 lnk_creat(register ARCHD *arcn)
191 #else
192 int
193 lnk_creat(arcn)
194 	register ARCHD *arcn;
195 #endif
196 {
197 	struct stat sb;
198 
199 	/*
200 	 * we may be running as root, so we have to be sure that link target
201 	 * is not a directory, so we lstat and check
202 	 */
203 	if (lstat(arcn->ln_name, &sb) < 0) {
204 		sys_warn(1,errno,"Unable to link to %s from %s", arcn->ln_name,
205 		    arcn->name);
206 		return(-1);
207 	}
208 
209 	if (S_ISDIR(sb.st_mode)) {
210 		pax_warn(1, "A hard link to the directory %s is not allowed",
211 		    arcn->ln_name);
212 		return(-1);
213 	}
214 
215 	return(mk_link(arcn->ln_name, &sb, arcn->name, 0));
216 }
217 
218 /*
219  * cross_lnk()
220  *	Create a hard link to arcn->org_name from arcn->name. Only used in copy
221  *	with the -l flag. No pax_warning or error if this does not succeed (we will
222  *	then just create the file)
223  * Return:
224  *	1 if copy() should try to create this file node
225  *	0 if cross_lnk() ok, -1 for fatal flaw (like linking to self).
226  */
227 
228 #if __STDC__
229 int
230 cross_lnk(register ARCHD *arcn)
231 #else
232 int
233 cross_lnk(arcn)
234 	register ARCHD *arcn;
235 #endif
236 {
237 	/*
238 	 * try to make a link to orginal file (-l flag in copy mode). make sure
239 	 * we do not try to link to directories in case we are running as root
240 	 * (and it might succeed).
241 	 */
242 	if (arcn->type == PAX_DIR)
243 		return(1);
244 	return(mk_link(arcn->org_name, &(arcn->sb), arcn->name, 1));
245 }
246 
247 /*
248  * chk_same()
249  *	In copy mode if we are not trying to make hard links between the src
250  *	and destinations, make sure we are not going to overwrite ourselves by
251  *	accident. This slows things down a little, but we have to protect all
252  *	those people who make typing errors.
253  * Return:
254  *	1 the target does not exist, go ahead and copy
255  *	0 skip it file exists (-k) or may be the same as source file
256  */
257 
258 #if __STDC__
259 int
260 chk_same(register ARCHD *arcn)
261 #else
262 int
263 chk_same(arcn)
264 	register ARCHD *arcn;
265 #endif
266 {
267 	struct stat sb;
268 
269 	/*
270 	 * if file does not exist, return. if file exists and -k, skip it
271 	 * quietly
272 	 */
273 	if (lstat(arcn->name, &sb) < 0)
274 		return(1);
275 	if (kflag)
276 		return(0);
277 
278 	/*
279 	 * better make sure the user does not have src == dest by mistake
280 	 */
281 	if ((arcn->sb.st_dev == sb.st_dev) && (arcn->sb.st_ino == sb.st_ino)) {
282 		pax_warn(1, "Unable to copy %s, file would overwrite itself",
283 		    arcn->name);
284 		return(0);
285 	}
286 	return(1);
287 }
288 
289 /*
290  * mk_link()
291  *	try to make a hard link between two files. if ign set, we do not
292  *	complain.
293  * Return:
294  *	0 if successful (or we are done with this file but no error, such as
295  *	finding the from file exists and the user has set -k).
296  *	1 when ign was set to indicates we could not make the link but we
297  *	should try to copy/extract the file as that might work (and is an
298  *	allowed option). -1 an error occurred.
299  */
300 
301 #if __STDC__
302 static int
303 mk_link(register char *to, register struct stat *to_sb, register char *from,
304 	int ign)
305 #else
306 static int
307 mk_link(to, to_sb, from, ign)
308 	register char *to;
309 	register struct stat *to_sb;
310 	register char *from;
311 	int ign;
312 #endif
313 {
314 	struct stat sb;
315 	int oerrno;
316 
317 	/*
318 	 * if from file exists, it has to be unlinked to make the link. If the
319 	 * file exists and -k is set, skip it quietly
320 	 */
321 	if (lstat(from, &sb) == 0) {
322 		if (kflag)
323 			return(0);
324 
325 		/*
326 		 * make sure it is not the same file, protect the user
327 		 */
328 		if ((to_sb->st_dev==sb.st_dev)&&(to_sb->st_ino == sb.st_ino)) {
329 			pax_warn(1, "Unable to link file %s to itself", to);
330 			return(-1);;
331 		}
332 
333 		/*
334 		 * try to get rid of the file, based on the type
335 		 */
336 		if (S_ISDIR(sb.st_mode)) {
337 			if (rmdir(from) < 0) {
338 				sys_warn(1, errno, "Unable to remove %s", from);
339 				return(-1);
340 			}
341 		} else if (unlink(from) < 0) {
342 			if (!ign) {
343 				sys_warn(1, errno, "Unable to remove %s", from);
344 				return(-1);
345 			}
346 			return(1);
347 		}
348 	}
349 
350 	/*
351 	 * from file is gone (or did not exist), try to make the hard link.
352 	 * if it fails, check the path and try it again (if chk_path() says to
353 	 * try again)
354 	 */
355 	for (;;) {
356 		if (link(to, from) == 0)
357 			break;
358 		oerrno = errno;
359 		if (chk_path(from, to_sb->st_uid, to_sb->st_gid) == 0)
360 			continue;
361 		if (!ign) {
362 			sys_warn(1, oerrno, "Could not link to %s from %s", to,
363 			    from);
364 			return(-1);
365 		}
366 		return(1);
367 	}
368 
369 	/*
370 	 * all right the link was made
371 	 */
372 	return(0);
373 }
374 
375 /*
376  * node_creat()
377  *	create an entry in the file system (other than a file or hard link).
378  *	If successful, sets uid/gid modes and times as required.
379  * Return:
380  *	0 if ok, -1 otherwise
381  */
382 
383 #if __STDC__
384 int
385 node_creat(register ARCHD *arcn)
386 #else
387 int
388 node_creat(arcn)
389 	register ARCHD *arcn;
390 #endif
391 {
392 	register int res;
393 	register int ign = 0;
394 	register int oerrno;
395 	register int pass = 0;
396 	mode_t file_mode;
397 	struct stat sb;
398 
399 	/*
400 	 * create node based on type, if that fails try to unlink the node and
401 	 * try again. finally check the path and try again. As noted in the
402 	 * file and link creation routines, this method seems to exhibit the
403 	 * best performance in general use workloads.
404 	 */
405 	file_mode = arcn->sb.st_mode & FILEBITS;
406 
407 	for (;;) {
408 		switch(arcn->type) {
409 		case PAX_DIR:
410 			res = mkdir(arcn->name, file_mode);
411 			if (ign)
412 				res = 0;
413 			break;
414 		case PAX_CHR:
415 			file_mode |= S_IFCHR;
416 			res = mknod(arcn->name, file_mode, arcn->sb.st_rdev);
417 			break;
418 		case PAX_BLK:
419 			file_mode |= S_IFBLK;
420 			res = mknod(arcn->name, file_mode, arcn->sb.st_rdev);
421 			break;
422 		case PAX_FIF:
423 			res = mkfifo(arcn->name, file_mode);
424 			break;
425 		case PAX_SCK:
426 			/*
427 			 * Skip sockets, operation has no meaning under BSD
428 			 */
429 			pax_warn(0,
430 			    "%s skipped. Sockets cannot be copied or extracted",
431 			    arcn->name);
432 			return(-1);
433 		case PAX_SLK:
434 			if ((res = symlink(arcn->ln_name, arcn->name)) == 0)
435 				return(0);
436 			break;
437 		case PAX_CTG:
438 		case PAX_HLK:
439 		case PAX_HRG:
440 		case PAX_REG:
441 		default:
442 			/*
443 			 * we should never get here
444 			 */
445 			pax_warn(0, "%s has an unknown file type, skipping",
446 				arcn->name);
447 			return(-1);
448 		}
449 
450 		/*
451 		 * if we were able to create the node break out of the loop,
452 		 * otherwise try to unlink the node and try again. if that
453 		 * fails check the full path and try a final time.
454 		 */
455 		if (res == 0)
456 			break;
457 
458 		/*
459 		 * we failed to make the node
460 		 */
461 		oerrno = errno;
462 		if ((ign = unlnk_exist(arcn->name, arcn->type)) < 0)
463 			return(-1);
464 
465 		if (++pass <= 1)
466 			continue;
467 
468 		if (chk_path(arcn->name,arcn->sb.st_uid,arcn->sb.st_gid) < 0) {
469 			sys_warn(1, oerrno, "Could not create: %s", arcn->name);
470 			return(-1);
471 		}
472 	}
473 
474 	/*
475 	 * we were able to create the node. set uid/gid, modes and times
476 	 */
477 	if (pids)
478 		res = set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid);
479 	else
480 		res = 0;
481 
482 	/*
483 	 * IMPORTANT SECURITY NOTE:
484 	 * if not preserving mode or we cannot set uid/gid, then PROHIBIT any
485 	 * set uid/gid bits
486 	 */
487 	if (!pmode || res)
488 		arcn->sb.st_mode &= ~(SETBITS);
489 	if (pmode)
490 		set_pmode(arcn->name, arcn->sb.st_mode);
491 
492 	if (arcn->type == PAX_DIR) {
493 		/*
494 		 * Dirs must be processed again at end of extract to set times
495 		 * and modes to agree with those stored in the archive. However
496 		 * to allow extract to continue, we may have to also set owner
497 		 * rights. This allows nodes in the archive that are children
498 		 * of this directory to be extracted without failure. Both time
499 		 * and modes will be fixed after the entire archive is read and
500 		 * before pax exits.
501 		 */
502 		if (access(arcn->name, R_OK | W_OK | X_OK) < 0) {
503 			if (lstat(arcn->name, &sb) < 0) {
504 				sys_warn(0, errno,"Could not access %s (stat)",
505 				    arcn->name);
506 				set_pmode(arcn->name,file_mode | S_IRWXU);
507 			} else {
508 				/*
509 				 * We have to add rights to the dir, so we make
510 				 * sure to restore the mode. The mode must be
511 				 * restored AS CREATED and not as stored if
512 				 * pmode is not set.
513 				 */
514 				set_pmode(arcn->name,
515 				    ((sb.st_mode & FILEBITS) | S_IRWXU));
516 				if (!pmode)
517 					arcn->sb.st_mode = sb.st_mode;
518 			}
519 
520 			/*
521 			 * we have to force the mode to what was set here,
522 			 * since we changed it from the default as created.
523 			 */
524 			add_dir(arcn->name, arcn->nlen, &(arcn->sb), 1);
525 		} else if (pmode || patime || pmtime)
526 			add_dir(arcn->name, arcn->nlen, &(arcn->sb), 0);
527 	}
528 
529 	if (patime || pmtime)
530 		set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0);
531 	return(0);
532 }
533 
534 /*
535  * unlnk_exist()
536  *	Remove node from file system with the specified name. We pass the type
537  *	of the node that is going to replace it. When we try to create a
538  *	directory and find that it already exists, we allow processing to
539  *	continue as proper modes etc will always be set for it later on.
540  * Return:
541  *	0 is ok to proceed, no file with the specified name exists
542  *	-1 we were unable to remove the node, or we should not remove it (-k)
543  *	1 we found a directory and we were going to create a directory.
544  */
545 
546 #if __STDC__
547 int
548 unlnk_exist(register char *name, register int type)
549 #else
550 int
551 unlnk_exist(name, type)
552 	register char *name;
553 	register int type;
554 #endif
555 {
556 	struct stat sb;
557 
558 	/*
559 	 * the file does not exist, or -k we are done
560 	 */
561 	if (lstat(name, &sb) < 0)
562 		return(0);
563 	if (kflag)
564 		return(-1);
565 
566 	if (S_ISDIR(sb.st_mode)) {
567 		/*
568 		 * try to remove a directory, if it fails and we were going to
569 		 * create a directory anyway, tell the caller (return a 1)
570 		 */
571 		if (rmdir(name) < 0) {
572 			if (type == PAX_DIR)
573 				return(1);
574 			sys_warn(1,errno,"Unable to remove directory %s", name);
575 			return(-1);
576 		}
577 		return(0);
578 	}
579 
580 	/*
581 	 * try to get rid of all non-directory type nodes
582 	 */
583 	if (unlink(name) < 0) {
584 		sys_warn(1, errno, "Could not unlink %s", name);
585 		return(-1);
586 	}
587 	return(0);
588 }
589 
590 /*
591  * chk_path()
592  *	We were trying to create some kind of node in the file system and it
593  *	failed. chk_path() makes sure the path up to the node exists and is
594  *	writeable. When we have to create a directory that is missing along the
595  *	path somewhere, the directory we create will be set to the same
596  *	uid/gid as the file has (when uid and gid are being preserved).
597  *	NOTE: this routine is a real performance loss. It is only used as a
598  *	last resort when trying to create entries in the file system.
599  * Return:
600  *	-1 when it could find nothing it is allowed to fix.
601  *	0 otherwise
602  */
603 
604 #if __STDC__
605 int
606 chk_path( register char *name, uid_t st_uid, gid_t st_gid)
607 #else
608 int
609 chk_path(name, st_uid, st_gid)
610 	register char *name;
611 	uid_t st_uid;
612 	gid_t st_gid;
613 #endif
614 {
615 	register char *spt = name;
616 	struct stat sb;
617 	int retval = -1;
618 
619 	/*
620 	 * watch out for paths with nodes stored directly in / (e.g. /bozo)
621 	 */
622 	if (*spt == '/')
623 		++spt;
624 
625 	for(;;) {
626 		/*
627 		 * work foward from the first / and check each part of the path
628 		 */
629 		spt = strchr(spt, '/');
630 		if (spt == NULL)
631 			break;
632 		*spt = '\0';
633 
634 		/*
635 		 * if it exists we assume it is a directory, it is not within
636 		 * the spec (at least it seems to read that way) to alter the
637 		 * file system for nodes NOT EXPLICITLY stored on the archive.
638 		 * If that assumption is changed, you would test the node here
639 		 * and figure out how to get rid of it (probably like some
640 		 * recursive unlink()) or fix up the directory permissions if
641 		 * required (do an access()).
642 		 */
643 		if (lstat(name, &sb) == 0) {
644 			*(spt++) = '/';
645 			continue;
646 		}
647 
648 		/*
649 		 * the path fails at this point, see if we can create the
650 		 * needed directory and continue on
651 		 */
652 		if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
653 			*spt = '/';
654 			retval = -1;
655 			break;
656 		}
657 
658 		/*
659 		 * we were able to create the directory. We will tell the
660 		 * caller that we found something to fix, and it is ok to try
661 		 * and create the node again.
662 		 */
663 		retval = 0;
664 		if (pids)
665 			(void)set_ids(name, st_uid, st_gid);
666 
667 		/*
668 		 * make sure the user doen't have some strange umask that
669 		 * causes this newly created directory to be unusable. We fix
670 		 * the modes and restore them back to the creation default at
671 		 * the end of pax
672 		 */
673 		if ((access(name, R_OK | W_OK | X_OK) < 0) &&
674 		    (lstat(name, &sb) == 0)) {
675 			set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU));
676 			add_dir(name, spt - name, &sb, 1);
677 		}
678 		*(spt++) = '/';
679 		continue;
680 	}
681 	return(retval);
682 }
683 
684 /*
685  * set_ftime()
686  *	Set the access time and modification time for a named file. If frc is
687  *	non-zero we force these times to be set even if the the user did not
688  *	request access and/or modification time preservation (this is also
689  *	used by -t to reset access times).
690  *	When ign is zero, only those times the user has asked for are set, the
691  *	other ones are left alone. We do not assume the un-documented feature
692  *	of many utimes() implementations that consider a 0 time value as a do
693  *	not set request.
694  */
695 
696 #if __STDC__
697 void
698 set_ftime(char *fnm, time_t mtime, time_t atime, int frc)
699 #else
700 void
701 set_ftime(fnm, mtime, atime, frc)
702 	char *fnm;
703 	time_t mtime;
704 	time_t atime;
705 	int frc;
706 #endif
707 {
708 	static struct timeval tv[2] = {{0L, 0L}, {0L, 0L}};
709 	struct stat sb;
710 
711 	tv[0].tv_sec = (long)atime;
712 	tv[1].tv_sec = (long)mtime;
713 	if (!frc && (!patime || !pmtime)) {
714 		/*
715 		 * if we are not forcing, only set those times the user wants
716 		 * set. We get the current values of the times if we need them.
717 		 */
718 		if (lstat(fnm, &sb) == 0) {
719 			if (!patime)
720 				tv[0].tv_sec = (long)sb.st_atime;
721 			if (!pmtime)
722 				tv[1].tv_sec = (long)sb.st_mtime;
723 		} else
724 			sys_warn(0,errno,"Unable to obtain file stats %s", fnm);
725 	}
726 
727 	/*
728 	 * set the times
729 	 */
730 	if (utimes(fnm, tv) < 0)
731 		sys_warn(1, errno, "Access/modification time set failed on: %s",
732 		    fnm);
733 	return;
734 }
735 
736 /*
737  * set_ids()
738  *	set the uid and gid of a file system node
739  * Return:
740  *	0 when set, -1 on failure
741  */
742 
743 #if __STDC__
744 int
745 set_ids(char *fnm, uid_t uid, gid_t gid)
746 #else
747 int
748 set_ids(fnm, uid, gid)
749 	char *fnm;
750 	uid_t uid;
751 	gid_t gid;
752 #endif
753 {
754 	if (chown(fnm, uid, gid) < 0) {
755 		sys_warn(1, errno, "Unable to set file uid/gid of %s", fnm);
756 		return(-1);
757 	}
758 	return(0);
759 }
760 
761 /*
762  * set_pmode()
763  *	Set file access mode
764  */
765 
766 #if __STDC__
767 void
768 set_pmode(char *fnm, mode_t mode)
769 #else
770 void
771 set_pmode(fnm, mode)
772 	char *fnm;
773 	mode_t mode;
774 #endif
775 {
776 	mode &= ABITS;
777 	if (chmod(fnm, mode) < 0)
778 		sys_warn(1, errno, "Could not set permissions on %s", fnm);
779 	return;
780 }
781 
782 /*
783  * file_write()
784  *	Write/copy a file (during copy or archive extract). This routine knows
785  *	how to copy files with lseek holes in it. (Which are read as file
786  *	blocks containing all 0's but do not have any file blocks associated
787  *	with the data). Typical examples of these are files created by dbm
788  *	variants (.pag files). While the file size of these files are huge, the
789  *	actual storage is quite small (the files are sparse). The problem is
790  *	the holes read as all zeros so are probably stored on the archive that
791  *	way (there is no way to determine if the file block is really a hole,
792  *	we only know that a file block of all zero's can be a hole).
793  *	At this writing, no major archive format knows how to archive files
794  *	with holes. However, on extraction (or during copy, -rw) we have to
795  *	deal with these files. Without detecting the holes, the files can
796  *	consume a lot of file space if just written to disk. This replacement
797  *	for write when passed the basic allocation size of a file system block,
798  *	uses lseek whenever it detects the input data is all 0 within that
799  *	file block. In more detail, the strategy is as follows:
800  *	While the input is all zero keep doing an lseek. Keep track of when we
801  *	pass over file block boundries. Only write when we hit a non zero
802  *	input. once we have written a file block, we continue to write it to
803  *	the end (we stop looking at the input). When we reach the start of the
804  *	next file block, start checking for zero blocks again. Working on file
805  *	block boundries significantly reduces the overhead when copying files
806  *	that are NOT very sparse. This overhead (when compared to a write) is
807  *	almost below the measurement resolution on many systems. Without it,
808  *	files with holes cannot be safely copied. It does has a side effect as
809  *	it can put holes into files that did not have them before, but that is
810  *	not a problem since the file contents are unchanged (in fact it saves
811  *	file space). (Except on paging files for diskless clients. But since we
812  *	cannot determine one of those file from here, we ignore them). If this
813  *	ever ends up on a system where CTG files are supported and the holes
814  *	are not desired, just do a conditional test in those routines that
815  *	call file_write() and have it call write() instead. BEFORE CLOSING THE
816  *	FILE, make sure to call file_flush() when the last write finishes with
817  *	an empty block. A lot of file systems will not create an lseek hole at
818  *	the end. In this case we drop a single 0 at the end to force the
819  *	trailing 0's in the file.
820  *	---Parameters---
821  *	rem: how many bytes left in this file system block
822  *	isempt: have we written to the file block yet (is it empty)
823  *	sz: basic file block allocation size
824  *	cnt: number of bytes on this write
825  *	str: buffer to write
826  * Return:
827  *	number of bytes written, -1 on write (or lseek) error.
828  */
829 
830 #if __STDC__
831 int
832 file_write(int fd, char *str, register int cnt, int *rem, int *isempt, int sz,
833 	char *name)
834 #else
835 int
836 file_write(fd, str, cnt, rem, isempt, sz, name)
837 	int fd;
838 	char *str;
839 	register int cnt;
840 	int *rem;
841 	int *isempt;
842 	int sz;
843 	char *name;
844 #endif
845 {
846 	register char *pt;
847 	register char *end;
848 	register int wcnt;
849 	register char *st = str;
850 
851 	/*
852 	 * while we have data to process
853 	 */
854 	while (cnt) {
855 		if (!*rem) {
856 			/*
857 			 * We are now at the start of file system block again
858 			 * (or what we think one is...). start looking for
859 			 * empty blocks again
860 			 */
861 			*isempt = 1;
862 			*rem = sz;
863 		}
864 
865 		/*
866 		 * only examine up to the end of the current file block or
867 		 * remaining characters to write, whatever is smaller
868 		 */
869 		wcnt = MIN(cnt, *rem);
870 		cnt -= wcnt;
871 		*rem -= wcnt;
872 		if (*isempt) {
873 			/*
874 			 * have not written to this block yet, so we keep
875 			 * looking for zero's
876 			 */
877 			pt = st;
878 			end = st + wcnt;
879 
880 			/*
881 			 * look for a zero filled buffer
882 			 */
883 			while ((pt < end) && (*pt == '\0'))
884 				++pt;
885 
886 			if (pt == end) {
887 				/*
888 				 * skip, buf is empty so far
889 				 */
890 				if (lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) {
891 					sys_warn(1,errno,"File seek on %s",
892 					    name);
893 					return(-1);
894 				}
895 				st = pt;
896 				continue;
897 			}
898 			/*
899 			 * drat, the buf is not zero filled
900 			 */
901 			*isempt = 0;
902 		}
903 
904 		/*
905 		 * have non-zero data in this file system block, have to write
906 		 */
907 		if (write(fd, st, wcnt) != wcnt) {
908 			sys_warn(1, errno, "Failed write to file %s", name);
909 			return(-1);
910 		}
911 		st += wcnt;
912 	}
913 	return(st - str);
914 }
915 
916 /*
917  * file_flush()
918  *	when the last file block in a file is zero, many file systems will not
919  *	let us create a hole at the end. To get the last block with zeros, we
920  *	write the last BYTE with a zero (back up one byte and write a zero).
921  */
922 
923 #if __STDC__
924 void
925 file_flush(int fd, char *fname, int isempt)
926 #else
927 void
928 file_flush(fd, fname, isempt)
929 	int fd;
930 	char *fname;
931 	int isempt;
932 #endif
933 {
934 	static char blnk[] = "\0";
935 
936 	/*
937 	 * silly test, but make sure we are only called when the last block is
938 	 * filled with all zeros.
939 	 */
940 	if (!isempt)
941 		return;
942 
943 	/*
944 	 * move back one byte and write a zero
945 	 */
946 	if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) {
947 		sys_warn(1, errno, "Failed seek on file %s", fname);
948 		return;
949 	}
950 
951 	if (write(fd, blnk, 1) < 0)
952 		sys_warn(1, errno, "Failed write to file %s", fname);
953 	return;
954 }
955 
956 /*
957  * rdfile_close()
958  *	close a file we have beed reading (to copy or archive). If we have to
959  *	reset access time (tflag) do so (the times are stored in arcn).
960  */
961 
962 #if __STDC__
963 void
964 rdfile_close(register ARCHD *arcn, register int *fd)
965 #else
966 void
967 rdfile_close(arcn, fd)
968 	register ARCHD *arcn;
969 	register int *fd;
970 #endif
971 {
972 	/*
973 	 * make sure the file is open
974 	 */
975 	if (*fd < 0)
976 		return;
977 
978 	(void)close(*fd);
979 	*fd = -1;
980 	if (!tflag)
981 		return;
982 
983 	/*
984 	 * user wants last access time reset
985 	 */
986 	set_ftime(arcn->org_name, arcn->sb.st_mtime, arcn->sb.st_atime, 1);
987 	return;
988 }
989 
990 /*
991  * set_crc()
992  *	read a file to calculate its crc. This is a real drag. Archive formats
993  *	that have this, end up reading the file twice (we have to write the
994  *	header WITH the crc before writing the file contents. Oh well...
995  * Return:
996  *	0 if was able to calculate the crc, -1 otherwise
997  */
998 
999 #if __STDC__
1000 int
1001 set_crc(register ARCHD *arcn, register int fd)
1002 #else
1003 int
1004 set_crc(arcn, fd)
1005 	register ARCHD *arcn;
1006 	register int fd;
1007 #endif
1008 {
1009 	register int i;
1010 	register int res;
1011 	off_t cpcnt = 0L;
1012 	u_long size;
1013 	unsigned long crc = 0L;
1014 	char tbuf[FILEBLK];
1015 	struct stat sb;
1016 
1017 	if (fd < 0) {
1018 		/*
1019 		 * hmm, no fd, should never happen. well no crc then.
1020 		 */
1021 		arcn->crc = 0L;
1022 		return(0);
1023 	}
1024 
1025 	if ((size = (u_long)arcn->sb.st_blksize) > (u_long)sizeof(tbuf))
1026 		size = (u_long)sizeof(tbuf);
1027 
1028 	/*
1029 	 * read all the bytes we think that there are in the file. If the user
1030 	 * is trying to archive an active file, forget this file.
1031 	 */
1032 	for(;;) {
1033 		if ((res = read(fd, tbuf, size)) <= 0)
1034 			break;
1035 		cpcnt += res;
1036 		for (i = 0; i < res; ++i)
1037 			crc += (tbuf[i] & 0xff);
1038 	}
1039 
1040 	/*
1041 	 * safety check. we want to avoid archiving files that are active as
1042 	 * they can create inconsistant archive copies.
1043 	 */
1044 	if (cpcnt != arcn->sb.st_size)
1045 		pax_warn(1, "File changed size %s", arcn->org_name);
1046 	else if (fstat(fd, &sb) < 0)
1047 		sys_warn(1, errno, "Failed stat on %s", arcn->org_name);
1048 	else if (arcn->sb.st_mtime != sb.st_mtime)
1049 		pax_warn(1, "File %s was modified during read", arcn->org_name);
1050 	else if (lseek(fd, (off_t)0L, SEEK_SET) < 0)
1051 		sys_warn(1, errno, "File rewind failed on: %s", arcn->org_name);
1052 	else {
1053 		arcn->crc = crc;
1054 		return(0);
1055 	}
1056 	return(-1);
1057 }
1058