On 13 Feb 2022, at 17:05, Wolfram Schneider <[email protected]> wrote:
> The branch main has been updated by wosch:
> 
> URL: 
> https://cgit.FreeBSD.org/src/commit/?id=93885bb04182c104be1ec410a5ccb105f1ec5ff2
> 
> commit 93885bb04182c104be1ec410a5ccb105f1ec5ff2
> Author:     Wolfram Schneider <[email protected]>
> AuthorDate: 2022-02-13 17:00:22 +0000
> Commit:     Wolfram Schneider <[email protected]>
> CommitDate: 2022-02-13 17:00:22 +0000
> 
>    Better help message if locate database does not exists
> 
>    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(-)
> 
> 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);
> 
> int
> main(int argc, char **argv)
> @@ -216,7 +218,6 @@ main(int argc, char **argv)
>         exit(0);
> }
> 
> -
> /*
>  * Arguments:
>  * db database
> @@ -235,8 +236,16 @@ search_fopen(char *db, char **s)
>                       *(s+1) = NULL;
>               }
>       } 
> -     else if ((fp = fopen(db, "r")) == NULL)
> -             err(1,  "`%s'", db);
> +     else { 
> +             if (!check_size(db))
> +                     exit(1);
> +
> +             if ((fp = fopen(db, "r")) == NULL) {
> +                     warn("`%s'", db);
> +                     rebuild_message(db);
> +                     exit(1);
> +             }
> +     }
> 
>       /* count only chars or lines */
>       if (f_statistic) {
> @@ -261,6 +270,7 @@ search_fopen(char *db, char **s)
> } 
> 
> #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 = open(db, O_RDONLY)) == -1 ||
> -         fstat(fd, &sb) == -1)
> -             err(1, "`%s'", db);
> +
> +     if (!check_size(db))
> +             exit(1);
> +
> +     if (stat(db, &sb) == -1)
> +             err(1, "stat");
> +
>       len = sb.st_size;
> -     if (len < (2*NBG))
> -             errx(1,
> -                 "database too small: %s\nRun /usr/libexec/locate.updatedb",
> -                 db);
> +
> +     if ((fd = open(db, O_RDONLY)) == -1) {
> +             warn("%s", db);
> +             rebuild_message(db);
> +             exit(1);
> +        }
> 
>       if ((p = 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>
> 
> #include "locate.h"
> +#include "pathnames.h"
> 
> 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) == 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) 
> +{
> +        struct stat sb;
> +        off_t len;
> +
> +     if (stat(db, &sb) == -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 = 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


Reply via email to