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