1 diff -u -r ../elfutils-0.170/src/ar.c ./src/ar.c
2 --- ../elfutils-0.170/src/ar.c 2017-06-19 16:45:16.000000000 +0200
3 +++ ./src/ar.c 2017-12-10 01:12:10.164427616 +0100
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.
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>
48 -#include <printversion.h>
53 -/* Name and version of program. */
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 -/* Handle program arguments. */
286 -parse_opt (int key, char *arg __attribute__ ((unused)),
287 - struct argp_state *state __attribute__ ((unused)))
298 - if (operation != oper_none)
300 - error (0, 0, gettext ("More than one operation specified"));
301 - argp_help (&argp, stderr, ARGP_HELP_SEE,
302 - program_invocation_short_name);
303 - exit (EXIT_FAILURE);
309 - operation = oper_delete;
312 - operation = oper_move;
315 - operation = oper_print;
318 - operation = oper_qappend;
321 - operation = oper_replace;
324 - operation = oper_list;
327 - operation = oper_extract;
338 - ipos = ipos_before;
342 - suppress_create_msg = true;
346 - dont_replace_existing = true;
350 - instance_specifed = true;
354 - preserve_dates = true;
362 - force_symtab = true;
366 - allow_truncate_fname = true;
370 - update_newer = true;
378 - return ARGP_ERR_UNKNOWN;
385 -open_archive (const char *arfname, int flags, int mode, Elf **elf,
386 - struct stat *st, bool miss_allowed)
388 - int fd = open (arfname, flags, mode);
394 - error (EXIT_FAILURE, errno, gettext ("cannot open archive '%s'"),
400 - Elf_Cmd cmd = flags == O_RDONLY ? ELF_C_READ_MMAP : ELF_C_RDWR_MMAP;
402 - *elf = elf_begin (fd, cmd, NULL);
404 - error (EXIT_FAILURE, 0, gettext ("cannot open archive '%s': %s"),
405 - arfname, elf_errmsg (-1));
407 - if (flags == O_RDONLY && elf_kind (*elf) != ELF_K_AR)
408 - error (EXIT_FAILURE, 0, gettext ("%s: not an archive file"), arfname);
411 - if (st != NULL && fstat (fd, st) != 0)
412 - error (EXIT_FAILURE, errno, gettext ("cannot stat archive '%s'"),
420 -not_found (int argc, char *argv[argc], bool found[argc])
422 - for (int i = 0; i < argc; ++i)
424 - printf (gettext ("no entry %s in archive\n"), argv[i]);
429 -copy_content (Elf *elf, int newfd, off_t off, size_t n)
432 - char *rawfile = elf_rawfile (elf, &len);
434 - assert (off + n <= len);
436 - /* Tell the kernel we will read all the pages sequentially. */
437 - size_t ps = sysconf (_SC_PAGESIZE);
439 - posix_madvise (rawfile + (off & ~(ps - 1)), n, POSIX_MADV_SEQUENTIAL);
441 - return write_retry (newfd, rawfile + off, n) != (ssize_t) n;
446 -do_oper_extract (int oper, const char *arfname, char **argv, int argc,
450 - memset (found, '\0', sizeof (found));
452 - size_t name_max = 0;
453 - inline bool should_truncate_fname (void)
455 - if (errno == ENAMETOOLONG && allow_truncate_fname)
459 - long int len = pathconf (".", _PC_NAME_MAX);
463 - return name_max != 0;
468 - off_t index_off = -1;
469 - size_t index_size = 0;
470 - off_t cur_off = SARMAG;
474 - int fd = open_archive (arfname, O_RDONLY, 0, &elf, NULL, false);
476 - if (hcreate (2 * argc) == 0)
477 - error (EXIT_FAILURE, errno, gettext ("cannot create hash table"));
479 - for (int cnt = 0; cnt < argc; ++cnt)
481 - ENTRY entry = { .key = argv[cnt], .data = &argv[cnt] };
482 - if (hsearch (entry, ENTER) == NULL)
483 - error (EXIT_FAILURE, errno,
484 - gettext ("cannot insert into hash table"));
490 - if (fstat (fd, &st) != 0)
492 - error (0, errno, gettext ("cannot stat '%s'"), arfname);
499 - Elf_Cmd cmd = ELF_C_READ_MMAP;
501 - while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
503 - Elf_Arhdr *arhdr = elf_getarhdr (subelf);
505 - if (strcmp (arhdr->ar_name, "/") == 0)
507 - index_off = elf_getaroff (subelf);
508 - index_size = arhdr->ar_size;
511 - if (strcmp (arhdr->ar_name, "//") == 0)
516 - arlib_add_symbols (elf, arfname, arhdr->ar_name, cur_off);
517 - cur_off += (((arhdr->ar_size + 1) & ~((off_t) 1))
518 - + sizeof (struct ar_hdr));
521 - bool do_extract = argc <= 0;
525 - entry.key = arhdr->ar_name;
526 - ENTRY *res = hsearch (entry, FIND);
527 - if (res != NULL && (instance < 0 || instance-- == 0)
528 - && !found[(char **) res->data - argv])
529 - found[(char **) res->data - argv] = do_extract = true;
536 - if (oper == oper_print)
538 - printf ("\n<%s>\n\n", arhdr->ar_name);
540 - /* We have to flush now because now we use the descriptor
544 - else if (oper == oper_list)
547 - strftime (datestr, sizeof (datestr), "%b %e %H:%M %Y",
548 - localtime (&arhdr->ar_date));
550 - printf ("%c%c%c%c%c%c%c%c%c %u/%u %6ju %s %s\n",
551 - (arhdr->ar_mode & S_IRUSR) ? 'r' : '-',
552 - (arhdr->ar_mode & S_IWUSR) ? 'w' : '-',
553 - (arhdr->ar_mode & S_IXUSR)
554 - ? ((arhdr->ar_mode & S_ISUID) ? 's' : 'x')
555 - : ((arhdr->ar_mode & S_ISUID) ? 'S' : '-'),
556 - (arhdr->ar_mode & S_IRGRP) ? 'r' : '-',
557 - (arhdr->ar_mode & S_IWGRP) ? 'w' : '-',
558 - (arhdr->ar_mode & S_IXGRP)
559 - ? ((arhdr->ar_mode & S_ISGID) ? 's' : 'x')
560 - : ((arhdr->ar_mode & S_ISGID) ? 'S' : '-'),
561 - (arhdr->ar_mode & S_IROTH) ? 'r' : '-',
562 - (arhdr->ar_mode & S_IWOTH) ? 'w' : '-',
563 - (arhdr->ar_mode & S_IXOTH)
564 - ? ((arhdr->ar_mode & S_ISVTX) ? 't' : 'x')
565 - : ((arhdr->ar_mode & S_ISVTX) ? 'T' : '-'),
568 - (uintmax_t) arhdr->ar_size,
573 - printf ("x - %s\n", arhdr->ar_name);
576 - if (oper == oper_list)
579 - puts (arhdr->ar_name);
585 - char *data = elf_rawfile (subelf, &nleft);
588 - error (0, 0, gettext ("cannot read content of %s: %s"),
589 - arhdr->ar_name, elf_errmsg (-1));
595 - char tempfname[] = "XXXXXX";
596 - bool use_mkstemp = true;
598 - if (oper == oper_print)
599 - xfd = STDOUT_FILENO;
602 - xfd = mkstemp (tempfname);
603 - if (unlikely (xfd == -1))
605 - /* We cannot create a temporary file. Try to overwrite
606 - the file or create it if it does not exist. */
607 - int flags = O_WRONLY | O_CREAT;
608 - if (dont_replace_existing)
612 - xfd = open (arhdr->ar_name, flags, 0600);
613 - if (unlikely (xfd == -1))
615 - int printlen = INT_MAX;
617 - if (should_truncate_fname ())
619 - /* Try to truncate the name. First find out by how
621 - printlen = name_max;
622 - char truncfname[name_max + 1];
623 - *((char *) mempcpy (truncfname, arhdr->ar_name,
626 - xfd = open (truncfname, flags, 0600);
631 - error (0, errno, gettext ("cannot open %.*s"),
632 - (int) printlen, arhdr->ar_name);
638 - use_mkstemp = false;
643 - while ((n = TEMP_FAILURE_RETRY (write (xfd, data, nleft))) != -1)
651 - if (unlikely (n == -1))
653 - error (0, errno, gettext ("failed to write %s"), arhdr->ar_name);
655 - unlink (tempfname);
660 - if (oper != oper_print)
662 - /* Fix up the mode. */
663 - if (unlikely (fchmod (xfd, arhdr->ar_mode) != 0))
665 - error (0, errno, gettext ("cannot change mode of %s"),
670 - if (preserve_dates)
672 - struct timespec tv[2];
673 - tv[0].tv_sec = arhdr->ar_date;
675 - tv[1].tv_sec = arhdr->ar_date;
678 - if (unlikely (futimens (xfd, tv) != 0))
681 - gettext ("cannot change modification time of %s"),
687 - /* If we used a temporary file, move it do the right
693 - if (dont_replace_existing)
695 - r = link (tempfname, arhdr->ar_name);
696 - if (likely (r == 0))
697 - unlink (tempfname);
700 - r = rename (tempfname, arhdr->ar_name);
702 - if (unlikely (r) != 0)
704 - int printlen = INT_MAX;
706 - if (should_truncate_fname ())
708 - /* Try to truncate the name. First find out by how
710 - printlen = name_max;
711 - char truncfname[name_max + 1];
712 - *((char *) mempcpy (truncfname, arhdr->ar_name,
715 - if (dont_replace_existing)
717 - r = link (tempfname, truncfname);
718 - if (likely (r == 0))
719 - unlink (tempfname);
722 - r = rename (tempfname, truncfname);
727 - error (0, errno, gettext ("\
728 -cannot rename temporary file to %.*s"),
729 - printlen, arhdr->ar_name);
730 - unlink (tempfname);
741 - cmd = elf_next (subelf);
742 - if (elf_end (subelf) != 0)
743 - error (1, 0, "%s: %s", arfname, elf_errmsg (-1));
752 - if (symtab.symsnamelen != 0
753 - /* We have to rewrite the file also if it initially had an index
754 - but now does not need one anymore. */
755 - || (symtab.symsnamelen == 0 && index_size != 0))
757 - char tmpfname[strlen (arfname) + 7];
758 - strcpy (stpcpy (tmpfname, arfname), "XXXXXX");
759 - int newfd = mkstemp (tmpfname);
760 - if (unlikely (newfd == -1))
763 - error (0, errno, gettext ("cannot create new file"));
768 - /* Create the header. */
769 - if (unlikely (write_retry (newfd, ARMAG, SARMAG) != SARMAG))
771 - // XXX Use /prof/self/fd/%d ???
779 - /* Create the new file. There are three parts as far we are
780 - concerned: 1. original context before the index, 2. the
781 - new index, 3. everything after the new index. */
783 - if (index_off != -1)
784 - rest_off = (index_off + sizeof (struct ar_hdr)
785 - + ((index_size + 1) & ~1ul));
789 - if ((symtab.symsnamelen != 0
790 - && ((write_retry (newfd, symtab.symsoff,
792 - != (ssize_t) symtab.symsofflen)
793 - || (write_retry (newfd, symtab.symsname,
794 - symtab.symsnamelen)
795 - != (ssize_t) symtab.symsnamelen)))
796 - /* Even if the original file had content before the
797 - symbol table, we write it in the correct order. */
798 - || (index_off != SARMAG
799 - && copy_content (elf, newfd, SARMAG, index_off - SARMAG))
800 - || copy_content (elf, newfd, rest_off, st.st_size - rest_off)
801 - /* Set the mode of the new file to the same values the
802 - original file has. */
803 - || fchmod (newfd, st.st_mode & ALLPERMS) != 0
804 - /* Never complain about fchown failing. */
805 - || (({asm ("" :: "r" (fchown (newfd, st.st_uid,
807 - close (newfd) != 0)
808 - || (newfd = -1, rename (tmpfname, arfname) != 0))
818 - not_found (argc, argv, found);
829 - long int long_name_off;
830 - struct armem *next;
842 -write_member (struct armem *memb, off_t *startp, off_t *lenp, Elf *elf,
843 - off_t end_off, int newfd)
845 - struct ar_hdr arhdr;
846 - /* The ar_name is not actually zero teminated, but we need that for
847 - snprintf. Also if the name is too long, then the string starts
848 - with '/' plus an index off number (decimal). */
849 - char tmpbuf[sizeof (arhdr.ar_name) + 2];
851 - bool changed_header = memb->long_name_off != -1;
852 - if (changed_header)
854 - /* In case of a long file name we assume the archive header
855 - changed and we write it here. */
856 - memcpy (&arhdr, elf_rawfile (elf, NULL) + *startp, sizeof (arhdr));
858 - snprintf (tmpbuf, sizeof (tmpbuf), "/%-*ld",
859 - (int) sizeof (arhdr.ar_name), memb->long_name_off);
860 - changed_header = memcmp (arhdr.ar_name, tmpbuf,
861 - sizeof (arhdr.ar_name)) != 0;
864 - /* If the files are adjacent in the old file extend the range. */
865 - if (*startp != -1 && !changed_header && *startp + *lenp == memb->old_off)
867 - /* Extend the current range. */
868 - *lenp += (memb->next != NULL
869 - ? memb->next->off : end_off) - memb->off;
873 - /* Write out the old range. */
874 - if (*startp != -1 && copy_content (elf, newfd, *startp, *lenp))
877 - *startp = memb->old_off;
878 - *lenp = (memb->next != NULL ? memb->next->off : end_off) - memb->off;
880 - if (changed_header)
882 - memcpy (arhdr.ar_name, tmpbuf, sizeof (arhdr.ar_name));
884 - if (unlikely (write_retry (newfd, &arhdr, sizeof (arhdr))
885 - != sizeof (arhdr)))
888 - *startp += sizeof (struct ar_hdr);
889 - assert ((size_t) *lenp >= sizeof (struct ar_hdr));
890 - *lenp -= sizeof (struct ar_hdr);
896 -/* Store the name in the long name table if necessary.
897 - Record its offset or -1 if we did not need to use the table. */
899 -remember_long_name (struct armem *mem, const char *name, size_t namelen)
901 - mem->long_name_off = (namelen > MAX_AR_NAME_LEN
902 - ? arlib_add_long_name (name, namelen)
907 -do_oper_delete (const char *arfname, char **argv, int argc,
910 - bool *found = alloca (sizeof (bool) * argc);
911 - memset (found, '\0', sizeof (bool) * argc);
913 - /* List of the files we keep. */
914 - struct armem *to_copy = NULL;
919 - int fd = open_archive (arfname, O_RDONLY, 0, &elf, &st, false);
921 - if (hcreate (2 * argc) == 0)
922 - error (EXIT_FAILURE, errno, gettext ("cannot create hash table"));
924 - for (int cnt = 0; cnt < argc; ++cnt)
926 - ENTRY entry = { .key = argv[cnt], .data = &argv[cnt] };
927 - if (hsearch (entry, ENTER) == NULL)
928 - error (EXIT_FAILURE, errno,
929 - gettext ("cannot insert into hash table"));
934 - off_t cur_off = SARMAG;
935 - Elf_Cmd cmd = ELF_C_READ_MMAP;
937 - while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
939 - Elf_Arhdr *arhdr = elf_getarhdr (subelf);
941 - /* Ignore the symbol table and the long file name table here. */
942 - if (strcmp (arhdr->ar_name, "/") == 0
943 - || strcmp (arhdr->ar_name, "//") == 0)
946 - bool do_delete = argc <= 0;
950 - entry.key = arhdr->ar_name;
951 - ENTRY *res = hsearch (entry, FIND);
952 - if (res != NULL && (instance < 0 || instance-- == 0)
953 - && !found[(char **) res->data - argv])
954 - found[(char **) res->data - argv] = do_delete = true;
960 - printf ("d - %s\n", arhdr->ar_name);
964 - struct armem *newp = alloca (sizeof (struct armem));
965 - newp->old_off = elf_getaroff (subelf);
966 - newp->off = cur_off;
968 - cur_off += (((arhdr->ar_size + 1) & ~((off_t) 1))
969 - + sizeof (struct ar_hdr));
971 - if (to_copy == NULL)
972 - to_copy = newp->next = newp;
975 - newp->next = to_copy->next;
976 - to_copy = to_copy->next = newp;
979 - /* If we recreate the symbol table read the file's symbol
981 - arlib_add_symbols (subelf, arfname, arhdr->ar_name, newp->off);
983 - /* Remember long file names. */
984 - remember_long_name (newp, arhdr->ar_name, strlen (arhdr->ar_name));
988 - cmd = elf_next (subelf);
989 - if (elf_end (subelf) != 0)
990 - error (1, 0, "%s: %s", arfname, elf_errmsg (-1));
997 - /* Create a new, temporary file in the same directory as the
999 - char tmpfname[strlen (arfname) + 7];
1000 - strcpy (stpcpy (tmpfname, arfname), "XXXXXX");
1001 - int newfd = mkstemp (tmpfname);
1002 - if (unlikely (newfd == -1))
1005 - /* Create the header. */
1006 - if (unlikely (write_retry (newfd, ARMAG, SARMAG) != SARMAG))
1008 - // XXX Use /prof/self/fd/%d ???
1010 - unlink (tmpfname);
1014 - error (0, errno, gettext ("cannot create new file"));
1019 - /* If the archive is empty that is all we have to do. */
1020 - if (likely (to_copy != NULL))
1022 - /* Write the symbol table or the long file name table or both. */
1023 - if (symtab.symsnamelen != 0
1024 - && ((write_retry (newfd, symtab.symsoff, symtab.symsofflen)
1025 - != (ssize_t) symtab.symsofflen)
1026 - || (write_retry (newfd, symtab.symsname, symtab.symsnamelen)
1027 - != (ssize_t) symtab.symsnamelen)))
1028 - goto nonew_unlink;
1030 - if (symtab.longnameslen > sizeof (struct ar_hdr)
1031 - && (write_retry (newfd, symtab.longnames, symtab.longnameslen)
1032 - != (ssize_t) symtab.longnameslen))
1033 - goto nonew_unlink;
1035 - /* NULL-terminate the list of files to copy. */
1036 - struct armem *last = to_copy;
1037 - to_copy = to_copy->next;
1038 - last->next = NULL;
1044 - if (write_member (to_copy, &start, &len, elf, cur_off, newfd) != 0)
1045 - goto nonew_unlink;
1046 - while ((to_copy = to_copy->next) != NULL);
1048 - /* Write the last part. */
1049 - if (copy_content (elf, newfd, start, len))
1050 - goto nonew_unlink;
1053 - /* Set the mode of the new file to the same values the original file
1055 - if (fchmod (newfd, st.st_mode & ALLPERMS) != 0
1056 - /* Never complain about fchown failing. */
1057 - || (({asm ("" :: "r" (fchown (newfd, st.st_uid, st.st_gid))); }),
1058 - close (newfd) != 0)
1059 - || (newfd = -1, rename (tmpfname, arfname) != 0))
1060 - goto nonew_unlink;
1071 - not_found (argc, argv, found);
1078 -no0print (bool ofmt, char *buf, int bufsize, long int val)
1080 - char tmpbuf[bufsize + 1];
1081 - snprintf (tmpbuf, sizeof (tmpbuf), ofmt ? "%-*lo" : "%-*ld", bufsize, val);
1082 - memcpy (buf, tmpbuf, bufsize);
1087 -do_oper_insert (int oper, const char *arfname, char **argv, int argc,
1088 - const char *member)
1093 - int fd = open_archive (arfname, O_RDONLY, 0, &elf, &st, oper != oper_move);
1095 - /* List of the files we keep. */
1096 - struct armem *all = NULL;
1097 - struct armem *after_memberelem = NULL;
1098 - struct armem **found = alloca (sizeof (*found) * argc);
1099 - memset (found, '\0', sizeof (*found) * argc);
1103 - /* Initialize early for no_old case. */
1104 - off_t cur_off = SARMAG;
1108 - if (!suppress_create_msg)
1109 - fprintf (stderr, "%s: creating %s\n",
1110 - program_invocation_short_name, arfname);
1115 - /* Store the names of all files from the command line in a hash
1116 - table so that we can match it. Note that when no file name is
1117 - given we are basically doing nothing except recreating the
1119 - if (oper != oper_qappend)
1121 - if (hcreate (2 * argc) == 0)
1122 - error (EXIT_FAILURE, errno, gettext ("cannot create hash table"));
1124 - for (int cnt = 0; cnt < argc; ++cnt)
1127 - entry.key = full_path ? argv[cnt] : basename (argv[cnt]);
1128 - entry.data = &argv[cnt];
1129 - if (hsearch (entry, ENTER) == NULL)
1130 - error (EXIT_FAILURE, errno,
1131 - gettext ("cannot insert into hash table"));
1135 - /* While iterating over the current content of the archive we must
1136 - determine a number of things: which archive members to keep,
1137 - which are replaced, and where to insert the new members. */
1138 - Elf_Cmd cmd = ELF_C_READ_MMAP;
1140 - while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
1142 - Elf_Arhdr *arhdr = elf_getarhdr (subelf);
1144 - /* Ignore the symbol table and the long file name table here. */
1145 - if (strcmp (arhdr->ar_name, "/") == 0
1146 - || strcmp (arhdr->ar_name, "//") == 0)
1149 - struct armem *newp = alloca (sizeof (struct armem));
1150 - newp->old_off = elf_getaroff (subelf);
1151 - newp->size = arhdr->ar_size;
1152 - newp->sec = arhdr->ar_date;
1155 - /* Remember long file names. */
1156 - remember_long_name (newp, arhdr->ar_name, strlen (arhdr->ar_name));
1158 - /* Check whether this is a file we are looking for. */
1159 - if (oper != oper_qappend)
1161 - /* Check whether this is the member used as the insert point. */
1162 - if (member != NULL && strcmp (arhdr->ar_name, member) == 0)
1164 - /* Note that all == NULL means insert at the beginning. */
1165 - if (ipos == ipos_before)
1166 - after_memberelem = all;
1168 - after_memberelem = newp;
1173 - entry.key = arhdr->ar_name;
1174 - ENTRY *res = hsearch (entry, FIND);
1175 - if (res != NULL && found[(char **) res->data - argv] == NULL)
1177 - found[(char **) res->data - argv] = newp;
1179 - /* If we insert before or after a certain element move
1180 - all files to a special list. */
1181 - if (unlikely (ipos != ipos_none || oper == oper_move))
1183 - if (after_memberelem == newp)
1184 - /* Since we remove this element even though we should
1185 - insert everything after it, we in fact insert
1186 - everything after the previous element. */
1187 - after_memberelem = all;
1195 - all = newp->next = newp;
1198 - newp->next = all->next;
1199 - all = all->next = newp;
1203 - cmd = elf_next (subelf);
1204 - if (elf_end (subelf) != 0)
1205 - error (EXIT_FAILURE, 0, "%s: %s", arfname, elf_errmsg (-1));
1208 - if (oper != oper_qappend)
1212 - if (member != NULL)
1213 - error (EXIT_FAILURE, 0, gettext ("position member %s not found"),
1216 - if (oper == oper_move)
1218 - /* Make sure all requested elements are found in the archive. */
1219 - for (int cnt = 0; cnt < argc; ++cnt)
1221 - if (found[cnt] == NULL)
1223 - fprintf (stderr, gettext ("%s: no entry %s in archive!\n"),
1224 - program_invocation_short_name, argv[cnt]);
1229 - printf ("m - %s\n", argv[cnt]);
1234 - /* Open all the new files, get their sizes and add all symbols. */
1235 - for (int cnt = 0; cnt < argc; ++cnt)
1237 - const char *bname = basename (argv[cnt]);
1238 - size_t bnamelen = strlen (bname);
1239 - if (found[cnt] == NULL)
1241 - found[cnt] = alloca (sizeof (struct armem));
1242 - found[cnt]->old_off = -1;
1244 - remember_long_name (found[cnt], bname, bnamelen);
1247 - struct stat newst;
1249 - int newfd = open (argv[cnt], O_RDONLY);
1252 - error (0, errno, gettext ("cannot open %s"), argv[cnt]);
1255 - else if (fstat (newfd, &newst) == -1)
1257 - error (0, errno, gettext ("cannot stat %s"), argv[cnt]);
1261 - else if (!S_ISREG (newst.st_mode))
1263 - error (0, errno, gettext ("%s is no regular file"), argv[cnt]);
1267 - else if (update_newer
1268 - && found[cnt]->old_off != -1l
1269 - && found[cnt]->sec > st.st_mtime)
1270 - /* Do nothing, the file in the archive is younger. */
1272 - else if ((newelf = elf_begin (newfd, ELF_C_READ_MMAP, NULL))
1276 - gettext ("cannot get ELF descriptor for %s: %s\n"),
1277 - argv[cnt], elf_errmsg (-1));
1283 - printf ("%c - %s\n",
1284 - found[cnt]->old_off == -1l ? 'a' : 'r', argv[cnt]);
1286 - found[cnt]->elf = newelf;
1287 - found[cnt]->sec = arlib_deterministic_output ? 0 : newst.st_mtime;
1288 - found[cnt]->uid = arlib_deterministic_output ? 0 : newst.st_uid;
1289 - found[cnt]->gid = arlib_deterministic_output ? 0 : newst.st_gid;
1290 - found[cnt]->mode = newst.st_mode;
1291 - found[cnt]->name = bname;
1293 - found[cnt]->mem = elf_rawfile (newelf, &found[cnt]->size);
1294 - if (found[cnt]->mem == NULL
1295 - || elf_cntl (newelf, ELF_C_FDDONE) != 0)
1296 - error (EXIT_FAILURE, 0, gettext ("cannot read %s: %s"),
1297 - argv[cnt], elf_errmsg (-1));
1301 - if (found[cnt]->old_off != -1l)
1302 - /* Remember long file names. */
1303 - remember_long_name (found[cnt], bname, bnamelen);
1321 - /* If we have no entry point so far add at the end. AFTER_MEMBERELEM
1322 - being NULL when adding before an entry means add at the beginning. */
1323 - if (ipos != ipos_before && after_memberelem == NULL)
1324 - after_memberelem = all;
1326 - /* Convert the circular list into a normal list first. */
1329 - struct armem *tmp = all;
1334 - struct armem *last_added = after_memberelem;
1335 - for (int cnt = 0; cnt < argc; ++cnt)
1336 - if (oper != oper_replace || found[cnt]->old_off == -1)
1338 - if (last_added == NULL)
1340 - found[cnt]->next = all;
1341 - last_added = all = found[cnt];
1345 - found[cnt]->next = last_added->next;
1346 - last_added = last_added->next = found[cnt];
1350 - /* Finally compute the offset and add the symbols for the files
1351 - after the insert point. */
1352 - if (likely (all != NULL))
1353 - for (struct armem *memp = all; memp != NULL; memp = memp->next)
1355 - memp->off = cur_off;
1357 - if (memp->mem == NULL)
1360 - /* Fake initializing arhdr and subelf to keep gcc calm. */
1361 - asm ("" : "=m" (arhdr), "=m" (subelf));
1362 - if (elf_rand (elf, memp->old_off) == 0
1363 - || (subelf = elf_begin (fd, ELF_C_READ_MMAP, elf)) == NULL
1364 - || (arhdr = elf_getarhdr (subelf)) == NULL)
1365 - /* This should never happen since we already looked at the
1366 - archive content. But who knows... */
1367 - error (EXIT_FAILURE, 0, "%s: %s", arfname, elf_errmsg (-1));
1369 - arlib_add_symbols (subelf, arfname, arhdr->ar_name, cur_off);
1374 - arlib_add_symbols (memp->elf, arfname, memp->name, cur_off);
1376 - cur_off += (((memp->size + 1) & ~((off_t) 1))
1377 - + sizeof (struct ar_hdr));
1380 - /* Now we have all the information for the symbol table and long
1381 - file name table. Construct the final layout. */
1382 - arlib_finalize ();
1384 - /* Create a new, temporary file in the same directory as the
1386 - char tmpfname[strlen (arfname) + 7];
1387 - strcpy (stpcpy (tmpfname, arfname), "XXXXXX");
1390 - newfd = mkstemp (tmpfname);
1393 - newfd = open (arfname, O_RDWR | O_CREAT | O_EXCL, DEFFILEMODE);
1394 - if (newfd == -1 && errno == EEXIST)
1395 - /* Bah, first the file did not exist, now it does. Restart. */
1396 - return do_oper_insert (oper, arfname, argv, argc, member);
1398 - if (unlikely (newfd == -1))
1401 - /* Create the header. */
1402 - if (unlikely (write_retry (newfd, ARMAG, SARMAG) != SARMAG))
1407 - // XXX Use /prof/self/fd/%d ???
1408 - unlink (tmpfname);
1413 - error (0, errno, gettext ("cannot create new file"));
1418 - /* If the new archive is not empty we actually have something to do. */
1419 - if (likely (all != NULL))
1421 - /* Write the symbol table or the long file name table or both. */
1422 - if (symtab.symsnamelen != 0
1423 - && ((write_retry (newfd, symtab.symsoff, symtab.symsofflen)
1424 - != (ssize_t) symtab.symsofflen)
1425 - || (write_retry (newfd, symtab.symsname, symtab.symsnamelen)
1426 - != (ssize_t) symtab.symsnamelen)))
1427 - goto nonew_unlink;
1429 - if (symtab.longnameslen > sizeof (struct ar_hdr)
1430 - && (write_retry (newfd, symtab.longnames, symtab.longnameslen)
1431 - != (ssize_t) symtab.longnameslen))
1432 - goto nonew_unlink;
1437 - while (all != NULL)
1439 - if (all->mem != NULL)
1441 - /* This is a new file. If there is anything from the
1442 - archive left to be written do it now. */
1443 - if (start != -1 && copy_content (elf, newfd, start, len))
1444 - goto nonew_unlink;
1449 - /* Create the header. */
1450 - struct ar_hdr arhdr;
1451 - /* The ar_name is not actually zero teminated, but we
1452 - need that for snprintf. Also if the name is too
1453 - long, then the string starts with '/' plus an index
1454 - off number (decimal). */
1455 - char tmpbuf[sizeof (arhdr.ar_name) + 2];
1456 - if (all->long_name_off == -1)
1458 - size_t namelen = strlen (all->name);
1459 - char *p = mempcpy (arhdr.ar_name, all->name, namelen);
1461 - memset (p, ' ', sizeof (arhdr.ar_name) - namelen - 1);
1465 - snprintf (tmpbuf, sizeof (tmpbuf), "/%-*ld",
1466 - (int) sizeof (arhdr.ar_name), all->long_name_off);
1467 - memcpy (arhdr.ar_name, tmpbuf, sizeof (arhdr.ar_name));
1470 - no0print (false, arhdr.ar_date, sizeof (arhdr.ar_date),
1472 - no0print (false, arhdr.ar_uid, sizeof (arhdr.ar_uid), all->uid);
1473 - no0print (false, arhdr.ar_gid, sizeof (arhdr.ar_gid), all->gid);
1474 - no0print (true, arhdr.ar_mode, sizeof (arhdr.ar_mode),
1476 - no0print (false, arhdr.ar_size, sizeof (arhdr.ar_size),
1478 - memcpy (arhdr.ar_fmag, ARFMAG, sizeof (arhdr.ar_fmag));
1480 - if (unlikely (write_retry (newfd, &arhdr, sizeof (arhdr))
1481 - != sizeof (arhdr)))
1482 - goto nonew_unlink;
1484 - /* Now the file itself. */
1485 - if (unlikely (write_retry (newfd, all->mem, all->size)
1486 - != (off_t) all->size))
1487 - goto nonew_unlink;
1489 - /* Pad the file if its size is odd. */
1490 - if ((all->size & 1) != 0)
1491 - if (unlikely (write_retry (newfd, "\n", 1) != 1))
1492 - goto nonew_unlink;
1496 - /* This is a member from the archive. */
1497 - if (write_member (all, &start, &len, elf, cur_off, newfd)
1499 - goto nonew_unlink;
1505 - /* Write the last part. */
1506 - if (start != -1 && copy_content (elf, newfd, start, len))
1507 - goto nonew_unlink;
1510 - /* Set the mode of the new file to the same values the original file
1513 - && (fchmod (newfd, st.st_mode & ALLPERMS) != 0
1514 - /* Never complain about fchown failing. */
1515 - || (({asm ("" :: "r" (fchown (newfd, st.st_uid, st.st_gid))); }),
1516 - close (newfd) != 0)
1517 - || (newfd = -1, rename (tmpfname, arfname) != 0)))
1518 - goto nonew_unlink;
1533 -#include "debugpred.h"
1534 +int main() { return 0; }