Deleted Added
full compact
file_subs.c (90110) file_subs.c (90113)
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 *

--- 26 unchanged lines hidden (view full) ---

35 * SUCH DAMAGE.
36 */
37
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)file_subs.c 8.1 (Berkeley) 5/31/93";
41#endif
42static const char rcsid[] =
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 *

--- 26 unchanged lines hidden (view full) ---

35 * SUCH DAMAGE.
36 */
37
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)file_subs.c 8.1 (Berkeley) 5/31/93";
41#endif
42static const char rcsid[] =
43 "$FreeBSD: head/bin/pax/file_subs.c 90110 2002-02-02 06:48:10Z imp $";
43 "$FreeBSD: head/bin/pax/file_subs.c 90113 2002-02-02 07:07:59Z imp $";
44#endif /* not lint */
45
46#include <sys/types.h>
47#include <sys/time.h>
48#include <sys/stat.h>
49#include <unistd.h>
50#include <fcntl.h>
51#include <string.h>
52#include <stdio.h>
53#include <errno.h>
54#include <sys/uio.h>
55#include <stdlib.h>
56#include "pax.h"
57#include "options.h"
58#include "extern.h"
59
60static int
44#endif /* not lint */
45
46#include <sys/types.h>
47#include <sys/time.h>
48#include <sys/stat.h>
49#include <unistd.h>
50#include <fcntl.h>
51#include <string.h>
52#include <stdio.h>
53#include <errno.h>
54#include <sys/uio.h>
55#include <stdlib.h>
56#include "pax.h"
57#include "options.h"
58#include "extern.h"
59
60static int
61mk_link(register char *,register struct stat *,register char *, int);
61mk_link(char *,struct stat *,char *, int);
62
63/*
64 * routines that deal with file operations such as: creating, removing;
65 * and setting access modes, uid/gid and times of files
66 */
67
68#define FILEBITS (S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
69#define SETBITS (S_ISUID | S_ISGID)
70#define ABITS (FILEBITS | SETBITS)
71
72/*
73 * file_creat()
74 * Create and open a file.
75 * Return:
76 * file descriptor or -1 for failure
77 */
78
79int
62
63/*
64 * routines that deal with file operations such as: creating, removing;
65 * and setting access modes, uid/gid and times of files
66 */
67
68#define FILEBITS (S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
69#define SETBITS (S_ISUID | S_ISGID)
70#define ABITS (FILEBITS | SETBITS)
71
72/*
73 * file_creat()
74 * Create and open a file.
75 * Return:
76 * file descriptor or -1 for failure
77 */
78
79int
80file_creat(register ARCHD *arcn)
80file_creat(ARCHD *arcn)
81{
82 int fd = -1;
83 mode_t file_mode;
84 int oerrno;
85
86 /*
87 * assume file doesn't exist, so just try to create it, most times this
88 * works. We have to take special handling when the file does exist. To

--- 40 unchanged lines hidden (view full) ---

129 * file_close()
130 * Close file descriptor to a file just created by pax. Sets modes,
131 * ownership and times as required.
132 * Return:
133 * 0 for success, -1 for failure
134 */
135
136void
81{
82 int fd = -1;
83 mode_t file_mode;
84 int oerrno;
85
86 /*
87 * assume file doesn't exist, so just try to create it, most times this
88 * works. We have to take special handling when the file does exist. To

--- 40 unchanged lines hidden (view full) ---

129 * file_close()
130 * Close file descriptor to a file just created by pax. Sets modes,
131 * ownership and times as required.
132 * Return:
133 * 0 for success, -1 for failure
134 */
135
136void
137file_close(register ARCHD *arcn, int fd)
137file_close(ARCHD *arcn, int fd)
138{
139 int res = 0;
140
141 if (fd < 0)
142 return;
143 if (close(fd) < 0)
144 syswarn(0, errno, "Unable to close file descriptor on %s",
145 arcn->name);

--- 23 unchanged lines hidden (view full) ---

169 * lnk_creat()
170 * Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name
171 * must exist;
172 * Return:
173 * 0 if ok, -1 otherwise
174 */
175
176int
138{
139 int res = 0;
140
141 if (fd < 0)
142 return;
143 if (close(fd) < 0)
144 syswarn(0, errno, "Unable to close file descriptor on %s",
145 arcn->name);

--- 23 unchanged lines hidden (view full) ---

169 * lnk_creat()
170 * Create a hard link to arcn->ln_name from arcn->name. arcn->ln_name
171 * must exist;
172 * Return:
173 * 0 if ok, -1 otherwise
174 */
175
176int
177lnk_creat(register ARCHD *arcn)
177lnk_creat(ARCHD *arcn)
178{
179 struct stat sb;
180
181 /*
182 * we may be running as root, so we have to be sure that link target
183 * is not a directory, so we lstat and check
184 */
185 if (lstat(arcn->ln_name, &sb) < 0) {

--- 17 unchanged lines hidden (view full) ---

203 * with the -l flag. No warning or error if this does not succeed (we will
204 * then just create the file)
205 * Return:
206 * 1 if copy() should try to create this file node
207 * 0 if cross_lnk() ok, -1 for fatal flaw (like linking to self).
208 */
209
210int
178{
179 struct stat sb;
180
181 /*
182 * we may be running as root, so we have to be sure that link target
183 * is not a directory, so we lstat and check
184 */
185 if (lstat(arcn->ln_name, &sb) < 0) {

--- 17 unchanged lines hidden (view full) ---

203 * with the -l flag. No warning or error if this does not succeed (we will
204 * then just create the file)
205 * Return:
206 * 1 if copy() should try to create this file node
207 * 0 if cross_lnk() ok, -1 for fatal flaw (like linking to self).
208 */
209
210int
211cross_lnk(register ARCHD *arcn)
211cross_lnk(ARCHD *arcn)
212{
213 /*
214 * try to make a link to original file (-l flag in copy mode). make sure
215 * we do not try to link to directories in case we are running as root
216 * (and it might succeed).
217 */
218 if (arcn->type == PAX_DIR)
219 return(1);

--- 7 unchanged lines hidden (view full) ---

227 * accident. This slows things down a little, but we have to protect all
228 * those people who make typing errors.
229 * Return:
230 * 1 the target does not exist, go ahead and copy
231 * 0 skip it file exists (-k) or may be the same as source file
232 */
233
234int
212{
213 /*
214 * try to make a link to original file (-l flag in copy mode). make sure
215 * we do not try to link to directories in case we are running as root
216 * (and it might succeed).
217 */
218 if (arcn->type == PAX_DIR)
219 return(1);

--- 7 unchanged lines hidden (view full) ---

227 * accident. This slows things down a little, but we have to protect all
228 * those people who make typing errors.
229 * Return:
230 * 1 the target does not exist, go ahead and copy
231 * 0 skip it file exists (-k) or may be the same as source file
232 */
233
234int
235chk_same(register ARCHD *arcn)
235chk_same(ARCHD *arcn)
236{
237 struct stat sb;
238
239 /*
240 * if file does not exist, return. if file exists and -k, skip it
241 * quietly
242 */
243 if (lstat(arcn->name, &sb) < 0)

--- 20 unchanged lines hidden (view full) ---

264 * 0 if successful (or we are done with this file but no error, such as
265 * finding the from file exists and the user has set -k).
266 * 1 when ign was set to indicates we could not make the link but we
267 * should try to copy/extract the file as that might work (and is an
268 * allowed option). -1 an error occurred.
269 */
270
271static int
236{
237 struct stat sb;
238
239 /*
240 * if file does not exist, return. if file exists and -k, skip it
241 * quietly
242 */
243 if (lstat(arcn->name, &sb) < 0)

--- 20 unchanged lines hidden (view full) ---

264 * 0 if successful (or we are done with this file but no error, such as
265 * finding the from file exists and the user has set -k).
266 * 1 when ign was set to indicates we could not make the link but we
267 * should try to copy/extract the file as that might work (and is an
268 * allowed option). -1 an error occurred.
269 */
270
271static int
272mk_link(register char *to, register struct stat *to_sb, register char *from,
272mk_link(char *to, struct stat *to_sb, char *from,
273 int ign)
274{
275 struct stat sb;
276 int oerrno;
277
278 /*
279 * if from file exists, it has to be unlinked to make the link. If the
280 * file exists and -k is set, skip it quietly

--- 56 unchanged lines hidden (view full) ---

337 * node_creat()
338 * create an entry in the file system (other than a file or hard link).
339 * If successful, sets uid/gid modes and times as required.
340 * Return:
341 * 0 if ok, -1 otherwise
342 */
343
344int
273 int ign)
274{
275 struct stat sb;
276 int oerrno;
277
278 /*
279 * if from file exists, it has to be unlinked to make the link. If the
280 * file exists and -k is set, skip it quietly

--- 56 unchanged lines hidden (view full) ---

337 * node_creat()
338 * create an entry in the file system (other than a file or hard link).
339 * If successful, sets uid/gid modes and times as required.
340 * Return:
341 * 0 if ok, -1 otherwise
342 */
343
344int
345node_creat(register ARCHD *arcn)
345node_creat(ARCHD *arcn)
346{
346{
347 register int res;
348 register int ign = 0;
349 register int oerrno;
350 register int pass = 0;
347 int res;
348 int ign = 0;
349 int oerrno;
350 int pass = 0;
351 mode_t file_mode;
352 struct stat sb;
353
354 /*
355 * create node based on type, if that fails try to unlink the node and
356 * try again. finally check the path and try again. As noted in the
357 * file and link creation routines, this method seems to exhibit the
358 * best performance in general use workloads.

--- 142 unchanged lines hidden (view full) ---

501 * continue as proper modes etc will always be set for it later on.
502 * Return:
503 * 0 is ok to proceed, no file with the specified name exists
504 * -1 we were unable to remove the node, or we should not remove it (-k)
505 * 1 we found a directory and we were going to create a directory.
506 */
507
508int
351 mode_t file_mode;
352 struct stat sb;
353
354 /*
355 * create node based on type, if that fails try to unlink the node and
356 * try again. finally check the path and try again. As noted in the
357 * file and link creation routines, this method seems to exhibit the
358 * best performance in general use workloads.

--- 142 unchanged lines hidden (view full) ---

501 * continue as proper modes etc will always be set for it later on.
502 * Return:
503 * 0 is ok to proceed, no file with the specified name exists
504 * -1 we were unable to remove the node, or we should not remove it (-k)
505 * 1 we found a directory and we were going to create a directory.
506 */
507
508int
509unlnk_exist(register char *name, register int type)
509unlnk_exist(char *name, int type)
510{
511 struct stat sb;
512
513 /*
514 * the file does not exist, or -k we are done
515 */
516 if (lstat(name, &sb) < 0)
517 return(0);

--- 34 unchanged lines hidden (view full) ---

552 * NOTE: this routine is a real performance loss. It is only used as a
553 * last resort when trying to create entries in the file system.
554 * Return:
555 * -1 when it could find nothing it is allowed to fix.
556 * 0 otherwise
557 */
558
559int
510{
511 struct stat sb;
512
513 /*
514 * the file does not exist, or -k we are done
515 */
516 if (lstat(name, &sb) < 0)
517 return(0);

--- 34 unchanged lines hidden (view full) ---

552 * NOTE: this routine is a real performance loss. It is only used as a
553 * last resort when trying to create entries in the file system.
554 * Return:
555 * -1 when it could find nothing it is allowed to fix.
556 * 0 otherwise
557 */
558
559int
560chk_path( register char *name, uid_t st_uid, gid_t st_gid)
560chk_path( char *name, uid_t st_uid, gid_t st_gid)
561{
561{
562 register char *spt = name;
562 char *spt = name;
563 struct stat sb;
564 int retval = -1;
565
566 /*
567 * watch out for paths with nodes stored directly in / (e.g. /bozo)
568 */
569 if (*spt == '/')
570 ++spt;

--- 206 unchanged lines hidden (view full) ---

777 * sz: basic file block allocation size
778 * cnt: number of bytes on this write
779 * str: buffer to write
780 * Return:
781 * number of bytes written, -1 on write (or lseek) error.
782 */
783
784int
563 struct stat sb;
564 int retval = -1;
565
566 /*
567 * watch out for paths with nodes stored directly in / (e.g. /bozo)
568 */
569 if (*spt == '/')
570 ++spt;

--- 206 unchanged lines hidden (view full) ---

777 * sz: basic file block allocation size
778 * cnt: number of bytes on this write
779 * str: buffer to write
780 * Return:
781 * number of bytes written, -1 on write (or lseek) error.
782 */
783
784int
785file_write(int fd, char *str, register int cnt, int *rem, int *isempt, int sz,
785file_write(int fd, char *str, int cnt, int *rem, int *isempt, int sz,
786 char *name)
787{
786 char *name)
787{
788 register char *pt;
789 register char *end;
790 register int wcnt;
791 register char *st = str;
788 char *pt;
789 char *end;
790 int wcnt;
791 char *st = str;
792
793 /*
794 * while we have data to process
795 */
796 while (cnt) {
797 if (!*rem) {
798 /*
799 * We are now at the start of file system block again

--- 89 unchanged lines hidden (view full) ---

889
890/*
891 * rdfile_close()
892 * close a file we have beed reading (to copy or archive). If we have to
893 * reset access time (tflag) do so (the times are stored in arcn).
894 */
895
896void
792
793 /*
794 * while we have data to process
795 */
796 while (cnt) {
797 if (!*rem) {
798 /*
799 * We are now at the start of file system block again

--- 89 unchanged lines hidden (view full) ---

889
890/*
891 * rdfile_close()
892 * close a file we have beed reading (to copy or archive). If we have to
893 * reset access time (tflag) do so (the times are stored in arcn).
894 */
895
896void
897rdfile_close(register ARCHD *arcn, register int *fd)
897rdfile_close(ARCHD *arcn, int *fd)
898{
899 /*
900 * make sure the file is open
901 */
902 if (*fd < 0)
903 return;
904
905 (void)close(*fd);

--- 13 unchanged lines hidden (view full) ---

919 * read a file to calculate its crc. This is a real drag. Archive formats
920 * that have this, end up reading the file twice (we have to write the
921 * header WITH the crc before writing the file contents. Oh well...
922 * Return:
923 * 0 if was able to calculate the crc, -1 otherwise
924 */
925
926int
898{
899 /*
900 * make sure the file is open
901 */
902 if (*fd < 0)
903 return;
904
905 (void)close(*fd);

--- 13 unchanged lines hidden (view full) ---

919 * read a file to calculate its crc. This is a real drag. Archive formats
920 * that have this, end up reading the file twice (we have to write the
921 * header WITH the crc before writing the file contents. Oh well...
922 * Return:
923 * 0 if was able to calculate the crc, -1 otherwise
924 */
925
926int
927set_crc(register ARCHD *arcn, register int fd)
927set_crc(ARCHD *arcn, int fd)
928{
928{
929 register int i;
930 register int res;
929 int i;
930 int res;
931 off_t cpcnt = 0L;
932 u_long size;
933 unsigned long crc = 0L;
934 char tbuf[FILEBLK];
935 struct stat sb;
936
937 if (fd < 0) {
938 /*

--- 39 unchanged lines hidden ---
931 off_t cpcnt = 0L;
932 u_long size;
933 unsigned long crc = 0L;
934 char tbuf[FILEBLK];
935 struct stat sb;
936
937 if (fd < 0) {
938 /*

--- 39 unchanged lines hidden ---