blob: 818115bd93823be31435d90288a8ca32d4b1712f [file] [log] [blame]
Brian Silverman86497922018-02-10 19:28:39 -05001/* Create, modify, and extract from archives.
2 Copyright (C) 2005-2012, 2016, 2017 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2005.
5
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 elfutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19#ifdef HAVE_CONFIG_H
20# include <config.h>
21#endif
22
23#include <argp.h>
24#include <assert.h>
25#include <error.h>
26#include <fcntl.h>
27#include <gelf.h>
28#include <libintl.h>
29#include <limits.h>
30#include <locale.h>
31#include <search.h>
32#include <stdbool.h>
33#include <stdlib.h>
34#include <stdio.h>
35#include <stdio_ext.h>
36#include <string.h>
37#include <time.h>
38#include <unistd.h>
39#include <sys/mman.h>
40#include <sys/stat.h>
41#include <sys/time.h>
42
43#include <system.h>
44#include <printversion.h>
45
46#include "arlib.h"
47
48
49/* Name and version of program. */
50ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
51
52/* Prototypes for local functions. */
53static int do_oper_extract (int oper, const char *arfname, char **argv,
54 int argc, long int instance);
55static int do_oper_delete (const char *arfname, char **argv, int argc,
56 long int instance);
57static int do_oper_insert (int oper, const char *arfname, char **argv,
58 int argc, const char *member);
59
60
61/* Bug report address. */
62ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
63
64
65/* Definitions of arguments for argp functions. */
66static const struct argp_option options[] =
67{
68 { NULL, 0, NULL, 0, N_("Commands:"), 1 },
69 { NULL, 'd', NULL, 0, N_("Delete files from archive."), 0 },
70 { NULL, 'm', NULL, 0, N_("Move files in archive."), 0 },
71 { NULL, 'p', NULL, 0, N_("Print files in archive."), 0 },
72 { NULL, 'q', NULL, 0, N_("Quick append files to archive."), 0 },
73 { NULL, 'r', NULL, 0,
74 N_("Replace existing or insert new file into archive."), 0 },
75 { NULL, 't', NULL, 0, N_("Display content of archive."), 0 },
76 { NULL, 'x', NULL, 0, N_("Extract files from archive."), 0 },
77
78 { NULL, 0, NULL, 0, N_("Command Modifiers:"), 2 },
79 { NULL, 'o', NULL, 0, N_("Preserve original dates."), 0 },
80 { NULL, 'N', NULL, 0, N_("Use instance [COUNT] of name."), 0 },
81 { NULL, 'C', NULL, 0,
82 N_("Do not replace existing files with extracted files."), 0 },
83 { NULL, 'T', NULL, 0, N_("Allow filename to be truncated if necessary."),
84 0 },
85 { NULL, 'v', NULL, 0, N_("Provide verbose output."), 0 },
86 { NULL, 's', NULL, 0, N_("Force regeneration of symbol table."), 0 },
87 { NULL, 'a', NULL, 0, N_("Insert file after [MEMBER]."), 0 },
88 { NULL, 'b', NULL, 0, N_("Insert file before [MEMBER]."), 0 },
89 { NULL, 'i', NULL, 0, N_("Same as -b."), 0 },
90 { NULL, 'c', NULL, 0, N_("Suppress message when library has to be created."),
91 0 },
92 { NULL, 'P', NULL, 0, N_("Use full path for file matching."), 0 },
93 { NULL, 'u', NULL, 0, N_("Update only older files in archive."), 0 },
94
95 { NULL, 0, NULL, 0, NULL, 0 }
96};
97
98/* Short description of program. */
99static const char doc[] = N_("Create, modify, and extract from archives.");
100
101/* Strings for arguments in help texts. */
102static const char args_doc[] = N_("[MEMBER] [COUNT] ARCHIVE [FILE...]");
103
104/* Prototype for option handler. */
105static error_t parse_opt (int key, char *arg, struct argp_state *state);
106
107/* Data structure to communicate with argp functions. */
108static struct argp argp =
109{
110 options, parse_opt, args_doc, doc, arlib_argp_children, NULL, NULL
111};
112
113
114/* What operation to perform. */
115static enum
116 {
117 oper_none,
118 oper_delete,
119 oper_move,
120 oper_print,
121 oper_qappend,
122 oper_replace,
123 oper_list,
124 oper_extract
125 } operation;
126
127/* Modifiers. */
128static bool verbose;
129static bool preserve_dates;
130static bool instance_specifed;
131static bool dont_replace_existing;
132static bool allow_truncate_fname;
133static bool force_symtab;
134static bool suppress_create_msg;
135static bool full_path;
136static bool update_newer;
137static enum { ipos_none, ipos_before, ipos_after } ipos;
138
139
140int
141main (int argc, char *argv[])
142{
143 /* We use no threads here which can interfere with handling a stream. */
144 (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
145 (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
146 (void) __fsetlocking (stderr, FSETLOCKING_BYCALLER);
147
148 /* Set locale. */
149 (void) setlocale (LC_ALL, "");
150
151 /* Make sure the message catalog can be found. */
152 (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
153
154 /* Initialize the message catalog. */
155 (void) textdomain (PACKAGE_TARNAME);
156
157 /* For historical reasons the options in the first parameter need
158 not be preceded by a dash. Add it now if necessary. */
159 if (argc > 1 && argv[1][0] != '-')
160 {
161 size_t len = strlen (argv[1]) + 1;
162 char *newp = alloca (len + 1);
163 newp[0] = '-';
164 memcpy (&newp[1], argv[1], len);
165 argv[1] = newp;
166 }
167
168 /* Parse and process arguments. */
169 int remaining;
170 (void) argp_parse (&argp, argc, argv, ARGP_IN_ORDER, &remaining, NULL);
171
172 /* Tell the library which version we are expecting. */
173 (void) elf_version (EV_CURRENT);
174
175 /* Handle the [MEMBER] parameter. */
176 const char *member = NULL;
177 if (ipos != ipos_none)
178 {
179 /* Only valid for certain operations. */
180 if (operation != oper_move && operation != oper_replace)
181 error (1, 0, gettext ("\
182'a', 'b', and 'i' are only allowed with the 'm' and 'r' options"));
183
184 if (remaining == argc)
185 {
186 error (0, 0, gettext ("\
187MEMBER parameter required for 'a', 'b', and 'i' modifiers"));
188 argp_help (&argp, stderr, ARGP_HELP_USAGE | ARGP_HELP_SEE,
189 program_invocation_short_name);
190 exit (EXIT_FAILURE);
191 }
192
193 member = argv[remaining++];
194 }
195
196 /* Handle the [COUNT] parameter. */
197 long int instance = -1;
198 if (instance_specifed)
199 {
200 /* Only valid for certain operations. */
201 if (operation != oper_extract && operation != oper_delete)
202 error (1, 0, gettext ("\
203'N' is only meaningful with the 'x' and 'd' options"));
204
205 if (remaining == argc)
206 {
207 error (0, 0, gettext ("COUNT parameter required"));
208 argp_help (&argp, stderr, ARGP_HELP_SEE,
209 program_invocation_short_name);
210 exit (EXIT_FAILURE);
211 }
212
213 char *endp;
214 errno = 0;
215 if (((instance = strtol (argv[remaining], &endp, 10)) == LONG_MAX
216 && errno == ERANGE)
217 || instance <= 0
218 || *endp != '\0')
219 error (1, 0, gettext ("invalid COUNT parameter %s"), argv[remaining]);
220
221 ++remaining;
222 }
223
224 if ((dont_replace_existing || allow_truncate_fname)
225 && unlikely (operation != oper_extract))
226 error (1, 0, gettext ("'%c' is only meaningful with the 'x' option"),
227 dont_replace_existing ? 'C' : 'T');
228
229 /* There must at least be one more parameter specifying the archive. */
230 if (remaining == argc)
231 {
232 error (0, 0, gettext ("archive name required"));
233 argp_help (&argp, stderr, ARGP_HELP_SEE, program_invocation_short_name);
234 exit (EXIT_FAILURE);
235 }
236
237 const char *arfname = argv[remaining++];
238 argv += remaining;
239 argc -= remaining;
240
241 int status;
242 switch (operation)
243 {
244 case oper_none:
245 error (0, 0, gettext ("command option required"));
246 argp_help (&argp, stderr, ARGP_HELP_STD_ERR,
247 program_invocation_short_name);
248 status = 1;
249 break;
250
251 case oper_list:
252 case oper_print:
253 status = do_oper_extract (operation, arfname, argv, argc, -1);
254 break;
255
256 case oper_extract:
257 status = do_oper_extract (operation, arfname, argv, argc, instance);
258 break;
259
260 case oper_delete:
261 status = do_oper_delete (arfname, argv, argc, instance);
262 break;
263
264 case oper_move:
265 case oper_qappend:
266 case oper_replace:
267 status = do_oper_insert (operation, arfname, argv, argc, member);
268 break;
269
270 default:
271 assert (! "should not happen");
272 status = 1;
273 break;
274 }
275
276 return status;
277}
278
279
280/* Handle program arguments. */
281static error_t
282parse_opt (int key, char *arg __attribute__ ((unused)),
283 struct argp_state *state __attribute__ ((unused)))
284{
285 switch (key)
286 {
287 case 'd':
288 case 'm':
289 case 'p':
290 case 'q':
291 case 'r':
292 case 't':
293 case 'x':
294 if (operation != oper_none)
295 {
296 error (0, 0, gettext ("More than one operation specified"));
297 argp_help (&argp, stderr, ARGP_HELP_SEE,
298 program_invocation_short_name);
299 exit (EXIT_FAILURE);
300 }
301
302 switch (key)
303 {
304 case 'd':
305 operation = oper_delete;
306 break;
307 case 'm':
308 operation = oper_move;
309 break;
310 case 'p':
311 operation = oper_print;
312 break;
313 case 'q':
314 operation = oper_qappend;
315 break;
316 case 'r':
317 operation = oper_replace;
318 break;
319 case 't':
320 operation = oper_list;
321 break;
322 case 'x':
323 operation = oper_extract;
324 break;
325 }
326 break;
327
328 case 'a':
329 ipos = ipos_after;
330 break;
331
332 case 'b':
333 case 'i':
334 ipos = ipos_before;
335 break;
336
337 case 'c':
338 suppress_create_msg = true;
339 break;
340
341 case 'C':
342 dont_replace_existing = true;
343 break;
344
345 case 'N':
346 instance_specifed = true;
347 break;
348
349 case 'o':
350 preserve_dates = true;
351 break;
352
353 case 'P':
354 full_path = true;
355 break;
356
357 case 's':
358 force_symtab = true;
359 break;
360
361 case 'T':
362 allow_truncate_fname = true;
363 break;
364
365 case 'u':
366 update_newer = true;
367 break;
368
369 case 'v':
370 verbose = true;
371 break;
372
373 default:
374 return ARGP_ERR_UNKNOWN;
375 }
376 return 0;
377}
378
379
380static int
381open_archive (const char *arfname, int flags, int mode, Elf **elf,
382 struct stat *st, bool miss_allowed)
383{
384 int fd = open (arfname, flags, mode);
385 if (fd == -1)
386 {
387 if (miss_allowed)
388 return -1;
389
390 error (EXIT_FAILURE, errno, gettext ("cannot open archive '%s'"),
391 arfname);
392 }
393
394 if (elf != NULL)
395 {
396 Elf_Cmd cmd = flags == O_RDONLY ? ELF_C_READ_MMAP : ELF_C_RDWR_MMAP;
397
398 *elf = elf_begin (fd, cmd, NULL);
399 if (*elf == NULL)
400 error (EXIT_FAILURE, 0, gettext ("cannot open archive '%s': %s"),
401 arfname, elf_errmsg (-1));
402
403 if (flags == O_RDONLY && elf_kind (*elf) != ELF_K_AR)
404 error (EXIT_FAILURE, 0, gettext ("%s: not an archive file"), arfname);
405 }
406
407 if (st != NULL && fstat (fd, st) != 0)
408 error (EXIT_FAILURE, errno, gettext ("cannot stat archive '%s'"),
409 arfname);
410
411 return fd;
412}
413
414
415static void
416not_found (int argc, char *argv[argc], bool found[argc])
417{
418 for (int i = 0; i < argc; ++i)
419 if (!found[i])
420 printf (gettext ("no entry %s in archive\n"), argv[i]);
421}
422
423
424static int
425copy_content (Elf *elf, int newfd, off_t off, size_t n)
426{
427 size_t len;
428 char *rawfile = elf_rawfile (elf, &len);
429
430 assert (off + n <= len);
431
432 /* Tell the kernel we will read all the pages sequentially. */
433 size_t ps = sysconf (_SC_PAGESIZE);
434 if (n > 2 * ps)
435 posix_madvise (rawfile + (off & ~(ps - 1)), n, POSIX_MADV_SEQUENTIAL);
436
437 return write_retry (newfd, rawfile + off, n) != (ssize_t) n;
438}
439
440
441static int
442do_oper_extract (int oper, const char *arfname, char **argv, int argc,
443 long int instance)
444{
445 bool found[argc > 0 ? argc : 1];
446 memset (found, '\0', sizeof (found));
447
448 size_t name_max = 0;
449 inline bool should_truncate_fname (void)
450 {
451 if (errno == ENAMETOOLONG && allow_truncate_fname)
452 {
453 if (name_max == 0)
454 {
455 long int len = pathconf (".", _PC_NAME_MAX);
456 if (len > 0)
457 name_max = len;
458 }
459 return name_max != 0;
460 }
461 return false;
462 }
463
464 off_t index_off = -1;
465 size_t index_size = 0;
466 off_t cur_off = SARMAG;
467
468 int status = 0;
469 Elf *elf;
470 int fd = open_archive (arfname, O_RDONLY, 0, &elf, NULL, false);
471
472 if (hcreate (2 * argc) == 0)
473 error (EXIT_FAILURE, errno, gettext ("cannot create hash table"));
474
475 for (int cnt = 0; cnt < argc; ++cnt)
476 {
477 ENTRY entry = { .key = argv[cnt], .data = &argv[cnt] };
478 if (hsearch (entry, ENTER) == NULL)
479 error (EXIT_FAILURE, errno,
480 gettext ("cannot insert into hash table"));
481 }
482
483 struct stat st;
484 if (force_symtab)
485 {
486 if (fstat (fd, &st) != 0)
487 {
488 error (0, errno, gettext ("cannot stat '%s'"), arfname);
489 close (fd);
490 return 1;
491 }
492 arlib_init ();
493 }
494
495 Elf_Cmd cmd = ELF_C_READ_MMAP;
496 Elf *subelf;
497 while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
498 {
499 Elf_Arhdr *arhdr = elf_getarhdr (subelf);
500
501 if (strcmp (arhdr->ar_name, "/") == 0)
502 {
503 index_off = elf_getaroff (subelf);
504 index_size = arhdr->ar_size;
505 goto next;
506 }
507 if (strcmp (arhdr->ar_name, "//") == 0)
508 goto next;
509
510 if (force_symtab)
511 {
512 arlib_add_symbols (elf, arfname, arhdr->ar_name, cur_off);
513 cur_off += (((arhdr->ar_size + 1) & ~((off_t) 1))
514 + sizeof (struct ar_hdr));
515 }
516
517 bool do_extract = argc <= 0;
518 if (!do_extract)
519 {
520 ENTRY entry;
521 entry.key = arhdr->ar_name;
522 ENTRY *res = hsearch (entry, FIND);
523 if (res != NULL && (instance < 0 || instance-- == 0)
524 && !found[(char **) res->data - argv])
525 found[(char **) res->data - argv] = do_extract = true;
526 }
527
528 if (do_extract)
529 {
530 if (verbose)
531 {
532 if (oper == oper_print)
533 {
534 printf ("\n<%s>\n\n", arhdr->ar_name);
535
536 /* We have to flush now because now we use the descriptor
537 directly. */
538 fflush (stdout);
539 }
540 else if (oper == oper_list)
541 {
542 char datestr[100];
543 strftime (datestr, sizeof (datestr), "%b %e %H:%M %Y",
544 localtime (&arhdr->ar_date));
545
546 printf ("%c%c%c%c%c%c%c%c%c %u/%u %6ju %s %s\n",
547 (arhdr->ar_mode & S_IRUSR) ? 'r' : '-',
548 (arhdr->ar_mode & S_IWUSR) ? 'w' : '-',
549 (arhdr->ar_mode & S_IXUSR)
550 ? ((arhdr->ar_mode & S_ISUID) ? 's' : 'x')
551 : ((arhdr->ar_mode & S_ISUID) ? 'S' : '-'),
552 (arhdr->ar_mode & S_IRGRP) ? 'r' : '-',
553 (arhdr->ar_mode & S_IWGRP) ? 'w' : '-',
554 (arhdr->ar_mode & S_IXGRP)
555 ? ((arhdr->ar_mode & S_ISGID) ? 's' : 'x')
556 : ((arhdr->ar_mode & S_ISGID) ? 'S' : '-'),
557 (arhdr->ar_mode & S_IROTH) ? 'r' : '-',
558 (arhdr->ar_mode & S_IWOTH) ? 'w' : '-',
559 (arhdr->ar_mode & S_IXOTH)
560 ? ((arhdr->ar_mode & S_ISVTX) ? 't' : 'x')
561 : ((arhdr->ar_mode & S_ISVTX) ? 'T' : '-'),
562 arhdr->ar_uid,
563 arhdr->ar_gid,
564 (uintmax_t) arhdr->ar_size,
565 datestr,
566 arhdr->ar_name);
567 }
568 else
569 printf ("x - %s\n", arhdr->ar_name);
570 }
571
572 if (oper == oper_list)
573 {
574 if (!verbose)
575 puts (arhdr->ar_name);
576
577 goto next;
578 }
579
580 size_t nleft;
581 char *data = elf_rawfile (subelf, &nleft);
582 if (data == NULL)
583 {
584 error (0, 0, gettext ("cannot read content of %s: %s"),
585 arhdr->ar_name, elf_errmsg (-1));
586 status = 1;
587 goto next;
588 }
589
590 int xfd;
591 char tempfname[] = "XXXXXX";
592 bool use_mkstemp = true;
593
594 if (oper == oper_print)
595 xfd = STDOUT_FILENO;
596 else
597 {
598 xfd = mkstemp (tempfname);
599 if (unlikely (xfd == -1))
600 {
601 /* We cannot create a temporary file. Try to overwrite
602 the file or create it if it does not exist. */
603 int flags = O_WRONLY | O_CREAT;
604 if (dont_replace_existing)
605 flags |= O_EXCL;
606 else
607 flags |= O_TRUNC;
608 xfd = open (arhdr->ar_name, flags, 0600);
609 if (unlikely (xfd == -1))
610 {
611 int printlen = INT_MAX;
612
613 if (should_truncate_fname ())
614 {
615 /* Try to truncate the name. First find out by how
616 much. */
617 printlen = name_max;
618 char truncfname[name_max + 1];
619 *((char *) mempcpy (truncfname, arhdr->ar_name,
620 name_max)) = '\0';
621
622 xfd = open (truncfname, flags, 0600);
623 }
624
625 if (xfd == -1)
626 {
627 error (0, errno, gettext ("cannot open %.*s"),
628 (int) printlen, arhdr->ar_name);
629 status = 1;
630 goto next;
631 }
632 }
633
634 use_mkstemp = false;
635 }
636 }
637
638 ssize_t n;
639 while ((n = TEMP_FAILURE_RETRY (write (xfd, data, nleft))) != -1)
640 {
641 nleft -= n;
642 if (nleft == 0)
643 break;
644 data += n;
645 }
646
647 if (unlikely (n == -1))
648 {
649 error (0, errno, gettext ("failed to write %s"), arhdr->ar_name);
650 status = 1;
651 unlink (tempfname);
652 close (xfd);
653 goto next;
654 }
655
656 if (oper != oper_print)
657 {
658 /* Fix up the mode. */
659 if (unlikely (fchmod (xfd, arhdr->ar_mode) != 0))
660 {
661 error (0, errno, gettext ("cannot change mode of %s"),
662 arhdr->ar_name);
663 status = 0;
664 }
665
666 if (preserve_dates)
667 {
668 struct timespec tv[2];
669 tv[0].tv_sec = arhdr->ar_date;
670 tv[0].tv_nsec = 0;
671 tv[1].tv_sec = arhdr->ar_date;
672 tv[1].tv_nsec = 0;
673
674 if (unlikely (futimens (xfd, tv) != 0))
675 {
676 error (0, errno,
677 gettext ("cannot change modification time of %s"),
678 arhdr->ar_name);
679 status = 1;
680 }
681 }
682
683 /* If we used a temporary file, move it do the right
684 name now. */
685 if (use_mkstemp)
686 {
687 int r;
688
689 if (dont_replace_existing)
690 {
691 r = link (tempfname, arhdr->ar_name);
692 if (likely (r == 0))
693 unlink (tempfname);
694 }
695 else
696 r = rename (tempfname, arhdr->ar_name);
697
698 if (unlikely (r) != 0)
699 {
700 int printlen = INT_MAX;
701
702 if (should_truncate_fname ())
703 {
704 /* Try to truncate the name. First find out by how
705 much. */
706 printlen = name_max;
707 char truncfname[name_max + 1];
708 *((char *) mempcpy (truncfname, arhdr->ar_name,
709 name_max)) = '\0';
710
711 if (dont_replace_existing)
712 {
713 r = link (tempfname, truncfname);
714 if (likely (r == 0))
715 unlink (tempfname);
716 }
717 else
718 r = rename (tempfname, truncfname);
719 }
720
721 if (r != 0)
722 {
723 error (0, errno, gettext ("\
724cannot rename temporary file to %.*s"),
725 printlen, arhdr->ar_name);
726 unlink (tempfname);
727 status = 1;
728 }
729 }
730 }
731
732 close (xfd);
733 }
734 }
735
736 next:
737 cmd = elf_next (subelf);
738 if (elf_end (subelf) != 0)
739 error (1, 0, "%s: %s", arfname, elf_errmsg (-1));
740 }
741
742 hdestroy ();
743
744 if (force_symtab)
745 {
746 arlib_finalize ();
747
748 if (symtab.symsnamelen != 0
749 /* We have to rewrite the file also if it initially had an index
750 but now does not need one anymore. */
751 || (symtab.symsnamelen == 0 && index_size != 0))
752 {
753 char tmpfname[strlen (arfname) + 7];
754 strcpy (stpcpy (tmpfname, arfname), "XXXXXX");
755 int newfd = mkstemp (tmpfname);
756 if (unlikely (newfd == -1))
757 {
758 nonew:
759 error (0, errno, gettext ("cannot create new file"));
760 status = 1;
761 }
762 else
763 {
764 /* Create the header. */
765 if (unlikely (write_retry (newfd, ARMAG, SARMAG) != SARMAG))
766 {
767 // XXX Use /prof/self/fd/%d ???
768 nonew_unlink:
769 unlink (tmpfname);
770 if (newfd != -1)
771 close (newfd);
772 goto nonew;
773 }
774
775 /* Create the new file. There are three parts as far we are
776 concerned: 1. original context before the index, 2. the
777 new index, 3. everything after the new index. */
778 off_t rest_off;
779 if (index_off != -1)
780 rest_off = (index_off + sizeof (struct ar_hdr)
781 + ((index_size + 1) & ~1ul));
782 else
783 rest_off = SARMAG;
784
785 if ((symtab.symsnamelen != 0
786 && ((write_retry (newfd, symtab.symsoff,
787 symtab.symsofflen)
788 != (ssize_t) symtab.symsofflen)
789 || (write_retry (newfd, symtab.symsname,
790 symtab.symsnamelen)
791 != (ssize_t) symtab.symsnamelen)))
792 /* Even if the original file had content before the
793 symbol table, we write it in the correct order. */
794 || (index_off != SARMAG
795 && copy_content (elf, newfd, SARMAG, index_off - SARMAG))
796 || copy_content (elf, newfd, rest_off, st.st_size - rest_off)
797 /* Set the mode of the new file to the same values the
798 original file has. */
799 || fchmod (newfd, st.st_mode & ALLPERMS) != 0
800 /* Never complain about fchown failing. */
801 || (({asm ("" :: "r" (fchown (newfd, st.st_uid,
802 st.st_gid))); }),
803 close (newfd) != 0)
804 || (newfd = -1, rename (tmpfname, arfname) != 0))
805 goto nonew_unlink;
806 }
807 }
808 }
809
810 elf_end (elf);
811
812 close (fd);
813
814 not_found (argc, argv, found);
815
816 return status;
817}
818
819
820struct armem
821{
822 off_t off;
823 off_t old_off;
824 size_t size;
825 long int long_name_off;
826 struct armem *next;
827 void *mem;
828 time_t sec;
829 uid_t uid;
830 gid_t gid;
831 mode_t mode;
832 const char *name;
833 Elf *elf;
834};
835
836
837static int
838write_member (struct armem *memb, off_t *startp, off_t *lenp, Elf *elf,
839 off_t end_off, int newfd)
840{
841 struct ar_hdr arhdr;
842 /* The ar_name is not actually zero teminated, but we need that for
843 snprintf. Also if the name is too long, then the string starts
844 with '/' plus an index off number (decimal). */
845 char tmpbuf[sizeof (arhdr.ar_name) + 2];
846
847 bool changed_header = memb->long_name_off != -1;
848 if (changed_header)
849 {
850 /* In case of a long file name we assume the archive header
851 changed and we write it here. */
852 memcpy (&arhdr, elf_rawfile (elf, NULL) + *startp, sizeof (arhdr));
853
854 snprintf (tmpbuf, sizeof (tmpbuf), "/%-*ld",
855 (int) sizeof (arhdr.ar_name), memb->long_name_off);
856 changed_header = memcmp (arhdr.ar_name, tmpbuf,
857 sizeof (arhdr.ar_name)) != 0;
858 }
859
860 /* If the files are adjacent in the old file extend the range. */
861 if (*startp != -1 && !changed_header && *startp + *lenp == memb->old_off)
862 {
863 /* Extend the current range. */
864 *lenp += (memb->next != NULL
865 ? memb->next->off : end_off) - memb->off;
866 return 0;
867 }
868
869 /* Write out the old range. */
870 if (*startp != -1 && copy_content (elf, newfd, *startp, *lenp))
871 return -1;
872
873 *startp = memb->old_off;
874 *lenp = (memb->next != NULL ? memb->next->off : end_off) - memb->off;
875
876 if (changed_header)
877 {
878 memcpy (arhdr.ar_name, tmpbuf, sizeof (arhdr.ar_name));
879
880 if (unlikely (write_retry (newfd, &arhdr, sizeof (arhdr))
881 != sizeof (arhdr)))
882 return -1;
883
884 *startp += sizeof (struct ar_hdr);
885 assert ((size_t) *lenp >= sizeof (struct ar_hdr));
886 *lenp -= sizeof (struct ar_hdr);
887 }
888
889 return 0;
890}
891
892/* Store the name in the long name table if necessary.
893 Record its offset or -1 if we did not need to use the table. */
894static void
895remember_long_name (struct armem *mem, const char *name, size_t namelen)
896{
897 mem->long_name_off = (namelen > MAX_AR_NAME_LEN
898 ? arlib_add_long_name (name, namelen)
899 : -1l);
900}
901
902static int
903do_oper_delete (const char *arfname, char **argv, int argc,
904 long int instance)
905{
906 bool *found = alloca (sizeof (bool) * argc);
907 memset (found, '\0', sizeof (bool) * argc);
908
909 /* List of the files we keep. */
910 struct armem *to_copy = NULL;
911
912 int status = 0;
913 Elf *elf;
914 struct stat st;
915 int fd = open_archive (arfname, O_RDONLY, 0, &elf, &st, false);
916
917 if (hcreate (2 * argc) == 0)
918 error (EXIT_FAILURE, errno, gettext ("cannot create hash table"));
919
920 for (int cnt = 0; cnt < argc; ++cnt)
921 {
922 ENTRY entry = { .key = argv[cnt], .data = &argv[cnt] };
923 if (hsearch (entry, ENTER) == NULL)
924 error (EXIT_FAILURE, errno,
925 gettext ("cannot insert into hash table"));
926 }
927
928 arlib_init ();
929
930 off_t cur_off = SARMAG;
931 Elf_Cmd cmd = ELF_C_READ_MMAP;
932 Elf *subelf;
933 while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
934 {
935 Elf_Arhdr *arhdr = elf_getarhdr (subelf);
936
937 /* Ignore the symbol table and the long file name table here. */
938 if (strcmp (arhdr->ar_name, "/") == 0
939 || strcmp (arhdr->ar_name, "//") == 0)
940 goto next;
941
942 bool do_delete = argc <= 0;
943 if (!do_delete)
944 {
945 ENTRY entry;
946 entry.key = arhdr->ar_name;
947 ENTRY *res = hsearch (entry, FIND);
948 if (res != NULL && (instance < 0 || instance-- == 0)
949 && !found[(char **) res->data - argv])
950 found[(char **) res->data - argv] = do_delete = true;
951 }
952
953 if (do_delete)
954 {
955 if (verbose)
956 printf ("d - %s\n", arhdr->ar_name);
957 }
958 else
959 {
960 struct armem *newp = alloca (sizeof (struct armem));
961 newp->old_off = elf_getaroff (subelf);
962 newp->off = cur_off;
963
964 cur_off += (((arhdr->ar_size + 1) & ~((off_t) 1))
965 + sizeof (struct ar_hdr));
966
967 if (to_copy == NULL)
968 to_copy = newp->next = newp;
969 else
970 {
971 newp->next = to_copy->next;
972 to_copy = to_copy->next = newp;
973 }
974
975 /* If we recreate the symbol table read the file's symbol
976 table now. */
977 arlib_add_symbols (subelf, arfname, arhdr->ar_name, newp->off);
978
979 /* Remember long file names. */
980 remember_long_name (newp, arhdr->ar_name, strlen (arhdr->ar_name));
981 }
982
983 next:
984 cmd = elf_next (subelf);
985 if (elf_end (subelf) != 0)
986 error (1, 0, "%s: %s", arfname, elf_errmsg (-1));
987 }
988
989 arlib_finalize ();
990
991 hdestroy ();
992
993 /* Create a new, temporary file in the same directory as the
994 original file. */
995 char tmpfname[strlen (arfname) + 7];
996 strcpy (stpcpy (tmpfname, arfname), "XXXXXX");
997 int newfd = mkstemp (tmpfname);
998 if (unlikely (newfd == -1))
999 goto nonew;
1000
1001 /* Create the header. */
1002 if (unlikely (write_retry (newfd, ARMAG, SARMAG) != SARMAG))
1003 {
1004 // XXX Use /prof/self/fd/%d ???
1005 nonew_unlink:
1006 unlink (tmpfname);
1007 if (newfd != -1)
1008 close (newfd);
1009 nonew:
1010 error (0, errno, gettext ("cannot create new file"));
1011 status = 1;
1012 goto errout;
1013 }
1014
1015 /* If the archive is empty that is all we have to do. */
1016 if (likely (to_copy != NULL))
1017 {
1018 /* Write the symbol table or the long file name table or both. */
1019 if (symtab.symsnamelen != 0
1020 && ((write_retry (newfd, symtab.symsoff, symtab.symsofflen)
1021 != (ssize_t) symtab.symsofflen)
1022 || (write_retry (newfd, symtab.symsname, symtab.symsnamelen)
1023 != (ssize_t) symtab.symsnamelen)))
1024 goto nonew_unlink;
1025
1026 if (symtab.longnameslen > sizeof (struct ar_hdr)
1027 && (write_retry (newfd, symtab.longnames, symtab.longnameslen)
1028 != (ssize_t) symtab.longnameslen))
1029 goto nonew_unlink;
1030
1031 /* NULL-terminate the list of files to copy. */
1032 struct armem *last = to_copy;
1033 to_copy = to_copy->next;
1034 last->next = NULL;
1035
1036 off_t start = -1;
1037 off_t len = -1;
1038
1039 do
1040 if (write_member (to_copy, &start, &len, elf, cur_off, newfd) != 0)
1041 goto nonew_unlink;
1042 while ((to_copy = to_copy->next) != NULL);
1043
1044 /* Write the last part. */
1045 if (copy_content (elf, newfd, start, len))
1046 goto nonew_unlink;
1047 }
1048
1049 /* Set the mode of the new file to the same values the original file
1050 has. */
1051 if (fchmod (newfd, st.st_mode & ALLPERMS) != 0
1052 /* Never complain about fchown failing. */
1053 || (({asm ("" :: "r" (fchown (newfd, st.st_uid, st.st_gid))); }),
1054 close (newfd) != 0)
1055 || (newfd = -1, rename (tmpfname, arfname) != 0))
1056 goto nonew_unlink;
1057
1058 errout:
1059 elf_end (elf);
1060
1061 arlib_fini ();
1062
1063 close (fd);
1064
1065 not_found (argc, argv, found);
1066
1067 return status;
1068}
1069
1070
1071/* Prints the given value in the given buffer without a trailing zero char.
1072 Returns false if the given value doesn't fit in the given buffer. */
1073static bool
1074no0print (bool ofmt, char *buf, int bufsize, long int val)
1075{
1076 char tmpbuf[bufsize + 1];
1077 int ret = snprintf (tmpbuf, sizeof (tmpbuf), ofmt ? "%-*lo" : "%-*ld",
1078 bufsize, val);
1079 if (ret >= (int) sizeof (tmpbuf))
1080 return false;
1081 memcpy (buf, tmpbuf, bufsize);
1082 return true;
1083}
1084
1085
1086static int
1087do_oper_insert (int oper, const char *arfname, char **argv, int argc,
1088 const char *member)
1089{
1090 int status = 0;
1091 Elf *elf = NULL;
1092 struct stat st;
1093 int fd = open_archive (arfname, O_RDONLY, 0, &elf, &st, oper != oper_move);
1094
1095 /* List of the files we keep. */
1096 struct armem *all = NULL;
1097 struct armem *after_memberelem = NULL;
1098 struct armem **found = alloca (sizeof (*found) * argc);
1099 memset (found, '\0', sizeof (*found) * argc);
1100
1101 arlib_init ();
1102
1103 /* Initialize early for no_old case. */
1104 off_t cur_off = SARMAG;
1105
1106 if (fd == -1)
1107 {
1108 if (!suppress_create_msg)
1109 fprintf (stderr, "%s: creating %s\n",
1110 program_invocation_short_name, arfname);
1111
1112 goto no_old;
1113 }
1114
1115 /* Store the names of all files from the command line in a hash
1116 table so that we can match it. Note that when no file name is
1117 given we are basically doing nothing except recreating the
1118 index. */
1119 if (oper != oper_qappend)
1120 {
1121 if (hcreate (2 * argc) == 0)
1122 error (EXIT_FAILURE, errno, gettext ("cannot create hash table"));
1123
1124 for (int cnt = 0; cnt < argc; ++cnt)
1125 {
1126 ENTRY entry;
1127 entry.key = full_path ? argv[cnt] : basename (argv[cnt]);
1128 entry.data = &argv[cnt];
1129 if (hsearch (entry, ENTER) == NULL)
1130 error (EXIT_FAILURE, errno,
1131 gettext ("cannot insert into hash table"));
1132 }
1133 }
1134
1135 /* While iterating over the current content of the archive we must
1136 determine a number of things: which archive members to keep,
1137 which are replaced, and where to insert the new members. */
1138 Elf_Cmd cmd = ELF_C_READ_MMAP;
1139 Elf *subelf;
1140 while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
1141 {
1142 Elf_Arhdr *arhdr = elf_getarhdr (subelf);
1143
1144 /* Ignore the symbol table and the long file name table here. */
1145 if (strcmp (arhdr->ar_name, "/") == 0
1146 || strcmp (arhdr->ar_name, "//") == 0)
1147 goto next;
1148
1149 struct armem *newp = alloca (sizeof (struct armem));
1150 newp->old_off = elf_getaroff (subelf);
1151 newp->size = arhdr->ar_size;
1152 newp->sec = arhdr->ar_date;
1153 newp->mem = NULL;
1154
1155 /* Remember long file names. */
1156 remember_long_name (newp, arhdr->ar_name, strlen (arhdr->ar_name));
1157
1158 /* Check whether this is a file we are looking for. */
1159 if (oper != oper_qappend)
1160 {
1161 /* Check whether this is the member used as the insert point. */
1162 if (member != NULL && strcmp (arhdr->ar_name, member) == 0)
1163 {
1164 /* Note that all == NULL means insert at the beginning. */
1165 if (ipos == ipos_before)
1166 after_memberelem = all;
1167 else
1168 after_memberelem = newp;
1169 member = NULL;
1170 }
1171
1172 ENTRY entry;
1173 entry.key = arhdr->ar_name;
1174 ENTRY *res = hsearch (entry, FIND);
1175 if (res != NULL && found[(char **) res->data - argv] == NULL)
1176 {
1177 found[(char **) res->data - argv] = newp;
1178
1179 /* If we insert before or after a certain element move
1180 all files to a special list. */
1181 if (unlikely (ipos != ipos_none || oper == oper_move))
1182 {
1183 if (after_memberelem == newp)
1184 /* Since we remove this element even though we should
1185 insert everything after it, we in fact insert
1186 everything after the previous element. */
1187 after_memberelem = all;
1188
1189 goto next;
1190 }
1191 }
1192 }
1193
1194 if (all == NULL)
1195 all = newp->next = newp;
1196 else
1197 {
1198 newp->next = all->next;
1199 all = all->next = newp;
1200 }
1201
1202 next:
1203 cmd = elf_next (subelf);
1204 if (elf_end (subelf) != 0)
1205 error (EXIT_FAILURE, 0, "%s: %s", arfname, elf_errmsg (-1));
1206 }
1207
1208 if (oper != oper_qappend)
1209 hdestroy ();
1210
1211 no_old:
1212 if (member != NULL)
1213 error (EXIT_FAILURE, 0, gettext ("position member %s not found"),
1214 member);
1215
1216 if (oper == oper_move)
1217 {
1218 /* Make sure all requested elements are found in the archive. */
1219 for (int cnt = 0; cnt < argc; ++cnt)
1220 {
1221 if (found[cnt] == NULL)
1222 {
1223 fprintf (stderr, gettext ("%s: no entry %s in archive!\n"),
1224 program_invocation_short_name, argv[cnt]);
1225 status = 1;
1226 }
1227
1228 if (verbose)
1229 printf ("m - %s\n", argv[cnt]);
1230 }
1231 }
1232 else
1233 {
1234 /* Open all the new files, get their sizes and add all symbols. */
1235 for (int cnt = 0; cnt < argc; ++cnt)
1236 {
1237 const char *bname = basename (argv[cnt]);
1238 size_t bnamelen = strlen (bname);
1239 if (found[cnt] == NULL)
1240 {
1241 found[cnt] = alloca (sizeof (struct armem));
1242 found[cnt]->old_off = -1;
1243
1244 remember_long_name (found[cnt], bname, bnamelen);
1245 }
1246
1247 struct stat newst;
1248 Elf *newelf;
1249 int newfd = open (argv[cnt], O_RDONLY);
1250 if (newfd == -1)
1251 {
1252 error (0, errno, gettext ("cannot open %s"), argv[cnt]);
1253 status = 1;
1254 }
1255 else if (fstat (newfd, &newst) == -1)
1256 {
1257 error (0, errno, gettext ("cannot stat %s"), argv[cnt]);
1258 close (newfd);
1259 status = 1;
1260 }
1261 else if (!S_ISREG (newst.st_mode))
1262 {
1263 error (0, errno, gettext ("%s is no regular file"), argv[cnt]);
1264 close (newfd);
1265 status = 1;
1266 }
1267 else if (update_newer
1268 && found[cnt]->old_off != -1l
1269 && found[cnt]->sec > st.st_mtime)
1270 /* Do nothing, the file in the archive is younger. */
1271 close (newfd);
1272 else if ((newelf = elf_begin (newfd, ELF_C_READ_MMAP, NULL))
1273 == NULL)
1274 {
1275 fprintf (stderr,
1276 gettext ("cannot get ELF descriptor for %s: %s\n"),
1277 argv[cnt], elf_errmsg (-1));
1278 status = 1;
1279 }
1280 else
1281 {
1282 if (verbose)
1283 printf ("%c - %s\n",
1284 found[cnt]->old_off == -1l ? 'a' : 'r', argv[cnt]);
1285
1286 found[cnt]->elf = newelf;
1287 found[cnt]->sec = arlib_deterministic_output ? 0 : newst.st_mtime;
1288 found[cnt]->uid = arlib_deterministic_output ? 0 : newst.st_uid;
1289 found[cnt]->gid = arlib_deterministic_output ? 0 : newst.st_gid;
1290 found[cnt]->mode = newst.st_mode;
1291 found[cnt]->name = bname;
1292
1293 found[cnt]->mem = elf_rawfile (newelf, &found[cnt]->size);
1294 if (found[cnt]->mem == NULL
1295 || elf_cntl (newelf, ELF_C_FDDONE) != 0)
1296 error (EXIT_FAILURE, 0, gettext ("cannot read %s: %s"),
1297 argv[cnt], elf_errmsg (-1));
1298
1299 close (newfd);
1300
1301 if (found[cnt]->old_off != -1l)
1302 /* Remember long file names. */
1303 remember_long_name (found[cnt], bname, bnamelen);
1304 }
1305 }
1306 }
1307
1308 if (status != 0)
1309 {
1310 elf_end (elf);
1311
1312 arlib_fini ();
1313
1314 close (fd);
1315
1316 return status;
1317 }
1318
1319 /* If we have no entry point so far add at the end. AFTER_MEMBERELEM
1320 being NULL when adding before an entry means add at the beginning. */
1321 if (ipos != ipos_before && after_memberelem == NULL)
1322 after_memberelem = all;
1323
1324 /* Convert the circular list into a normal list first. */
1325 if (all != NULL)
1326 {
1327 struct armem *tmp = all;
1328 all = all->next;
1329 tmp->next = NULL;
1330 }
1331
1332 struct armem *last_added = after_memberelem;
1333 for (int cnt = 0; cnt < argc; ++cnt)
1334 if (oper != oper_replace || found[cnt]->old_off == -1)
1335 {
1336 if (last_added == NULL)
1337 {
1338 found[cnt]->next = all;
1339 last_added = all = found[cnt];
1340 }
1341 else
1342 {
1343 found[cnt]->next = last_added->next;
1344 last_added = last_added->next = found[cnt];
1345 }
1346 }
1347
1348 /* Finally compute the offset and add the symbols for the files
1349 after the insert point. */
1350 if (likely (all != NULL))
1351 for (struct armem *memp = all; memp != NULL; memp = memp->next)
1352 {
1353 memp->off = cur_off;
1354
1355 if (memp->mem == NULL)
1356 {
1357 Elf_Arhdr *arhdr;
1358 /* Fake initializing arhdr and subelf to keep gcc calm. */
1359 asm ("" : "=m" (arhdr), "=m" (subelf));
1360 if (elf_rand (elf, memp->old_off) == 0
1361 || (subelf = elf_begin (fd, ELF_C_READ_MMAP, elf)) == NULL
1362 || (arhdr = elf_getarhdr (subelf)) == NULL)
1363 /* This should never happen since we already looked at the
1364 archive content. But who knows... */
1365 error (EXIT_FAILURE, 0, "%s: %s", arfname, elf_errmsg (-1));
1366
1367 arlib_add_symbols (subelf, arfname, arhdr->ar_name, cur_off);
1368
1369 elf_end (subelf);
1370 }
1371 else
1372 arlib_add_symbols (memp->elf, arfname, memp->name, cur_off);
1373
1374 cur_off += (((memp->size + 1) & ~((off_t) 1))
1375 + sizeof (struct ar_hdr));
1376 }
1377
1378 /* Now we have all the information for the symbol table and long
1379 file name table. Construct the final layout. */
1380 arlib_finalize ();
1381
1382 /* Create a new, temporary file in the same directory as the
1383 original file. */
1384 char tmpfname[strlen (arfname) + 7];
1385 strcpy (stpcpy (tmpfname, arfname), "XXXXXX");
1386 int newfd;
1387 if (fd != -1)
1388 newfd = mkstemp (tmpfname);
1389 else
1390 {
1391 newfd = open (arfname, O_RDWR | O_CREAT | O_EXCL, DEFFILEMODE);
1392 if (newfd == -1 && errno == EEXIST)
1393 /* Bah, first the file did not exist, now it does. Restart. */
1394 return do_oper_insert (oper, arfname, argv, argc, member);
1395 }
1396 if (unlikely (newfd == -1))
1397 goto nonew;
1398
1399 /* Create the header. */
1400 if (unlikely (write_retry (newfd, ARMAG, SARMAG) != SARMAG))
1401 {
1402 nonew_unlink:
1403 if (fd != -1)
1404 {
1405 // XXX Use /prof/self/fd/%d ???
1406 unlink (tmpfname);
1407 if (newfd != -1)
1408 close (newfd);
1409 }
1410 nonew:
1411 error (0, errno, gettext ("cannot create new file"));
1412 status = 1;
1413 goto errout;
1414 }
1415
1416 /* If the new archive is not empty we actually have something to do. */
1417 if (likely (all != NULL))
1418 {
1419 /* Write the symbol table or the long file name table or both. */
1420 if (symtab.symsnamelen != 0
1421 && ((write_retry (newfd, symtab.symsoff, symtab.symsofflen)
1422 != (ssize_t) symtab.symsofflen)
1423 || (write_retry (newfd, symtab.symsname, symtab.symsnamelen)
1424 != (ssize_t) symtab.symsnamelen)))
1425 goto nonew_unlink;
1426
1427 if (symtab.longnameslen > sizeof (struct ar_hdr)
1428 && (write_retry (newfd, symtab.longnames, symtab.longnameslen)
1429 != (ssize_t) symtab.longnameslen))
1430 goto nonew_unlink;
1431
1432 off_t start = -1;
1433 off_t len = -1;
1434
1435 while (all != NULL)
1436 {
1437 if (all->mem != NULL)
1438 {
1439 /* This is a new file. If there is anything from the
1440 archive left to be written do it now. */
1441 if (start != -1 && copy_content (elf, newfd, start, len))
1442 goto nonew_unlink;
1443
1444 start = -1;
1445 len = -1;
1446
1447 /* Create the header. */
1448 struct ar_hdr arhdr;
1449 /* The ar_name is not actually zero teminated, but we
1450 need that for snprintf. Also if the name is too
1451 long, then the string starts with '/' plus an index
1452 off number (decimal). */
1453 char tmpbuf[sizeof (arhdr.ar_name) + 2];
1454 if (all->long_name_off == -1)
1455 {
1456 size_t namelen = strlen (all->name);
1457 char *p = mempcpy (arhdr.ar_name, all->name, namelen);
1458 *p++ = '/';
1459 memset (p, ' ', sizeof (arhdr.ar_name) - namelen - 1);
1460 }
1461 else
1462 {
1463 snprintf (tmpbuf, sizeof (tmpbuf), "/%-*ld",
1464 (int) sizeof (arhdr.ar_name), all->long_name_off);
1465 memcpy (arhdr.ar_name, tmpbuf, sizeof (arhdr.ar_name));
1466 }
1467
1468 if (! no0print (false, arhdr.ar_date, sizeof (arhdr.ar_date),
1469 all->sec))
1470 {
1471 error (0, errno, gettext ("cannot represent ar_date"));
1472 goto nonew_unlink;
1473 }
1474 if (! no0print (false, arhdr.ar_uid, sizeof (arhdr.ar_uid),
1475 all->uid))
1476 {
1477 error (0, errno, gettext ("cannot represent ar_uid"));
1478 goto nonew_unlink;
1479 }
1480 if (! no0print (false, arhdr.ar_gid, sizeof (arhdr.ar_gid),
1481 all->gid))
1482 {
1483 error (0, errno, gettext ("cannot represent ar_gid"));
1484 goto nonew_unlink;
1485 }
1486 if (! no0print (true, arhdr.ar_mode, sizeof (arhdr.ar_mode),
1487 all->mode))
1488 {
1489 error (0, errno, gettext ("cannot represent ar_mode"));
1490 goto nonew_unlink;
1491 }
1492 if (! no0print (false, arhdr.ar_size, sizeof (arhdr.ar_size),
1493 all->size))
1494 {
1495 error (0, errno, gettext ("cannot represent ar_size"));
1496 goto nonew_unlink;
1497 }
1498 memcpy (arhdr.ar_fmag, ARFMAG, sizeof (arhdr.ar_fmag));
1499
1500 if (unlikely (write_retry (newfd, &arhdr, sizeof (arhdr))
1501 != sizeof (arhdr)))
1502 goto nonew_unlink;
1503
1504 /* Now the file itself. */
1505 if (unlikely (write_retry (newfd, all->mem, all->size)
1506 != (off_t) all->size))
1507 goto nonew_unlink;
1508
1509 /* Pad the file if its size is odd. */
1510 if ((all->size & 1) != 0)
1511 if (unlikely (write_retry (newfd, "\n", 1) != 1))
1512 goto nonew_unlink;
1513 }
1514 else
1515 {
1516 /* This is a member from the archive. */
1517 if (write_member (all, &start, &len, elf, cur_off, newfd)
1518 != 0)
1519 goto nonew_unlink;
1520 }
1521
1522 all = all->next;
1523 }
1524
1525 /* Write the last part. */
1526 if (start != -1 && copy_content (elf, newfd, start, len))
1527 goto nonew_unlink;
1528 }
1529
1530 /* Set the mode of the new file to the same values the original file
1531 has. */
1532 if (fd != -1
1533 && (fchmod (newfd, st.st_mode & ALLPERMS) != 0
1534 /* Never complain about fchown failing. */
1535 || (({asm ("" :: "r" (fchown (newfd, st.st_uid, st.st_gid))); }),
1536 close (newfd) != 0)
1537 || (newfd = -1, rename (tmpfname, arfname) != 0)))
1538 goto nonew_unlink;
1539
1540 errout:
1541 for (int cnt = 0; cnt < argc; ++cnt)
1542 elf_end (found[cnt]->elf);
1543
1544 elf_end (elf);
1545
1546 arlib_fini ();
1547
1548 if (fd != -1)
1549 close (fd);
1550
1551 return status;
1552}
1553
1554
1555#include "debugpred.h"