Commit | Line | Data |
---|---|---|
20f08463 KB |
1 | /*- |
2 | * Copyright (c) 1990 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Hugh Smith at The University of Guelph. | |
7 | * | |
8 | * %sccs.include.redist.c% | |
9 | */ | |
10 | ||
11 | #ifndef lint | |
ad4425ce | 12 | static char sccsid[] = "@(#)replace.c 5.8 (Berkeley) %G%"; |
20f08463 KB |
13 | #endif /* not lint */ |
14 | ||
15 | #include <sys/param.h> | |
16 | #include <sys/stat.h> | |
20f08463 | 17 | #include <fcntl.h> |
20f08463 | 18 | #include <dirent.h> |
abe76123 KB |
19 | #include <errno.h> |
20 | #include <unistd.h> | |
20f08463 KB |
21 | #include <ar.h> |
22 | #include <stdio.h> | |
1d7fb98b | 23 | #include <string.h> |
20f08463 | 24 | #include "archive.h" |
1d7fb98b | 25 | #include "extern.h" |
20f08463 KB |
26 | |
27 | extern CHDR chdr; /* converted header */ | |
28 | extern char *archive; /* archive name */ | |
29 | extern char *tname; /* temporary file "name" */ | |
30 | ||
31 | /* | |
32 | * replace -- | |
33 | * Replace or add named members to archive. Entries already in the | |
34 | * archive are swapped in place. Others are added before or after | |
35 | * the key entry, based on the a, b and i options. If the u option | |
36 | * is specified, modification dates select for replacement. | |
37 | */ | |
38 | replace(argv) | |
39 | char **argv; | |
40 | { | |
af52bb8a | 41 | extern char *posarg, *posname; /* positioning file name */ |
20f08463 KB |
42 | register char *file; |
43 | register int afd, curfd, mods, sfd; | |
44 | struct stat sb; | |
45 | CF cf; | |
46 | off_t size, tsize; | |
47 | int err, exists, tfd1, tfd2; | |
48 | char *rname(); | |
49 | ||
ad4425ce | 50 | err = 0; |
20f08463 KB |
51 | /* |
52 | * If doesn't exist, simply append to the archive. There's | |
53 | * a race here, but it's pretty short, and not worth fixing. | |
54 | */ | |
55 | exists = !stat(archive, &sb); | |
56 | afd = open_archive(O_CREAT|O_RDWR); | |
57 | ||
58 | if (!exists) { | |
59 | tfd1 = -1; | |
60 | tfd2 = tmp(); | |
61 | goto append; | |
62 | } | |
63 | ||
64 | tfd1 = tmp(); /* Files before key file. */ | |
65 | tfd2 = tmp(); /* Files after key file. */ | |
20f08463 KB |
66 | |
67 | /* | |
68 | * Break archive into two parts -- entries before and after the key | |
69 | * entry. If positioning before the key, place the key at the | |
70 | * beginning of the after key entries and if positioning after the | |
71 | * key, place the key at the end of the before key entries. Put it | |
72 | * all back together at the end. | |
73 | */ | |
bf53959e | 74 | mods = (options & (AR_A|AR_B)); |
ad4425ce | 75 | for (curfd = tfd1; get_arobj(afd);) { |
1d7fb98b | 76 | if (*argv && (file = files(argv))) { |
20f08463 KB |
77 | if ((sfd = open(file, O_RDONLY)) < 0) { |
78 | err = 1; | |
79 | (void)fprintf(stderr, "ar: %s: %s.\n", | |
80 | file, strerror(errno)); | |
81 | goto useold; | |
82 | } | |
83 | (void)fstat(sfd, &sb); | |
84 | if (options & AR_U && sb.st_mtime <= chdr.date) | |
85 | goto useold; | |
86 | ||
87 | if (options & AR_V) | |
af52bb8a | 88 | (void)printf("r - %s\n", file); |
20f08463 | 89 | |
abe76123 | 90 | /* Read from disk, write to an archive; pad on write */ |
20f08463 | 91 | SETCF(sfd, file, curfd, tname, WPAD); |
ce478bb7 | 92 | put_arobj(&cf, &sb); |
20f08463 | 93 | (void)close(sfd); |
ce478bb7 | 94 | skip_arobj(afd); |
20f08463 KB |
95 | continue; |
96 | } | |
97 | ||
98 | if (mods && compare(posname)) { | |
99 | mods = 0; | |
100 | if (options & AR_B) | |
101 | curfd = tfd2; | |
abe76123 | 102 | /* Read and write to an archive; pad on both. */ |
20f08463 | 103 | SETCF(afd, archive, curfd, tname, RPAD|WPAD); |
ce478bb7 | 104 | put_arobj(&cf, (struct stat *)NULL); |
20f08463 KB |
105 | if (options & AR_A) |
106 | curfd = tfd2; | |
107 | } else { | |
abe76123 | 108 | /* Read and write to an archive; pad on both. */ |
20f08463 | 109 | useold: SETCF(afd, archive, curfd, tname, RPAD|WPAD); |
ce478bb7 | 110 | put_arobj(&cf, (struct stat *)NULL); |
20f08463 KB |
111 | } |
112 | } | |
113 | ||
114 | if (mods) { | |
115 | (void)fprintf(stderr, "ar: %s: archive member not found.\n", | |
af52bb8a | 116 | posarg); |
20f08463 KB |
117 | close_archive(afd); |
118 | return(1); | |
119 | } | |
120 | ||
121 | /* Append any left-over arguments to the end of the after file. */ | |
122 | append: while (file = *argv++) { | |
123 | if (options & AR_V) | |
af52bb8a | 124 | (void)printf("a - %s\n", file); |
20f08463 KB |
125 | if ((sfd = open(file, O_RDONLY)) < 0) { |
126 | err = 1; | |
127 | (void)fprintf(stderr, "ar: %s: %s.\n", | |
128 | file, strerror(errno)); | |
129 | continue; | |
130 | } | |
131 | (void)fstat(sfd, &sb); | |
abe76123 | 132 | /* Read from disk, write to an archive; pad on write. */ |
bf53959e KB |
133 | SETCF(sfd, file, |
134 | options & (AR_A|AR_B) ? tfd1 : tfd2, tname, WPAD); | |
ce478bb7 | 135 | put_arobj(&cf, &sb); |
20f08463 KB |
136 | (void)close(sfd); |
137 | } | |
138 | ||
139 | (void)lseek(afd, (off_t)SARMAG, SEEK_SET); | |
140 | ||
abe76123 | 141 | SETCF(tfd1, tname, afd, archive, NOPAD); |
20f08463 KB |
142 | if (tfd1 != -1) { |
143 | tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR); | |
144 | (void)lseek(tfd1, (off_t)0, SEEK_SET); | |
ce478bb7 | 145 | copy_ar(&cf, size); |
20f08463 KB |
146 | } else |
147 | tsize = 0; | |
148 | ||
149 | tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR); | |
150 | (void)lseek(tfd2, (off_t)0, SEEK_SET); | |
151 | cf.rfd = tfd2; | |
ce478bb7 | 152 | copy_ar(&cf, size); |
20f08463 KB |
153 | |
154 | (void)ftruncate(afd, tsize + SARMAG); | |
155 | close_archive(afd); | |
156 | return(err); | |
157 | } |