Tiny Core Base > TCB Tips & Tricks

How to find which extension provides a file

<< < (4/14) > >>

patrikg:
Sorry for not getting the performance working with sqlite3 :( ---
Tried to get it working so the command line should report the performance.

I can't getting it working with the ".timer on" with the command line version of sqlite3.
The ".timer on" doesn't report the speed sadly when executing from the command line.

But if you get into the interactive mode of sqlite3 it just works so you get a report how many seconds the question took.

It says in "man" you could supply the -cmd ".timer on" but it doesn't work for me, but some another commands like -cmd ".help" works out of the box.

And if "you" choose sqlite3 you could also store some more metadata like version, date and so on.

Maybe also store ldd output of the executable, so you can easily see the dependency's.

Rich:
Hi nick65go

--- Quote from: nick65go on November 15, 2024, 09:34:49 AM --- ... OR, if the speed is not the main goal (but nice for multi-CPU core) then maybe the user confort for seach parameters (GUI options will be nice also). But of course, the pro[fessionals] already know the use of "" and back-quotes, etc.
--- End quote ---
I think backwards compatibility and user friendly search parameters
should be the primary goals.

I did a rewrite of provides.sh as follows:
1. The default search mode is unchanged.
2. No backslashes to enter. The script adds them if needed.
3. No quoting of search term.
4. End of search term can be marked with an anchor ($).
5. Added a brief usage message.
6. This works for both the command line and the GUI.

Examples:
Search for plain file name:

--- Code: ---tc@E310:~/Scripting/Provides$ ./provides.sh grep | wc -l
51
tc@E310:~/Scripting/Provides$
--- End code ---

Search for file name with leading slash:

--- Code: ---tc@E310:~/Scripting/Provides$ ./provides.sh /grep
boost-1.65-dev.tcz
boost-dev.tcz
emacs.cedet.tcz
emacs.doc.tcz
emacs.minimal.tcz
emacs.prog.tcz
fpc-src.tcz
fpc.tcz
grep.tcz
kompozer.tcz
libreoffice.tcz
mariadb-test.tcz
python3.6.tcz
tc@E310:~/Scripting/Provides$
--- End code ---

Search for file name with partial path:

--- Code: ---tc@E310:~/Scripting/Provides$ ./provides.sh bin/grep
emacs.minimal.tcz
fpc.tcz
grep.tcz
tc@E310:~/Scripting/Provides$
--- End code ---

Search for file name with partial path and trailing anchor:

--- Code: ---tc@E310:~/Scripting/Provides$ ./provides.sh bin/grep$
grep.tcz
tc@E310:~/Scripting/Provides$
--- End code ---

Added help message:

--- Code: ---tc@E310:~/Scripting/Provides$ ./provides.sh --help

Finds extension(s) that provide a filename.
Filenames in list being searched include full paths, for example:
        usr/local/bin/grep

Usage:
        provides.sh FileName

Examples:
        This also finds all extensions that include local in their pathes.
        provides.sh cal

        This finds *bin/cal*. Including sbin/cal, bin/calibrate, etc.
        provides.sh bin/cal

        This finds *bin/cal. Including sbin/cal, usr/bin/cal, etc.
        provides.sh bin/cal$

The more specific the search, the fewer the results returned.


tc@E310:~/Scripting/Provides$
--- End code ---

And the script itself:

--- Code: ---#!/bin/busybox ash
. /etc/init.d/tc-functions
useBusybox

# --------------------------------------------------------------------------- #
Usage()
{
        echo "
Finds extension(s) that provide a filename.
Filenames in list being searched include full paths, for example:
usr/local/bin/grep

Usage:
        ${0##*/} FileName

Examples:
This also finds all extensions that include local in their pathes.
        ${0##*/} cal

This finds *bin/cal*. Including sbin/cal, bin/calibrate, etc.
        ${0##*/} bin/cal

This finds *bin/cal. Including sbin/cal, usr/bin/cal, etc.
        ${0##*/} bin/cal$

The more specific the search, the fewer the results returned.

"
        exit
}
# --------------------------------------------------------------------------- #



# See if user needs help.
case "$1" in
        -h) Usage;;
        -help) Usage;;
        --help) Usage;;
        "") exit 1;;
esac

# Flag to indicate whether we want an exact match. 0=No  1=Yes.
Exact=0

TARGET="$1"

TCEDIR="/etc/sysconfig/tcedir"
DB="provides.db"

getMirror
cd "$TCEDIR"
if zsync -i "$TCEDIR"/"$DB" -q "$MIRROR"/"$DB".zsync
then
rm -f "$DB".zs-old
else
if [ ! -f "$TCEDIR"/"$DB" ]
then
  wget -O "$TCEDIR"/"$DB".gz "$MIRROR"/"$DB".gz
  gunzip "$TCEDIR"/"$DB".gz
fi
fi
chmod g+rw "$TCEDIR"/"$DB"
cd - > /dev/null

# Save string length of TARGET.
Length=${#TARGET}

# Remove trailing $ (exact match request) if present.
TARGET="${TARGET%$}"

# If TARGET is now shorter, exact match was requested.
[ ${#TARGET} -lt $Length ] && Exact=1

# Replace all instances of / with \/.
TARGET="${TARGET//\//\\\/}"

if [ $Exact -eq 0 ]
then
awk 'BEGIN {FS="\n";RS=""} /'${TARGET}'/{print $1}' "$TCEDIR"/"$DB"
else
awk 'BEGIN {FS="\n";RS=""} /'${TARGET}'\n/||/'${TARGET}'$/{print $1}' "$TCEDIR"/"$DB"
fi
--- End code ---

polikuo:
Hi, Rich.

Do you think its possible to implement some checker so that Provides.sh only sync with the repo if the db file is older than ... say a day ?
Syncing takes a lot of time.
I don't know the exact syntax, but "find -mtime" should work ?
Perhaps you could wrap up the syncing code into a shell function ?

Rich:
Hi polikuo
A quick read of the man page suggests that only the provides.db.zsync
file should get downloaded and only if it changed since the last download.

This a time based version I used to use:

--- Code: ---# Maximum age of  provides.db  in seconds before it is considered to be stale and
# needs to be redownloaded.
MaxAge=3600

PROVIDES="provides.db"


RefreshProvidesDB()
{
# This downloads a fresh copy of provides.db if any of the following are true:
# 1. The file is not in the current directory.
# 2. The file is older than 1 hour (3600 seconds).

cd /etc/sysconfig/tcedir

# Make sure the proper  provides.db  exists.
if [ -f "$PROVIDES" ]
then
        # Compute number of seconds since provides.db modified (downloaded).
        Age=$(( $(date +%s) - $(date -r "$PROVIDES"  +%s) ))
        if [ $Age -lt $MaxAge ]
        then
                # File is recent enough to use.
                return
        fi
        # File is too old, delete it.
        rm "$PROVIDES"
fi

# Fetch a fresh copy of the file.
wget -q -O "$PROVIDES" "$URL$PROVIDES"
if [ $? -ne 0 ]
then
        echo "Download failed for: $URL$PROVIDES"
        exit 1
fi

# Make sure it has a current timestamp.
touch "$PROVIDES"

cd - > /dev/null

}

--- End code ---

Rich:
Hi polikuo

--- Quote from: polikuo on November 15, 2024, 02:02:41 PM --- ... Perhaps you could wrap up the syncing code into a shell function ?
--- End quote ---
Done. :)


--- Code: ---#!/bin/busybox ash
. /etc/init.d/tc-functions
useBusybox

# --------------------------------------------------------------------------- #
UpdateProvidesDB()
{
getMirror
cd "$TCEDIR"
if zsync -i "$TCEDIR"/"$DB" -q "$MIRROR"/"$DB".zsync
then
rm -f "$DB".zs-old
else
if [ ! -f "$TCEDIR"/"$DB" ]
then
  wget -O "$TCEDIR"/"$DB".gz "$MIRROR"/"$DB".gz
  gunzip "$TCEDIR"/"$DB".gz
fi
fi
chmod g+rw "$DB"
cd - > /dev/null
}
# --------------------------------------------------------------------------- #

# --------------------------------------------------------------------------- #
Usage()
{
        echo "
Finds extension(s) that provide a filename.
Filenames in list being searched include full paths, for example:
usr/local/bin/grep

Usage:
        ${0##*/} FileName

Examples:
This also finds all extensions that include local in their pathes.
        ${0##*/} cal

This finds *bin/cal*. Including sbin/cal, bin/calibrate, etc.
        ${0##*/} bin/cal

This finds *bin/cal. Including sbin/cal, usr/bin/cal, etc.
        ${0##*/} bin/cal$

The more specific the search, the fewer the results returned.

"
        exit
}
# --------------------------------------------------------------------------- #



# See if user needs help.
case "$1" in
        -h) Usage;;
        -help) Usage;;
        --help) Usage;;
        "") exit 1;;
esac

# Flag to indicate whether we want an exact match. 0=No  1=Yes.
Exact=0

TARGET="$1"

TCEDIR="/etc/sysconfig/tcedir"
DB="provides.db"

# Run zsync on the provides.db file.
UpdateProvidesDB

# Save string length of TARGET.
Length=${#TARGET}

# Remove trailing $ (exact match request) if present.
TARGET="${TARGET%$}"

# If TARGET is now shorter, exact match was requested.
[ ${#TARGET} -lt $Length ] && Exact=1

# Replace all instances of / with \/.
TARGET="${TARGET//\//\\\/}"

if [ $Exact -eq 0 ]
then
awk 'BEGIN {FS="\n";RS=""} /'${TARGET}'/{print $1}' "$TCEDIR"/"$DB"
else
awk 'BEGIN {FS="\n";RS=""} /'${TARGET}'\n/||/'${TARGET}'$/{print $1}' "$TCEDIR"/"$DB"
fi
--- End code ---

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version