diff --git a/usr.bin/locate/locate/locate.c b/usr.bin/locate/locate/locate.c --- a/usr.bin/locate/locate/locate.c +++ b/usr.bin/locate/locate/locate.c @@ -124,6 +124,8 @@ 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 @@ exit(0); } - /* * Arguments: * db database @@ -235,8 +236,16 @@ *(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 @@ } #ifdef MMAP + /* * Arguments: * db database @@ -273,14 +283,20 @@ 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 --- a/usr.bin/locate/locate/util.c +++ b/usr.bin/locate/locate/util.c @@ -41,8 +41,10 @@ #include #include #include +#include #include "locate.h" +#include "pathnames.h" char **colon(char **, char*, char*); char *patprep(char *); @@ -268,3 +270,37 @@ } return(word); } + + +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); + rebuild_message(db); + return(0); + } + len = sb.st_size; + + if (len < (2 * NBG)) { + warnx("the locate database '%s' is less than %d bytes large.", db, (2 * NBG)); + rebuild_message(db); + return(0); + } + + return(1); +} +