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