Annotation of src/usr.bin/ar/replace.c, Revision 1.7
1.7 ! deraadt 1: /* $OpenBSD: replace.c,v 1.6 2003/06/12 20:58:08 deraadt 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: #include <sys/param.h>
37: #include <sys/stat.h>
38:
39: #include <ar.h>
40: #include <dirent.h>
41: #include <err.h>
42: #include <fcntl.h>
43: #include <stdio.h>
44: #include <string.h>
45: #include <unistd.h>
46:
47: #include "archive.h"
48: #include "extern.h"
49:
50: /*
51: * replace --
52: * Replace or add named members to archive. Entries already in the
53: * archive are swapped in place. Others are added before or after
54: * the key entry, based on the a, b and i options. If the u option
55: * is specified, modification dates select for replacement.
56: */
57: int
1.6 deraadt 58: replace(char **argv)
1.1 deraadt 59: {
60: char *file;
61: int afd, curfd, errflg, exists, mods, sfd, tfd1, tfd2;
62: struct stat sb;
63: CF cf;
64: off_t size, tsize;
65:
66: errflg = 0;
67: /*
68: * If doesn't exist, simply append to the archive. There's
69: * a race here, but it's pretty short, and not worth fixing.
70: */
71: exists = !stat(archive, &sb);
72: afd = open_archive(O_CREAT|O_RDWR);
73:
74: if (!exists) {
75: tfd1 = -1;
76: tfd2 = tmp();
77: goto append;
78: }
79:
80: tfd1 = tmp(); /* Files before key file. */
81: tfd2 = tmp(); /* Files after key file. */
82:
83: /*
84: * Break archive into two parts -- entries before and after the key
85: * entry. If positioning before the key, place the key at the
86: * beginning of the after key entries and if positioning after the
87: * key, place the key at the end of the before key entries. Put it
88: * all back together at the end.
89: */
90: mods = (options & (AR_A|AR_B));
91: for (curfd = tfd1; get_arobj(afd);) {
92: if (*argv && (file = files(argv))) {
93: if ((sfd = open(file, O_RDONLY)) < 0) {
94: errflg = 1;
95: warn("%s", file);
96: goto useold;
97: }
98: (void)fstat(sfd, &sb);
99: if (options & AR_U && sb.st_mtime <= chdr.date) {
100: close(sfd);
101: goto useold;
102: }
103:
104: if (options & AR_V)
105: (void)printf("r - %s\n", file);
106:
107: /* Read from disk, write to an archive; pad on write */
108: SETCF(sfd, file, curfd, tname, WPAD);
109: put_arobj(&cf, &sb);
110: (void)close(sfd);
111: skip_arobj(afd);
112: continue;
113: }
114:
115: if (mods && compare(posname)) {
116: mods = 0;
117: if (options & AR_B)
118: curfd = tfd2;
119: /* Read and write to an archive; pad on both. */
120: SETCF(afd, archive, curfd, tname, RPAD|WPAD);
1.3 kstailey 121: put_arobj(&cf, NULL);
1.1 deraadt 122: if (options & AR_A)
123: curfd = tfd2;
124: } else {
125: /* Read and write to an archive; pad on both. */
126: useold: SETCF(afd, archive, curfd, tname, RPAD|WPAD);
1.3 kstailey 127: put_arobj(&cf, NULL);
1.1 deraadt 128: }
129: }
130:
131: if (mods) {
132: warnx("%s: archive member not found", posarg);
133: close_archive(afd);
134: return (1);
135: }
136:
137: /* Append any left-over arguments to the end of the after file. */
1.4 deraadt 138: append: while ((file = *argv++)) {
1.1 deraadt 139: if (options & AR_V)
140: (void)printf("a - %s\n", file);
141: if ((sfd = open(file, O_RDONLY)) < 0) {
142: errflg = 1;
143: warn("%s", file);
144: continue;
145: }
146: (void)fstat(sfd, &sb);
147: /* Read from disk, write to an archive; pad on write. */
148: SETCF(sfd, file,
149: options & (AR_A|AR_B) ? tfd1 : tfd2, tname, WPAD);
150: put_arobj(&cf, &sb);
151: (void)close(sfd);
152: }
153:
154: (void)lseek(afd, (off_t)SARMAG, SEEK_SET);
155:
156: SETCF(tfd1, tname, afd, archive, NOPAD);
157: if (tfd1 != -1) {
158: tsize = size = lseek(tfd1, (off_t)0, SEEK_CUR);
159: (void)lseek(tfd1, (off_t)0, SEEK_SET);
160: copy_ar(&cf, size);
161: } else
162: tsize = 0;
163:
164: tsize += size = lseek(tfd2, (off_t)0, SEEK_CUR);
165: (void)lseek(tfd2, (off_t)0, SEEK_SET);
166: cf.rfd = tfd2;
167: copy_ar(&cf, size);
168:
169: (void)ftruncate(afd, tsize + SARMAG);
170: close_archive(afd);
171: return (errflg);
172: }