English Language flag
// Log In
// CVSweb
Project: FreeWRT
// Summary // Activity // Search // Tracker // Lists // News // SCM // Wiki

SCM Repository

ViewVC logotype

Contents of /branches/freewrt_1_0/tools/paxmirabilis/src/file_subs.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3816 - (show annotations) (download)
Wed Oct 29 17:58:40 2008 UTC (5 years, 3 months ago) by tg
File MIME type: text/plain
File size: 29589 byte(s)
FreeWRT trunk:

• merge new MirCPIO upstream, changes:
  – allow multiple -v options not only for tar but also for cpio, pax
  – add code to work around broken archives such as the CPIO archive inside
    Fedora Core 4 glibc-common-2.3.6-3.i386.rpm which carry the actual data
    of hardlinks not in the first but a later (here, the last) occurence of
    the file in question: iff hardlinking succeeds (no cross-device!), size
    of the linked files is 0, size of the archive member is greater than 0,
    we are extracting, but not to stdout, proceed writing out the data.
• add mircpio, mirpax, mirtar links in order to prevent interfering with
  native tools when both are in $PATH (this has worked since the last
  update, can be used now)

FreeWRT 1.0-stable:

• merge paxmirabilis complete from trunk

note: untested

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

root@freewrt.org:443
ViewVC Help
Powered by ViewVC 1.1.20