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