Date: Sun, 13 Feb 2022 17:26:33 +0000 From: Jessica Clarke <jrtc27@freebsd.org> To: Wolfram Schneider <wosch@FreeBSD.org> Cc: "src-committers@freebsd.org" <src-committers@FreeBSD.org>, "dev-commits-src-all@freebsd.org" <dev-commits-src-all@FreeBSD.org>, "dev-commits-src-main@freebsd.org" <dev-commits-src-main@FreeBSD.org> Subject: Re: git: 93885bb04182 - main - Better help message if locate database does not exists Message-ID: <38127DA5-9FEC-416F-8F1B-97CBA5D070CA@freebsd.org> In-Reply-To: <202202131705.21DH5XG4094170@gitrepo.freebsd.org> References: <202202131705.21DH5XG4094170@gitrepo.freebsd.org>
next in thread | previous in thread | raw e-mail | index | archive | help
On 13 Feb 2022, at 17:05, Wolfram Schneider <wosch@FreeBSD.org> wrote: > The branch main has been updated by wosch: >=20 > URL: = https://cgit.FreeBSD.org/src/commit/?id=3D93885bb04182c104be1ec410a5ccb105= f1ec5ff2 >=20 > commit 93885bb04182c104be1ec410a5ccb105f1ec5ff2 > Author: Wolfram Schneider <wosch@FreeBSD.org> > AuthorDate: 2022-02-13 17:00:22 +0000 > Commit: Wolfram Schneider <wosch@FreeBSD.org> > CommitDate: 2022-02-13 17:00:22 +0000 >=20 > Better help message if locate database does not exists >=20 > PR: 211501 > Reported by: Oliver Peter > Reviewed by: Pau Amma > Differential Revision: https://reviews.freebsd.org/D34243 > --- > usr.bin/locate/locate/locate.c | 36 = ++++++++++++++++++++++++++---------- > usr.bin/locate/locate/util.c | 36 = ++++++++++++++++++++++++++++++++++++ > 2 files changed, 62 insertions(+), 10 deletions(-) >=20 > diff --git a/usr.bin/locate/locate/locate.c = b/usr.bin/locate/locate/locate.c > index 0bbd95ac696d..014fa7bcc301 100644 > --- a/usr.bin/locate/locate/locate.c > +++ b/usr.bin/locate/locate/locate.c > @@ -124,6 +124,8 @@ extern int getwf(FILE *); > extern u_char *tolower_word(u_char *); > extern int check_bigram_char(int); > extern char *patprep(char *); > +extern void rebuild_message(char *db); > +extern int check_size(char *db); >=20 > int > main(int argc, char **argv) > @@ -216,7 +218,6 @@ main(int argc, char **argv) > exit(0); > } >=20 > - > /* > * Arguments: > * db database > @@ -235,8 +236,16 @@ search_fopen(char *db, char **s) > *(s+1) =3D NULL; > } > }=20 > - else if ((fp =3D fopen(db, "r")) =3D=3D NULL) > - err(1, "`%s'", db); > + else {=20 > + if (!check_size(db)) > + exit(1); > + > + if ((fp =3D fopen(db, "r")) =3D=3D NULL) { > + warn("`%s'", db); > + rebuild_message(db); > + exit(1); > + } > + } >=20 > /* count only chars or lines */ > if (f_statistic) { > @@ -261,6 +270,7 @@ search_fopen(char *db, char **s) > }=20 >=20 > #ifdef MMAP > + Stray blank line; closing #endif is flush with }. > /* > * Arguments: > * db database > @@ -273,14 +283,20 @@ search_mmap(char *db, char **s) > int fd; > caddr_t p; > off_t len; > - if ((fd =3D open(db, O_RDONLY)) =3D=3D -1 || > - fstat(fd, &sb) =3D=3D -1) > - err(1, "`%s'", db); > + > + if (!check_size(db)) > + exit(1); > + > + if (stat(db, &sb) =3D=3D -1) > + err(1, "stat"); > + > len =3D sb.st_size; > - if (len < (2*NBG)) > - errx(1, > - "database too small: %s\nRun = /usr/libexec/locate.updatedb", > - db); > + > + if ((fd =3D open(db, O_RDONLY)) =3D=3D -1) { > + warn("%s", db); > + rebuild_message(db); > + exit(1); > + } >=20 > if ((p =3D mmap((caddr_t)0, (size_t)len, > PROT_READ, MAP_SHARED, > diff --git a/usr.bin/locate/locate/util.c = b/usr.bin/locate/locate/util.c > index 1d15f83b6826..aba90b1deda7 100644 > --- a/usr.bin/locate/locate/util.c > +++ b/usr.bin/locate/locate/util.c > @@ -41,8 +41,10 @@ > #include <err.h> > #include <arpa/inet.h> > #include <stdio.h> > +#include <sys/stat.h> >=20 > #include "locate.h" > +#include "pathnames.h" >=20 > char **colon(char **, char*, char*); > char *patprep(char *); > @@ -268,3 +270,37 @@ getwf(fp) > } > return(word); > } > + > + Stray blank line. > +void > +rebuild_message(char *db) > +{ > + /* only for the default locate database */ > + if (strcmp(_PATH_FCODES, db) =3D=3D 0) { > + fprintf(stderr, "\nTo create a new database, please run = the following command as root:\n\n"); > + fprintf(stderr, " = /etc/periodic/weekly/310.locate\n\n"); > + } > +} > + > +int > +check_size(char *db)=20 > +{ > + struct stat sb; > + off_t len; > + > + if (stat(db, &sb) =3D=3D -1) { > + warnx("the locate database '%s' does not exists.", db); stat(2) can fail for other reasons. But also s/exists/exist/. > + rebuild_message(db); > + return(0); return (0); > + } > + len =3D sb.st_size; > + > + if (len < (2 * NBG)) { > + warnx("the locate database '%s' is less than %d bytes = large.", db, (2 * NBG)); Sounds clumsy; "... is smaller than %d bytes."? > + rebuild_message(db); > + return(0); return (0); > + } > + > + return(1); return (1); > +} > + Stray blank line? Jess
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?38127DA5-9FEC-416F-8F1B-97CBA5D070CA>