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