Cleanup of more packages
[termux-packages] / packages / elfutils / ar.c.patch
1 diff -u -r ../elfutils-0.166/src/ar.c ./src/ar.c
2 --- ../elfutils-0.166/src/ar.c 2015-11-27 08:36:29.000000000 -0500
3 +++ ./src/ar.c 2016-05-04 14:29:53.035376083 -0400
4 @@ -1,1536 +1 @@
5 -/* Create, modify, and extract from archives.
6 - Copyright (C) 2005-2012 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 -
49 -#include "arlib.h"
50 -
51 -
52 -/* Name and version of program. */
53 -static void print_version (FILE *stream, struct argp_state *state);
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 -/* Print the version information. */
285 -static void
286 -print_version (FILE *stream, struct argp_state *state __attribute__ ((unused)))
287 -{
288 - fprintf (stream, "ar (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
289 - fprintf (stream, gettext ("\
290 -Copyright (C) %s Red Hat, Inc.\n\
291 -This is free software; see the source for copying conditions. There is NO\n\
292 -warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
293 -"), "2012");
294 - fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
295 -}
296 -
297 -
298 -/* Handle program arguments. */
299 -static error_t
300 -parse_opt (int key, char *arg __attribute__ ((unused)),
301 - struct argp_state *state __attribute__ ((unused)))
302 -{
303 - switch (key)
304 - {
305 - case 'd':
306 - case 'm':
307 - case 'p':
308 - case 'q':
309 - case 'r':
310 - case 't':
311 - case 'x':
312 - if (operation != oper_none)
313 - {
314 - error (0, 0, gettext ("More than one operation specified"));
315 - argp_help (&argp, stderr, ARGP_HELP_SEE,
316 - program_invocation_short_name);
317 - exit (EXIT_FAILURE);
318 - }
319 -
320 - switch (key)
321 - {
322 - case 'd':
323 - operation = oper_delete;
324 - break;
325 - case 'm':
326 - operation = oper_move;
327 - break;
328 - case 'p':
329 - operation = oper_print;
330 - break;
331 - case 'q':
332 - operation = oper_qappend;
333 - break;
334 - case 'r':
335 - operation = oper_replace;
336 - break;
337 - case 't':
338 - operation = oper_list;
339 - break;
340 - case 'x':
341 - operation = oper_extract;
342 - break;
343 - }
344 - break;
345 -
346 - case 'a':
347 - ipos = ipos_after;
348 - break;
349 -
350 - case 'b':
351 - case 'i':
352 - ipos = ipos_before;
353 - break;
354 -
355 - case 'c':
356 - suppress_create_msg = true;
357 - break;
358 -
359 - case 'C':
360 - dont_replace_existing = true;
361 - break;
362 -
363 - case 'N':
364 - instance_specifed = true;
365 - break;
366 -
367 - case 'o':
368 - preserve_dates = true;
369 - break;
370 -
371 - case 'P':
372 - full_path = true;
373 - break;
374 -
375 - case 's':
376 - force_symtab = true;
377 - break;
378 -
379 - case 'T':
380 - allow_truncate_fname = true;
381 - break;
382 -
383 - case 'u':
384 - update_newer = true;
385 - break;
386 -
387 - case 'v':
388 - verbose = true;
389 - break;
390 -
391 - default:
392 - return ARGP_ERR_UNKNOWN;
393 - }
394 - return 0;
395 -}
396 -
397 -
398 -static int
399 -open_archive (const char *arfname, int flags, int mode, Elf **elf,
400 - struct stat *st, bool miss_allowed)
401 -{
402 - int fd = open (arfname, flags, mode);
403 - if (fd == -1)
404 - {
405 - if (miss_allowed)
406 - return -1;
407 -
408 - error (EXIT_FAILURE, errno, gettext ("cannot open archive '%s'"),
409 - arfname);
410 - }
411 -
412 - if (elf != NULL)
413 - {
414 - Elf_Cmd cmd = flags == O_RDONLY ? ELF_C_READ_MMAP : ELF_C_RDWR_MMAP;
415 -
416 - *elf = elf_begin (fd, cmd, NULL);
417 - if (*elf == NULL)
418 - error (EXIT_FAILURE, 0, gettext ("cannot open archive '%s': %s"),
419 - arfname, elf_errmsg (-1));
420 -
421 - if (flags == O_RDONLY && elf_kind (*elf) != ELF_K_AR)
422 - error (EXIT_FAILURE, 0, gettext ("%s: not an archive file"), arfname);
423 - }
424 -
425 - if (st != NULL && fstat (fd, st) != 0)
426 - error (EXIT_FAILURE, errno, gettext ("cannot stat archive '%s'"),
427 - arfname);
428 -
429 - return fd;
430 -}
431 -
432 -
433 -static void
434 -not_found (int argc, char *argv[argc], bool found[argc])
435 -{
436 - for (int i = 0; i < argc; ++i)
437 - if (!found[i])
438 - printf (gettext ("no entry %s in archive\n"), argv[i]);
439 -}
440 -
441 -
442 -static int
443 -copy_content (Elf *elf, int newfd, off_t off, size_t n)
444 -{
445 - size_t len;
446 - char *rawfile = elf_rawfile (elf, &len);
447 -
448 - assert (off + n <= len);
449 -
450 - /* Tell the kernel we will read all the pages sequentially. */
451 - size_t ps = sysconf (_SC_PAGESIZE);
452 - if (n > 2 * ps)
453 - posix_madvise (rawfile + (off & ~(ps - 1)), n, POSIX_MADV_SEQUENTIAL);
454 -
455 - return write_retry (newfd, rawfile + off, n) != (ssize_t) n;
456 -}
457 -
458 -
459 -static int
460 -do_oper_extract (int oper, const char *arfname, char **argv, int argc,
461 - long int instance)
462 -{
463 - bool found[argc];
464 - memset (found, '\0', sizeof (found));
465 -
466 - size_t name_max = 0;
467 - inline bool should_truncate_fname (void)
468 - {
469 - if (errno == ENAMETOOLONG && allow_truncate_fname)
470 - {
471 - if (name_max == 0)
472 - {
473 - long int len = pathconf (".", _PC_NAME_MAX);
474 - if (len > 0)
475 - name_max = len;
476 - }
477 - return name_max != 0;
478 - }
479 - return false;
480 - }
481 -
482 - off_t index_off = -1;
483 - size_t index_size = 0;
484 - off_t cur_off = SARMAG;
485 -
486 - int status = 0;
487 - Elf *elf;
488 - int fd = open_archive (arfname, O_RDONLY, 0, &elf, NULL, false);
489 -
490 - if (hcreate (2 * argc) == 0)
491 - error (EXIT_FAILURE, errno, gettext ("cannot create hash table"));
492 -
493 - for (int cnt = 0; cnt < argc; ++cnt)
494 - {
495 - ENTRY entry = { .key = argv[cnt], .data = &argv[cnt] };
496 - if (hsearch (entry, ENTER) == NULL)
497 - error (EXIT_FAILURE, errno,
498 - gettext ("cannot insert into hash table"));
499 - }
500 -
501 - struct stat st;
502 - if (force_symtab)
503 - {
504 - if (fstat (fd, &st) != 0)
505 - {
506 - error (0, errno, gettext ("cannot stat '%s'"), arfname);
507 - close (fd);
508 - return 1;
509 - }
510 - arlib_init ();
511 - }
512 -
513 - Elf_Cmd cmd = ELF_C_READ_MMAP;
514 - Elf *subelf;
515 - while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
516 - {
517 - Elf_Arhdr *arhdr = elf_getarhdr (subelf);
518 -
519 - if (strcmp (arhdr->ar_name, "/") == 0)
520 - {
521 - index_off = elf_getaroff (subelf);
522 - index_size = arhdr->ar_size;
523 - goto next;
524 - }
525 - if (strcmp (arhdr->ar_name, "//") == 0)
526 - goto next;
527 -
528 - if (force_symtab)
529 - {
530 - arlib_add_symbols (elf, arfname, arhdr->ar_name, cur_off);
531 - cur_off += (((arhdr->ar_size + 1) & ~((off_t) 1))
532 - + sizeof (struct ar_hdr));
533 - }
534 -
535 - bool do_extract = argc <= 0;
536 - if (!do_extract)
537 - {
538 - ENTRY entry;
539 - entry.key = arhdr->ar_name;
540 - ENTRY *res = hsearch (entry, FIND);
541 - if (res != NULL && (instance < 0 || instance-- == 0)
542 - && !found[(char **) res->data - argv])
543 - found[(char **) res->data - argv] = do_extract = true;
544 - }
545 -
546 - if (do_extract)
547 - {
548 - if (verbose)
549 - {
550 - if (oper == oper_print)
551 - {
552 - printf ("\n<%s>\n\n", arhdr->ar_name);
553 -
554 - /* We have to flush now because now we use the descriptor
555 - directly. */
556 - fflush (stdout);
557 - }
558 - else if (oper == oper_list)
559 - {
560 - char datestr[100];
561 - strftime (datestr, sizeof (datestr), "%b %e %H:%M %Y",
562 - localtime (&arhdr->ar_date));
563 -
564 - printf ("%c%c%c%c%c%c%c%c%c %u/%u %6ju %s %s\n",
565 - (arhdr->ar_mode & S_IRUSR) ? 'r' : '-',
566 - (arhdr->ar_mode & S_IWUSR) ? 'w' : '-',
567 - (arhdr->ar_mode & S_IXUSR)
568 - ? ((arhdr->ar_mode & S_ISUID) ? 's' : 'x')
569 - : ((arhdr->ar_mode & S_ISUID) ? 'S' : '-'),
570 - (arhdr->ar_mode & S_IRGRP) ? 'r' : '-',
571 - (arhdr->ar_mode & S_IWGRP) ? 'w' : '-',
572 - (arhdr->ar_mode & S_IXGRP)
573 - ? ((arhdr->ar_mode & S_ISGID) ? 's' : 'x')
574 - : ((arhdr->ar_mode & S_ISGID) ? 'S' : '-'),
575 - (arhdr->ar_mode & S_IROTH) ? 'r' : '-',
576 - (arhdr->ar_mode & S_IWOTH) ? 'w' : '-',
577 - (arhdr->ar_mode & S_IXOTH)
578 - ? ((arhdr->ar_mode & S_ISVTX) ? 't' : 'x')
579 - : ((arhdr->ar_mode & S_ISVTX) ? 'T' : '-'),
580 - arhdr->ar_uid,
581 - arhdr->ar_gid,
582 - (uintmax_t) arhdr->ar_size,
583 - datestr,
584 - arhdr->ar_name);
585 - }
586 - else
587 - printf ("x - %s\n", arhdr->ar_name);
588 - }
589 -
590 - if (oper == oper_list)
591 - {
592 - if (!verbose)
593 - puts (arhdr->ar_name);
594 -
595 - goto next;
596 - }
597 -
598 - size_t nleft;
599 - char *data = elf_rawfile (subelf, &nleft);
600 - if (data == NULL)
601 - {
602 - error (0, 0, gettext ("cannot read content of %s: %s"),
603 - arhdr->ar_name, elf_errmsg (-1));
604 - status = 1;
605 - goto next;
606 - }
607 -
608 - int xfd;
609 - char tempfname[] = "XXXXXX";
610 - bool use_mkstemp = true;
611 -
612 - if (oper == oper_print)
613 - xfd = STDOUT_FILENO;
614 - else
615 - {
616 - xfd = mkstemp (tempfname);
617 - if (unlikely (xfd == -1))
618 - {
619 - /* We cannot create a temporary file. Try to overwrite
620 - the file or create it if it does not exist. */
621 - int flags = O_WRONLY | O_CREAT;
622 - if (dont_replace_existing)
623 - flags |= O_EXCL;
624 - else
625 - flags |= O_TRUNC;
626 - xfd = open (arhdr->ar_name, flags, 0600);
627 - if (unlikely (xfd == -1))
628 - {
629 - int printlen = INT_MAX;
630 -
631 - if (should_truncate_fname ())
632 - {
633 - /* Try to truncate the name. First find out by how
634 - much. */
635 - printlen = name_max;
636 - char truncfname[name_max + 1];
637 - *((char *) mempcpy (truncfname, arhdr->ar_name,
638 - name_max)) = '\0';
639 -
640 - xfd = open (truncfname, flags, 0600);
641 - }
642 -
643 - if (xfd == -1)
644 - {
645 - error (0, errno, gettext ("cannot open %.*s"),
646 - (int) printlen, arhdr->ar_name);
647 - status = 1;
648 - goto next;
649 - }
650 - }
651 -
652 - use_mkstemp = false;
653 - }
654 - }
655 -
656 - ssize_t n;
657 - while ((n = TEMP_FAILURE_RETRY (write (xfd, data, nleft))) != -1)
658 - {
659 - nleft -= n;
660 - if (nleft == 0)
661 - break;
662 - data += n;
663 - }
664 -
665 - if (unlikely (n == -1))
666 - {
667 - error (0, errno, gettext ("failed to write %s"), arhdr->ar_name);
668 - status = 1;
669 - unlink (tempfname);
670 - close (xfd);
671 - goto next;
672 - }
673 -
674 - if (oper != oper_print)
675 - {
676 - /* Fix up the mode. */
677 - if (unlikely (fchmod (xfd, arhdr->ar_mode) != 0))
678 - {
679 - error (0, errno, gettext ("cannot change mode of %s"),
680 - arhdr->ar_name);
681 - status = 0;
682 - }
683 -
684 - if (preserve_dates)
685 - {
686 - struct timespec tv[2];
687 - tv[0].tv_sec = arhdr->ar_date;
688 - tv[0].tv_nsec = 0;
689 - tv[1].tv_sec = arhdr->ar_date;
690 - tv[1].tv_nsec = 0;
691 -
692 - if (unlikely (futimens (xfd, tv) != 0))
693 - {
694 - error (0, errno,
695 - gettext ("cannot change modification time of %s"),
696 - arhdr->ar_name);
697 - status = 1;
698 - }
699 - }
700 -
701 - /* If we used a temporary file, move it do the right
702 - name now. */
703 - if (use_mkstemp)
704 - {
705 - int r;
706 -
707 - if (dont_replace_existing)
708 - {
709 - r = link (tempfname, arhdr->ar_name);
710 - if (likely (r == 0))
711 - unlink (tempfname);
712 - }
713 - else
714 - r = rename (tempfname, arhdr->ar_name);
715 -
716 - if (unlikely (r) != 0)
717 - {
718 - int printlen = INT_MAX;
719 -
720 - if (should_truncate_fname ())
721 - {
722 - /* Try to truncate the name. First find out by how
723 - much. */
724 - printlen = name_max;
725 - char truncfname[name_max + 1];
726 - *((char *) mempcpy (truncfname, arhdr->ar_name,
727 - name_max)) = '\0';
728 -
729 - if (dont_replace_existing)
730 - {
731 - r = link (tempfname, truncfname);
732 - if (likely (r == 0))
733 - unlink (tempfname);
734 - }
735 - else
736 - r = rename (tempfname, truncfname);
737 - }
738 -
739 - if (r != 0)
740 - {
741 - error (0, errno, gettext ("\
742 -cannot rename temporary file to %.*s"),
743 - printlen, arhdr->ar_name);
744 - unlink (tempfname);
745 - status = 1;
746 - }
747 - }
748 - }
749 -
750 - close (xfd);
751 - }
752 - }
753 -
754 - next:
755 - cmd = elf_next (subelf);
756 - if (elf_end (subelf) != 0)
757 - error (1, 0, "%s: %s", arfname, elf_errmsg (-1));
758 - }
759 -
760 - hdestroy ();
761 -
762 - if (force_symtab)
763 - {
764 - arlib_finalize ();
765 -
766 - if (symtab.symsnamelen != 0
767 - /* We have to rewrite the file also if it initially had an index
768 - but now does not need one anymore. */
769 - || (symtab.symsnamelen == 0 && index_size != 0))
770 - {
771 - char tmpfname[strlen (arfname) + 7];
772 - strcpy (stpcpy (tmpfname, arfname), "XXXXXX");
773 - int newfd = mkstemp (tmpfname);
774 - if (unlikely (newfd == -1))
775 - {
776 - nonew:
777 - error (0, errno, gettext ("cannot create new file"));
778 - status = 1;
779 - }
780 - else
781 - {
782 - /* Create the header. */
783 - if (unlikely (write_retry (newfd, ARMAG, SARMAG) != SARMAG))
784 - {
785 - // XXX Use /prof/self/fd/%d ???
786 - nonew_unlink:
787 - unlink (tmpfname);
788 - if (newfd != -1)
789 - close (newfd);
790 - goto nonew;
791 - }
792 -
793 - /* Create the new file. There are three parts as far we are
794 - concerned: 1. original context before the index, 2. the
795 - new index, 3. everything after the new index. */
796 - off_t rest_off;
797 - if (index_off != -1)
798 - rest_off = (index_off + sizeof (struct ar_hdr)
799 - + ((index_size + 1) & ~1ul));
800 - else
801 - rest_off = SARMAG;
802 -
803 - if ((symtab.symsnamelen != 0
804 - && ((write_retry (newfd, symtab.symsoff,
805 - symtab.symsofflen)
806 - != (ssize_t) symtab.symsofflen)
807 - || (write_retry (newfd, symtab.symsname,
808 - symtab.symsnamelen)
809 - != (ssize_t) symtab.symsnamelen)))
810 - /* Even if the original file had content before the
811 - symbol table, we write it in the correct order. */
812 - || (index_off != SARMAG
813 - && copy_content (elf, newfd, SARMAG, index_off - SARMAG))
814 - || copy_content (elf, newfd, rest_off, st.st_size - rest_off)
815 - /* Set the mode of the new file to the same values the
816 - original file has. */
817 - || fchmod (newfd, st.st_mode & ALLPERMS) != 0
818 - /* Never complain about fchown failing. */
819 - || (({asm ("" :: "r" (fchown (newfd, st.st_uid,
820 - st.st_gid))); }),
821 - close (newfd) != 0)
822 - || (newfd = -1, rename (tmpfname, arfname) != 0))
823 - goto nonew_unlink;
824 - }
825 - }
826 - }
827 -
828 - elf_end (elf);
829 -
830 - close (fd);
831 -
832 - not_found (argc, argv, found);
833 -
834 - return status;
835 -}
836 -
837 -
838 -struct armem
839 -{
840 - off_t off;
841 - off_t old_off;
842 - size_t size;
843 - long int long_name_off;
844 - struct armem *next;
845 - void *mem;
846 - time_t sec;
847 - uid_t uid;
848 - gid_t gid;
849 - mode_t mode;
850 - const char *name;
851 - Elf *elf;
852 -};
853 -
854 -
855 -static int
856 -write_member (struct armem *memb, off_t *startp, off_t *lenp, Elf *elf,
857 - off_t end_off, int newfd)
858 -{
859 - struct ar_hdr arhdr;
860 - char tmpbuf[sizeof (arhdr.ar_name) + 1];
861 -
862 - bool changed_header = memb->long_name_off != -1;
863 - if (changed_header)
864 - {
865 - /* In case of a long file name we assume the archive header
866 - changed and we write it here. */
867 - memcpy (&arhdr, elf_rawfile (elf, NULL) + *startp, sizeof (arhdr));
868 -
869 - snprintf (tmpbuf, sizeof (tmpbuf), "/%-*ld",
870 - (int) sizeof (arhdr.ar_name), memb->long_name_off);
871 - changed_header = memcmp (arhdr.ar_name, tmpbuf,
872 - sizeof (arhdr.ar_name)) != 0;
873 - }
874 -
875 - /* If the files are adjacent in the old file extend the range. */
876 - if (*startp != -1 && !changed_header && *startp + *lenp == memb->old_off)
877 - {
878 - /* Extend the current range. */
879 - *lenp += (memb->next != NULL
880 - ? memb->next->off : end_off) - memb->off;
881 - return 0;
882 - }
883 -
884 - /* Write out the old range. */
885 - if (*startp != -1 && copy_content (elf, newfd, *startp, *lenp))
886 - return -1;
887 -
888 - *startp = memb->old_off;
889 - *lenp = (memb->next != NULL ? memb->next->off : end_off) - memb->off;
890 -
891 - if (changed_header)
892 - {
893 - memcpy (arhdr.ar_name, tmpbuf, sizeof (arhdr.ar_name));
894 -
895 - if (unlikely (write_retry (newfd, &arhdr, sizeof (arhdr))
896 - != sizeof (arhdr)))
897 - return -1;
898 -
899 - *startp += sizeof (struct ar_hdr);
900 - assert ((size_t) *lenp >= sizeof (struct ar_hdr));
901 - *lenp -= sizeof (struct ar_hdr);
902 - }
903 -
904 - return 0;
905 -}
906 -
907 -/* Store the name in the long name table if necessary.
908 - Record its offset or -1 if we did not need to use the table. */
909 -static void
910 -remember_long_name (struct armem *mem, const char *name, size_t namelen)
911 -{
912 - mem->long_name_off = (namelen > MAX_AR_NAME_LEN
913 - ? arlib_add_long_name (name, namelen)
914 - : -1l);
915 -}
916 -
917 -static int
918 -do_oper_delete (const char *arfname, char **argv, int argc,
919 - long int instance)
920 -{
921 - bool *found = alloca (sizeof (bool) * argc);
922 - memset (found, '\0', sizeof (bool) * argc);
923 -
924 - /* List of the files we keep. */
925 - struct armem *to_copy = NULL;
926 -
927 - int status = 0;
928 - Elf *elf;
929 - struct stat st;
930 - int fd = open_archive (arfname, O_RDONLY, 0, &elf, &st, false);
931 -
932 - if (hcreate (2 * argc) == 0)
933 - error (EXIT_FAILURE, errno, gettext ("cannot create hash table"));
934 -
935 - for (int cnt = 0; cnt < argc; ++cnt)
936 - {
937 - ENTRY entry = { .key = argv[cnt], .data = &argv[cnt] };
938 - if (hsearch (entry, ENTER) == NULL)
939 - error (EXIT_FAILURE, errno,
940 - gettext ("cannot insert into hash table"));
941 - }
942 -
943 - arlib_init ();
944 -
945 - off_t cur_off = SARMAG;
946 - Elf_Cmd cmd = ELF_C_READ_MMAP;
947 - Elf *subelf;
948 - while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
949 - {
950 - Elf_Arhdr *arhdr = elf_getarhdr (subelf);
951 -
952 - /* Ignore the symbol table and the long file name table here. */
953 - if (strcmp (arhdr->ar_name, "/") == 0
954 - || strcmp (arhdr->ar_name, "//") == 0)
955 - goto next;
956 -
957 - bool do_delete = argc <= 0;
958 - if (!do_delete)
959 - {
960 - ENTRY entry;
961 - entry.key = arhdr->ar_name;
962 - ENTRY *res = hsearch (entry, FIND);
963 - if (res != NULL && (instance < 0 || instance-- == 0)
964 - && !found[(char **) res->data - argv])
965 - found[(char **) res->data - argv] = do_delete = true;
966 - }
967 -
968 - if (do_delete)
969 - {
970 - if (verbose)
971 - printf ("d - %s\n", arhdr->ar_name);
972 - }
973 - else
974 - {
975 - struct armem *newp = alloca (sizeof (struct armem));
976 - newp->old_off = elf_getaroff (subelf);
977 - newp->off = cur_off;
978 -
979 - cur_off += (((arhdr->ar_size + 1) & ~((off_t) 1))
980 - + sizeof (struct ar_hdr));
981 -
982 - if (to_copy == NULL)
983 - to_copy = newp->next = newp;
984 - else
985 - {
986 - newp->next = to_copy->next;
987 - to_copy = to_copy->next = newp;
988 - }
989 -
990 - /* If we recreate the symbol table read the file's symbol
991 - table now. */
992 - arlib_add_symbols (subelf, arfname, arhdr->ar_name, newp->off);
993 -
994 - /* Remember long file names. */
995 - remember_long_name (newp, arhdr->ar_name, strlen (arhdr->ar_name));
996 - }
997 -
998 - next:
999 - cmd = elf_next (subelf);
1000 - if (elf_end (subelf) != 0)
1001 - error (1, 0, "%s: %s", arfname, elf_errmsg (-1));
1002 - }
1003 -
1004 - arlib_finalize ();
1005 -
1006 - hdestroy ();
1007 -
1008 - /* Create a new, temporary file in the same directory as the
1009 - original file. */
1010 - char tmpfname[strlen (arfname) + 7];
1011 - strcpy (stpcpy (tmpfname, arfname), "XXXXXX");
1012 - int newfd = mkstemp (tmpfname);
1013 - if (unlikely (newfd == -1))
1014 - goto nonew;
1015 -
1016 - /* Create the header. */
1017 - if (unlikely (write_retry (newfd, ARMAG, SARMAG) != SARMAG))
1018 - {
1019 - // XXX Use /prof/self/fd/%d ???
1020 - nonew_unlink:
1021 - unlink (tmpfname);
1022 - if (newfd != -1)
1023 - close (newfd);
1024 - nonew:
1025 - error (0, errno, gettext ("cannot create new file"));
1026 - status = 1;
1027 - goto errout;
1028 - }
1029 -
1030 - /* If the archive is empty that is all we have to do. */
1031 - if (likely (to_copy != NULL))
1032 - {
1033 - /* Write the symbol table or the long file name table or both. */
1034 - if (symtab.symsnamelen != 0
1035 - && ((write_retry (newfd, symtab.symsoff, symtab.symsofflen)
1036 - != (ssize_t) symtab.symsofflen)
1037 - || (write_retry (newfd, symtab.symsname, symtab.symsnamelen)
1038 - != (ssize_t) symtab.symsnamelen)))
1039 - goto nonew_unlink;
1040 -
1041 - if (symtab.longnameslen > sizeof (struct ar_hdr)
1042 - && (write_retry (newfd, symtab.longnames, symtab.longnameslen)
1043 - != (ssize_t) symtab.longnameslen))
1044 - goto nonew_unlink;
1045 -
1046 - /* NULL-terminate the list of files to copy. */
1047 - struct armem *last = to_copy;
1048 - to_copy = to_copy->next;
1049 - last->next = NULL;
1050 -
1051 - off_t start = -1;
1052 - off_t len = -1;
1053 -
1054 - do
1055 - if (write_member (to_copy, &start, &len, elf, cur_off, newfd) != 0)
1056 - goto nonew_unlink;
1057 - while ((to_copy = to_copy->next) != NULL);
1058 -
1059 - /* Write the last part. */
1060 - if (copy_content (elf, newfd, start, len))
1061 - goto nonew_unlink;
1062 - }
1063 -
1064 - /* Set the mode of the new file to the same values the original file
1065 - has. */
1066 - if (fchmod (newfd, st.st_mode & ALLPERMS) != 0
1067 - /* Never complain about fchown failing. */
1068 - || (({asm ("" :: "r" (fchown (newfd, st.st_uid, st.st_gid))); }),
1069 - close (newfd) != 0)
1070 - || (newfd = -1, rename (tmpfname, arfname) != 0))
1071 - goto nonew_unlink;
1072 -
1073 - errout:
1074 -#ifdef DEBUG
1075 - elf_end (elf);
1076 -
1077 - arlib_fini ();
1078 -
1079 - close (fd);
1080 -#endif
1081 -
1082 - not_found (argc, argv, found);
1083 -
1084 - return status;
1085 -}
1086 -
1087 -
1088 -static void
1089 -no0print (bool ofmt, char *buf, int bufsize, long int val)
1090 -{
1091 - char tmpbuf[bufsize + 1];
1092 - snprintf (tmpbuf, sizeof (tmpbuf), ofmt ? "%-*lo" : "%-*ld", bufsize, val);
1093 - memcpy (buf, tmpbuf, bufsize);
1094 -}
1095 -
1096 -
1097 -static int
1098 -do_oper_insert (int oper, const char *arfname, char **argv, int argc,
1099 - const char *member)
1100 -{
1101 - int status = 0;
1102 - Elf *elf;
1103 - struct stat st;
1104 - int fd = open_archive (arfname, O_RDONLY, 0, &elf, &st, oper != oper_move);
1105 -
1106 - /* List of the files we keep. */
1107 - struct armem *all = NULL;
1108 - struct armem *after_memberelem = NULL;
1109 - struct armem **found = alloca (sizeof (*found) * argc);
1110 - memset (found, '\0', sizeof (*found) * argc);
1111 -
1112 - arlib_init ();
1113 -
1114 - /* Initialize early for no_old case. */
1115 - off_t cur_off = SARMAG;
1116 -
1117 - if (fd == -1)
1118 - {
1119 - if (!suppress_create_msg)
1120 - fprintf (stderr, "%s: creating %s\n",
1121 - program_invocation_short_name, arfname);
1122 -
1123 - goto no_old;
1124 - }
1125 -
1126 - /* Store the names of all files from the command line in a hash
1127 - table so that we can match it. Note that when no file name is
1128 - given we are basically doing nothing except recreating the
1129 - index. */
1130 - if (oper != oper_qappend)
1131 - {
1132 - if (hcreate (2 * argc) == 0)
1133 - error (EXIT_FAILURE, errno, gettext ("cannot create hash table"));
1134 -
1135 - for (int cnt = 0; cnt < argc; ++cnt)
1136 - {
1137 - ENTRY entry;
1138 - entry.key = full_path ? argv[cnt] : basename (argv[cnt]);
1139 - entry.data = &argv[cnt];
1140 - if (hsearch (entry, ENTER) == NULL)
1141 - error (EXIT_FAILURE, errno,
1142 - gettext ("cannot insert into hash table"));
1143 - }
1144 - }
1145 -
1146 - /* While iterating over the current content of the archive we must
1147 - determine a number of things: which archive members to keep,
1148 - which are replaced, and where to insert the new members. */
1149 - Elf_Cmd cmd = ELF_C_READ_MMAP;
1150 - Elf *subelf;
1151 - while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
1152 - {
1153 - Elf_Arhdr *arhdr = elf_getarhdr (subelf);
1154 -
1155 - /* Ignore the symbol table and the long file name table here. */
1156 - if (strcmp (arhdr->ar_name, "/") == 0
1157 - || strcmp (arhdr->ar_name, "//") == 0)
1158 - goto next;
1159 -
1160 - struct armem *newp = alloca (sizeof (struct armem));
1161 - newp->old_off = elf_getaroff (subelf);
1162 - newp->size = arhdr->ar_size;
1163 - newp->sec = arhdr->ar_date;
1164 - newp->mem = NULL;
1165 -
1166 - /* Remember long file names. */
1167 - remember_long_name (newp, arhdr->ar_name, strlen (arhdr->ar_name));
1168 -
1169 - /* Check whether this is a file we are looking for. */
1170 - if (oper != oper_qappend)
1171 - {
1172 - /* Check whether this is the member used as the insert point. */
1173 - if (member != NULL && strcmp (arhdr->ar_name, member) == 0)
1174 - {
1175 - /* Note that all == NULL means insert at the beginning. */
1176 - if (ipos == ipos_before)
1177 - after_memberelem = all;
1178 - else
1179 - after_memberelem = newp;
1180 - member = NULL;
1181 - }
1182 -
1183 - ENTRY entry;
1184 - entry.key = arhdr->ar_name;
1185 - ENTRY *res = hsearch (entry, FIND);
1186 - if (res != NULL && found[(char **) res->data - argv] == NULL)
1187 - {
1188 - found[(char **) res->data - argv] = newp;
1189 -
1190 - /* If we insert before or after a certain element move
1191 - all files to a special list. */
1192 - if (unlikely (ipos != ipos_none || oper == oper_move))
1193 - {
1194 - if (after_memberelem == newp)
1195 - /* Since we remove this element even though we should
1196 - insert everything after it, we in fact insert
1197 - everything after the previous element. */
1198 - after_memberelem = all;
1199 -
1200 - goto next;
1201 - }
1202 - }
1203 - }
1204 -
1205 - if (all == NULL)
1206 - all = newp->next = newp;
1207 - else
1208 - {
1209 - newp->next = all->next;
1210 - all = all->next = newp;
1211 - }
1212 -
1213 - next:
1214 - cmd = elf_next (subelf);
1215 - if (elf_end (subelf) != 0)
1216 - error (EXIT_FAILURE, 0, "%s: %s", arfname, elf_errmsg (-1));
1217 - }
1218 -
1219 - if (oper != oper_qappend)
1220 - hdestroy ();
1221 -
1222 - no_old:
1223 - if (member != NULL)
1224 - error (EXIT_FAILURE, 0, gettext ("position member %s not found"),
1225 - member);
1226 -
1227 - if (oper == oper_move)
1228 - {
1229 - /* Make sure all requested elements are found in the archive. */
1230 - for (int cnt = 0; cnt < argc; ++cnt)
1231 - {
1232 - if (found[cnt] == NULL)
1233 - {
1234 - fprintf (stderr, gettext ("%s: no entry %s in archive!\n"),
1235 - program_invocation_short_name, argv[cnt]);
1236 - status = 1;
1237 - }
1238 -
1239 - if (verbose)
1240 - printf ("m - %s\n", argv[cnt]);
1241 - }
1242 - }
1243 - else
1244 - {
1245 - /* Open all the new files, get their sizes and add all symbols. */
1246 - for (int cnt = 0; cnt < argc; ++cnt)
1247 - {
1248 - const char *bname = basename (argv[cnt]);
1249 - size_t bnamelen = strlen (bname);
1250 - if (found[cnt] == NULL)
1251 - {
1252 - found[cnt] = alloca (sizeof (struct armem));
1253 - found[cnt]->old_off = -1;
1254 -
1255 - remember_long_name (found[cnt], bname, bnamelen);
1256 - }
1257 -
1258 - struct stat newst;
1259 - Elf *newelf;
1260 - int newfd = open (argv[cnt], O_RDONLY);
1261 - if (newfd == -1)
1262 - {
1263 - error (0, errno, gettext ("cannot open %s"), argv[cnt]);
1264 - status = 1;
1265 - }
1266 - else if (fstat (newfd, &newst) == -1)
1267 - {
1268 - error (0, errno, gettext ("cannot stat %s"), argv[cnt]);
1269 - close (newfd);
1270 - status = 1;
1271 - }
1272 - else if (!S_ISREG (newst.st_mode))
1273 - {
1274 - error (0, errno, gettext ("%s is no regular file"), argv[cnt]);
1275 - close (newfd);
1276 - status = 1;
1277 - }
1278 - else if (update_newer
1279 - && found[cnt]->old_off != -1l
1280 - && found[cnt]->sec > st.st_mtime)
1281 - /* Do nothing, the file in the archive is younger. */
1282 - close (newfd);
1283 - else if ((newelf = elf_begin (newfd, ELF_C_READ_MMAP, NULL))
1284 - == NULL)
1285 - {
1286 - fprintf (stderr,
1287 - gettext ("cannot get ELF descriptor for %s: %s\n"),
1288 - argv[cnt], elf_errmsg (-1));
1289 - status = 1;
1290 - }
1291 - else
1292 - {
1293 - if (verbose)
1294 - printf ("%c - %s\n",
1295 - found[cnt]->old_off == -1l ? 'a' : 'r', argv[cnt]);
1296 -
1297 - found[cnt]->elf = newelf;
1298 - found[cnt]->sec = arlib_deterministic_output ? 0 : newst.st_mtime;
1299 - found[cnt]->uid = arlib_deterministic_output ? 0 : newst.st_uid;
1300 - found[cnt]->gid = arlib_deterministic_output ? 0 : newst.st_gid;
1301 - found[cnt]->mode = newst.st_mode;
1302 - found[cnt]->name = bname;
1303 -
1304 - found[cnt]->mem = elf_rawfile (newelf, &found[cnt]->size);
1305 - if (found[cnt]->mem == NULL
1306 - || elf_cntl (newelf, ELF_C_FDDONE) != 0)
1307 - error (EXIT_FAILURE, 0, gettext ("cannot read %s: %s"),
1308 - argv[cnt], elf_errmsg (-1));
1309 -
1310 - close (newfd);
1311 -
1312 - if (found[cnt]->old_off != -1l)
1313 - /* Remember long file names. */
1314 - remember_long_name (found[cnt], bname, bnamelen);
1315 - }
1316 - }
1317 - }
1318 -
1319 - if (status != 0)
1320 - {
1321 -#ifdef DEBUG
1322 - elf_end (elf);
1323 -
1324 - arlib_fini ();
1325 -
1326 - close (fd);
1327 -#endif
1328 -
1329 - return status;
1330 - }
1331 -
1332 - /* If we have no entry point so far add at the end. AFTER_MEMBERELEM
1333 - being NULL when adding before an entry means add at the beginning. */
1334 - if (ipos != ipos_before && after_memberelem == NULL)
1335 - after_memberelem = all;
1336 -
1337 - /* Convert the circular list into a normal list first. */
1338 - if (all != NULL)
1339 - {
1340 - struct armem *tmp = all;
1341 - all = all->next;
1342 - tmp->next = NULL;
1343 - }
1344 -
1345 - struct armem *last_added = after_memberelem;
1346 - for (int cnt = 0; cnt < argc; ++cnt)
1347 - if (oper != oper_replace || found[cnt]->old_off == -1)
1348 - {
1349 - if (last_added == NULL)
1350 - {
1351 - found[cnt]->next = all;
1352 - last_added = all = found[cnt];
1353 - }
1354 - else
1355 - {
1356 - found[cnt]->next = last_added->next;
1357 - last_added = last_added->next = found[cnt];
1358 - }
1359 - }
1360 -
1361 - /* Finally compute the offset and add the symbols for the files
1362 - after the insert point. */
1363 - if (likely (all != NULL))
1364 - for (struct armem *memp = all; memp != NULL; memp = memp->next)
1365 - {
1366 - memp->off = cur_off;
1367 -
1368 - if (memp->mem == NULL)
1369 - {
1370 - Elf_Arhdr *arhdr;
1371 - /* Fake initializing arhdr and subelf to keep gcc calm. */
1372 - asm ("" : "=m" (arhdr), "=m" (subelf));
1373 - if (elf_rand (elf, memp->old_off) == 0
1374 - || (subelf = elf_begin (fd, ELF_C_READ_MMAP, elf)) == NULL
1375 - || (arhdr = elf_getarhdr (subelf)) == NULL)
1376 - /* This should never happen since we already looked at the
1377 - archive content. But who knows... */
1378 - error (EXIT_FAILURE, 0, "%s: %s", arfname, elf_errmsg (-1));
1379 -
1380 - arlib_add_symbols (subelf, arfname, arhdr->ar_name, cur_off);
1381 -
1382 - elf_end (subelf);
1383 - }
1384 - else
1385 - arlib_add_symbols (memp->elf, arfname, memp->name, cur_off);
1386 -
1387 - cur_off += (((memp->size + 1) & ~((off_t) 1))
1388 - + sizeof (struct ar_hdr));
1389 - }
1390 -
1391 - /* Now we have all the information for the symbol table and long
1392 - file name table. Construct the final layout. */
1393 - arlib_finalize ();
1394 -
1395 - /* Create a new, temporary file in the same directory as the
1396 - original file. */
1397 - char tmpfname[strlen (arfname) + 7];
1398 - strcpy (stpcpy (tmpfname, arfname), "XXXXXX");
1399 - int newfd;
1400 - if (fd != -1)
1401 - newfd = mkstemp (tmpfname);
1402 - else
1403 - {
1404 - newfd = open (arfname, O_RDWR | O_CREAT | O_EXCL, DEFFILEMODE);
1405 - if (newfd == -1 && errno == EEXIST)
1406 - /* Bah, first the file did not exist, now it does. Restart. */
1407 - return do_oper_insert (oper, arfname, argv, argc, member);
1408 - }
1409 - if (unlikely (newfd == -1))
1410 - goto nonew;
1411 -
1412 - /* Create the header. */
1413 - if (unlikely (write_retry (newfd, ARMAG, SARMAG) != SARMAG))
1414 - {
1415 - nonew_unlink:
1416 - if (fd != -1)
1417 - {
1418 - // XXX Use /prof/self/fd/%d ???
1419 - unlink (tmpfname);
1420 - if (newfd != -1)
1421 - close (newfd);
1422 - }
1423 - nonew:
1424 - error (0, errno, gettext ("cannot create new file"));
1425 - status = 1;
1426 - goto errout;
1427 - }
1428 -
1429 - /* If the new archive is not empty we actually have something to do. */
1430 - if (likely (all != NULL))
1431 - {
1432 - /* Write the symbol table or the long file name table or both. */
1433 - if (symtab.symsnamelen != 0
1434 - && ((write_retry (newfd, symtab.symsoff, symtab.symsofflen)
1435 - != (ssize_t) symtab.symsofflen)
1436 - || (write_retry (newfd, symtab.symsname, symtab.symsnamelen)
1437 - != (ssize_t) symtab.symsnamelen)))
1438 - goto nonew_unlink;
1439 -
1440 - if (symtab.longnameslen > sizeof (struct ar_hdr)
1441 - && (write_retry (newfd, symtab.longnames, symtab.longnameslen)
1442 - != (ssize_t) symtab.longnameslen))
1443 - goto nonew_unlink;
1444 -
1445 - off_t start = -1;
1446 - off_t len = -1;
1447 -
1448 - while (all != NULL)
1449 - {
1450 - if (all->mem != NULL)
1451 - {
1452 - /* This is a new file. If there is anything from the
1453 - archive left to be written do it now. */
1454 - if (start != -1 && copy_content (elf, newfd, start, len))
1455 - goto nonew_unlink;
1456 -
1457 - start = -1;
1458 - len = -1;
1459 -
1460 - /* Create the header. */
1461 - struct ar_hdr arhdr;
1462 - char tmpbuf[sizeof (arhdr.ar_name) + 1];
1463 - if (all->long_name_off == -1)
1464 - {
1465 - size_t namelen = strlen (all->name);
1466 - char *p = mempcpy (arhdr.ar_name, all->name, namelen);
1467 - *p++ = '/';
1468 - memset (p, ' ', sizeof (arhdr.ar_name) - namelen - 1);
1469 - }
1470 - else
1471 - {
1472 - snprintf (tmpbuf, sizeof (arhdr.ar_name) + 1, "/%-*ld",
1473 - (int) sizeof (arhdr.ar_name), all->long_name_off);
1474 - memcpy (arhdr.ar_name, tmpbuf, sizeof (arhdr.ar_name));
1475 - }
1476 -
1477 - no0print (false, arhdr.ar_date, sizeof (arhdr.ar_date),
1478 - all->sec);
1479 - no0print (false, arhdr.ar_uid, sizeof (arhdr.ar_uid), all->uid);
1480 - no0print (false, arhdr.ar_gid, sizeof (arhdr.ar_gid), all->gid);
1481 - no0print (true, arhdr.ar_mode, sizeof (arhdr.ar_mode),
1482 - all->mode);
1483 - no0print (false, arhdr.ar_size, sizeof (arhdr.ar_size),
1484 - all->size);
1485 - memcpy (arhdr.ar_fmag, ARFMAG, sizeof (arhdr.ar_fmag));
1486 -
1487 - if (unlikely (write_retry (newfd, &arhdr, sizeof (arhdr))
1488 - != sizeof (arhdr)))
1489 - goto nonew_unlink;
1490 -
1491 - /* Now the file itself. */
1492 - if (unlikely (write_retry (newfd, all->mem, all->size)
1493 - != (off_t) all->size))
1494 - goto nonew_unlink;
1495 -
1496 - /* Pad the file if its size is odd. */
1497 - if ((all->size & 1) != 0)
1498 - if (unlikely (write_retry (newfd, "\n", 1) != 1))
1499 - goto nonew_unlink;
1500 - }
1501 - else
1502 - {
1503 - /* This is a member from the archive. */
1504 - if (write_member (all, &start, &len, elf, cur_off, newfd)
1505 - != 0)
1506 - goto nonew_unlink;
1507 - }
1508 -
1509 - all = all->next;
1510 - }
1511 -
1512 - /* Write the last part. */
1513 - if (start != -1 && copy_content (elf, newfd, start, len))
1514 - goto nonew_unlink;
1515 - }
1516 -
1517 - /* Set the mode of the new file to the same values the original file
1518 - has. */
1519 - if (fd != -1
1520 - && (fchmod (newfd, st.st_mode & ALLPERMS) != 0
1521 - /* Never complain about fchown failing. */
1522 - || (({asm ("" :: "r" (fchown (newfd, st.st_uid, st.st_gid))); }),
1523 - close (newfd) != 0)
1524 - || (newfd = -1, rename (tmpfname, arfname) != 0)))
1525 - goto nonew_unlink;
1526 -
1527 - errout:
1528 -#ifdef DEBUG
1529 - elf_end (elf);
1530 -
1531 - arlib_fini ();
1532 -
1533 - close (fd);
1534 -#endif
1535 -
1536 - return status;
1537 -}
1538 -
1539 -
1540 -#include "debugpred.h"
1541 +int main() { return 0; }