From patchwork Fri Oct 25 13:22:36 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Simon Marchi (Code Review)" X-Patchwork-Id: 35310 Received: (qmail 58865 invoked by alias); 25 Oct 2019 13:22:43 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 58775 invoked by uid 89); 25 Oct 2019 13:22:43 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-20.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT autolearn=ham version=3.3.1 spammy= X-HELO: mx1.osci.io X-Gerrit-PatchSet: 3 Date: Fri, 25 Oct 2019 09:22:36 -0400 From: "Carlos O'Donell (Code Review)" To: Florian Weimer , libc-alpha@sourceware.org Auto-Submitted: auto-generated X-Gerrit-MessageType: newpatchset Subject: [review v3] localedef: Add verbose messages for failure paths. X-Gerrit-Change-Id: I28b9f680711ff00252a2cb15625b774cc58ecb9d X-Gerrit-Change-Number: 303 X-Gerrit-ChangeURL: X-Gerrit-Commit: ac93e61c44f3d72ca1b650c62831cdd24b3223c4 In-Reply-To: References: Reply-To: carlos@redhat.com, fweimer@redhat.com, libc-alpha@sourceware.org MIME-Version: 1.0 Content-Disposition: inline User-Agent: Gerrit/3.0.3 Message-Id: <20191025132236.BC08C204C9@gnutoolchain-gerrit.osci.io> Change URL: https://gnutoolchain-gerrit.osci.io/r/c/glibc/+/303 ...................................................................... localedef: Add verbose messages for failure paths. During testing of localedef running in a minimal container there were several error cases which were hard to diagnose since they appeared as strerror (errno) values printed by the higher level functions. This change adds three new verbose messages for potential failure paths. The new messages give the user the opportunity to use -v and display additional information about why localedef might be failing. I found these messages useful myself while writing a localedef container test for --no-hard-links. Change-Id: I28b9f680711ff00252a2cb15625b774cc58ecb9d --- A include/programs/xasprintf.h M locale/Makefile M locale/programs/localedef.c M locale/programs/localedef.h A locale/programs/xasprintf.c 5 files changed, 86 insertions(+), 25 deletions(-) diff --git a/include/programs/xasprintf.h b/include/programs/xasprintf.h new file mode 100644 index 0000000..c2de4e7 --- /dev/null +++ b/include/programs/xasprintf.h @@ -0,0 +1,23 @@ +/* asprintf with out of memory checking + Copyright (C) 2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . */ + +#ifndef _XASPRINTF_H +#define _XASPRINTF_H 1 + +extern int xasprintf (char **string_ptr, const char *format, ...); + +#endif /* xasprintf.h */ diff --git a/locale/Makefile b/locale/Makefile index 1a19b6f..943df35 100644 --- a/locale/Makefile +++ b/locale/Makefile @@ -56,7 +56,7 @@ localedef-aux := md5 locale-modules := locale locale-spec lib-modules := charmap-dir simple-hash xmalloc xstrdup \ - record-status + record-status xasprintf GPERF = gperf diff --git a/locale/programs/localedef.c b/locale/programs/localedef.c index 3dcf15f..30bc038 100644 --- a/locale/programs/localedef.c +++ b/locale/programs/localedef.c @@ -434,20 +434,15 @@ { case ARGP_KEY_HELP_EXTRA: /* We print some extra information. */ - if (asprintf (&tp, gettext ("\ + xasprintf (&tp, gettext ("\ For bug reporting instructions, please see:\n\ -%s.\n"), REPORT_BUGS_TO) < 0) - return NULL; - if (asprintf (&cp, gettext ("\ +%s.\n"), REPORT_BUGS_TO); + xasprintf (&cp, gettext ("\ System's directory for character maps : %s\n\ repertoire maps: %s\n\ locale path : %s\n\ %s"), - CHARMAP_PATH, REPERTOIREMAP_PATH, LOCALE_PATH, tp) < 0) - { - free (tp); - return NULL; - } + CHARMAP_PATH, REPERTOIREMAP_PATH, LOCALE_PATH, tp); return cp; default: break; @@ -518,15 +513,12 @@ '/'. */ ssize_t n; if (normal == NULL) - n = asprintf (&result, "%s%s/%s%c", output_prefix ?: "", - COMPLOCALEDIR, path, '\0'); + n = xasprintf (&result, "%s%s/%s%c", output_prefix ?: "", + COMPLOCALEDIR, path, '\0'); else - n = asprintf (&result, "%s%s/%.*s%s%s%c", - output_prefix ?: "", COMPLOCALEDIR, - (int) (startp - path), path, normal, endp, '\0'); - - if (n < 0) - return NULL; + n = xasprintf (&result, "%s%s/%.*s%s%s%c", + output_prefix ?: "", COMPLOCALEDIR, + (int) (startp - path), path, normal, endp, '\0'); endp = result + n - 1; } @@ -546,13 +538,23 @@ errno = 0; if (no_archive && euidaccess (result, W_OK) == -1) - /* Perhaps the directory does not exist now. Try to create it. */ - if (errno == ENOENT) - { - errno = 0; - if (mkdir (result, 0777) < 0) - return NULL; - } + { + /* Perhaps the directory does not exist now. Try to create it. */ + if (errno == ENOENT) + { + errno = 0; + if (mkdir (result, 0777) < 0) + { + record_verbose (stderr, + _("cannot create output path \"%s\": %s"), + result, strerror (errno)); + return NULL; + } + } + else + record_verbose (stderr, _("no write permission to output path: %s"), + strerror (errno)); + } *endp++ = '/'; *endp = '\0'; diff --git a/locale/programs/localedef.h b/locale/programs/localedef.h index 80da0b0..ad2a927 100644 --- a/locale/programs/localedef.h +++ b/locale/programs/localedef.h @@ -123,6 +123,7 @@ /* Prototypes for a few program-wide used functions. */ #include +#include /* Mark given locale as to be read. */ diff --git a/locale/programs/xasprintf.c b/locale/programs/xasprintf.c new file mode 100644 index 0000000..07b18d1 --- /dev/null +++ b/locale/programs/xasprintf.c @@ -0,0 +1,35 @@ +/* asprintf with out of memory checking + Copyright (C) 2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published + by the Free Software Foundation; version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see . */ + +#include +#include +#include +#include +#include + +int +xasprintf (char **string_ptr, const char *format, ...) +{ + va_list arg; + int ret; + va_start (arg, format); + ret = vasprintf (string_ptr, format, arg); + if (ret == -1) + error (EXIT_FAILURE, 0, _("memory exhausted")); + va_end (arg); + return ret; +}