Bug 211501

Summary: locate(1): Misleading help message
Product: Base System Reporter: Oliver Peter <lists>
Component: miscAssignee: Wolfram Schneider <wosch>
Status: Closed FIXED    
Severity: Affects Only Me CC: pauamma, wosch
Priority: --- Keywords: patch
Version: CURRENT   
Hardware: Any   
OS: Any   
URL: https://reviews.freebsd.org/D34243

Description Oliver Peter 2016-08-01 12:19:03 UTC
Using locate(1) on a fresh install prints misleading and slightly dangerous message:

[root@host:~]# locate emacs
locate: database too small: /var/db/locate.database
Run /usr/libexec/locate.updatedb
[root@host:~]# /usr/libexec/locate.updatedb
>>> WARNING
>>> Executing updatedb as root.  This WILL reveal all filenames
>>> on your machine to all login users, which is a security risk.

It would be better to link to the periodic job in /etc:


[root@host:~]# /etc/periodic/weekly/310.locate 

Rebuilding locate database:

diff --git a/usr.bin/locate/locate/locate.c b/usr.bin/locate/locate/locate.c
index 3a87b14..a6aa8de 100644
--- a/usr.bin/locate/locate/locate.c
+++ b/usr.bin/locate/locate/locate.c
@@ -293,7 +293,7 @@ search_mmap(db, s)
        len = sb.st_size;
        if (len < (2*NBG))
                errx(1,
-                   "database too small: %s\nRun /usr/libexec/locate.updatedb",
+                   "database too small: %s\nRun /etc/periodic/weekly/310.locate",
                    db);
 
        if ((p = mmap((caddr_t)0, (size_t)len,
Comment 1 Wolfram Schneider freebsd_committer freebsd_triage 2017-10-15 08:11:51 UTC
I agree that the error message is misleading. 

First, the locate program should make a distinction between an empty database (zero bytes) and a corrupt database (>0 && <256 bytes).

Then it should suggest to rebuild the database as the cronjob would do.
Comment 2 Oliver Peter 2018-01-09 12:26:15 UTC
(In reply to Wolfram Schneider from comment #1)

Empty testcase:
  backup locate # rm /var/db/locate.database
  backup locate # touch /var/db/locate.database
  backup locate # ./locate emacs
  locate: database empty: /var/db/locate.database
  Run /etc/periodic/weekly/310.locate                                                                                                                                         

Corruption testcase:
  backup locate # echo foobar >> /var/db/locate.database
  backup locate # ./locate emacs
  locate: database corrupted: /var/db/locate.database
  Run /etc/periodic/weekly/310.locate

backup src # git --no-pager diff
diff --git a/usr.bin/locate/locate/locate.c b/usr.bin/locate/locate/locate.c
index 31b75efd1b0..4e2aa161587 100644
--- a/usr.bin/locate/locate/locate.c
+++ b/usr.bin/locate/locate/locate.c
@@ -297,9 +297,13 @@ search_mmap(char *db, char **s)
            fstat(fd, &sb) == -1)
                err(1, "`%s'", db);
        len = sb.st_size;
-       if (len < (2*NBG))
+       if (len > 0 && len < (2*NBG))
                errx(1,
-                   "database too small: %s\nRun /usr/libexec/locate.updatedb",
+                   "database corrupted: %s\nRun /etc/periodic/weekly/310.locate",
+                   db);
+       if (len == 0)
+               errx(1,
+                   "database empty: %s\nRun /etc/periodic/weekly/310.locate",
                    db);
 
        if ((p = mmap((caddr_t)0, (size_t)len,
Comment 3 commit-hook freebsd_committer freebsd_triage 2022-02-13 17:06:04 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=93885bb04182c104be1ec410a5ccb105f1ec5ff2

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

    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(-)