Merge branch 'vendor/NCURSES'
[dragonfly.git] / bin / pax / file_subs.c
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. 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  * @(#)file_subs.c      8.1 (Berkeley) 5/31/93
34  * $FreeBSD: src/bin/pax/file_subs.c,v 1.12.2.1 2001/08/01 05:03:11 obrien Exp $
35  * $DragonFly: src/bin/pax/file_subs.c,v 1.8 2006/09/27 21:58:08 pavalos Exp $
36  */
37
38 #include <sys/types.h>
39 #include <sys/time.h>
40 #include <sys/stat.h>
41 #include <unistd.h>
42 #include <fcntl.h>
43 #include <string.h>
44 #include <stdio.h>
45 #include <errno.h>
46 #include <sys/uio.h>
47 #include <stdlib.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 accidently open the device by mistake (or block waiting to open).
84          * If we find that the open has failed, then spend the effort to
85          * 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 = ((arcn->type == PAX_SLK) ?
425                     set_lids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid) :
426                     set_ids(arcn->name, arcn->sb.st_uid, arcn->sb.st_gid));
427         else
428                 res = 0;
429
430         /*
431          * symlinks are done now.
432          */
433         if (arcn->type == PAX_SLK)
434                 return(0);
435
436         /*
437          * IMPORTANT SECURITY NOTE:
438          * if not preserving mode or we cannot set uid/gid, then PROHIBIT any
439          * set uid/gid bits
440          */
441         if (!pmode || res)
442                 arcn->sb.st_mode &= ~(SETBITS);
443         if (pmode)
444                 set_pmode(arcn->name, arcn->sb.st_mode);
445
446         if (arcn->type == PAX_DIR && strcmp(NM_CPIO, argv0) != 0) {
447                 /*
448                  * Dirs must be processed again at end of extract to set times
449                  * and modes to agree with those stored in the archive. However
450                  * to allow extract to continue, we may have to also set owner
451                  * rights. This allows nodes in the archive that are children
452                  * of this directory to be extracted without failure. Both time
453                  * and modes will be fixed after the entire archive is read and
454                  * before pax exits.
455                  */
456                 if (access(arcn->name, R_OK | W_OK | X_OK) < 0) {
457                         if (lstat(arcn->name, &sb) < 0) {
458                                 syswarn(0, errno,"Could not access %s (stat)",
459                                     arcn->name);
460                                 set_pmode(arcn->name,file_mode | S_IRWXU);
461                         } else {
462                                 /*
463                                  * We have to add rights to the dir, so we make
464                                  * sure to restore the mode. The mode must be
465                                  * restored AS CREATED and not as stored if
466                                  * pmode is not set.
467                                  */
468                                 set_pmode(arcn->name,
469                                     ((sb.st_mode & FILEBITS) | S_IRWXU));
470                                 if (!pmode)
471                                         arcn->sb.st_mode = sb.st_mode;
472                         }
473
474                         /*
475                          * we have to force the mode to what was set here,
476                          * since we changed it from the default as created.
477                          */
478                         add_dir(arcn->name, arcn->nlen, &(arcn->sb), 1);
479                 } else if (pmode || patime || pmtime)
480                         add_dir(arcn->name, arcn->nlen, &(arcn->sb), 0);
481         }
482
483         if (patime || pmtime)
484                 set_ftime(arcn->name, arcn->sb.st_mtime, arcn->sb.st_atime, 0);
485         return(0);
486 }
487
488 /*
489  * unlnk_exist()
490  *      Remove node from file system with the specified name. We pass the type
491  *      of the node that is going to replace it. When we try to create a
492  *      directory and find that it already exists, we allow processing to
493  *      continue as proper modes etc will always be set for it later on.
494  * Return:
495  *      0 is ok to proceed, no file with the specified name exists
496  *      -1 we were unable to remove the node, or we should not remove it (-k)
497  *      1 we found a directory and we were going to create a directory.
498  */
499
500 int
501 unlnk_exist(char *name, int type)
502 {
503         struct stat sb;
504
505         /*
506          * the file does not exist, or -k we are done
507          */
508         if (lstat(name, &sb) < 0)
509                 return(0);
510         if (kflag)
511                 return(-1);
512
513         if (S_ISDIR(sb.st_mode)) {
514                 /*
515                  * try to remove a directory, if it fails and we were going to
516                  * create a directory anyway, tell the caller (return a 1)
517                  */
518                 if (rmdir(name) < 0) {
519                         if (type == PAX_DIR)
520                                 return(1);
521                         syswarn(1,errno,"Unable to remove directory %s", name);
522                         return(-1);
523                 }
524                 return(0);
525         }
526
527         /*
528          * try to get rid of all non-directory type nodes
529          */
530         if (unlink(name) < 0) {
531                 syswarn(1, errno, "Could not unlink %s", name);
532                 return(-1);
533         }
534         return(0);
535 }
536
537 /*
538  * chk_path()
539  *      We were trying to create some kind of node in the file system and it
540  *      failed. chk_path() makes sure the path up to the node exists and is
541  *      writeable. When we have to create a directory that is missing along the
542  *      path somewhere, the directory we create will be set to the same
543  *      uid/gid as the file has (when uid and gid are being preserved).
544  *      NOTE: this routine is a real performance loss. It is only used as a
545  *      last resort when trying to create entries in the file system.
546  * Return:
547  *      -1 when it could find nothing it is allowed to fix.
548  *      0 otherwise
549  */
550
551 int
552 chk_path( char *name, uid_t st_uid, gid_t st_gid)
553 {
554         char *spt = name;
555         struct stat sb;
556         int retval = -1;
557
558         /*
559          * watch out for paths with nodes stored directly in / (e.g. /bozo)
560          */
561         if (*spt == '/')
562                 ++spt;
563
564         for(;;) {
565                 /*
566                  * work forward from the first / and check each part of the path
567                  */
568                 spt = strchr(spt, '/');
569                 if (spt == NULL)
570                         break;
571                 *spt = '\0';
572
573                 /*
574                  * if it exists we assume it is a directory, it is not within
575                  * the spec (at least it seems to read that way) to alter the
576                  * file system for nodes NOT EXPLICITLY stored on the archive.
577                  * If that assumption is changed, you would test the node here
578                  * and figure out how to get rid of it (probably like some
579                  * recursive unlink()) or fix up the directory permissions if
580                  * required (do an access()).
581                  */
582                 if (lstat(name, &sb) == 0) {
583                         *(spt++) = '/';
584                         continue;
585                 }
586
587                 /*
588                  * the path fails at this point, see if we can create the
589                  * needed directory and continue on
590                  */
591                 if (mkdir(name, S_IRWXU | S_IRWXG | S_IRWXO) < 0) {
592                         *spt = '/';
593                         retval = -1;
594                         break;
595                 }
596
597                 /*
598                  * we were able to create the directory. We will tell the
599                  * caller that we found something to fix, and it is ok to try
600                  * and create the node again.
601                  */
602                 retval = 0;
603                 if (pids)
604                         set_ids(name, st_uid, st_gid);
605
606                 /*
607                  * make sure the user doesn't have some strange umask that
608                  * causes this newly created directory to be unusable. We fix
609                  * the modes and restore them back to the creation default at
610                  * the end of pax
611                  */
612                 if ((access(name, R_OK | W_OK | X_OK) < 0) &&
613                     (lstat(name, &sb) == 0)) {
614                         set_pmode(name, ((sb.st_mode & FILEBITS) | S_IRWXU));
615                         add_dir(name, spt - name, &sb, 1);
616                 }
617                 *(spt++) = '/';
618                 continue;
619         }
620         return(retval);
621 }
622
623 /*
624  * set_ftime()
625  *      Set the access time and modification time for a named file. If frc is
626  *      non-zero we force these times to be set even if the user did not
627  *      request access and/or modification time preservation (this is also
628  *      used by -t to reset access times).
629  *      When ign is zero, only those times the user has asked for are set, the
630  *      other ones are left alone. We do not assume the un-documented feature
631  *      of many utimes() implementations that consider a 0 time value as a do
632  *      not set request.
633  */
634
635 void
636 set_ftime(char *fnm, time_t mtime, time_t atime, int frc)
637 {
638         static struct timeval tv[2] = {{0L, 0L}, {0L, 0L}};
639         struct stat sb;
640
641         tv[0].tv_sec = (long)atime;
642         tv[1].tv_sec = (long)mtime;
643         if (!frc && (!patime || !pmtime)) {
644                 /*
645                  * if we are not forcing, only set those times the user wants
646                  * set. We get the current values of the times if we need them.
647                  */
648                 if (lstat(fnm, &sb) == 0) {
649                         if (!patime)
650                                 tv[0].tv_sec = (long)sb.st_atime;
651                         if (!pmtime)
652                                 tv[1].tv_sec = (long)sb.st_mtime;
653                 } else
654                         syswarn(0,errno,"Unable to obtain file stats %s", fnm);
655         }
656
657         /*
658          * set the times
659          */
660         if (utimes(fnm, tv) < 0)
661                 syswarn(1, errno, "Access/modification time set failed on: %s",
662                     fnm);
663         return;
664 }
665
666 /*
667  * set_ids()
668  *      set the uid and gid of a file system node
669  * Return:
670  *      0 when set, -1 on failure
671  */
672
673 int
674 set_ids(char *fnm, uid_t uid, gid_t gid)
675 {
676         if (chown(fnm, uid, gid) < 0) {
677                 /*
678                  * ignore EPERM unless in verbose mode or being run by root.
679                  * if running as pax, POSIX requires a warning.
680                  */
681                 if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag ||
682                     geteuid() == 0)
683                         syswarn(1, errno, "Unable to set file uid/gid of %s",
684                             fnm);
685                 return(-1);
686         }
687         return(0);
688 }
689
690 /*
691  * set_lids()
692  *      set the uid and gid of a file system node
693  * Return:
694  *      0 when set, -1 on failure
695  */
696
697 int
698 set_lids(char *fnm, uid_t uid, gid_t gid)
699 {
700         if (lchown(fnm, uid, gid) < 0) {
701                 /*
702                  * ignore EPERM unless in verbose mode or being run by root.
703                  * if running as pax, POSIX requires a warning.
704                  */
705                 if (strcmp(NM_PAX, argv0) == 0 || errno != EPERM || vflag ||
706                     geteuid() == 0)
707                         syswarn(1, errno, "Unable to set file uid/gid of %s",
708                             fnm);
709                 return(-1);
710         }
711         return(0);
712 }
713
714 /*
715  * set_pmode()
716  *      Set file access mode
717  */
718
719 void
720 set_pmode(char *fnm, mode_t mode)
721 {
722         mode &= ABITS;
723         if (chmod(fnm, mode) < 0)
724                 syswarn(1, errno, "Could not set permissions on %s", fnm);
725         return;
726 }
727
728 /*
729  * file_write()
730  *      Write/copy a file (during copy or archive extract). This routine knows
731  *      how to copy files with lseek holes in it. (Which are read as file
732  *      blocks containing all 0's but do not have any file blocks associated
733  *      with the data). Typical examples of these are files created by dbm
734  *      variants (.pag files). While the file size of these files are huge, the
735  *      actual storage is quite small (the files are sparse). The problem is
736  *      the holes read as all zeros so are probably stored on the archive that
737  *      way (there is no way to determine if the file block is really a hole,
738  *      we only know that a file block of all zero's can be a hole).
739  *      At this writing, no major archive format knows how to archive files
740  *      with holes. However, on extraction (or during copy, -rw) we have to
741  *      deal with these files. Without detecting the holes, the files can
742  *      consume a lot of file space if just written to disk. This replacement
743  *      for write when passed the basic allocation size of a file system block,
744  *      uses lseek whenever it detects the input data is all 0 within that
745  *      file block. In more detail, the strategy is as follows:
746  *      While the input is all zero keep doing an lseek. Keep track of when we
747  *      pass over file block boundaries. Only write when we hit a non zero
748  *      input. once we have written a file block, we continue to write it to
749  *      the end (we stop looking at the input). When we reach the start of the
750  *      next file block, start checking for zero blocks again. Working on file
751  *      block boundaries significantly reduces the overhead when copying files
752  *      that are NOT very sparse. This overhead (when compared to a write) is
753  *      almost below the measurement resolution on many systems. Without it,
754  *      files with holes cannot be safely copied. It does has a side effect as
755  *      it can put holes into files that did not have them before, but that is
756  *      not a problem since the file contents are unchanged (in fact it saves
757  *      file space). (Except on paging files for diskless clients. But since we
758  *      cannot determine one of those file from here, we ignore them). If this
759  *      ever ends up on a system where CTG files are supported and the holes
760  *      are not desired, just do a conditional test in those routines that
761  *      call file_write() and have it call write() instead. BEFORE CLOSING THE
762  *      FILE, make sure to call file_flush() when the last write finishes with
763  *      an empty block. A lot of file systems will not create an lseek hole at
764  *      the end. In this case we drop a single 0 at the end to force the
765  *      trailing 0's in the file.
766  *      ---Parameters---
767  *      rem: how many bytes left in this file system block
768  *      isempt: have we written to the file block yet (is it empty)
769  *      sz: basic file block allocation size
770  *      cnt: number of bytes on this write
771  *      str: buffer to write
772  * Return:
773  *      number of bytes written, -1 on write (or lseek) error.
774  */
775
776 int
777 file_write(int fd, char *str, int cnt, int *rem, int *isempt, int sz,
778         char *name)
779 {
780         char *pt;
781         char *end;
782         int wcnt;
783         char *st = str;
784
785         /*
786          * while we have data to process
787          */
788         while (cnt) {
789                 if (!*rem) {
790                         /*
791                          * We are now at the start of file system block again
792                          * (or what we think one is...). start looking for
793                          * empty blocks again
794                          */
795                         *isempt = 1;
796                         *rem = sz;
797                 }
798
799                 /*
800                  * only examine up to the end of the current file block or
801                  * remaining characters to write, whatever is smaller
802                  */
803                 wcnt = MIN(cnt, *rem);
804                 cnt -= wcnt;
805                 *rem -= wcnt;
806                 if (*isempt) {
807                         /*
808                          * have not written to this block yet, so we keep
809                          * looking for zero's
810                          */
811                         pt = st;
812                         end = st + wcnt;
813
814                         /*
815                          * look for a zero filled buffer
816                          */
817                         while ((pt < end) && (*pt == '\0'))
818                                 ++pt;
819
820                         if (pt == end) {
821                                 /*
822                                  * skip, buf is empty so far
823                                  */
824                                 if (lseek(fd, (off_t)wcnt, SEEK_CUR) < 0) {
825                                         syswarn(1,errno,"File seek on %s",
826                                             name);
827                                         return(-1);
828                                 }
829                                 st = pt;
830                                 continue;
831                         }
832                         /*
833                          * drat, the buf is not zero filled
834                          */
835                         *isempt = 0;
836                 }
837
838                 /*
839                  * have non-zero data in this file system block, have to write
840                  */
841                 if (write(fd, st, wcnt) != wcnt) {
842                         syswarn(1, errno, "Failed write to file %s", name);
843                         return(-1);
844                 }
845                 st += wcnt;
846         }
847         return(st - str);
848 }
849
850 /*
851  * file_flush()
852  *      when the last file block in a file is zero, many file systems will not
853  *      let us create a hole at the end. To get the last block with zeros, we
854  *      write the last BYTE with a zero (back up one byte and write a zero).
855  */
856
857 void
858 file_flush(int fd, char *fname, int isempt)
859 {
860         static char blnk[] = "\0";
861
862         /*
863          * silly test, but make sure we are only called when the last block is
864          * filled with all zeros.
865          */
866         if (!isempt)
867                 return;
868
869         /*
870          * move back one byte and write a zero
871          */
872         if (lseek(fd, (off_t)-1, SEEK_CUR) < 0) {
873                 syswarn(1, errno, "Failed seek on file %s", fname);
874                 return;
875         }
876
877         if (write(fd, blnk, 1) < 0)
878                 syswarn(1, errno, "Failed write to file %s", fname);
879         return;
880 }
881
882 /*
883  * rdfile_close()
884  *      close a file we have beed reading (to copy or archive). If we have to
885  *      reset access time (tflag) do so (the times are stored in arcn).
886  */
887
888 void
889 rdfile_close(ARCHD *arcn, int *fd)
890 {
891         /*
892          * make sure the file is open
893          */
894         if (*fd < 0)
895                 return;
896
897         close(*fd);
898         *fd = -1;
899         if (!tflag)
900                 return;
901
902         /*
903          * user wants last access time reset
904          */
905         set_ftime(arcn->org_name, arcn->sb.st_mtime, arcn->sb.st_atime, 1);
906         return;
907 }
908
909 /*
910  * set_crc()
911  *      read a file to calculate its crc. This is a real drag. Archive formats
912  *      that have this, end up reading the file twice (we have to write the
913  *      header WITH the crc before writing the file contents. Oh well...
914  * Return:
915  *      0 if was able to calculate the crc, -1 otherwise
916  */
917
918 int
919 set_crc(ARCHD *arcn, int fd)
920 {
921         int i;
922         int res;
923         off_t cpcnt = 0L;
924         u_long size;
925         unsigned long crc = 0L;
926         char tbuf[FILEBLK];
927         struct stat sb;
928
929         if (fd < 0) {
930                 /*
931                  * hmm, no fd, should never happen. well no crc then.
932                  */
933                 arcn->crc = 0L;
934                 return(0);
935         }
936
937         if ((size = (u_long)arcn->sb.st_blksize) > (u_long)sizeof(tbuf))
938                 size = (u_long)sizeof(tbuf);
939
940         /*
941          * read all the bytes we think that there are in the file. If the user
942          * is trying to archive an active file, forget this file.
943          */
944         for(;;) {
945                 if ((res = read(fd, tbuf, size)) <= 0)
946                         break;
947                 cpcnt += res;
948                 for (i = 0; i < res; ++i)
949                         crc += (tbuf[i] & 0xff);
950         }
951
952         /*
953          * safety check. we want to avoid archiving files that are active as
954          * they can create inconsistent archive copies.
955          */
956         if (cpcnt != arcn->sb.st_size)
957                 paxwarn(1, "File changed size %s", arcn->org_name);
958         else if (fstat(fd, &sb) < 0)
959                 syswarn(1, errno, "Failed stat on %s", arcn->org_name);
960         else if (arcn->sb.st_mtime != sb.st_mtime)
961                 paxwarn(1, "File %s was modified during read", arcn->org_name);
962         else if (lseek(fd, (off_t)0L, SEEK_SET) < 0)
963                 syswarn(1, errno, "File rewind failed on: %s", arcn->org_name);
964         else {
965                 arcn->crc = crc;
966                 return(0);
967         }
968         return(-1);
969 }