1 diff -u -r ../elfutils-0.166/src/ar.c ./src/ar.c
2 --- ../elfutils-0.166/src/ar.c 2015-11-27 08:36:29.000000000 -0500
3 +++ ./src/ar.c 2016-05-04 14:29:53.035376083 -0400
5 -/* Create, modify, and extract from archives.
6 - Copyright (C) 2005-2012 Red Hat, Inc.
7 - This file is part of elfutils.
8 - Written by Ulrich Drepper <drepper@redhat.com>, 2005.
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.
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.
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/>. */
39 -#include <stdio_ext.h>
43 -#include <sys/mman.h>
44 -#include <sys/stat.h>
45 -#include <sys/time.h>
52 -/* Name and version of program. */
53 -static void print_version (FILE *stream, struct argp_state *state);
54 -ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
56 -/* Prototypes for local functions. */
57 -static int do_oper_extract (int oper, const char *arfname, char **argv,
58 - int argc, long int instance);
59 -static int do_oper_delete (const char *arfname, char **argv, int argc,
61 -static int do_oper_insert (int oper, const char *arfname, char **argv,
62 - int argc, const char *member);
65 -/* Bug report address. */
66 -ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
69 -/* Definitions of arguments for argp functions. */
70 -static const struct argp_option options[] =
72 - { NULL, 0, NULL, 0, N_("Commands:"), 1 },
73 - { NULL, 'd', NULL, 0, N_("Delete files from archive."), 0 },
74 - { NULL, 'm', NULL, 0, N_("Move files in archive."), 0 },
75 - { NULL, 'p', NULL, 0, N_("Print files in archive."), 0 },
76 - { NULL, 'q', NULL, 0, N_("Quick append files to archive."), 0 },
77 - { NULL, 'r', NULL, 0,
78 - N_("Replace existing or insert new file into archive."), 0 },
79 - { NULL, 't', NULL, 0, N_("Display content of archive."), 0 },
80 - { NULL, 'x', NULL, 0, N_("Extract files from archive."), 0 },
82 - { NULL, 0, NULL, 0, N_("Command Modifiers:"), 2 },
83 - { NULL, 'o', NULL, 0, N_("Preserve original dates."), 0 },
84 - { NULL, 'N', NULL, 0, N_("Use instance [COUNT] of name."), 0 },
85 - { NULL, 'C', NULL, 0,
86 - N_("Do not replace existing files with extracted files."), 0 },
87 - { NULL, 'T', NULL, 0, N_("Allow filename to be truncated if necessary."),
89 - { NULL, 'v', NULL, 0, N_("Provide verbose output."), 0 },
90 - { NULL, 's', NULL, 0, N_("Force regeneration of symbol table."), 0 },
91 - { NULL, 'a', NULL, 0, N_("Insert file after [MEMBER]."), 0 },
92 - { NULL, 'b', NULL, 0, N_("Insert file before [MEMBER]."), 0 },
93 - { NULL, 'i', NULL, 0, N_("Same as -b."), 0 },
94 - { NULL, 'c', NULL, 0, N_("Suppress message when library has to be created."),
96 - { NULL, 'P', NULL, 0, N_("Use full path for file matching."), 0 },
97 - { NULL, 'u', NULL, 0, N_("Update only older files in archive."), 0 },
99 - { NULL, 0, NULL, 0, NULL, 0 }
102 -/* Short description of program. */
103 -static const char doc[] = N_("Create, modify, and extract from archives.");
105 -/* Strings for arguments in help texts. */
106 -static const char args_doc[] = N_("[MEMBER] [COUNT] ARCHIVE [FILE...]");
108 -/* Prototype for option handler. */
109 -static error_t parse_opt (int key, char *arg, struct argp_state *state);
111 -/* Data structure to communicate with argp functions. */
112 -static struct argp argp =
114 - options, parse_opt, args_doc, doc, arlib_argp_children, NULL, NULL
118 -/* What operation to perform. */
132 -static bool verbose;
133 -static bool preserve_dates;
134 -static bool instance_specifed;
135 -static bool dont_replace_existing;
136 -static bool allow_truncate_fname;
137 -static bool force_symtab;
138 -static bool suppress_create_msg;
139 -static bool full_path;
140 -static bool update_newer;
141 -static enum { ipos_none, ipos_before, ipos_after } ipos;
145 -main (int argc, char *argv[])
147 - /* We use no threads here which can interfere with handling a stream. */
148 - (void) __fsetlocking (stdin, FSETLOCKING_BYCALLER);
149 - (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
150 - (void) __fsetlocking (stderr, FSETLOCKING_BYCALLER);
153 - (void) setlocale (LC_ALL, "");
155 - /* Make sure the message catalog can be found. */
156 - (void) bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
158 - /* Initialize the message catalog. */
159 - (void) textdomain (PACKAGE_TARNAME);
161 - /* For historical reasons the options in the first parameter need
162 - not be preceded by a dash. Add it now if necessary. */
163 - if (argc > 1 && argv[1][0] != '-')
165 - size_t len = strlen (argv[1]) + 1;
166 - char *newp = alloca (len + 1);
168 - memcpy (&newp[1], argv[1], len);
172 - /* Parse and process arguments. */
174 - (void) argp_parse (&argp, argc, argv, ARGP_IN_ORDER, &remaining, NULL);
176 - /* Tell the library which version we are expecting. */
177 - (void) elf_version (EV_CURRENT);
179 - /* Handle the [MEMBER] parameter. */
180 - const char *member = NULL;
181 - if (ipos != ipos_none)
183 - /* Only valid for certain operations. */
184 - if (operation != oper_move && operation != oper_replace)
185 - error (1, 0, gettext ("\
186 -'a', 'b', and 'i' are only allowed with the 'm' and 'r' options"));
188 - if (remaining == argc)
190 - error (0, 0, gettext ("\
191 -MEMBER parameter required for 'a', 'b', and 'i' modifiers"));
192 - argp_help (&argp, stderr, ARGP_HELP_USAGE | ARGP_HELP_SEE,
193 - program_invocation_short_name);
194 - exit (EXIT_FAILURE);
197 - member = argv[remaining++];
200 - /* Handle the [COUNT] parameter. */
201 - long int instance = -1;
202 - if (instance_specifed)
204 - /* Only valid for certain operations. */
205 - if (operation != oper_extract && operation != oper_delete)
206 - error (1, 0, gettext ("\
207 -'N' is only meaningful with the 'x' and 'd' options"));
209 - if (remaining == argc)
211 - error (0, 0, gettext ("COUNT parameter required"));
212 - argp_help (&argp, stderr, ARGP_HELP_SEE,
213 - program_invocation_short_name);
214 - exit (EXIT_FAILURE);
219 - if (((instance = strtol (argv[remaining], &endp, 10)) == LONG_MAX
220 - && errno == ERANGE)
223 - error (1, 0, gettext ("invalid COUNT parameter %s"), argv[remaining]);
228 - if ((dont_replace_existing || allow_truncate_fname)
229 - && unlikely (operation != oper_extract))
230 - error (1, 0, gettext ("'%c' is only meaningful with the 'x' option"),
231 - dont_replace_existing ? 'C' : 'T');
233 - /* There must at least be one more parameter specifying the archive. */
234 - if (remaining == argc)
236 - error (0, 0, gettext ("archive name required"));
237 - argp_help (&argp, stderr, ARGP_HELP_SEE, program_invocation_short_name);
238 - exit (EXIT_FAILURE);
241 - const char *arfname = argv[remaining++];
249 - error (0, 0, gettext ("command option required"));
250 - argp_help (&argp, stderr, ARGP_HELP_STD_ERR,
251 - program_invocation_short_name);
257 - status = do_oper_extract (operation, arfname, argv, argc, -1);
261 - status = do_oper_extract (operation, arfname, argv, argc, instance);
265 - status = do_oper_delete (arfname, argv, argc, instance);
271 - status = do_oper_insert (operation, arfname, argv, argc, member);
275 - assert (! "should not happen");
284 -/* Print the version information. */
286 -print_version (FILE *stream, struct argp_state *state __attribute__ ((unused)))
288 - fprintf (stream, "ar (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
289 - fprintf (stream, gettext ("\
290 -Copyright (C) %s Red Hat, Inc.\n\
291 -This is free software; see the source for copying conditions. There is NO\n\
292 -warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
294 - fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
298 -/* Handle program arguments. */
300 -parse_opt (int key, char *arg __attribute__ ((unused)),
301 - struct argp_state *state __attribute__ ((unused)))
312 - if (operation != oper_none)
314 - error (0, 0, gettext ("More than one operation specified"));
315 - argp_help (&argp, stderr, ARGP_HELP_SEE,
316 - program_invocation_short_name);
317 - exit (EXIT_FAILURE);
323 - operation = oper_delete;
326 - operation = oper_move;
329 - operation = oper_print;
332 - operation = oper_qappend;
335 - operation = oper_replace;
338 - operation = oper_list;
341 - operation = oper_extract;
352 - ipos = ipos_before;
356 - suppress_create_msg = true;
360 - dont_replace_existing = true;
364 - instance_specifed = true;
368 - preserve_dates = true;
376 - force_symtab = true;
380 - allow_truncate_fname = true;
384 - update_newer = true;
392 - return ARGP_ERR_UNKNOWN;
399 -open_archive (const char *arfname, int flags, int mode, Elf **elf,
400 - struct stat *st, bool miss_allowed)
402 - int fd = open (arfname, flags, mode);
408 - error (EXIT_FAILURE, errno, gettext ("cannot open archive '%s'"),
414 - Elf_Cmd cmd = flags == O_RDONLY ? ELF_C_READ_MMAP : ELF_C_RDWR_MMAP;
416 - *elf = elf_begin (fd, cmd, NULL);
418 - error (EXIT_FAILURE, 0, gettext ("cannot open archive '%s': %s"),
419 - arfname, elf_errmsg (-1));
421 - if (flags == O_RDONLY && elf_kind (*elf) != ELF_K_AR)
422 - error (EXIT_FAILURE, 0, gettext ("%s: not an archive file"), arfname);
425 - if (st != NULL && fstat (fd, st) != 0)
426 - error (EXIT_FAILURE, errno, gettext ("cannot stat archive '%s'"),
434 -not_found (int argc, char *argv[argc], bool found[argc])
436 - for (int i = 0; i < argc; ++i)
438 - printf (gettext ("no entry %s in archive\n"), argv[i]);
443 -copy_content (Elf *elf, int newfd, off_t off, size_t n)
446 - char *rawfile = elf_rawfile (elf, &len);
448 - assert (off + n <= len);
450 - /* Tell the kernel we will read all the pages sequentially. */
451 - size_t ps = sysconf (_SC_PAGESIZE);
453 - posix_madvise (rawfile + (off & ~(ps - 1)), n, POSIX_MADV_SEQUENTIAL);
455 - return write_retry (newfd, rawfile + off, n) != (ssize_t) n;
460 -do_oper_extract (int oper, const char *arfname, char **argv, int argc,
464 - memset (found, '\0', sizeof (found));
466 - size_t name_max = 0;
467 - inline bool should_truncate_fname (void)
469 - if (errno == ENAMETOOLONG && allow_truncate_fname)
473 - long int len = pathconf (".", _PC_NAME_MAX);
477 - return name_max != 0;
482 - off_t index_off = -1;
483 - size_t index_size = 0;
484 - off_t cur_off = SARMAG;
488 - int fd = open_archive (arfname, O_RDONLY, 0, &elf, NULL, false);
490 - if (hcreate (2 * argc) == 0)
491 - error (EXIT_FAILURE, errno, gettext ("cannot create hash table"));
493 - for (int cnt = 0; cnt < argc; ++cnt)
495 - ENTRY entry = { .key = argv[cnt], .data = &argv[cnt] };
496 - if (hsearch (entry, ENTER) == NULL)
497 - error (EXIT_FAILURE, errno,
498 - gettext ("cannot insert into hash table"));
504 - if (fstat (fd, &st) != 0)
506 - error (0, errno, gettext ("cannot stat '%s'"), arfname);
513 - Elf_Cmd cmd = ELF_C_READ_MMAP;
515 - while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
517 - Elf_Arhdr *arhdr = elf_getarhdr (subelf);
519 - if (strcmp (arhdr->ar_name, "/") == 0)
521 - index_off = elf_getaroff (subelf);
522 - index_size = arhdr->ar_size;
525 - if (strcmp (arhdr->ar_name, "//") == 0)
530 - arlib_add_symbols (elf, arfname, arhdr->ar_name, cur_off);
531 - cur_off += (((arhdr->ar_size + 1) & ~((off_t) 1))
532 - + sizeof (struct ar_hdr));
535 - bool do_extract = argc <= 0;
539 - entry.key = arhdr->ar_name;
540 - ENTRY *res = hsearch (entry, FIND);
541 - if (res != NULL && (instance < 0 || instance-- == 0)
542 - && !found[(char **) res->data - argv])
543 - found[(char **) res->data - argv] = do_extract = true;
550 - if (oper == oper_print)
552 - printf ("\n<%s>\n\n", arhdr->ar_name);
554 - /* We have to flush now because now we use the descriptor
558 - else if (oper == oper_list)
561 - strftime (datestr, sizeof (datestr), "%b %e %H:%M %Y",
562 - localtime (&arhdr->ar_date));
564 - printf ("%c%c%c%c%c%c%c%c%c %u/%u %6ju %s %s\n",
565 - (arhdr->ar_mode & S_IRUSR) ? 'r' : '-',
566 - (arhdr->ar_mode & S_IWUSR) ? 'w' : '-',
567 - (arhdr->ar_mode & S_IXUSR)
568 - ? ((arhdr->ar_mode & S_ISUID) ? 's' : 'x')
569 - : ((arhdr->ar_mode & S_ISUID) ? 'S' : '-'),
570 - (arhdr->ar_mode & S_IRGRP) ? 'r' : '-',
571 - (arhdr->ar_mode & S_IWGRP) ? 'w' : '-',
572 - (arhdr->ar_mode & S_IXGRP)
573 - ? ((arhdr->ar_mode & S_ISGID) ? 's' : 'x')
574 - : ((arhdr->ar_mode & S_ISGID) ? 'S' : '-'),
575 - (arhdr->ar_mode & S_IROTH) ? 'r' : '-',
576 - (arhdr->ar_mode & S_IWOTH) ? 'w' : '-',
577 - (arhdr->ar_mode & S_IXOTH)
578 - ? ((arhdr->ar_mode & S_ISVTX) ? 't' : 'x')
579 - : ((arhdr->ar_mode & S_ISVTX) ? 'T' : '-'),
582 - (uintmax_t) arhdr->ar_size,
587 - printf ("x - %s\n", arhdr->ar_name);
590 - if (oper == oper_list)
593 - puts (arhdr->ar_name);
599 - char *data = elf_rawfile (subelf, &nleft);
602 - error (0, 0, gettext ("cannot read content of %s: %s"),
603 - arhdr->ar_name, elf_errmsg (-1));
609 - char tempfname[] = "XXXXXX";
610 - bool use_mkstemp = true;
612 - if (oper == oper_print)
613 - xfd = STDOUT_FILENO;
616 - xfd = mkstemp (tempfname);
617 - if (unlikely (xfd == -1))
619 - /* We cannot create a temporary file. Try to overwrite
620 - the file or create it if it does not exist. */
621 - int flags = O_WRONLY | O_CREAT;
622 - if (dont_replace_existing)
626 - xfd = open (arhdr->ar_name, flags, 0600);
627 - if (unlikely (xfd == -1))
629 - int printlen = INT_MAX;
631 - if (should_truncate_fname ())
633 - /* Try to truncate the name. First find out by how
635 - printlen = name_max;
636 - char truncfname[name_max + 1];
637 - *((char *) mempcpy (truncfname, arhdr->ar_name,
640 - xfd = open (truncfname, flags, 0600);
645 - error (0, errno, gettext ("cannot open %.*s"),
646 - (int) printlen, arhdr->ar_name);
652 - use_mkstemp = false;
657 - while ((n = TEMP_FAILURE_RETRY (write (xfd, data, nleft))) != -1)
665 - if (unlikely (n == -1))
667 - error (0, errno, gettext ("failed to write %s"), arhdr->ar_name);
669 - unlink (tempfname);
674 - if (oper != oper_print)
676 - /* Fix up the mode. */
677 - if (unlikely (fchmod (xfd, arhdr->ar_mode) != 0))
679 - error (0, errno, gettext ("cannot change mode of %s"),
684 - if (preserve_dates)
686 - struct timespec tv[2];
687 - tv[0].tv_sec = arhdr->ar_date;
689 - tv[1].tv_sec = arhdr->ar_date;
692 - if (unlikely (futimens (xfd, tv) != 0))
695 - gettext ("cannot change modification time of %s"),
701 - /* If we used a temporary file, move it do the right
707 - if (dont_replace_existing)
709 - r = link (tempfname, arhdr->ar_name);
710 - if (likely (r == 0))
711 - unlink (tempfname);
714 - r = rename (tempfname, arhdr->ar_name);
716 - if (unlikely (r) != 0)
718 - int printlen = INT_MAX;
720 - if (should_truncate_fname ())
722 - /* Try to truncate the name. First find out by how
724 - printlen = name_max;
725 - char truncfname[name_max + 1];
726 - *((char *) mempcpy (truncfname, arhdr->ar_name,
729 - if (dont_replace_existing)
731 - r = link (tempfname, truncfname);
732 - if (likely (r == 0))
733 - unlink (tempfname);
736 - r = rename (tempfname, truncfname);
741 - error (0, errno, gettext ("\
742 -cannot rename temporary file to %.*s"),
743 - printlen, arhdr->ar_name);
744 - unlink (tempfname);
755 - cmd = elf_next (subelf);
756 - if (elf_end (subelf) != 0)
757 - error (1, 0, "%s: %s", arfname, elf_errmsg (-1));
766 - if (symtab.symsnamelen != 0
767 - /* We have to rewrite the file also if it initially had an index
768 - but now does not need one anymore. */
769 - || (symtab.symsnamelen == 0 && index_size != 0))
771 - char tmpfname[strlen (arfname) + 7];
772 - strcpy (stpcpy (tmpfname, arfname), "XXXXXX");
773 - int newfd = mkstemp (tmpfname);
774 - if (unlikely (newfd == -1))
777 - error (0, errno, gettext ("cannot create new file"));
782 - /* Create the header. */
783 - if (unlikely (write_retry (newfd, ARMAG, SARMAG) != SARMAG))
785 - // XXX Use /prof/self/fd/%d ???
793 - /* Create the new file. There are three parts as far we are
794 - concerned: 1. original context before the index, 2. the
795 - new index, 3. everything after the new index. */
797 - if (index_off != -1)
798 - rest_off = (index_off + sizeof (struct ar_hdr)
799 - + ((index_size + 1) & ~1ul));
803 - if ((symtab.symsnamelen != 0
804 - && ((write_retry (newfd, symtab.symsoff,
806 - != (ssize_t) symtab.symsofflen)
807 - || (write_retry (newfd, symtab.symsname,
808 - symtab.symsnamelen)
809 - != (ssize_t) symtab.symsnamelen)))
810 - /* Even if the original file had content before the
811 - symbol table, we write it in the correct order. */
812 - || (index_off != SARMAG
813 - && copy_content (elf, newfd, SARMAG, index_off - SARMAG))
814 - || copy_content (elf, newfd, rest_off, st.st_size - rest_off)
815 - /* Set the mode of the new file to the same values the
816 - original file has. */
817 - || fchmod (newfd, st.st_mode & ALLPERMS) != 0
818 - /* Never complain about fchown failing. */
819 - || (({asm ("" :: "r" (fchown (newfd, st.st_uid,
821 - close (newfd) != 0)
822 - || (newfd = -1, rename (tmpfname, arfname) != 0))
832 - not_found (argc, argv, found);
843 - long int long_name_off;
844 - struct armem *next;
856 -write_member (struct armem *memb, off_t *startp, off_t *lenp, Elf *elf,
857 - off_t end_off, int newfd)
859 - struct ar_hdr arhdr;
860 - char tmpbuf[sizeof (arhdr.ar_name) + 1];
862 - bool changed_header = memb->long_name_off != -1;
863 - if (changed_header)
865 - /* In case of a long file name we assume the archive header
866 - changed and we write it here. */
867 - memcpy (&arhdr, elf_rawfile (elf, NULL) + *startp, sizeof (arhdr));
869 - snprintf (tmpbuf, sizeof (tmpbuf), "/%-*ld",
870 - (int) sizeof (arhdr.ar_name), memb->long_name_off);
871 - changed_header = memcmp (arhdr.ar_name, tmpbuf,
872 - sizeof (arhdr.ar_name)) != 0;
875 - /* If the files are adjacent in the old file extend the range. */
876 - if (*startp != -1 && !changed_header && *startp + *lenp == memb->old_off)
878 - /* Extend the current range. */
879 - *lenp += (memb->next != NULL
880 - ? memb->next->off : end_off) - memb->off;
884 - /* Write out the old range. */
885 - if (*startp != -1 && copy_content (elf, newfd, *startp, *lenp))
888 - *startp = memb->old_off;
889 - *lenp = (memb->next != NULL ? memb->next->off : end_off) - memb->off;
891 - if (changed_header)
893 - memcpy (arhdr.ar_name, tmpbuf, sizeof (arhdr.ar_name));
895 - if (unlikely (write_retry (newfd, &arhdr, sizeof (arhdr))
896 - != sizeof (arhdr)))
899 - *startp += sizeof (struct ar_hdr);
900 - assert ((size_t) *lenp >= sizeof (struct ar_hdr));
901 - *lenp -= sizeof (struct ar_hdr);
907 -/* Store the name in the long name table if necessary.
908 - Record its offset or -1 if we did not need to use the table. */
910 -remember_long_name (struct armem *mem, const char *name, size_t namelen)
912 - mem->long_name_off = (namelen > MAX_AR_NAME_LEN
913 - ? arlib_add_long_name (name, namelen)
918 -do_oper_delete (const char *arfname, char **argv, int argc,
921 - bool *found = alloca (sizeof (bool) * argc);
922 - memset (found, '\0', sizeof (bool) * argc);
924 - /* List of the files we keep. */
925 - struct armem *to_copy = NULL;
930 - int fd = open_archive (arfname, O_RDONLY, 0, &elf, &st, false);
932 - if (hcreate (2 * argc) == 0)
933 - error (EXIT_FAILURE, errno, gettext ("cannot create hash table"));
935 - for (int cnt = 0; cnt < argc; ++cnt)
937 - ENTRY entry = { .key = argv[cnt], .data = &argv[cnt] };
938 - if (hsearch (entry, ENTER) == NULL)
939 - error (EXIT_FAILURE, errno,
940 - gettext ("cannot insert into hash table"));
945 - off_t cur_off = SARMAG;
946 - Elf_Cmd cmd = ELF_C_READ_MMAP;
948 - while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
950 - Elf_Arhdr *arhdr = elf_getarhdr (subelf);
952 - /* Ignore the symbol table and the long file name table here. */
953 - if (strcmp (arhdr->ar_name, "/") == 0
954 - || strcmp (arhdr->ar_name, "//") == 0)
957 - bool do_delete = argc <= 0;
961 - entry.key = arhdr->ar_name;
962 - ENTRY *res = hsearch (entry, FIND);
963 - if (res != NULL && (instance < 0 || instance-- == 0)
964 - && !found[(char **) res->data - argv])
965 - found[(char **) res->data - argv] = do_delete = true;
971 - printf ("d - %s\n", arhdr->ar_name);
975 - struct armem *newp = alloca (sizeof (struct armem));
976 - newp->old_off = elf_getaroff (subelf);
977 - newp->off = cur_off;
979 - cur_off += (((arhdr->ar_size + 1) & ~((off_t) 1))
980 - + sizeof (struct ar_hdr));
982 - if (to_copy == NULL)
983 - to_copy = newp->next = newp;
986 - newp->next = to_copy->next;
987 - to_copy = to_copy->next = newp;
990 - /* If we recreate the symbol table read the file's symbol
992 - arlib_add_symbols (subelf, arfname, arhdr->ar_name, newp->off);
994 - /* Remember long file names. */
995 - remember_long_name (newp, arhdr->ar_name, strlen (arhdr->ar_name));
999 - cmd = elf_next (subelf);
1000 - if (elf_end (subelf) != 0)
1001 - error (1, 0, "%s: %s", arfname, elf_errmsg (-1));
1004 - arlib_finalize ();
1008 - /* Create a new, temporary file in the same directory as the
1010 - char tmpfname[strlen (arfname) + 7];
1011 - strcpy (stpcpy (tmpfname, arfname), "XXXXXX");
1012 - int newfd = mkstemp (tmpfname);
1013 - if (unlikely (newfd == -1))
1016 - /* Create the header. */
1017 - if (unlikely (write_retry (newfd, ARMAG, SARMAG) != SARMAG))
1019 - // XXX Use /prof/self/fd/%d ???
1021 - unlink (tmpfname);
1025 - error (0, errno, gettext ("cannot create new file"));
1030 - /* If the archive is empty that is all we have to do. */
1031 - if (likely (to_copy != NULL))
1033 - /* Write the symbol table or the long file name table or both. */
1034 - if (symtab.symsnamelen != 0
1035 - && ((write_retry (newfd, symtab.symsoff, symtab.symsofflen)
1036 - != (ssize_t) symtab.symsofflen)
1037 - || (write_retry (newfd, symtab.symsname, symtab.symsnamelen)
1038 - != (ssize_t) symtab.symsnamelen)))
1039 - goto nonew_unlink;
1041 - if (symtab.longnameslen > sizeof (struct ar_hdr)
1042 - && (write_retry (newfd, symtab.longnames, symtab.longnameslen)
1043 - != (ssize_t) symtab.longnameslen))
1044 - goto nonew_unlink;
1046 - /* NULL-terminate the list of files to copy. */
1047 - struct armem *last = to_copy;
1048 - to_copy = to_copy->next;
1049 - last->next = NULL;
1055 - if (write_member (to_copy, &start, &len, elf, cur_off, newfd) != 0)
1056 - goto nonew_unlink;
1057 - while ((to_copy = to_copy->next) != NULL);
1059 - /* Write the last part. */
1060 - if (copy_content (elf, newfd, start, len))
1061 - goto nonew_unlink;
1064 - /* Set the mode of the new file to the same values the original file
1066 - if (fchmod (newfd, st.st_mode & ALLPERMS) != 0
1067 - /* Never complain about fchown failing. */
1068 - || (({asm ("" :: "r" (fchown (newfd, st.st_uid, st.st_gid))); }),
1069 - close (newfd) != 0)
1070 - || (newfd = -1, rename (tmpfname, arfname) != 0))
1071 - goto nonew_unlink;
1082 - not_found (argc, argv, found);
1089 -no0print (bool ofmt, char *buf, int bufsize, long int val)
1091 - char tmpbuf[bufsize + 1];
1092 - snprintf (tmpbuf, sizeof (tmpbuf), ofmt ? "%-*lo" : "%-*ld", bufsize, val);
1093 - memcpy (buf, tmpbuf, bufsize);
1098 -do_oper_insert (int oper, const char *arfname, char **argv, int argc,
1099 - const char *member)
1104 - int fd = open_archive (arfname, O_RDONLY, 0, &elf, &st, oper != oper_move);
1106 - /* List of the files we keep. */
1107 - struct armem *all = NULL;
1108 - struct armem *after_memberelem = NULL;
1109 - struct armem **found = alloca (sizeof (*found) * argc);
1110 - memset (found, '\0', sizeof (*found) * argc);
1114 - /* Initialize early for no_old case. */
1115 - off_t cur_off = SARMAG;
1119 - if (!suppress_create_msg)
1120 - fprintf (stderr, "%s: creating %s\n",
1121 - program_invocation_short_name, arfname);
1126 - /* Store the names of all files from the command line in a hash
1127 - table so that we can match it. Note that when no file name is
1128 - given we are basically doing nothing except recreating the
1130 - if (oper != oper_qappend)
1132 - if (hcreate (2 * argc) == 0)
1133 - error (EXIT_FAILURE, errno, gettext ("cannot create hash table"));
1135 - for (int cnt = 0; cnt < argc; ++cnt)
1138 - entry.key = full_path ? argv[cnt] : basename (argv[cnt]);
1139 - entry.data = &argv[cnt];
1140 - if (hsearch (entry, ENTER) == NULL)
1141 - error (EXIT_FAILURE, errno,
1142 - gettext ("cannot insert into hash table"));
1146 - /* While iterating over the current content of the archive we must
1147 - determine a number of things: which archive members to keep,
1148 - which are replaced, and where to insert the new members. */
1149 - Elf_Cmd cmd = ELF_C_READ_MMAP;
1151 - while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
1153 - Elf_Arhdr *arhdr = elf_getarhdr (subelf);
1155 - /* Ignore the symbol table and the long file name table here. */
1156 - if (strcmp (arhdr->ar_name, "/") == 0
1157 - || strcmp (arhdr->ar_name, "//") == 0)
1160 - struct armem *newp = alloca (sizeof (struct armem));
1161 - newp->old_off = elf_getaroff (subelf);
1162 - newp->size = arhdr->ar_size;
1163 - newp->sec = arhdr->ar_date;
1166 - /* Remember long file names. */
1167 - remember_long_name (newp, arhdr->ar_name, strlen (arhdr->ar_name));
1169 - /* Check whether this is a file we are looking for. */
1170 - if (oper != oper_qappend)
1172 - /* Check whether this is the member used as the insert point. */
1173 - if (member != NULL && strcmp (arhdr->ar_name, member) == 0)
1175 - /* Note that all == NULL means insert at the beginning. */
1176 - if (ipos == ipos_before)
1177 - after_memberelem = all;
1179 - after_memberelem = newp;
1184 - entry.key = arhdr->ar_name;
1185 - ENTRY *res = hsearch (entry, FIND);
1186 - if (res != NULL && found[(char **) res->data - argv] == NULL)
1188 - found[(char **) res->data - argv] = newp;
1190 - /* If we insert before or after a certain element move
1191 - all files to a special list. */
1192 - if (unlikely (ipos != ipos_none || oper == oper_move))
1194 - if (after_memberelem == newp)
1195 - /* Since we remove this element even though we should
1196 - insert everything after it, we in fact insert
1197 - everything after the previous element. */
1198 - after_memberelem = all;
1206 - all = newp->next = newp;
1209 - newp->next = all->next;
1210 - all = all->next = newp;
1214 - cmd = elf_next (subelf);
1215 - if (elf_end (subelf) != 0)
1216 - error (EXIT_FAILURE, 0, "%s: %s", arfname, elf_errmsg (-1));
1219 - if (oper != oper_qappend)
1223 - if (member != NULL)
1224 - error (EXIT_FAILURE, 0, gettext ("position member %s not found"),
1227 - if (oper == oper_move)
1229 - /* Make sure all requested elements are found in the archive. */
1230 - for (int cnt = 0; cnt < argc; ++cnt)
1232 - if (found[cnt] == NULL)
1234 - fprintf (stderr, gettext ("%s: no entry %s in archive!\n"),
1235 - program_invocation_short_name, argv[cnt]);
1240 - printf ("m - %s\n", argv[cnt]);
1245 - /* Open all the new files, get their sizes and add all symbols. */
1246 - for (int cnt = 0; cnt < argc; ++cnt)
1248 - const char *bname = basename (argv[cnt]);
1249 - size_t bnamelen = strlen (bname);
1250 - if (found[cnt] == NULL)
1252 - found[cnt] = alloca (sizeof (struct armem));
1253 - found[cnt]->old_off = -1;
1255 - remember_long_name (found[cnt], bname, bnamelen);
1258 - struct stat newst;
1260 - int newfd = open (argv[cnt], O_RDONLY);
1263 - error (0, errno, gettext ("cannot open %s"), argv[cnt]);
1266 - else if (fstat (newfd, &newst) == -1)
1268 - error (0, errno, gettext ("cannot stat %s"), argv[cnt]);
1272 - else if (!S_ISREG (newst.st_mode))
1274 - error (0, errno, gettext ("%s is no regular file"), argv[cnt]);
1278 - else if (update_newer
1279 - && found[cnt]->old_off != -1l
1280 - && found[cnt]->sec > st.st_mtime)
1281 - /* Do nothing, the file in the archive is younger. */
1283 - else if ((newelf = elf_begin (newfd, ELF_C_READ_MMAP, NULL))
1287 - gettext ("cannot get ELF descriptor for %s: %s\n"),
1288 - argv[cnt], elf_errmsg (-1));
1294 - printf ("%c - %s\n",
1295 - found[cnt]->old_off == -1l ? 'a' : 'r', argv[cnt]);
1297 - found[cnt]->elf = newelf;
1298 - found[cnt]->sec = arlib_deterministic_output ? 0 : newst.st_mtime;
1299 - found[cnt]->uid = arlib_deterministic_output ? 0 : newst.st_uid;
1300 - found[cnt]->gid = arlib_deterministic_output ? 0 : newst.st_gid;
1301 - found[cnt]->mode = newst.st_mode;
1302 - found[cnt]->name = bname;
1304 - found[cnt]->mem = elf_rawfile (newelf, &found[cnt]->size);
1305 - if (found[cnt]->mem == NULL
1306 - || elf_cntl (newelf, ELF_C_FDDONE) != 0)
1307 - error (EXIT_FAILURE, 0, gettext ("cannot read %s: %s"),
1308 - argv[cnt], elf_errmsg (-1));
1312 - if (found[cnt]->old_off != -1l)
1313 - /* Remember long file names. */
1314 - remember_long_name (found[cnt], bname, bnamelen);
1332 - /* If we have no entry point so far add at the end. AFTER_MEMBERELEM
1333 - being NULL when adding before an entry means add at the beginning. */
1334 - if (ipos != ipos_before && after_memberelem == NULL)
1335 - after_memberelem = all;
1337 - /* Convert the circular list into a normal list first. */
1340 - struct armem *tmp = all;
1345 - struct armem *last_added = after_memberelem;
1346 - for (int cnt = 0; cnt < argc; ++cnt)
1347 - if (oper != oper_replace || found[cnt]->old_off == -1)
1349 - if (last_added == NULL)
1351 - found[cnt]->next = all;
1352 - last_added = all = found[cnt];
1356 - found[cnt]->next = last_added->next;
1357 - last_added = last_added->next = found[cnt];
1361 - /* Finally compute the offset and add the symbols for the files
1362 - after the insert point. */
1363 - if (likely (all != NULL))
1364 - for (struct armem *memp = all; memp != NULL; memp = memp->next)
1366 - memp->off = cur_off;
1368 - if (memp->mem == NULL)
1371 - /* Fake initializing arhdr and subelf to keep gcc calm. */
1372 - asm ("" : "=m" (arhdr), "=m" (subelf));
1373 - if (elf_rand (elf, memp->old_off) == 0
1374 - || (subelf = elf_begin (fd, ELF_C_READ_MMAP, elf)) == NULL
1375 - || (arhdr = elf_getarhdr (subelf)) == NULL)
1376 - /* This should never happen since we already looked at the
1377 - archive content. But who knows... */
1378 - error (EXIT_FAILURE, 0, "%s: %s", arfname, elf_errmsg (-1));
1380 - arlib_add_symbols (subelf, arfname, arhdr->ar_name, cur_off);
1385 - arlib_add_symbols (memp->elf, arfname, memp->name, cur_off);
1387 - cur_off += (((memp->size + 1) & ~((off_t) 1))
1388 - + sizeof (struct ar_hdr));
1391 - /* Now we have all the information for the symbol table and long
1392 - file name table. Construct the final layout. */
1393 - arlib_finalize ();
1395 - /* Create a new, temporary file in the same directory as the
1397 - char tmpfname[strlen (arfname) + 7];
1398 - strcpy (stpcpy (tmpfname, arfname), "XXXXXX");
1401 - newfd = mkstemp (tmpfname);
1404 - newfd = open (arfname, O_RDWR | O_CREAT | O_EXCL, DEFFILEMODE);
1405 - if (newfd == -1 && errno == EEXIST)
1406 - /* Bah, first the file did not exist, now it does. Restart. */
1407 - return do_oper_insert (oper, arfname, argv, argc, member);
1409 - if (unlikely (newfd == -1))
1412 - /* Create the header. */
1413 - if (unlikely (write_retry (newfd, ARMAG, SARMAG) != SARMAG))
1418 - // XXX Use /prof/self/fd/%d ???
1419 - unlink (tmpfname);
1424 - error (0, errno, gettext ("cannot create new file"));
1429 - /* If the new archive is not empty we actually have something to do. */
1430 - if (likely (all != NULL))
1432 - /* Write the symbol table or the long file name table or both. */
1433 - if (symtab.symsnamelen != 0
1434 - && ((write_retry (newfd, symtab.symsoff, symtab.symsofflen)
1435 - != (ssize_t) symtab.symsofflen)
1436 - || (write_retry (newfd, symtab.symsname, symtab.symsnamelen)
1437 - != (ssize_t) symtab.symsnamelen)))
1438 - goto nonew_unlink;
1440 - if (symtab.longnameslen > sizeof (struct ar_hdr)
1441 - && (write_retry (newfd, symtab.longnames, symtab.longnameslen)
1442 - != (ssize_t) symtab.longnameslen))
1443 - goto nonew_unlink;
1448 - while (all != NULL)
1450 - if (all->mem != NULL)
1452 - /* This is a new file. If there is anything from the
1453 - archive left to be written do it now. */
1454 - if (start != -1 && copy_content (elf, newfd, start, len))
1455 - goto nonew_unlink;
1460 - /* Create the header. */
1461 - struct ar_hdr arhdr;
1462 - char tmpbuf[sizeof (arhdr.ar_name) + 1];
1463 - if (all->long_name_off == -1)
1465 - size_t namelen = strlen (all->name);
1466 - char *p = mempcpy (arhdr.ar_name, all->name, namelen);
1468 - memset (p, ' ', sizeof (arhdr.ar_name) - namelen - 1);
1472 - snprintf (tmpbuf, sizeof (arhdr.ar_name) + 1, "/%-*ld",
1473 - (int) sizeof (arhdr.ar_name), all->long_name_off);
1474 - memcpy (arhdr.ar_name, tmpbuf, sizeof (arhdr.ar_name));
1477 - no0print (false, arhdr.ar_date, sizeof (arhdr.ar_date),
1479 - no0print (false, arhdr.ar_uid, sizeof (arhdr.ar_uid), all->uid);
1480 - no0print (false, arhdr.ar_gid, sizeof (arhdr.ar_gid), all->gid);
1481 - no0print (true, arhdr.ar_mode, sizeof (arhdr.ar_mode),
1483 - no0print (false, arhdr.ar_size, sizeof (arhdr.ar_size),
1485 - memcpy (arhdr.ar_fmag, ARFMAG, sizeof (arhdr.ar_fmag));
1487 - if (unlikely (write_retry (newfd, &arhdr, sizeof (arhdr))
1488 - != sizeof (arhdr)))
1489 - goto nonew_unlink;
1491 - /* Now the file itself. */
1492 - if (unlikely (write_retry (newfd, all->mem, all->size)
1493 - != (off_t) all->size))
1494 - goto nonew_unlink;
1496 - /* Pad the file if its size is odd. */
1497 - if ((all->size & 1) != 0)
1498 - if (unlikely (write_retry (newfd, "\n", 1) != 1))
1499 - goto nonew_unlink;
1503 - /* This is a member from the archive. */
1504 - if (write_member (all, &start, &len, elf, cur_off, newfd)
1506 - goto nonew_unlink;
1512 - /* Write the last part. */
1513 - if (start != -1 && copy_content (elf, newfd, start, len))
1514 - goto nonew_unlink;
1517 - /* Set the mode of the new file to the same values the original file
1520 - && (fchmod (newfd, st.st_mode & ALLPERMS) != 0
1521 - /* Never complain about fchown failing. */
1522 - || (({asm ("" :: "r" (fchown (newfd, st.st_uid, st.st_gid))); }),
1523 - close (newfd) != 0)
1524 - || (newfd = -1, rename (tmpfname, arfname) != 0)))
1525 - goto nonew_unlink;
1540 -#include "debugpred.h"
1541 +int main() { return 0; }