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