Bug 211501 - locate(1): Misleading help message
Summary: locate(1): Misleading help message
Status: Open
Alias: None
Product: Base System
Classification: Unclassified
Component: misc (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: Wolfram Schneider
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2016-08-01 12:19 UTC by Oliver Peter
Modified: 2018-01-09 12:26 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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 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,