Annotation of src/usr.bin/ar/replace.c, Revision 1.4
1.4 ! deraadt 1: /* $OpenBSD: replace.c,v 1.3 1997/06/17 20:47:09 kstailey Exp $ */
1.1 deraadt 2: /* $NetBSD: replace.c,v 1.6 1995/03/26 03:28:01 glass Exp $ */
3:
4: /*-
5: * Copyright (c) 1990, 1993, 1994
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Hugh Smith at The University of Guelph.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
19: * 3. All advertising materials mentioning features or use of this software
20: * must display the following acknowledgement:
21: * This product includes software developed by the University of
22: * California, Berkeley and its contributors.
23: * 4. 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: #ifndef lint
41: #if 0
42: static char sccsid[] = "@(#)replace.c 8.3 (Berkeley) 4/2/94";
43: #else
1.4 ! deraadt 44: static char rcsid[] = "$OpenBSD: replace.c,v 1.3 1997/06/17 20:47:09 kstailey Exp $";
1.1 deraadt 45: #endif
46: #endif /* not lint */
47:
48: #include <sys/param.h>
49: #include <sys/stat.h>
50:
51: #include <ar.h>
52: #include <dirent.h>
53: #include <err.h>
54: #include <fcntl.h>
55: #include <stdio.h>
56: #include <string.h>
57: #include <unistd.h>
58:
59: #include "archive.h"
60: #include "extern.h"
61:
62: /*
63: * replace --
64: * Replace or add named members to archive. Entries already in the
65: * archive are swapped in place. Others are added before or after
66: * the key entry, based on the a, b and i options. If the u option
67: * is specified, modification dates select for replacement.
68: */
69: int
70: replace(argv)
71: char **argv;
72: {
73: char *file;
74: int afd, curfd, errflg, exists, mods, sfd, tfd1, tfd2;
75: struct stat sb;
76: CF cf;
77: off_t size, tsize;
78:
79: errflg = 0;
80: /*
81: * If doesn't exist, simply append to the archive. There's
82: * a race here, but it's pretty short, and not worth fixing.
83: */
84: exists = !stat(archive, &sb);
85: afd = open_archive(O_CREAT|O_RDWR);
86:
87: if (!exists) {
88: tfd1 = -1;
89: tfd2 = tmp();
90: goto append;
91: }
92:
93: tfd1 = tmp(); /* Files before key file. */
94: tfd2 = tmp(); /* Files after key file. */
95:
96: /*
97: * Break archive into two parts -- entries before and after the key
98: * entry. If positioning before the key, place the key at the
99: * beginning of the after key entries and if positioning after the
100: * key, place the key at the end of the before key entries. Put it
101: * all back together at the end.
102: */
103: mods = (options & (AR_A|AR_B));
104: for (curfd = tfd1; get_arobj(afd);) {
105: if (*argv && (file = files(argv))) {
106: if ((sfd = open(file, O_RDONLY)) < 0) {
107: errflg = 1;
108: warn("%s", file);
109: goto useold;
110: }
111: (void)fstat(sfd, &sb);
112: if (options & AR_U && sb.st_mtime <= chdr.date) {
113: close(sfd);
114: goto useold;
115: }
116:
117: if (options & AR_V)
118: (void)printf("r - %s\n", file);
119:
120: /* Read from disk, write to an archive; pad on write */
121: SETCF(sfd, file, curfd, tname, WPAD);
122: put_arobj(&cf, &sb);
123: (void)close(sfd);
124: skip_arobj(afd);
125: continue;
126: }
127:
128: if (mods && compare(posname)) {
129: mods = 0;
130: if (options & AR_B)
131: curfd = tfd2;
132: /* Read and write to an archive; pad on both. */
133: SETCF(afd, archive, curfd, tname, RPAD|WPAD);
1.3 kstailey 134: put_arobj(&cf, NULL);
1.1 deraadt 135: if (options & AR_A)
136: curfd = tfd2;
137: } else {
138: /* Read and write to an archive; pad on both. */
139: useold: SETCF(afd, archive, curfd, tname, RPAD|WPAD);
1.3 kstailey 140: put_arobj(&cf, NULL);
1.1 deraadt 141: }
142: }
143:
144: if (mods) {
145: warnx("%s: archive member not found", posarg);
146: close_archive(afd);
147: return (1);
148: }
149:
150: /* Append any left-over arguments to the end of the after file. */
1.4 ! deraadt 151: append: while ((file = *argv++)) {
1.1 deraadt 152: if (options & AR_V)
153: (void)printf("a - %s\n", file);
154: if ((sfd = open(file, O_RDONLY)) < 0) {
155: errflg = 1;
156: warn("%s", file);
157: continue;
158: }
159: (void)fstat(sfd, &sb);
160: /* Read from disk, write to an archive; pad on write. */
161: SETCF(sfd, file,
162: options & (AR_A|AR_B) ? tfd1 : tfd2, tname, WPAD);
163: put_arobj(&cf, &sb);
164: (void)close(sfd);
165: }
166:
167: (void)lseek(afd, (off_t)SARMAG, SEEK_SET);
168:
169: SETCF(tfd1, tname, afd, archive, NOPAD);
170: if (tfd1 != -1) {
171: tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR);
172: (void)lseek(tfd1, (off_t)0, SEEK_SET);
173: copy_ar(&cf, size);
174: } else
175: tsize = 0;
176:
177: tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR);
178: (void)lseek(tfd2, (off_t)0, SEEK_SET);
179: cf.rfd = tfd2;
180: copy_ar(&cf, size);
181:
182: (void)ftruncate(afd, tsize + SARMAG);
183: close_archive(afd);
184: return (errflg);
185: }