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