From 57aaefdceccdd590f81af4b274c7a3ee61ae62f1 Mon Sep 17 00:00:00 2001 From: Ed Maste Date: Fri, 13 Aug 2021 11:17:24 -0400 Subject: [PATCH] ar: diff reduction against ELF Tool Chain Remove ar_mode_* wrappers and call ar_read_archive / ar_write_archive directly. (This was originally committed in d20e9e02db3d but reverted due to a regression, now fixed with an update from cy@.) Obtained from: ELF Tool Chain Reviewed by: markj Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D31496 --- usr.bin/ar/acpyacc.y | 14 ++++++------ usr.bin/ar/ar.c | 27 ++++++----------------- usr.bin/ar/ar.h | 14 ++++-------- usr.bin/ar/read.c | 30 +++++--------------------- usr.bin/ar/write.c | 51 ++++++-------------------------------------- 5 files changed, 28 insertions(+), 108 deletions(-) diff --git a/usr.bin/ar/acpyacc.y b/usr.bin/ar/acpyacc.y index f6bfbd7957b5..34156e1d2d58 100644 --- a/usr.bin/ar/acpyacc.y +++ b/usr.bin/ar/acpyacc.y @@ -367,7 +367,7 @@ arscp_addlib(char *archive, struct list *list) return; arscp_mlist2argv(list); bsdar->addlib = archive; - ar_mode_A(bsdar); + ar_write_archive(bsdar, 'A'); arscp_free_argv(); arscp_free_mlist(list); } @@ -380,7 +380,7 @@ arscp_addmod(struct list *list) if (!arscp_target_exist()) return; arscp_mlist2argv(list); - ar_mode_q(bsdar); + ar_write_archive(bsdar, 'q'); arscp_free_argv(); arscp_free_mlist(list); } @@ -393,7 +393,7 @@ arscp_delete(struct list *list) if (!arscp_target_exist()) return; arscp_mlist2argv(list); - ar_mode_d(bsdar); + ar_write_archive(bsdar, 'd'); arscp_free_argv(); arscp_free_mlist(list); } @@ -406,7 +406,7 @@ arscp_extract(struct list *list) if (!arscp_target_exist()) return; arscp_mlist2argv(list); - ar_mode_x(bsdar); + ar_read_archive(bsdar, 'x'); arscp_free_argv(); arscp_free_mlist(list); } @@ -422,7 +422,7 @@ arscp_list(void) bsdar->argv = NULL; /* Always verbose. */ bsdar->options |= AR_V; - ar_mode_t(bsdar); + ar_read_archive(bsdar, 't'); bsdar->options &= ~AR_V; } @@ -449,7 +449,7 @@ arscp_dir(char *archive, struct list *list, char *rlt) } if (verbose) bsdar->options |= AR_V; - ar_mode_t(bsdar); + ar_read_archive(bsdar, 't'); bsdar->options &= ~AR_V; if (rlt) { @@ -473,7 +473,7 @@ arscp_replace(struct list *list) if (!arscp_target_exist()) return; arscp_mlist2argv(list); - ar_mode_r(bsdar); + ar_write_archive(bsdar, 'r'); arscp_free_argv(); arscp_free_mlist(list); } diff --git a/usr.bin/ar/ar.c b/usr.bin/ar/ar.c index aceb4d7f8487..fdfebb8eb459 100644 --- a/usr.bin/ar/ar.c +++ b/usr.bin/ar/ar.c @@ -151,7 +151,7 @@ main(int argc, char **argv) bsdar->options |= AR_D; bsdar->options |= AR_S; while ((bsdar->filename = *argv++) != NULL) - if (ar_mode_s(bsdar)) + if (ar_write_archive(bsdar, 's')) exitcode = EXIT_FAILURE; exit(exitcode); @@ -317,32 +317,17 @@ main(int argc, char **argv) if ((!bsdar->mode || strchr("ptx", bsdar->mode)) && bsdar->options & AR_S) { - exitcode = ar_mode_s(bsdar); + exitcode = ar_write_archive(bsdar, 's'); if (!bsdar->mode) exit(exitcode); } switch(bsdar->mode) { - case 'd': - exitcode = ar_mode_d(bsdar); + case 'd': case 'm': case 'q': case 'r': + exitcode = ar_write_archive(bsdar, bsdar->mode); break; - case 'm': - exitcode = ar_mode_m(bsdar); - break; - case 'p': - exitcode = ar_mode_p(bsdar); - break; - case 'q': - exitcode = ar_mode_q(bsdar); - break; - case 'r': - exitcode = ar_mode_r(bsdar); - break; - case 't': - exitcode = ar_mode_t(bsdar); - break; - case 'x': - exitcode = ar_mode_x(bsdar); + case 'p': case 't': case 'x': + exitcode = ar_read_archive(bsdar, bsdar->mode); break; default: bsdar_usage(); diff --git a/usr.bin/ar/ar.h b/usr.bin/ar/ar.h index 321680fed804..c8705e20eb45 100644 --- a/usr.bin/ar/ar.h +++ b/usr.bin/ar/ar.h @@ -115,14 +115,8 @@ struct bsdar { }; void bsdar_errc(struct bsdar *, int _code, const char *fmt, ...) __dead2; -void bsdar_warnc(struct bsdar *, int _code, const char *fmt, ...); -int ar_mode_d(struct bsdar *bsdar); -int ar_mode_m(struct bsdar *bsdar); -int ar_mode_p(struct bsdar *bsdar); -int ar_mode_q(struct bsdar *bsdar); -int ar_mode_r(struct bsdar *bsdar); -int ar_mode_s(struct bsdar *bsdar); -int ar_mode_t(struct bsdar *bsdar); -int ar_mode_x(struct bsdar *bsdar); -int ar_mode_A(struct bsdar *bsdar); void ar_mode_script(struct bsdar *ar); +int ar_read_archive(struct bsdar *ar, int mode); +int ar_write_archive(struct bsdar *ar, int mode); +void bsdar_errc(struct bsdar *, int _code, const char *fmt, ...) __dead2; +void bsdar_warnc(struct bsdar *, int _code, const char *fmt, ...); diff --git a/usr.bin/ar/read.c b/usr.bin/ar/read.c index 15044ab57a93..81e0bfce1b7e 100644 --- a/usr.bin/ar/read.c +++ b/usr.bin/ar/read.c @@ -34,6 +34,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -42,34 +43,11 @@ __FBSDID("$FreeBSD$"); #include "ar.h" -static int read_archive(struct bsdar *bsdar, char mode); - -int -ar_mode_p(struct bsdar *bsdar) -{ - - return (read_archive(bsdar, 'p')); -} - -int -ar_mode_t(struct bsdar *bsdar) -{ - - return (read_archive(bsdar, 't')); -} - -int -ar_mode_x(struct bsdar *bsdar) -{ - - return (read_archive(bsdar, 'x')); -} - /* * Handle read modes: 'x', 't' and 'p'. */ -static int -read_archive(struct bsdar *bsdar, char mode) +int +ar_read_archive(struct bsdar *bsdar, int mode) { struct archive *a; struct archive_entry *entry; @@ -87,6 +65,8 @@ read_archive(struct bsdar *bsdar, char mode) char find; int exitcode, flags, r, i; + assert(mode == 'p' || mode == 't' || mode == 'x'); + if ((a = archive_read_new()) == NULL) bsdar_errc(bsdar, 0, "archive_read_new failed"); archive_read_support_format_ar(a); diff --git a/usr.bin/ar/write.c b/usr.bin/ar/write.c index 3c4b20e305da..1b9ff18b155b 100644 --- a/usr.bin/ar/write.c +++ b/usr.bin/ar/write.c @@ -35,6 +35,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -66,54 +67,11 @@ static void insert_obj(struct bsdar *bsdar, struct ar_obj *obj, static void prefault_buffer(const char *buf, size_t s); static void read_objs(struct bsdar *bsdar, const char *archive, int checkargv); -static int write_archive(struct bsdar *bsdar, char mode); static void write_cleanup(struct bsdar *bsdar); static void write_data(struct bsdar *bsdar, struct archive *a, const void *buf, size_t s); static void write_objs(struct bsdar *bsdar); -int -ar_mode_d(struct bsdar *bsdar) -{ - - return (write_archive(bsdar, 'd')); -} - -int -ar_mode_m(struct bsdar *bsdar) -{ - - return (write_archive(bsdar, 'm')); -} - -int -ar_mode_q(struct bsdar *bsdar) -{ - - return (write_archive(bsdar, 'q')); -} - -int -ar_mode_r(struct bsdar *bsdar) -{ - - return (write_archive(bsdar, 'r')); -} - -int -ar_mode_s(struct bsdar *bsdar) -{ - - return (write_archive(bsdar, 's')); -} - -int -ar_mode_A(struct bsdar *bsdar) -{ - - return (write_archive(bsdar, 'A')); -} - /* * Create object from file, return created obj upon success, or NULL * when an error occurs or the member is not newer than existing @@ -376,8 +334,8 @@ read_objs(struct bsdar *bsdar, const char *archive, int checkargv) /* * Determine the constitution of resulting archive. */ -static int -write_archive(struct bsdar *bsdar, char mode) +int +ar_write_archive(struct bsdar *bsdar, int mode) { struct ar_obj *nobj, *obj, *obj_temp, *pos; struct stat sb; @@ -391,6 +349,9 @@ write_archive(struct bsdar *bsdar, char mode) pos = NULL; memset(&sb, 0, sizeof(sb)); + assert(mode == 'A' || mode == 'd' || mode == 'm' || mode == 'q' || + mode == 'r' || mode == 's'); + /* * Test if the specified archive exists, to figure out * whether we are creating one here.