From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5502 invoked by alias); 19 Sep 2012 19:53:48 -0000 Received: (qmail 5492 invoked by uid 22791); 19 Sep 2012 19:53:47 -0000 X-SWARE-Spam-Status: No, hits=-3.7 required=5.0 tests=AWL,BAYES_00,KHOP_THREADED,RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from server-nat-6.cs.umd.edu (HELO bacon.cs.umd.edu) (128.8.127.149) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 19 Sep 2012 19:53:33 +0000 Received: from [10.109.174.174] (129-2-129-155.wireless.umd.edu [129.2.129.155]) (Authenticated sender: khooyp) by bacon.cs.umd.edu (Postfix) with ESMTPSA id 5779EB406F5; Wed, 19 Sep 2012 15:53:22 -0400 (EDT) Subject: [PATCH 1/2]: Refactor relocate_path to also check if the relocated file/directory exists Mime-Version: 1.0 (Apple Message framework v1084) Content-Type: multipart/mixed; boundary=Apple-Mail-27-410008988 From: Khoo Yit Phang In-Reply-To: <20120919130040.GA20442@host2.jankratochvil.net> Date: Wed, 19 Sep 2012 19:53:00 -0000 Cc: Khoo Yit Phang , GDB Patches Message-Id: References: <21ACC598-F6B4-4117-BA7B-B316414DE9E3@cs.umd.edu> <20120919130040.GA20442@host2.jankratochvil.net> To: Jan Kratochvil X-CSD-MailScanner-ID: 5779EB406F5.AEE3A X-CSD-MailScanner: Found to be clean X-CSD-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-50, required 5, autolearn=not spam, ALL_TRUSTED -50.00) X-CSD-MailScanner-From: khooyp@cs.umd.edu X-CSD-MailScanner-Watermark: 1348689202.50992@OilTqYHF7hndXnbYvY2A7Q Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2012-09/txt/msg00398.txt.bz2 --Apple-Mail-27-410008988 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Content-length: 255 Hi Jan, I've split my data-directory patch into two. Here's the first one in prepar= ation for the next patch: it refactors relocate_path to also check if the r= elocated file or directory exists, to reduce some code redundancy. Yit September 19, 2012 --Apple-Mail-27-410008988 Content-Disposition: attachment; filename=refactor-relocate-path.txt Content-Type: text/plain; name="refactor-relocate-path.txt" Content-Transfer-Encoding: 7bit Content-length: 2818 gdb/ChangeLog 2012-09-19 Khoo Yit Phang Refactor relocate_path to also check if the relocated file/directory exists. * main.c (relocate_path): Add an isdir argument, and check that the relocated file/directory exists. (relocate_gdb_directory): Remove the directory check that is subsumed by relocate_path. (get_init_files): Remove the file check that is subsumed by relocate_path. diff --git a/gdb/main.c b/gdb/main.c --- a/gdb/main.c +++ b/gdb/main.c @@ -91,17 +91,34 @@ static void print_gdb_help (struct ui_file *); -/* Relocate a file or directory. PROGNAME is the name by which gdb - was invoked (i.e., argv[0]). INITIAL is the default value for the - file or directory. FLAG is true if the value is relocatable, false - otherwise. Returns a newly allocated string; this may return NULL - under the same conditions as make_relative_prefix. */ +/* Relocate a file or directory, checking if it exists. PROGNAME is the + name by which gdb was invoked (i.e., argv[0]). INITIAL is the default + value for the file or directory. ISDIR is true if INITIAL is a + directory. FLAG is true if the value is relocatable, false otherwise. + Returns a newly allocated string; this may return NULL under the same + conditions as make_relative_prefix, or if the relocated path does not + exist. */ static char * -relocate_path (const char *progname, const char *initial, int flag) +relocate_path (const char *progname, const char *initial, int isdir, + int flag) { if (flag) - return make_relative_prefix (progname, BINDIR, initial); + { + char *path; + path = make_relative_prefix (progname, BINDIR, initial); + if (path) + { + struct stat s; + + if (stat (path, &s) != 0 || (isdir && !S_ISDIR (s.st_mode))) + { + xfree (path); + path = NULL; + } + } + return path; + } return xstrdup (initial); } @@ -116,17 +133,7 @@ { char *dir; - dir = relocate_path (gdb_program_name, initial, flag); - if (dir) - { - struct stat s; - - if (stat (dir, &s) != 0 || !S_ISDIR (s.st_mode)) - { - xfree (dir); - dir = NULL; - } - } + dir = relocate_path (gdb_program_name, initial, 1, flag); if (!dir) dir = xstrdup (initial); @@ -162,15 +169,15 @@ if (!initialized) { - struct stat homebuf, cwdbuf, s; + struct stat homebuf, cwdbuf; char *homedir, *relocated_sysgdbinit; if (SYSTEM_GDBINIT[0]) { relocated_sysgdbinit = relocate_path (gdb_program_name, - SYSTEM_GDBINIT, + SYSTEM_GDBINIT, 0, SYSTEM_GDBINIT_RELOCATABLE); - if (relocated_sysgdbinit && stat (relocated_sysgdbinit, &s) == 0) + if (relocated_sysgdbinit) sysgdbinit = relocated_sysgdbinit; else xfree (relocated_sysgdbinit); --Apple-Mail-27-410008988--