Annotation of src/usr.bin/ar/replace.c, Revision 1.6
1.6 ! deraadt 1: /* $OpenBSD: replace.c,v 1.5 2003/06/03 02:56:05 millert 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.
1.5 millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
37: #if 0
38: static char sccsid[] = "@(#)replace.c 8.3 (Berkeley) 4/2/94";
39: #else
1.6 ! deraadt 40: static char rcsid[] = "$OpenBSD: replace.c,v 1.5 2003/06/03 02:56:05 millert Exp $";
1.1 deraadt 41: #endif
42: #endif /* not lint */
43:
44: #include <sys/param.h>
45: #include <sys/stat.h>
46:
47: #include <ar.h>
48: #include <dirent.h>
49: #include <err.h>
50: #include <fcntl.h>
51: #include <stdio.h>
52: #include <string.h>
53: #include <unistd.h>
54:
55: #include "archive.h"
56: #include "extern.h"
57:
58: /*
59: * replace --
60: * Replace or add named members to archive. Entries already in the
61: * archive are swapped in place. Others are added before or after
62: * the key entry, based on the a, b and i options. If the u option
63: * is specified, modification dates select for replacement.
64: */
65: int
1.6 ! deraadt 66: replace(char **argv)
1.1 deraadt 67: {
68: char *file;
69: int afd, curfd, errflg, exists, mods, sfd, tfd1, tfd2;
70: struct stat sb;
71: CF cf;
72: off_t size, tsize;
73:
74: errflg = 0;
75: /*
76: * If doesn't exist, simply append to the archive. There's
77: * a race here, but it's pretty short, and not worth fixing.
78: */
79: exists = !stat(archive, &sb);
80: afd = open_archive(O_CREAT|O_RDWR);
81:
82: if (!exists) {
83: tfd1 = -1;
84: tfd2 = tmp();
85: goto append;
86: }
87:
88: tfd1 = tmp(); /* Files before key file. */
89: tfd2 = tmp(); /* Files after key file. */
90:
91: /*
92: * Break archive into two parts -- entries before and after the key
93: * entry. If positioning before the key, place the key at the
94: * beginning of the after key entries and if positioning after the
95: * key, place the key at the end of the before key entries. Put it
96: * all back together at the end.
97: */
98: mods = (options & (AR_A|AR_B));
99: for (curfd = tfd1; get_arobj(afd);) {
100: if (*argv && (file = files(argv))) {
101: if ((sfd = open(file, O_RDONLY)) < 0) {
102: errflg = 1;
103: warn("%s", file);
104: goto useold;
105: }
106: (void)fstat(sfd, &sb);
107: if (options & AR_U && sb.st_mtime <= chdr.date) {
108: close(sfd);
109: goto useold;
110: }
111:
112: if (options & AR_V)
113: (void)printf("r - %s\n", file);
114:
115: /* Read from disk, write to an archive; pad on write */
116: SETCF(sfd, file, curfd, tname, WPAD);
117: put_arobj(&cf, &sb);
118: (void)close(sfd);
119: skip_arobj(afd);
120: continue;
121: }
122:
123: if (mods && compare(posname)) {
124: mods = 0;
125: if (options & AR_B)
126: curfd = tfd2;
127: /* Read and write to an archive; pad on both. */
128: SETCF(afd, archive, curfd, tname, RPAD|WPAD);
1.3 kstailey 129: put_arobj(&cf, NULL);
1.1 deraadt 130: if (options & AR_A)
131: curfd = tfd2;
132: } else {
133: /* Read and write to an archive; pad on both. */
134: useold: SETCF(afd, archive, curfd, tname, RPAD|WPAD);
1.3 kstailey 135: put_arobj(&cf, NULL);
1.1 deraadt 136: }
137: }
138:
139: if (mods) {
140: warnx("%s: archive member not found", posarg);
141: close_archive(afd);
142: return (1);
143: }
144:
145: /* Append any left-over arguments to the end of the after file. */
1.4 deraadt 146: append: while ((file = *argv++)) {
1.1 deraadt 147: if (options & AR_V)
148: (void)printf("a - %s\n", file);
149: if ((sfd = open(file, O_RDONLY)) < 0) {
150: errflg = 1;
151: warn("%s", file);
152: continue;
153: }
154: (void)fstat(sfd, &sb);
155: /* Read from disk, write to an archive; pad on write. */
156: SETCF(sfd, file,
157: options & (AR_A|AR_B) ? tfd1 : tfd2, tname, WPAD);
158: put_arobj(&cf, &sb);
159: (void)close(sfd);
160: }
161:
162: (void)lseek(afd, (off_t)SARMAG, SEEK_SET);
163:
164: SETCF(tfd1, tname, afd, archive, NOPAD);
165: if (tfd1 != -1) {
166: tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR);
167: (void)lseek(tfd1, (off_t)0, SEEK_SET);
168: copy_ar(&cf, size);
169: } else
170: tsize = 0;
171:
172: tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR);
173: (void)lseek(tfd2, (off_t)0, SEEK_SET);
174: cf.rfd = tfd2;
175: copy_ar(&cf, size);
176:
177: (void)ftruncate(afd, tsize + SARMAG);
178: close_archive(afd);
179: return (errflg);
180: }