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