write.c (d20e9e02db3dde383c3de1ce8cec3a8c35b3eee6) write.c (c5bf58add0d523fc6e6546f3fc10d0c8b972e8e7)
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2007 Kai Wang
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 21 unchanged lines hidden (view full) ---

30__FBSDID("$FreeBSD$");
31
32#include <sys/endian.h>
33#include <sys/mman.h>
34#include <sys/queue.h>
35#include <sys/stat.h>
36#include <archive.h>
37#include <archive_entry.h>
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2007 Kai Wang
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 21 unchanged lines hidden (view full) ---

30__FBSDID("$FreeBSD$");
31
32#include <sys/endian.h>
33#include <sys/mman.h>
34#include <sys/queue.h>
35#include <sys/stat.h>
36#include <archive.h>
37#include <archive_entry.h>
38#include <assert.h>
39#include <errno.h>
40#include <fcntl.h>
41#include <gelf.h>
42#include <libgen.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
38#include <errno.h>
39#include <fcntl.h>
40#include <gelf.h>
41#include <libgen.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <sysexits.h>
46#include <unistd.h>
47
48#include "ar.h"
49
50#define _ARMAG_LEN 8 /* length of ar magic string */
51#define _ARHDR_LEN 60 /* length of ar header */
52#define _INIT_AS_CAP 128 /* initial archive string table size */
53#define _INIT_SYMOFF_CAP (256*(sizeof(uint64_t))) /* initial so table size */

--- 8 unchanged lines hidden (view full) ---

62static void create_symtab_entry(struct bsdar *bsdar, void *maddr,
63 size_t size);
64static void free_obj(struct bsdar *bsdar, struct ar_obj *obj);
65static void insert_obj(struct bsdar *bsdar, struct ar_obj *obj,
66 struct ar_obj *pos);
67static void prefault_buffer(const char *buf, size_t s);
68static void read_objs(struct bsdar *bsdar, const char *archive,
69 int checkargv);
46#include <unistd.h>
47
48#include "ar.h"
49
50#define _ARMAG_LEN 8 /* length of ar magic string */
51#define _ARHDR_LEN 60 /* length of ar header */
52#define _INIT_AS_CAP 128 /* initial archive string table size */
53#define _INIT_SYMOFF_CAP (256*(sizeof(uint64_t))) /* initial so table size */

--- 8 unchanged lines hidden (view full) ---

62static void create_symtab_entry(struct bsdar *bsdar, void *maddr,
63 size_t size);
64static void free_obj(struct bsdar *bsdar, struct ar_obj *obj);
65static void insert_obj(struct bsdar *bsdar, struct ar_obj *obj,
66 struct ar_obj *pos);
67static void prefault_buffer(const char *buf, size_t s);
68static void read_objs(struct bsdar *bsdar, const char *archive,
69 int checkargv);
70static int write_archive(struct bsdar *bsdar, char mode);
70static void write_cleanup(struct bsdar *bsdar);
71static void write_data(struct bsdar *bsdar, struct archive *a,
72 const void *buf, size_t s);
73static void write_objs(struct bsdar *bsdar);
74
71static void write_cleanup(struct bsdar *bsdar);
72static void write_data(struct bsdar *bsdar, struct archive *a,
73 const void *buf, size_t s);
74static void write_objs(struct bsdar *bsdar);
75
76int
77ar_mode_d(struct bsdar *bsdar)
78{
79
80 return (write_archive(bsdar, 'd'));
81}
82
83int
84ar_mode_m(struct bsdar *bsdar)
85{
86
87 return (write_archive(bsdar, 'm'));
88}
89
90int
91ar_mode_q(struct bsdar *bsdar)
92{
93
94 return (write_archive(bsdar, 'q'));
95}
96
97int
98ar_mode_r(struct bsdar *bsdar)
99{
100
101 return (write_archive(bsdar, 'r'));
102}
103
104int
105ar_mode_s(struct bsdar *bsdar)
106{
107
108 return (write_archive(bsdar, 's'));
109}
110
111int
112ar_mode_A(struct bsdar *bsdar)
113{
114
115 return (write_archive(bsdar, 'A'));
116}
117
75/*
76 * Create object from file, return created obj upon success, or NULL
77 * when an error occurs or the member is not newer than existing
78 * one while -u is specified.
79 */
80static struct ar_obj *
81create_obj_from_file(struct bsdar *bsdar, const char *name, time_t mtime)
82{
83 struct ar_obj *obj;
84 struct stat sb;
85 const char *bname;
86 char *tmpname;
87
88 if (name == NULL)
89 return (NULL);
90
91 obj = malloc(sizeof(struct ar_obj));
92 if (obj == NULL)
118/*
119 * Create object from file, return created obj upon success, or NULL
120 * when an error occurs or the member is not newer than existing
121 * one while -u is specified.
122 */
123static struct ar_obj *
124create_obj_from_file(struct bsdar *bsdar, const char *name, time_t mtime)
125{
126 struct ar_obj *obj;
127 struct stat sb;
128 const char *bname;
129 char *tmpname;
130
131 if (name == NULL)
132 return (NULL);
133
134 obj = malloc(sizeof(struct ar_obj));
135 if (obj == NULL)
93 bsdar_errc(bsdar, errno, "malloc failed");
136 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
94 if ((obj->fd = open(name, O_RDONLY, 0)) < 0) {
95 bsdar_warnc(bsdar, errno, "can't open file: %s", name);
96 free(obj);
97 return (NULL);
98 }
99
100 tmpname = strdup(name);
101 if (tmpname == NULL)
137 if ((obj->fd = open(name, O_RDONLY, 0)) < 0) {
138 bsdar_warnc(bsdar, errno, "can't open file: %s", name);
139 free(obj);
140 return (NULL);
141 }
142
143 tmpname = strdup(name);
144 if (tmpname == NULL)
102 bsdar_errc(bsdar, errno, "strdup failed");
145 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed");
103 if ((bname = basename(tmpname)) == NULL)
146 if ((bname = basename(tmpname)) == NULL)
104 bsdar_errc(bsdar, errno, "basename failed");
147 bsdar_errc(bsdar, EX_SOFTWARE, errno, "basename failed");
105 if (bsdar->options & AR_TR && strlen(bname) > _TRUNCATE_LEN) {
106 if ((obj->name = malloc(_TRUNCATE_LEN + 1)) == NULL)
148 if (bsdar->options & AR_TR && strlen(bname) > _TRUNCATE_LEN) {
149 if ((obj->name = malloc(_TRUNCATE_LEN + 1)) == NULL)
107 bsdar_errc(bsdar, errno, "malloc failed");
150 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
108 (void)strncpy(obj->name, bname, _TRUNCATE_LEN);
109 obj->name[_TRUNCATE_LEN] = '\0';
110 } else
111 if ((obj->name = strdup(bname)) == NULL)
151 (void)strncpy(obj->name, bname, _TRUNCATE_LEN);
152 obj->name[_TRUNCATE_LEN] = '\0';
153 } else
154 if ((obj->name = strdup(bname)) == NULL)
112 bsdar_errc(bsdar, errno, "strdup failed");
155 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed");
113 free(tmpname);
114
115 if (fstat(obj->fd, &sb) < 0) {
116 bsdar_warnc(bsdar, errno, "can't fstat file: %s", obj->name);
117 goto giveup;
118 }
119 if (!S_ISREG(sb.st_mode)) {
120 bsdar_warnc(bsdar, 0, "%s is not an ordinary file", obj->name);

--- 36 unchanged lines hidden (view full) ---

157 }
158
159 if ((obj->maddr = mmap(NULL, obj->size, PROT_READ,
160 MAP_PRIVATE, obj->fd, (off_t)0)) == MAP_FAILED) {
161 bsdar_warnc(bsdar, errno, "can't mmap file: %s", obj->name);
162 goto giveup;
163 }
164 if (close(obj->fd) < 0)
156 free(tmpname);
157
158 if (fstat(obj->fd, &sb) < 0) {
159 bsdar_warnc(bsdar, errno, "can't fstat file: %s", obj->name);
160 goto giveup;
161 }
162 if (!S_ISREG(sb.st_mode)) {
163 bsdar_warnc(bsdar, 0, "%s is not an ordinary file", obj->name);

--- 36 unchanged lines hidden (view full) ---

200 }
201
202 if ((obj->maddr = mmap(NULL, obj->size, PROT_READ,
203 MAP_PRIVATE, obj->fd, (off_t)0)) == MAP_FAILED) {
204 bsdar_warnc(bsdar, errno, "can't mmap file: %s", obj->name);
205 goto giveup;
206 }
207 if (close(obj->fd) < 0)
165 bsdar_errc(bsdar, errno, "close failed: %s",
208 bsdar_errc(bsdar, EX_SOFTWARE, errno, "close failed: %s",
166 obj->name);
167
168 return (obj);
169
170giveup:
171 if (close(obj->fd) < 0)
209 obj->name);
210
211 return (obj);
212
213giveup:
214 if (close(obj->fd) < 0)
172 bsdar_errc(bsdar, errno, "close failed: %s",
215 bsdar_errc(bsdar, EX_SOFTWARE, errno, "close failed: %s",
173 obj->name);
174 free(obj->name);
175 free(obj);
176 return (NULL);
177}
178
179/*
180 * Free object itself and its associated allocations.

--- 13 unchanged lines hidden (view full) ---

194
195/*
196 * Insert obj to the tail, or before/after the pos obj.
197 */
198static void
199insert_obj(struct bsdar *bsdar, struct ar_obj *obj, struct ar_obj *pos)
200{
201 if (obj == NULL)
216 obj->name);
217 free(obj->name);
218 free(obj);
219 return (NULL);
220}
221
222/*
223 * Free object itself and its associated allocations.

--- 13 unchanged lines hidden (view full) ---

237
238/*
239 * Insert obj to the tail, or before/after the pos obj.
240 */
241static void
242insert_obj(struct bsdar *bsdar, struct ar_obj *obj, struct ar_obj *pos)
243{
244 if (obj == NULL)
202 bsdar_errc(bsdar, 0, "try to insert a null obj");
245 bsdar_errc(bsdar, EX_SOFTWARE, 0, "try to insert a null obj");
203
204 if (pos == NULL || obj == pos)
205 /*
206 * If the object to move happens to be the position obj,
207 * or if there is not a pos obj, move it to tail.
208 */
209 goto tail;
210

--- 26 unchanged lines hidden (view full) ---

237 const char *name;
238 const char *bname;
239 char *buff;
240 char **av;
241 size_t size;
242 int i, r, find;
243
244 if ((a = archive_read_new()) == NULL)
246
247 if (pos == NULL || obj == pos)
248 /*
249 * If the object to move happens to be the position obj,
250 * or if there is not a pos obj, move it to tail.
251 */
252 goto tail;
253

--- 26 unchanged lines hidden (view full) ---

280 const char *name;
281 const char *bname;
282 char *buff;
283 char **av;
284 size_t size;
285 int i, r, find;
286
287 if ((a = archive_read_new()) == NULL)
245 bsdar_errc(bsdar, 0, "archive_read_new failed");
288 bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_read_new failed");
246 archive_read_support_format_ar(a);
247 AC(archive_read_open_filename(a, archive, DEF_BLKSZ));
248 for (;;) {
249 r = archive_read_next_header(a, &entry);
250 if (r == ARCHIVE_FATAL)
289 archive_read_support_format_ar(a);
290 AC(archive_read_open_filename(a, archive, DEF_BLKSZ));
291 for (;;) {
292 r = archive_read_next_header(a, &entry);
293 if (r == ARCHIVE_FATAL)
251 bsdar_errc(bsdar, archive_errno(a), "%s",
294 bsdar_errc(bsdar, EX_DATAERR, archive_errno(a), "%s",
252 archive_error_string(a));
253 if (r == ARCHIVE_EOF)
254 break;
255 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY)
256 bsdar_warnc(bsdar, archive_errno(a), "%s",
257 archive_error_string(a));
258 if (r == ARCHIVE_RETRY) {
259 bsdar_warnc(bsdar, 0, "Retrying...");

--- 14 unchanged lines hidden (view full) ---

274 */
275 if (checkargv && bsdar->argc > 0) {
276 find = 0;
277 for(i = 0; i < bsdar->argc; i++) {
278 av = &bsdar->argv[i];
279 if (*av == NULL)
280 continue;
281 if ((bname = basename(*av)) == NULL)
295 archive_error_string(a));
296 if (r == ARCHIVE_EOF)
297 break;
298 if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY)
299 bsdar_warnc(bsdar, archive_errno(a), "%s",
300 archive_error_string(a));
301 if (r == ARCHIVE_RETRY) {
302 bsdar_warnc(bsdar, 0, "Retrying...");

--- 14 unchanged lines hidden (view full) ---

317 */
318 if (checkargv && bsdar->argc > 0) {
319 find = 0;
320 for(i = 0; i < bsdar->argc; i++) {
321 av = &bsdar->argv[i];
322 if (*av == NULL)
323 continue;
324 if ((bname = basename(*av)) == NULL)
282 bsdar_errc(bsdar, errno,
325 bsdar_errc(bsdar, EX_SOFTWARE, errno,
283 "basename failed");
284 if (strcmp(bname, name) != 0)
285 continue;
286
287 *av = NULL;
288 find = 1;
289 break;
290 }
291 if (!find)
292 continue;
293 }
294
295 size = archive_entry_size(entry);
296
297 if (size > 0) {
298 if ((buff = malloc(size)) == NULL)
326 "basename failed");
327 if (strcmp(bname, name) != 0)
328 continue;
329
330 *av = NULL;
331 find = 1;
332 break;
333 }
334 if (!find)
335 continue;
336 }
337
338 size = archive_entry_size(entry);
339
340 if (size > 0) {
341 if ((buff = malloc(size)) == NULL)
299 bsdar_errc(bsdar, errno, "malloc failed");
342 bsdar_errc(bsdar, EX_SOFTWARE, errno,
343 "malloc failed");
300 if (archive_read_data(a, buff, size) != (ssize_t)size) {
301 bsdar_warnc(bsdar, archive_errno(a), "%s",
302 archive_error_string(a));
303 free(buff);
304 continue;
305 }
306 } else
307 buff = NULL;
308
309 obj = malloc(sizeof(struct ar_obj));
310 if (obj == NULL)
344 if (archive_read_data(a, buff, size) != (ssize_t)size) {
345 bsdar_warnc(bsdar, archive_errno(a), "%s",
346 archive_error_string(a));
347 free(buff);
348 continue;
349 }
350 } else
351 buff = NULL;
352
353 obj = malloc(sizeof(struct ar_obj));
354 if (obj == NULL)
311 bsdar_errc(bsdar, errno, "malloc failed");
355 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
312 obj->maddr = buff;
313 if ((obj->name = strdup(name)) == NULL)
356 obj->maddr = buff;
357 if ((obj->name = strdup(name)) == NULL)
314 bsdar_errc(bsdar, errno, "strdup failed");
358 bsdar_errc(bsdar, EX_SOFTWARE, errno, "strdup failed");
315 obj->size = size;
316 obj->uid = archive_entry_uid(entry);
317 obj->gid = archive_entry_gid(entry);
318 obj->md = archive_entry_mode(entry);
319 obj->mtime = archive_entry_mtime(entry);
320 obj->dev = 0;
321 obj->ino = 0;
322

--- 6 unchanged lines hidden (view full) ---

329 }
330 AC(archive_read_close(a));
331 AC(archive_read_free(a));
332}
333
334/*
335 * Determine the constitution of resulting archive.
336 */
359 obj->size = size;
360 obj->uid = archive_entry_uid(entry);
361 obj->gid = archive_entry_gid(entry);
362 obj->md = archive_entry_mode(entry);
363 obj->mtime = archive_entry_mtime(entry);
364 obj->dev = 0;
365 obj->ino = 0;
366

--- 6 unchanged lines hidden (view full) ---

373 }
374 AC(archive_read_close(a));
375 AC(archive_read_free(a));
376}
377
378/*
379 * Determine the constitution of resulting archive.
380 */
337int
338ar_write_archive(struct bsdar *bsdar, int mode)
381static int
382write_archive(struct bsdar *bsdar, char mode)
339{
340 struct ar_obj *nobj, *obj, *obj_temp, *pos;
341 struct stat sb;
342 const char *bname;
343 char **av;
344 int exitcode, i;
345
346 TAILQ_INIT(&bsdar->v_obj);
347 exitcode = EXIT_SUCCESS;
348 nobj = NULL;
349 pos = NULL;
350 memset(&sb, 0, sizeof(sb));
351
383{
384 struct ar_obj *nobj, *obj, *obj_temp, *pos;
385 struct stat sb;
386 const char *bname;
387 char **av;
388 int exitcode, i;
389
390 TAILQ_INIT(&bsdar->v_obj);
391 exitcode = EXIT_SUCCESS;
392 nobj = NULL;
393 pos = NULL;
394 memset(&sb, 0, sizeof(sb));
395
352 assert(mode == 'A' || mode == 'd' || mode == 'm' || mode == 'q' ||
353 mode == 'r' || mode == 's');
354
355 /*
356 * Test if the specified archive exists, to figure out
357 * whether we are creating one here.
358 */
359 if (stat(bsdar->filename, &sb) != 0) {
360 if (errno != ENOENT) {
361 bsdar_warnc(bsdar, 0, "stat %s failed",
362 bsdar->filename);

--- 67 unchanged lines hidden (view full) ---

430 bsdar->options &= ~(AR_A | AR_B);
431 }
432
433 for (i = 0; i < bsdar->argc; i++) {
434 av = &bsdar->argv[i];
435
436 TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) {
437 if ((bname = basename(*av)) == NULL)
396 /*
397 * Test if the specified archive exists, to figure out
398 * whether we are creating one here.
399 */
400 if (stat(bsdar->filename, &sb) != 0) {
401 if (errno != ENOENT) {
402 bsdar_warnc(bsdar, 0, "stat %s failed",
403 bsdar->filename);

--- 67 unchanged lines hidden (view full) ---

471 bsdar->options &= ~(AR_A | AR_B);
472 }
473
474 for (i = 0; i < bsdar->argc; i++) {
475 av = &bsdar->argv[i];
476
477 TAILQ_FOREACH_SAFE(obj, &bsdar->v_obj, objs, obj_temp) {
478 if ((bname = basename(*av)) == NULL)
438 bsdar_errc(bsdar, errno, "basename failed");
479 bsdar_errc(bsdar, EX_SOFTWARE, errno,
480 "basename failed");
439 if (bsdar->options & AR_TR) {
440 if (strncmp(bname, obj->name, _TRUNCATE_LEN))
441 continue;
442 } else
443 if (strcmp(bname, obj->name) != 0)
444 continue;
445
446 if (mode == 'r') {

--- 111 unchanged lines hidden (view full) ---

558write_data(struct bsdar *bsdar, struct archive *a, const void *buf, size_t s)
559{
560 ssize_t written;
561
562 prefault_buffer(buf, s);
563 while (s > 0) {
564 written = archive_write_data(a, buf, s);
565 if (written < 0)
481 if (bsdar->options & AR_TR) {
482 if (strncmp(bname, obj->name, _TRUNCATE_LEN))
483 continue;
484 } else
485 if (strcmp(bname, obj->name) != 0)
486 continue;
487
488 if (mode == 'r') {

--- 111 unchanged lines hidden (view full) ---

600write_data(struct bsdar *bsdar, struct archive *a, const void *buf, size_t s)
601{
602 ssize_t written;
603
604 prefault_buffer(buf, s);
605 while (s > 0) {
606 written = archive_write_data(a, buf, s);
607 if (written < 0)
566 bsdar_errc(bsdar, archive_errno(a), "%s",
608 bsdar_errc(bsdar, EX_SOFTWARE, archive_errno(a), "%s",
567 archive_error_string(a));
568 buf = (const char *)buf + written;
569 s -= written;
570 }
571}
572
573/*
574 * Write the resulting archive members.

--- 7 unchanged lines hidden (view full) ---

582 size_t s_sz; /* size of archive symbol table. */
583 size_t pm_sz; /* size of pseudo members */
584 size_t w_sz; /* size of words in symbol table */
585 size_t i;
586 uint64_t nr;
587 uint32_t nr32;
588
589 if (elf_version(EV_CURRENT) == EV_NONE)
609 archive_error_string(a));
610 buf = (const char *)buf + written;
611 s -= written;
612 }
613}
614
615/*
616 * Write the resulting archive members.

--- 7 unchanged lines hidden (view full) ---

624 size_t s_sz; /* size of archive symbol table. */
625 size_t pm_sz; /* size of pseudo members */
626 size_t w_sz; /* size of words in symbol table */
627 size_t i;
628 uint64_t nr;
629 uint32_t nr32;
630
631 if (elf_version(EV_CURRENT) == EV_NONE)
590 bsdar_errc(bsdar, 0,
632 bsdar_errc(bsdar, EX_SOFTWARE, 0,
591 "ELF library initialization failed: %s", elf_errmsg(-1));
592
593 bsdar->rela_off = 0;
594
595 /* Create archive symbol table and archive string table, if need. */
596 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
597 if (!(bsdar->options & AR_SS) && obj->maddr != NULL)
598 create_symtab_entry(bsdar, obj->maddr, obj->size);

--- 49 unchanged lines hidden (view full) ---

648 */
649 for (i = 0; i < bsdar->s_cnt; i++)
650 ((uint32_t *)(bsdar->s_so))[i] =
651 htobe32(bsdar->s_so[i] + pm_sz);
652 }
653 }
654
655 if ((a = archive_write_new()) == NULL)
633 "ELF library initialization failed: %s", elf_errmsg(-1));
634
635 bsdar->rela_off = 0;
636
637 /* Create archive symbol table and archive string table, if need. */
638 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
639 if (!(bsdar->options & AR_SS) && obj->maddr != NULL)
640 create_symtab_entry(bsdar, obj->maddr, obj->size);

--- 49 unchanged lines hidden (view full) ---

690 */
691 for (i = 0; i < bsdar->s_cnt; i++)
692 ((uint32_t *)(bsdar->s_so))[i] =
693 htobe32(bsdar->s_so[i] + pm_sz);
694 }
695 }
696
697 if ((a = archive_write_new()) == NULL)
656 bsdar_errc(bsdar, 0, "archive_write_new failed");
698 bsdar_errc(bsdar, EX_SOFTWARE, 0, "archive_write_new failed");
657
658 archive_write_set_format_ar_svr4(a);
659
660 AC(archive_write_open_filename(a, bsdar->filename));
661
662 /*
663 * write the archive symbol table, if there is one.
664 * If options -s is explicitly specified or we are invoked
665 * as ranlib, write the symbol table even if it is empty.
666 */
667 if ((bsdar->s_cnt != 0 && !(bsdar->options & AR_SS)) ||
668 bsdar->options & AR_S) {
669 entry = archive_entry_new();
670 if (entry == NULL)
699
700 archive_write_set_format_ar_svr4(a);
701
702 AC(archive_write_open_filename(a, bsdar->filename));
703
704 /*
705 * write the archive symbol table, if there is one.
706 * If options -s is explicitly specified or we are invoked
707 * as ranlib, write the symbol table even if it is empty.
708 */
709 if ((bsdar->s_cnt != 0 && !(bsdar->options & AR_SS)) ||
710 bsdar->options & AR_S) {
711 entry = archive_entry_new();
712 if (entry == NULL)
671 bsdar_errc(bsdar, 0, "archive_entry_new failed");
713 bsdar_errc(bsdar, EX_SOFTWARE, 0,
714 "archive_entry_new failed");
672 if (w_sz == sizeof(uint64_t))
673 archive_entry_copy_pathname(entry, "/SYM64/");
674 else
675 archive_entry_copy_pathname(entry, "/");
676 if ((bsdar->options & AR_D) == 0)
677 archive_entry_set_mtime(entry, time(NULL), 0);
678 archive_entry_set_size(entry, (bsdar->s_cnt + 1) * w_sz +
679 bsdar->s_sn_sz);

--- 9 unchanged lines hidden (view full) ---

689 write_data(bsdar, a, bsdar->s_sn, bsdar->s_sn_sz);
690 archive_entry_free(entry);
691 }
692
693 /* write the archive string table, if any. */
694 if (bsdar->as != NULL) {
695 entry = archive_entry_new();
696 if (entry == NULL)
715 if (w_sz == sizeof(uint64_t))
716 archive_entry_copy_pathname(entry, "/SYM64/");
717 else
718 archive_entry_copy_pathname(entry, "/");
719 if ((bsdar->options & AR_D) == 0)
720 archive_entry_set_mtime(entry, time(NULL), 0);
721 archive_entry_set_size(entry, (bsdar->s_cnt + 1) * w_sz +
722 bsdar->s_sn_sz);

--- 9 unchanged lines hidden (view full) ---

732 write_data(bsdar, a, bsdar->s_sn, bsdar->s_sn_sz);
733 archive_entry_free(entry);
734 }
735
736 /* write the archive string table, if any. */
737 if (bsdar->as != NULL) {
738 entry = archive_entry_new();
739 if (entry == NULL)
697 bsdar_errc(bsdar, 0, "archive_entry_new failed");
740 bsdar_errc(bsdar, EX_SOFTWARE, 0,
741 "archive_entry_new failed");
698 archive_entry_copy_pathname(entry, "//");
699 archive_entry_set_size(entry, bsdar->as_sz);
700 AC(archive_write_header(a, entry));
701 write_data(bsdar, a, bsdar->as, bsdar->as_sz);
702 archive_entry_free(entry);
703 }
704
705 /* write normal members. */
706 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
707 entry = archive_entry_new();
708 if (entry == NULL)
742 archive_entry_copy_pathname(entry, "//");
743 archive_entry_set_size(entry, bsdar->as_sz);
744 AC(archive_write_header(a, entry));
745 write_data(bsdar, a, bsdar->as, bsdar->as_sz);
746 archive_entry_free(entry);
747 }
748
749 /* write normal members. */
750 TAILQ_FOREACH(obj, &bsdar->v_obj, objs) {
751 entry = archive_entry_new();
752 if (entry == NULL)
709 bsdar_errc(bsdar, 0, "archive_entry_new failed");
753 bsdar_errc(bsdar, EX_SOFTWARE, 0,
754 "archive_entry_new failed");
710 archive_entry_copy_pathname(entry, obj->name);
711 archive_entry_set_uid(entry, obj->uid);
712 archive_entry_set_gid(entry, obj->gid);
713 archive_entry_set_mode(entry, obj->md);
714 archive_entry_set_size(entry, obj->size);
715 archive_entry_set_mtime(entry, obj->mtime, 0);
716 archive_entry_set_dev(entry, obj->dev);
717 archive_entry_set_ino(entry, obj->ino);

--- 125 unchanged lines hidden (view full) ---

843static void
844add_to_ar_str_table(struct bsdar *bsdar, const char *name)
845{
846
847 if (bsdar->as == NULL) {
848 bsdar->as_cap = _INIT_AS_CAP;
849 bsdar->as_sz = 0;
850 if ((bsdar->as = malloc(bsdar->as_cap)) == NULL)
755 archive_entry_copy_pathname(entry, obj->name);
756 archive_entry_set_uid(entry, obj->uid);
757 archive_entry_set_gid(entry, obj->gid);
758 archive_entry_set_mode(entry, obj->md);
759 archive_entry_set_size(entry, obj->size);
760 archive_entry_set_mtime(entry, obj->mtime, 0);
761 archive_entry_set_dev(entry, obj->dev);
762 archive_entry_set_ino(entry, obj->ino);

--- 125 unchanged lines hidden (view full) ---

888static void
889add_to_ar_str_table(struct bsdar *bsdar, const char *name)
890{
891
892 if (bsdar->as == NULL) {
893 bsdar->as_cap = _INIT_AS_CAP;
894 bsdar->as_sz = 0;
895 if ((bsdar->as = malloc(bsdar->as_cap)) == NULL)
851 bsdar_errc(bsdar, errno, "malloc failed");
896 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
852 }
853
854 /*
855 * The space required for holding one member name in as table includes:
856 * strlen(name) + (1 for '/') + (1 for '\n') + (possibly 1 for padding).
857 */
858 while (bsdar->as_sz + strlen(name) + 3 > bsdar->as_cap) {
859 bsdar->as_cap *= 2;
860 bsdar->as = realloc(bsdar->as, bsdar->as_cap);
861 if (bsdar->as == NULL)
897 }
898
899 /*
900 * The space required for holding one member name in as table includes:
901 * strlen(name) + (1 for '/') + (1 for '\n') + (possibly 1 for padding).
902 */
903 while (bsdar->as_sz + strlen(name) + 3 > bsdar->as_cap) {
904 bsdar->as_cap *= 2;
905 bsdar->as = realloc(bsdar->as, bsdar->as_cap);
906 if (bsdar->as == NULL)
862 bsdar_errc(bsdar, errno, "realloc failed");
907 bsdar_errc(bsdar, EX_SOFTWARE, errno, "realloc failed");
863 }
864 strncpy(&bsdar->as[bsdar->as_sz], name, strlen(name));
865 bsdar->as_sz += strlen(name);
866 bsdar->as[bsdar->as_sz++] = '/';
867 bsdar->as[bsdar->as_sz++] = '\n';
868}
869
870/*
871 * Append to the archive symbol table buffer.
872 */
873static void
874add_to_ar_sym_table(struct bsdar *bsdar, const char *name)
875{
876
877 if (bsdar->s_so == NULL) {
878 if ((bsdar->s_so = malloc(_INIT_SYMOFF_CAP)) ==
879 NULL)
908 }
909 strncpy(&bsdar->as[bsdar->as_sz], name, strlen(name));
910 bsdar->as_sz += strlen(name);
911 bsdar->as[bsdar->as_sz++] = '/';
912 bsdar->as[bsdar->as_sz++] = '\n';
913}
914
915/*
916 * Append to the archive symbol table buffer.
917 */
918static void
919add_to_ar_sym_table(struct bsdar *bsdar, const char *name)
920{
921
922 if (bsdar->s_so == NULL) {
923 if ((bsdar->s_so = malloc(_INIT_SYMOFF_CAP)) ==
924 NULL)
880 bsdar_errc(bsdar, errno, "malloc failed");
925 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
881 bsdar->s_so_cap = _INIT_SYMOFF_CAP;
882 bsdar->s_cnt = 0;
883 }
884
885 if (bsdar->s_sn == NULL) {
886 if ((bsdar->s_sn = malloc(_INIT_SYMNAME_CAP)) == NULL)
926 bsdar->s_so_cap = _INIT_SYMOFF_CAP;
927 bsdar->s_cnt = 0;
928 }
929
930 if (bsdar->s_sn == NULL) {
931 if ((bsdar->s_sn = malloc(_INIT_SYMNAME_CAP)) == NULL)
887 bsdar_errc(bsdar, errno, "malloc failed");
932 bsdar_errc(bsdar, EX_SOFTWARE, errno, "malloc failed");
888 bsdar->s_sn_cap = _INIT_SYMNAME_CAP;
889 bsdar->s_sn_sz = 0;
890 }
891
892 if (bsdar->s_cnt * sizeof(uint64_t) >= bsdar->s_so_cap) {
893 bsdar->s_so_cap *= 2;
894 bsdar->s_so = realloc(bsdar->s_so, bsdar->s_so_cap);
895 if (bsdar->s_so == NULL)
933 bsdar->s_sn_cap = _INIT_SYMNAME_CAP;
934 bsdar->s_sn_sz = 0;
935 }
936
937 if (bsdar->s_cnt * sizeof(uint64_t) >= bsdar->s_so_cap) {
938 bsdar->s_so_cap *= 2;
939 bsdar->s_so = realloc(bsdar->s_so, bsdar->s_so_cap);
940 if (bsdar->s_so == NULL)
896 bsdar_errc(bsdar, errno, "realloc failed");
941 bsdar_errc(bsdar, EX_SOFTWARE, errno, "realloc failed");
897 }
898 bsdar->s_so[bsdar->s_cnt] = bsdar->rela_off;
899 if ((uint64_t)bsdar->rela_off > bsdar->s_so_max)
900 bsdar->s_so_max = (uint64_t)bsdar->rela_off;
901 bsdar->s_cnt++;
902
903 /*
904 * The space required for holding one symbol name in sn table includes:
905 * strlen(name) + (1 for '\n') + (possibly 1 for padding).
906 */
907 while (bsdar->s_sn_sz + strlen(name) + 2 > bsdar->s_sn_cap) {
908 bsdar->s_sn_cap *= 2;
909 bsdar->s_sn = realloc(bsdar->s_sn, bsdar->s_sn_cap);
910 if (bsdar->s_sn == NULL)
942 }
943 bsdar->s_so[bsdar->s_cnt] = bsdar->rela_off;
944 if ((uint64_t)bsdar->rela_off > bsdar->s_so_max)
945 bsdar->s_so_max = (uint64_t)bsdar->rela_off;
946 bsdar->s_cnt++;
947
948 /*
949 * The space required for holding one symbol name in sn table includes:
950 * strlen(name) + (1 for '\n') + (possibly 1 for padding).
951 */
952 while (bsdar->s_sn_sz + strlen(name) + 2 > bsdar->s_sn_cap) {
953 bsdar->s_sn_cap *= 2;
954 bsdar->s_sn = realloc(bsdar->s_sn, bsdar->s_sn_cap);
955 if (bsdar->s_sn == NULL)
911 bsdar_errc(bsdar, errno, "realloc failed");
956 bsdar_errc(bsdar, EX_SOFTWARE, errno, "realloc failed");
912 }
913 strncpy(&bsdar->s_sn[bsdar->s_sn_sz], name, strlen(name));
914 bsdar->s_sn_sz += strlen(name);
915 bsdar->s_sn[bsdar->s_sn_sz++] = '\0';
916}
957 }
958 strncpy(&bsdar->s_sn[bsdar->s_sn_sz], name, strlen(name));
959 bsdar->s_sn_sz += strlen(name);
960 bsdar->s_sn[bsdar->s_sn_sz++] = '\0';
961}