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