Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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  * @(#)archive.c        8.3 (Berkeley) 4/2/94
37  * $FreeBSD: src/usr.bin/ar/archive.c,v 1.10.6.1 2001/08/02 00:51:00 obrien Exp $
38  * $DragonFly: src/usr.bin/ar/Attic/archive.c,v 1.2 2003/06/17 04:29:25 dillon Exp $
39  */
40
41 #include <sys/param.h>
42 #include <sys/stat.h>
43
44 #include <ar.h>
45 #include <dirent.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <libgen.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #include "archive.h"
56 #include "extern.h"
57
58 typedef struct ar_hdr HDR;
59 static char hb[sizeof(HDR) + 1];        /* real header */
60
61 int
62 open_archive(mode)
63         int mode;
64 {
65         int created, fd, nr;
66         char buf[SARMAG];
67
68         created = 0;
69         if (mode & O_CREAT) {
70                 mode |= O_EXCL;
71                 if ((fd = open(archive, mode, DEFFILEMODE)) >= 0) {
72                         /* POSIX.2 puts create message on stderr. */
73                         if (!(options & AR_C))
74                                 warnx("creating archive %s", archive);
75                         created = 1;
76                         goto opened;
77                 }
78                 if (errno != EEXIST)
79                         error(archive);
80                 mode &= ~O_EXCL;
81         }
82         if ((fd = open(archive, mode, DEFFILEMODE)) < 0)
83                 error(archive);
84
85         /*
86          * Attempt to place a lock on the opened file - if we get an
87          * error then someone is already working on this library (or
88          * it's going across NFS).
89          */
90 opened: if (flock(fd, LOCK_EX|LOCK_NB) && errno != EOPNOTSUPP)
91                 error(archive);
92
93         /*
94          * If not created, O_RDONLY|O_RDWR indicates that it has to be
95          * in archive format.
96          */
97         if (!created &&
98             ((mode & O_ACCMODE) == O_RDONLY || (mode & O_ACCMODE) == O_RDWR)) {
99                 if ((nr = read(fd, buf, SARMAG) != SARMAG)) {
100                         if (nr >= 0)
101                                 badfmt();
102                         error(archive);
103                 } else if (bcmp(buf, ARMAG, SARMAG))
104                         badfmt();
105         } else if (write(fd, ARMAG, SARMAG) != SARMAG)
106                 error(archive);
107         return (fd);
108 }
109
110 void
111 close_archive(fd)
112         int fd;
113 {
114
115         (void)close(fd);                        /* Implicit unlock. */
116 }
117
118 /* Convert ar header field to an integer. */
119 #define AR_ATOI(from, to, len, base) { \
120         memmove(buf, from, len); \
121         buf[len] = '\0'; \
122         to = strtol(buf, (char **)NULL, base); \
123 }
124
125 /*
126  * get_arobj --
127  *      read the archive header for this member
128  */
129 int
130 get_arobj(fd)
131         int fd;
132 {
133         struct ar_hdr *hdr;
134         int len, nr;
135         char *p, buf[20];
136
137         nr = read(fd, hb, sizeof(HDR));
138         if (nr != sizeof(HDR)) {
139                 if (!nr)
140                         return (0);
141                 if (nr < 0)
142                         error(archive);
143                 badfmt();
144         }
145
146         hdr = (struct ar_hdr *)hb;
147         if (strncmp(hdr->ar_fmag, ARFMAG, sizeof(ARFMAG) - 1))
148                 badfmt();
149
150         /* Convert the header into the internal format. */
151 #define DECIMAL 10
152 #define OCTAL    8
153
154         AR_ATOI(hdr->ar_date, chdr.date, sizeof(hdr->ar_date), DECIMAL);
155         AR_ATOI(hdr->ar_uid, chdr.uid, sizeof(hdr->ar_uid), DECIMAL);
156         AR_ATOI(hdr->ar_gid, chdr.gid, sizeof(hdr->ar_gid), DECIMAL);
157         AR_ATOI(hdr->ar_mode, chdr.mode, sizeof(hdr->ar_mode), OCTAL);
158         AR_ATOI(hdr->ar_size, chdr.size, sizeof(hdr->ar_size), DECIMAL);
159
160         /* Leading spaces should never happen. */
161         if (hdr->ar_name[0] == ' ')
162                 badfmt();
163
164         /*
165          * Long name support.  Set the "real" size of the file, and the
166          * long name flag/size.
167          */
168         if (!bcmp(hdr->ar_name, AR_EFMT1, sizeof(AR_EFMT1) - 1)) {
169                 chdr.lname = len = atoi(hdr->ar_name + sizeof(AR_EFMT1) - 1);
170                 if (len <= 0 || len > MAXNAMLEN)
171                         badfmt();
172                 nr = read(fd, chdr.name, len);
173                 if (nr != len) {
174                         if (nr < 0)
175                                 error(archive);
176                         badfmt();
177                 }
178                 chdr.name[len] = 0;
179                 chdr.size -= len;
180         } else {
181                 chdr.lname = 0;
182                 memmove(chdr.name, hdr->ar_name, sizeof(hdr->ar_name));
183
184                 /* Strip trailing spaces, null terminate. */
185                 for (p = chdr.name + sizeof(hdr->ar_name) - 1; *p == ' '; --p);
186                 *++p = '\0';
187         }
188         return (1);
189 }
190
191 static int already_written;
192
193 /*
194  * put_arobj --
195  *      Write an archive member to a file.
196  */
197 void
198 put_arobj(cfp, sb)
199         CF *cfp;
200         struct stat *sb;
201 {
202         int lname;
203         char *name;
204         struct ar_hdr *hdr;
205         off_t size;
206
207         /*
208          * If passed an sb structure, reading a file from disk.  Get stat(2)
209          * information, build a name and construct a header.  (Files are named
210          * by their last component in the archive.)  If not, then just write
211          * the last header read.
212          */
213         if (sb) {
214                 name = basename(cfp->rname);
215                 (void)fstat(cfp->rfd, sb);
216
217                 /*
218                  * If not truncating names and the name is too long or contains
219                  * a space, use extended format 1.
220                  */
221                 lname = strlen(name);
222                 if (options & AR_TR) {
223                         if (lname > OLDARMAXNAME) {
224                                 (void)fflush(stdout);
225                                 warnx("warning: %s truncated to %.*s",
226                                     name, OLDARMAXNAME, name);
227                                 (void)fflush(stderr);
228                         }
229                         (void)sprintf(hb, HDR3, name,
230                             (long)sb->st_mtimespec.tv_sec, sb->st_uid,
231                             sb->st_gid, sb->st_mode, sb->st_size, ARFMAG);
232                         lname = 0;
233                 } else if (lname > sizeof(hdr->ar_name) || strchr(name, ' '))
234                         (void)sprintf(hb, HDR1, AR_EFMT1, lname,
235                             (long)sb->st_mtimespec.tv_sec, sb->st_uid,
236                             sb->st_gid, sb->st_mode, sb->st_size + lname,
237                             ARFMAG);
238                 else {
239                         lname = 0;
240                         (void)sprintf(hb, HDR2, name,
241                             (long)sb->st_mtimespec.tv_sec, sb->st_uid,
242                             sb->st_gid, sb->st_mode, sb->st_size, ARFMAG);
243                 }
244                 size = sb->st_size;
245         } else {
246                 lname = chdr.lname;
247                 name = chdr.name;
248                 size = chdr.size;
249         }
250
251         if (write(cfp->wfd, hb, sizeof(HDR)) != sizeof(HDR))
252                 error(cfp->wname);
253         if (lname) {
254                 if (write(cfp->wfd, name, lname) != lname)
255                         error(cfp->wname);
256                 already_written = lname;
257         }
258         copy_ar(cfp, size);
259         already_written = 0;
260 }
261
262 /*
263  * copy_ar --
264  *      Copy size bytes from one file to another - taking care to handle the
265  *      extra byte (for odd size files) when reading archives and writing an
266  *      extra byte if necessary when adding files to archive.  The length of
267  *      the object is the long name plus the object itself; the variable
268  *      already_written gets set if a long name was written.
269  *
270  *      The padding is really unnecessary, and is almost certainly a remnant
271  *      of early archive formats where the header included binary data which
272  *      a PDP-11 required to start on an even byte boundary.  (Or, perhaps,
273  *      because 16-bit word addressed copies were faster?)  Anyhow, it should
274  *      have been ripped out long ago.
275  */
276 void
277 copy_ar(cfp, size)
278         CF *cfp;
279         off_t size;
280 {
281         static char pad = '\n';
282         off_t sz;
283         int from, nr = 0, nw, off, to;
284         char buf[8*1024];
285
286         if (!(sz = size))
287                 return;
288
289         from = cfp->rfd;
290         to = cfp->wfd;
291         sz = size;
292         while (sz && (nr = read(from, buf, MIN(sz, sizeof(buf)))) > 0) {
293                 sz -= nr;
294                 for (off = 0; off < nr; nr -= off, off += nw)
295                         if ((nw = write(to, buf + off, nr)) < 0)
296                                 error(cfp->wname);
297         }
298         if (sz) {
299                 if (nr == 0)
300                         badfmt();
301                 error(cfp->rname);
302         }
303
304         if (cfp->flags & RPAD && (size + chdr.lname) & 1 &&
305             (nr = read(from, buf, 1)) != 1) {
306                 if (nr == 0)
307                         badfmt();
308                 error(cfp->rname);
309         }
310         if (cfp->flags & WPAD && (size + already_written) & 1 &&
311             write(to, &pad, 1) != 1)
312                 error(cfp->wname);
313 }
314
315 /*
316  * skip_arobj -
317  *      Skip over an object -- taking care to skip the pad bytes.
318  */
319 void
320 skip_arobj(fd)
321         int fd;
322 {
323         off_t len;
324
325         len = chdr.size + ( (chdr.size + chdr.lname) & 1);
326         if (lseek(fd, len, SEEK_CUR) == (off_t)-1)
327                 error(archive);
328 }