Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / ar / archive.c
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  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #ifndef lint
38 static const char sccsid[] = "@(#)archive.c     8.3 (Berkeley) 4/2/94";
39 static const char rcsid[] =
40   "$FreeBSD: src/usr.bin/ar/archive.c,v 1.10.6.1 2001/08/02 00:51:00 obrien Exp $";
41 #endif /* not lint */
42
43 #include <sys/param.h>
44 #include <sys/stat.h>
45
46 #include <ar.h>
47 #include <dirent.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <libgen.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56
57 #include "archive.h"
58 #include "extern.h"
59
60 typedef struct ar_hdr HDR;
61 static char hb[sizeof(HDR) + 1];        /* real header */
62
63 int
64 open_archive(mode)
65         int mode;
66 {
67         int created, fd, nr;
68         char buf[SARMAG];
69
70         created = 0;
71         if (mode & O_CREAT) {
72                 mode |= O_EXCL;
73                 if ((fd = open(archive, mode, DEFFILEMODE)) >= 0) {
74                         /* POSIX.2 puts create message on stderr. */
75                         if (!(options & AR_C))
76                                 warnx("creating archive %s", archive);
77                         created = 1;
78                         goto opened;
79                 }
80                 if (errno != EEXIST)
81                         error(archive);
82                 mode &= ~O_EXCL;
83         }
84         if ((fd = open(archive, mode, DEFFILEMODE)) < 0)
85                 error(archive);
86
87         /*
88          * Attempt to place a lock on the opened file - if we get an
89          * error then someone is already working on this library (or
90          * it's going across NFS).
91          */
92 opened: if (flock(fd, LOCK_EX|LOCK_NB) && errno != EOPNOTSUPP)
93                 error(archive);
94
95         /*
96          * If not created, O_RDONLY|O_RDWR indicates that it has to be
97          * in archive format.
98          */
99         if (!created &&
100             ((mode & O_ACCMODE) == O_RDONLY || (mode & O_ACCMODE) == O_RDWR)) {
101                 if ((nr = read(fd, buf, SARMAG) != SARMAG)) {
102                         if (nr >= 0)
103                                 badfmt();
104                         error(archive);
105                 } else if (bcmp(buf, ARMAG, SARMAG))
106                         badfmt();
107         } else if (write(fd, ARMAG, SARMAG) != SARMAG)
108                 error(archive);
109         return (fd);
110 }
111
112 void
113 close_archive(fd)
114         int fd;
115 {
116
117         (void)close(fd);                        /* Implicit unlock. */
118 }
119
120 /* Convert ar header field to an integer. */
121 #define AR_ATOI(from, to, len, base) { \
122         memmove(buf, from, len); \
123         buf[len] = '\0'; \
124         to = strtol(buf, (char **)NULL, base); \
125 }
126
127 /*
128  * get_arobj --
129  *      read the archive header for this member
130  */
131 int
132 get_arobj(fd)
133         int fd;
134 {
135         struct ar_hdr *hdr;
136         int len, nr;
137         char *p, buf[20];
138
139         nr = read(fd, hb, sizeof(HDR));
140         if (nr != sizeof(HDR)) {
141                 if (!nr)
142                         return (0);
143                 if (nr < 0)
144                         error(archive);
145                 badfmt();
146         }
147
148         hdr = (struct ar_hdr *)hb;
149         if (strncmp(hdr->ar_fmag, ARFMAG, sizeof(ARFMAG) - 1))
150                 badfmt();
151
152         /* Convert the header into the internal format. */
153 #define DECIMAL 10
154 #define OCTAL    8
155
156         AR_ATOI(hdr->ar_date, chdr.date, sizeof(hdr->ar_date), DECIMAL);
157         AR_ATOI(hdr->ar_uid, chdr.uid, sizeof(hdr->ar_uid), DECIMAL);
158         AR_ATOI(hdr->ar_gid, chdr.gid, sizeof(hdr->ar_gid), DECIMAL);
159         AR_ATOI(hdr->ar_mode, chdr.mode, sizeof(hdr->ar_mode), OCTAL);
160         AR_ATOI(hdr->ar_size, chdr.size, sizeof(hdr->ar_size), DECIMAL);
161
162         /* Leading spaces should never happen. */
163         if (hdr->ar_name[0] == ' ')
164                 badfmt();
165
166         /*
167          * Long name support.  Set the "real" size of the file, and the
168          * long name flag/size.
169          */
170         if (!bcmp(hdr->ar_name, AR_EFMT1, sizeof(AR_EFMT1) - 1)) {
171                 chdr.lname = len = atoi(hdr->ar_name + sizeof(AR_EFMT1) - 1);
172                 if (len <= 0 || len > MAXNAMLEN)
173                         badfmt();
174                 nr = read(fd, chdr.name, len);
175                 if (nr != len) {
176                         if (nr < 0)
177                                 error(archive);
178                         badfmt();
179                 }
180                 chdr.name[len] = 0;
181                 chdr.size -= len;
182         } else {
183                 chdr.lname = 0;
184                 memmove(chdr.name, hdr->ar_name, sizeof(hdr->ar_name));
185
186                 /* Strip trailing spaces, null terminate. */
187                 for (p = chdr.name + sizeof(hdr->ar_name) - 1; *p == ' '; --p);
188                 *++p = '\0';
189         }
190         return (1);
191 }
192
193 static int already_written;
194
195 /*
196  * put_arobj --
197  *      Write an archive member to a file.
198  */
199 void
200 put_arobj(cfp, sb)
201         CF *cfp;
202         struct stat *sb;
203 {
204         int lname;
205         char *name;
206         struct ar_hdr *hdr;
207         off_t size;
208
209         /*
210          * If passed an sb structure, reading a file from disk.  Get stat(2)
211          * information, build a name and construct a header.  (Files are named
212          * by their last component in the archive.)  If not, then just write
213          * the last header read.
214          */
215         if (sb) {
216                 name = basename(cfp->rname);
217                 (void)fstat(cfp->rfd, sb);
218
219                 /*
220                  * If not truncating names and the name is too long or contains
221                  * a space, use extended format 1.
222                  */
223                 lname = strlen(name);
224                 if (options & AR_TR) {
225                         if (lname > OLDARMAXNAME) {
226                                 (void)fflush(stdout);
227                                 warnx("warning: %s truncated to %.*s",
228                                     name, OLDARMAXNAME, name);
229                                 (void)fflush(stderr);
230                         }
231                         (void)sprintf(hb, HDR3, name,
232                             (long)sb->st_mtimespec.tv_sec, sb->st_uid,
233                             sb->st_gid, sb->st_mode, sb->st_size, ARFMAG);
234                         lname = 0;
235                 } else if (lname > sizeof(hdr->ar_name) || strchr(name, ' '))
236                         (void)sprintf(hb, HDR1, AR_EFMT1, lname,
237                             (long)sb->st_mtimespec.tv_sec, sb->st_uid,
238                             sb->st_gid, sb->st_mode, sb->st_size + lname,
239                             ARFMAG);
240                 else {
241                         lname = 0;
242                         (void)sprintf(hb, HDR2, name,
243                             (long)sb->st_mtimespec.tv_sec, sb->st_uid,
244                             sb->st_gid, sb->st_mode, sb->st_size, ARFMAG);
245                 }
246                 size = sb->st_size;
247         } else {
248                 lname = chdr.lname;
249                 name = chdr.name;
250                 size = chdr.size;
251         }
252
253         if (write(cfp->wfd, hb, sizeof(HDR)) != sizeof(HDR))
254                 error(cfp->wname);
255         if (lname) {
256                 if (write(cfp->wfd, name, lname) != lname)
257                         error(cfp->wname);
258                 already_written = lname;
259         }
260         copy_ar(cfp, size);
261         already_written = 0;
262 }
263
264 /*
265  * copy_ar --
266  *      Copy size bytes from one file to another - taking care to handle the
267  *      extra byte (for odd size files) when reading archives and writing an
268  *      extra byte if necessary when adding files to archive.  The length of
269  *      the object is the long name plus the object itself; the variable
270  *      already_written gets set if a long name was written.
271  *
272  *      The padding is really unnecessary, and is almost certainly a remnant
273  *      of early archive formats where the header included binary data which
274  *      a PDP-11 required to start on an even byte boundary.  (Or, perhaps,
275  *      because 16-bit word addressed copies were faster?)  Anyhow, it should
276  *      have been ripped out long ago.
277  */
278 void
279 copy_ar(cfp, size)
280         CF *cfp;
281         off_t size;
282 {
283         static char pad = '\n';
284         off_t sz;
285         int from, nr = 0, nw, off, to;
286         char buf[8*1024];
287
288         if (!(sz = size))
289                 return;
290
291         from = cfp->rfd;
292         to = cfp->wfd;
293         sz = size;
294         while (sz && (nr = read(from, buf, MIN(sz, sizeof(buf)))) > 0) {
295                 sz -= nr;
296                 for (off = 0; off < nr; nr -= off, off += nw)
297                         if ((nw = write(to, buf + off, nr)) < 0)
298                                 error(cfp->wname);
299         }
300         if (sz) {
301                 if (nr == 0)
302                         badfmt();
303                 error(cfp->rname);
304         }
305
306         if (cfp->flags & RPAD && (size + chdr.lname) & 1 &&
307             (nr = read(from, buf, 1)) != 1) {
308                 if (nr == 0)
309                         badfmt();
310                 error(cfp->rname);
311         }
312         if (cfp->flags & WPAD && (size + already_written) & 1 &&
313             write(to, &pad, 1) != 1)
314                 error(cfp->wname);
315 }
316
317 /*
318  * skip_arobj -
319  *      Skip over an object -- taking care to skip the pad bytes.
320  */
321 void
322 skip_arobj(fd)
323         int fd;
324 {
325         off_t len;
326
327         len = chdr.size + ( (chdr.size + chdr.lname) & 1);
328         if (lseek(fd, len, SEEK_CUR) == (off_t)-1)
329                 error(archive);
330 }