#StackBounty: #terminal #bsd #locate #darwin "locate" returns relative instead of absolute paths

Bounty: 100

One advised me to post here since this would be more appropriate (I am going to remove the previous one).

On my MacBook, I have the following entry in root’s crontab:

0 */4 * * * cd /private/var/root && ./locate.updatedb

I have an alias for locate in ~/.zshrc:

alias locate='locate -d /var/db/locate.database'   

When I do a "locate Table_Synthesis_GREY_" , in all the full paths of files found, the first slash of the path indicating the file system root is missing. Here is a sample of the ouput:

Users/henry/Projects/JUNK_AND_OLD/Travail_9_Paper_Combinaison_et_Methode_des_biais_communs_pour_ma_These_SAUVEGARDES/TABLES_FLAT_AND_NON_FLAT_to_include_into_my_Phd_paper_LAST/Table_Synthesis_Main_05_AOUT_2020/temp/Table_Synthesis_GREY_IMPORTANT_SAVE_5_AOUT_2020.run.xml
Users/henry/Projects/JUNK_AND_OLD/Travail_9_Paper_Combinaison_et_Methode_des_biais_communs_pour_ma_These_SAUVEGARDES/TABLES_FLAT_AND_NON_FLAT_to_include_into_my_Phd_paper_LAST/Table_Synthesis_Main_05_AOUT_2020/temp/Table_Synthesis_GREY_IMPORTANT_SAVE_5_AOUT_2020.tex
Users/henry/Projects/JUNK_AND_OLD/Travail_9_Paper_Combinaison_et_Methode_des_biais_communs_pour_ma_These_SAUVEGARDES/TABLES_FLAT_AND_NON_FLAT_to_include_into_my_Phd_paper_LAST/Table_Synthesis_Main_05_AOUT_2020/temp/Table_Synthesis_GREY_IMPORTANT_SAVE_5_AOUT_2020.zip
Users/henry/Projects/JUNK_AND_OLD/Travail_9_Paper_Combinaison_et_Methode_des_biais_communs_pour_ma_These_SAUVEGARDES/TABLES_FLAT_AND_NON_FLAT_to_include_into_my_Phd_paper_LAST/Table_Synthesis_Main_05_AOUT_2020/temp/Table_Synthesis_GREY_IMPORTANT_SAVE_5_AOUT_2020_ORIGINAL_INTO_zip_here.pdf
Users/henry/Projects/JUNK_AND_OLD/Travail_9_Paper_Combinaison_et_Methode_des_biais_communs_pour_ma_These_SAUVEGARDES/TABLES_FLAT_AND_NON_FLAT_to_include_into_my_Phd_paper_LAST/Table_Synthesis_Main_AFTER_05_AOUT_2020/FLAT_Synthesis/Table_Synthesis_GREY_FLAT.docx

How can I have paths output as absolute paths, i.e. starting at /Users? The desired output is

/Users/henry/Projects/JUNK_AND_OLD/Travail_9_Paper_Combinaison_et_Methode_des_biais_communs_pour_ma_These_SAUVEGARDES/TABLES_FLAT_AND_NON_FLAT_to_include_into_my_Phd_paper_LAST/Table_Synthesis_Main_AFTER_05_AOUT_2020/FLAT_Synthesis/Table_Synthesis_GREY_FLAT.docx

as opposed to

Users/henry/Projects/JUNK_AND_OLD/Travail_9_Paper_Combinaison_et_Methode_des_biais_communs_pour_ma_These_SAUVEGARDES/TABLES_FLAT_AND_NON_FLAT_to_include_into_my_Phd_paper_LAST/Table_Synthesis_Main_AFTER_05_AOUT_2020/FLAT_Synthesis/Table_Synthesis_GREY_FLAT.docx

UPDATE 1: I have copied /usr/libexec/locate.updatedb into my home root, that is in /var/root/ . I did that since I could not modify the /usr/libexec/locate.updatedb file, even being root or with sudo.

So in this other file /var/root/locate.updatedb, I could do the modification :

if [ "$(id -u)" = "99" ]; then

instead of initially :

if [ "$(id -u)" = "0" ]; then

What do you think about this trick ? and mostly why I have not the first slash displayed when results into my /Users/henry/.

0 */4 * * * cd /private/var/root ; /bin/sh locate.updatedb

but issue remains.

By the way, if results are located into /Applications or other main directories but not systematically, the first slash is well displayed.

The content of script is not very big, here is :

# $FreeBSD: src/usr.bin/locate/locate/updatedb.sh,v 1.20 2005/11/12 12:45:08 grog Exp $

if [ "$(id -u)" = "99" ]; then
    rc=0
    export FCODES=`sudo -u nobody mktemp -t updatedb`
    chown nobody $FCODES
    tmpdb=`su -fm nobody -c "$0"` || rc=1
    if [ $rc = 0 ]; then
        install -m 0444 -o nobody -g wheel $FCODES /var/db/locate.database
    fi
    rm $FCODES
    exit $rc
fi
: ${LOCATE_CONFIG="/etc/locate.rc"}
if [ -f "$LOCATE_CONFIG" -a -r "$LOCATE_CONFIG" ]; then
       . $LOCATE_CONFIG
fi

# The directory containing locate subprograms
: ${LIBEXECDIR:=/usr/libexec}; export LIBEXECDIR
: ${TMPDIR:=/tmp}; export TMPDIR
if ! TMPDIR=`mktemp -d $TMPDIR/locateXXXXXXXXXX`; then
    exit 1
fi

PATH=$LIBEXECDIR:/bin:/usr/bin:$PATH; export PATH

# 6497475
set -o noglob

: ${mklocatedb:=locate.mklocatedb}      # make locate database program
: ${FCODES:=/var/db/locate.database}    # the database
: ${SEARCHPATHS:="/"}                   # directories to be put in the database
: ${PRUNEPATHS:="/private/tmp /private/var/folders /private/var/tmp */Backups.backupdb"} # unwanted directories
: ${FILESYSTEMS:="hfs ufs apfs"}        # allowed filesystems
: ${find:=find}

case X"$SEARCHPATHS" in 
    X) echo "$0: empty variable SEARCHPATHS"; exit 1;; esac
case X"$FILESYSTEMS" in 
    X) echo "$0: empty variable FILESYSTEMS"; exit 1;; esac

# Make a list a paths to exclude in the locate run
excludes="! (" or=""
for fstype in $FILESYSTEMS
do
    excludes="$excludes $or -fstype $fstype"
    or="-or"
done
excludes="$excludes ) -prune"

case X"$PRUNEPATHS" in
    X) ;;
    *) for path in $PRUNEPATHS
    do
        excludes="$excludes -or -path $path -prune"
    done;;
esac

# Ignore the target of firmlinks
while read firmlink; do
    excludes="$excludes -or -path $firmlink -prune"
done <<< "$(awk -F't' '{print "/System/Volumes/Data/" $2}' /usr/share/firmlinks)"

tmp=$TMPDIR/_updatedb$$
trap 'rm -f $tmp; rmdir $TMPDIR; exit' 0 1 2 3 5 10 15

# search locally
# echo $find $SEARCHPATHS $excludes -or -print && exit
if $find -s $SEARCHPATHS $excludes -or -print 2>/dev/null |
        $mklocatedb -presort > $tmp
then
    case X"`$find $tmp -size -257c -print`" in
        X) cat $tmp > $FCODES;;
        *) echo "updatedb: locate database $tmp is empty"
           exit 1
    esac
fi

Anyone has a clue ?


Get this bounty!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.