Tiny Core Linux

Tiny Core Base => TCB Tips & Tricks => Topic started by: Rich on November 13, 2024, 09:53:34 AM

Title: How to find which extension provides a file
Post by: Rich on November 13, 2024, 09:53:34 AM
Hi gadget42
... when i use the "provides" function and search for "ps" it reports many results. how would the uninitiated decide which to use? ...
Good question.

The following was performed on TC10 x86 using the command line for
provides instead of the GUI. The only difference is the command line
requires quotes and the GUI can not handle quotes.

Trying to find which extension provides a program can be a frustrating
experience, especially with short names like ps or dd which will return
many results. A search for ps returned 580 results:
Code: [Select]
tc@E310:~$ provides.sh ps | wc -l
580
tc@E310:~$

The reason for so many matches is provides searches for a match anywhere
in the string, not an exact match. Searching for the calendar program called
cal returns 2661 results:
Code: [Select]
tc@E310:~$ provides.sh cal | wc -l
2661
tc@E310:~$
That's because it's also matching on every  usr/local/  string in the provides.db
file. That's important because we can use that to create a more precise match by
including some path information.

We know cal is a program so it's probably in a bin or sbin directory. We can cover
both cases with a search like this:
Code: [Select]
tc@E310:~$ provides.sh "bin\/cal"
ax25-apps.tcz
fox-apps.tcz
fox.tcz
util-linux.tcz
valgrind.tcz
xastir.tcz
tc@E310:~$
Notice we need to escape the path separator  "/"  with a backslash  "\".
As I said earlier, command line, use quotes. GUI, don't use quotes.

Let's see what happens for ps:
Code: [Select]
tc@E310:~$ provides.sh "bin\/ps"
aspell-dev.tcz
ghostscript.tcz
gnutls3.6.tcz
gnutls.tcz
lcms2.tcz
libcap-ng.tcz
libpsl.tcz
pax-utils.tcz
postgresql-10-client.tcz
postgresql-10.tcz
postgresql-11-client.tcz
postgresql-11.tcz
postgresql-12-client.tcz
postgresql-12.tcz
postgresql-9.5-client.tcz
postgresql-9.5.tcz
postgresql-9.6-client.tcz
postgresql-9.6.tcz
procps-ng.tcz
procps.tcz
pstree.tcz
putty.tcz
sc.tcz
tc@E310:~$
23 results.

Lets lengthen the path a little:
Code: [Select]
tc@E310:~$ provides.sh "local\/bin\/ps"
aspell-dev.tcz
ghostscript.tcz
gnutls3.6.tcz
gnutls.tcz
lcms2.tcz
libcap-ng.tcz
libpsl.tcz
pax-utils.tcz
procps-ng.tcz
procps.tcz
pstree.tcz
putty.tcz
sc.tcz
tc@E310:~$
That's a little better.

The previous search was picking up this from the postgresql extensions:
/usr/local/pgsql10/bin/psql


That brings me to another point.
Some extensions have another sub-directory between local and bin, sbin, or lib
as you just saw:
/usr/local/pgsql10/bin/psql

Just something to keep in mind when performing searches.
Title: Re: How to find which extension provides a file
Post by: Leee on November 13, 2024, 05:08:01 PM
While browsing through provides.db, I note that most file paths start with /usr/ but in a few extensions they start with usr/ or ./usr/Is this legit? irrelevant? indicative of a problem?
examples:  base-locale.tcz uses ./usr/  bash-completeion.tcz uses usr/
Title: Re: How to find which extension provides a file
Post by: Rich on November 13, 2024, 09:47:48 PM
Hi Leee
Yes, I've noticed that too. Whether tce-load does a copy to file
system or creates symlinks to the file system, the destination
is always "/".

That would translate to  //usr/ , /usr/ , or /./usr/ , all of which resolve
to the same location. Extra consecutive slashes are ignored. The
"./" means the current directory. So "/./" becomes root "current directory"
which is still the root directory. Hope that makes sense. Try this:
Code: [Select]
cd /./then do a directory listing and you'll see you're in the root directory.
Title: Re: How to find which extension provides a file
Post by: Rich on November 14, 2024, 01:01:01 AM
Hi gadget42
I've created a modified provides.sh script and copied it
to /usr/bin/provides.sh. Then I did:
Code: [Select]
sudo ln -rs /usr/bin/provides.sh /usr/bin/Provides.sh
tc@E310:~/Scripting/Provides$ ls -l /usr/bin/Provides.sh
lrwxrwxrwx 1 root root 11 Nov 14 00:02 /usr/bin/Provides.sh -> provides.sh

When called as provides.sh, it behaves as it always has, so the Apps GUI
won't know the difference.

When called as Provides.sh, it behaves like an anchor was added to
the end of the search term. So a search for ps would reject ps2pdf
because it does not end in ps.

Here is the search for ps repeated with both versions:
Code: [Select]
tc@E310:~/Scripting/Provides$ time provides.sh "\/local\/bin\/ps"
aspell-dev.tcz
ghostscript.tcz
gnutls3.6.tcz
gnutls.tcz
lcms2.tcz
libcap-ng.tcz
libpsl.tcz
pax-utils.tcz
procps-ng.tcz
procps.tcz
pstree.tcz
putty.tcz
sc.tcz
real    0m 1.81s
user    0m 0.90s
sys     0m 0.18s

Code: [Select]
tc@E310:~/Scripting/Provides$ time Provides.sh "\/local\/bin\/ps"
procps-ng.tcz
procps.tcz
real    0m 1.92s
user    0m 1.00s
sys     0m 0.20s

Here is the search for cal repeated with both versions:
Code: [Select]
tc@E310:~/Scripting/Provides$ time provides.sh "\/local\/bin\/cal"
ax25-apps.tcz
fox-apps.tcz
fox.tcz
util-linux.tcz
valgrind.tcz
xastir.tcz
real    0m 1.99s
user    0m 0.86s
sys     0m 0.24s

Code: [Select]
tc@E310:~/Scripting/Provides$ time Provides.sh "\/local\/bin\/cal"
util-linux.tcz
real    0m 1.96s
user    0m 0.98s
sys     0m 0.23s

This is the modified provides.sh:
Code: [Select]
#!/bin/busybox ash
. /etc/init.d/tc-functions
useBusybox

TARGET="$1"
[ -z "$TARGET" ] && exit 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
cd - > /dev/null

if [ "${0##*/}" == "provides.sh" ]
then
awk 'BEGIN {FS="\n";RS=""} /'${TARGET}'/{print $1}' "$TCEDIR"/"$DB"
else
awk -v Find="$TARGET" -v FindEnd="$TARGET$" 'BEGIN {FS="\n";RS=""} {if ( $0 ~ Find ) { for (i=2; i <= NF; i++) { if ( $i ~ FindEnd ) {print $1; break} } } }' "$TCEDIR"/"$DB"
fi

chmod g+rw "$TCEDIR"/"$DB"

What I don't understand is the purpose of the chmod command
at the end that executes after the script has finished running.
Title: Re: How to find which extension provides a file
Post by: polikuo on November 14, 2024, 02:56:08 AM
Hi

This is what I do in my systems.
Code: [Select]
#!/bin/sh
[ -z "$1" ] && exit 1

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

awk 'BEGIN {FS="\n";RS=""} /'"$1"'/{print $1}' "$TCEDIR"/"$DB"

I call it /usr/local/bin/prov, because it's a shrunken version of provides.sh that doesn't sync with the repo. (syncing takes time, especially if you're searching frequently.)

While searching, you can add "\n" to the end of the query.
Code: [Select]
tc@pi4:~$ prov 'bin\/ps\n'
procps-ng.tcz

You can do the same with the regular provides.sh (or the app browser) as well.

What I don't understand is the purpose of the chmod command
at the end that executes after the script has finished running.

I believe it was meant to make sure the file is modifiable by any user to sync with the repo.
Title: Re: How to find which extension provides a file
Post by: Rich on November 14, 2024, 11:28:21 AM
Hi polikuo
... While searching, you can add "\n" to the end of the query. ...
I was so focused figuring out how to include $ as an anchor that I
forgot to try \n. Thank you for pointing out the obvious and getting
me back on track.

Quote
... I believe it was meant to make sure the file is modifiable by any user to sync with the repo.
Makes sense. It just seemed like an odd place to put that line.

Looks like I spoke too soon. It seems using \n does not work if the item
you are searching for is in the last field of the record.

This is the record for squashfs-tools.tcz:
Code: [Select]
squashfs-tools.tcz
/usr/local/bin/mksquashfs
/usr/local/bin/unsquashfs

Searching for mksquashfs works:
Code: [Select]
tc@E310:~/Scripting/Provides$ provides.sh "bin\/mksquashfs\n"
squashfs-tools.tcz
tc@E310:~/Scripting/Provides$

Searching for unsquashfs (last field) fails:
Code: [Select]
tc@E310:~/Scripting/Provides$ provides.sh "bin\/unsquashfs\n"
tc@E310:~/Scripting/Provides$
Title: Re: How to find which extension provides a file
Post by: polikuo on November 14, 2024, 12:46:38 PM
It seems using \n does not work if the item
you are searching for is in the last field of the record.

Hi, Rich.
Interesting...
This is what I get on piCore64 15.0 (squashfs.tcz has extra files, so I'm using tar.tcz to demonstrate)

Code: [Select]
tc@pi4:/etc/sysconfig/tcedir$ grep -A3 squashfs provides.db
squashfs-tools.tcz
usr/local/bin/mksquashfs
usr/local/bin/unsquashfs
usr/local/bin/sqfscat
usr/local/bin/sqfstar

tc@pi4:/etc/sysconfig/tcedir$ grep -A3 'tar.tcz' provides.db
tar.tcz
usr/local/lib/tar/rmt
usr/local/bin/tar

tc@pi4:/etc/sysconfig/tcedir$ prov 'bin\/tar\n'
tc@pi4:/etc/sysconfig/tcedir$ prov 'bin\/tar$'
tar.tcz
tc@pi4:/etc/sysconfig/tcedir$ prov 'bin\/tar\n/||/bin\/tar$'
tar.tcz
tc@pi4:/etc/sysconfig/tcedir$ provides.sh 'bin\/tar\n'
tc@pi4:/etc/sysconfig/tcedir$ provides.sh 'bin\/tar$'
tar.tcz
tc@pi4:/etc/sysconfig/tcedir$ provides.sh 'bin\/tar\n/||/bin\/tar$'
tar.tcz

This is getting ridiculous...

Code: [Select]
tc@pi4:~$ p(){ awk 'BEGIN {FS="\n";RS=""} /'"$1"'\/'"$2"'\n/||/'"$1"'\/'"$2"'$/{print $1}' /etc/sysconfig/tcedir/provides.db; }
tc@pi4:~$ p bin tar
tar.tcz

Shrink the line
Code: [Select]
tc@pi4:~$ p(){ awk -v FS="\n" -v RS="" "/$1\/$2\n/||/$1\/$2$/{print \$1}" /etc/sysconfig/tcedir/provides.db; }
tc@pi4:~$ p bin tar
tar.tcz

Other cases

Code: [Select]
tc@pi4:/etc/sysconfig/tcedir$ grep -A3 submitqc.tcz provides.db
submitqc.tcz
/usr/local/bin/submitqc
/usr/local/share/doc/submitqc/COPYING

tc@pi4:/etc/sysconfig/tcedir$ p bin submitqc
submitqc.tcz
tc@pi4:/etc/sysconfig/tcedir$ p submitqc COPYING
submitqc.tcz
tc@pi4:/etc/sysconfig/tcedir$ grep -A3 wget.tcz provides.db
wget.tcz
/usr/local/etc/wgetrc
/usr/local/bin/wget

tc@pi4:/etc/sysconfig/tcedir$ p bin wget
wget.tcz
tc@pi4:/etc/sysconfig/tcedir$ p etc wgetrc
wget.tcz
Title: Re: How to find which extension provides a file
Post by: Rich on November 14, 2024, 01:48:55 PM
Hi polikuo
That looks like it works, but I don't think a user should be
forced to use an exact match. A user may not be sure what
they are searching for. For example, they might want to
find all extensions with files that have crypt in their name.
I've done those types of searches myself on occasion.

If a person needs to get more specific with the path, they
still need to do this:
Code: [Select]
p "local\/bin" ps
These are not criticisms, just observations. Your awk solution
appears to be more efficient and shorter than mine. Though
mine doesn't need quotes for some reason:
Code: [Select]
tc@E310:~$ Provides.sh local\/bin\/ps
procps-ng.tcz
procps.tcz
tc@E310:~$
Title: Re: How to find which extension provides a file
Post by: Leee on November 14, 2024, 01:58:48 PM
As so often happens, I learned a lot while messing around with provides.sh.
First, the format of provides.db is easy to produce and doesn't contain a lot of redundant data (does not have the TCZ name at the start of each line ), and remains human readable (unlike, for instance, an SQL datebase table), it really doesn't lend itself to simple searches.
Second, mucking around with FS and RS so that each record -does- begin with the TCZ name. Now -that- is neat.
Third, as usual, I learned a little more about awk,  Alas, I haven't yet learned enough about awk (I find the syntax to be... wait for it... awkward!) so...
I tried coding the exact search, using other tools, to search for lines ending in /${TARGET} and it worked perfectly... but took four minutes to run.
I'm going to continue following this thread (big thanks to all in the thread) and continue playing with my own script... but that will have to be -after- I finish dealing with some "old, failing hardware" issues.
Title: Re: How to find which extension provides a file
Post by: Leee on November 14, 2024, 02:23:16 PM
Hi polikuo
That looks like it works, but I don't think a user should be
forced to use an exact match. A user may not be sure what
they are searching for. For example, they might want to
find all extensions with files that have crypt in their name.
I've done those types of searches myself on occasion.
I think any search tool should default to an exact search, possibly with an option for a fuzzy search.  Less ideal (IMO) would be defaulting to a fuzzy search with an option for an exact search.  Having only a fuzzy search is just frustrating.

In the mean time, looking at provides.sh and realizing that it uses awk, provides the clue that  a regular slash character ( / ) needs to be escaped, makes the stock provides.sh usable with, for instance, provides.sh "ps \/ps\n" for an exact search.  I would never have otherwise have thought to escape the slash character.
Title: Re: How to find which extension provides a file
Post by: gadget42 on November 14, 2024, 03:05:36 PM
this forum is awesome...and at the same time humbling(much to learn grasshopper...sigh).
Title: Re: How to find which extension provides a file
Post by: Rich on November 14, 2024, 05:05:34 PM
Hi Leee
... I tried coding the exact search, using other tools, to search for lines ending in /${TARGET} and it worked perfectly... but took four minutes to run. ...
I once tried that too, but nothing came close to touching the speed of awk.
It's because awk is searching the entire record for a match, not individual
fields. A record looks like this:
Code: [Select]
Field1
field2
field3

Field1 is the .tcz name. Field2 and Field3 are filenames. The blank line at
the end marks the end of the record. In awk land, {print $1} means print
the contents of Field1. $2 would be Field2, and so on.

Not shown is $0 which represents the entire record and looks like this:
Code: [Select]
Field1\nField2\nField3The record is treated as one long string with embedded \n record
separators, which is what gets searched. Field3 has no field separator
because it's the end of the record. If I change {print $1} to {printf $0}
the whole record gets printed and printf doesn't add any \n characters:
Code: [Select]
tc@E310:~/Scripting/Provides$ ./prov.sh "bin\/egrep\n"
grep.tcz
/usr/local/bin/egrep
/usr/local/bin/fgrep
/usr/local/bin/greptc@E310:~/Scripting/Provides$
You can see it print Field1\nField2\nField3\n but Field4 is immediately
followed by the command prompt, which explains why search terms
in the last field were not found in reply #5.

It's because awk is scanning an entire record in one go that it's so fast.
If you tell awk to search records one field at a time, it too will run slower.
Title: Re: How to find which extension provides a file
Post by: patrikg on November 14, 2024, 05:24:28 PM
Maybe "some one"=me right now :) have little time to make a script that unsquashfs -l tcefile.tce list the tce files and make a db of that. It goes very fast with sqlite3 to search.

Here you go, make.sh makes a db and fill the file names and tcz package names in.
And search.sh make you search in the db, you can edit the file to change you needs.

Happy hacking:

Code: [Select]
make.sh
cat << 'EOF' > make.sh
#!/bin/bash
sqlite3 files.db "CREATE TABLE tcz_files (name TEXT NOT NULL,file TEXT NOT NULL);"
for filename in $(ls *.tcz)
do
   tczfile=$filename;for file in $(unsquashfs -lc $tczfile); do sqlite3 files.db "INSERT INTO tcz_files ( NAME , FILE ) VALUES ( '$tczfile' , '$(basename $file)' );" ; done
done
EOF

chmod u+x make.sh

search.sh
cat << 'EOF' > search.sh
sqlite3 -box files.db "SELECT * FROM tcz_files WHERE file LIKE '%$1%';"
EOF

chmod u+x search.sh

ls
files.db  gimp2.tcz  make.sh  search.sh  sqlite3-bin.tcz  sqlite3.tcz

./search.sh pdf
┌───────────┬───────────────┐
│   name    │     file      │
├───────────┼───────────────┤
│ gimp2.tcz │ file-pdf-load │
│ gimp2.tcz │ file-pdf-save │
└───────────┴───────────────┘

./search.sh sql
┌─────────────────┬─────────────────────┐
│      name       │        file         │
├─────────────────┼─────────────────────┤
│ sqlite3-bin.tcz │ sqlite3             │
│ sqlite3.tcz     │ libsqlite3.so       │
│ sqlite3.tcz     │ libsqlite3.so.0     │
│ sqlite3.tcz     │ libsqlite3.so.0.8.6 │
└─────────────────┴─────────────────────┘

sqlite3 -box files.db "SELECT COUNT(*) AS 'Number of filenames' FROM tcz_files;"
┌─────────────────────┐
│ Number of filenames │
├─────────────────────┤
│ 4191                │
└─────────────────────┘

ls -alh files.db
-rw-r--r-- 1 patrik users 164K 14 nov 22.34 files.db



Title: Re: How to find which extension provides a file
Post by: yvs on November 14, 2024, 09:30:07 PM
I think that number of files not so big that even grep/sed through text file is negligible.
For example using sed search in shell command_not_found_handler() I got:
Code: [Select]
% as
as found in: binutils

% time as
as found in: binutils
0.00s user 0.01s system 97% cpu 0.007 total

% time cpp
cpp found in: gcc
0.00s user 0.00s system 97% cpu 0.008 total

% time users
users found in: coreutils
0.01s user 0.00s system 95% cpu 0.008 total

i.e. it's about 10msec (time comparable to switching between contexts)
Title: Re: How to find which extension provides a file
Post by: nick65go on November 15, 2024, 09:34:49 AM
so, maybe is premature, but then a conclusion?
the winner is sqlite (compiled), or awk (compiled), or sh shell/scripts?

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.
Title: Re: How to find which extension provides a file
Post by: patrikg on November 15, 2024, 11:00:08 AM
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.

Title: Re: How to find which extension provides a file
Post by: Rich on November 15, 2024, 12:20:56 PM
Hi nick65go
... 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.
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: [Select]
tc@E310:~/Scripting/Provides$ ./provides.sh grep | wc -l
51
tc@E310:~/Scripting/Provides$

Search for file name with leading slash:
Code: [Select]
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$

Search for file name with partial path:
Code: [Select]
tc@E310:~/Scripting/Provides$ ./provides.sh bin/grep
emacs.minimal.tcz
fpc.tcz
grep.tcz
tc@E310:~/Scripting/Provides$

Search for file name with partial path and trailing anchor:
Code: [Select]
tc@E310:~/Scripting/Provides$ ./provides.sh bin/grep$
grep.tcz
tc@E310:~/Scripting/Provides$

Added help message:
Code: [Select]
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$

And the script itself:
Code: [Select]
#!/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
Title: Re: How to find which extension provides a file
Post by: polikuo on November 15, 2024, 02:02:41 PM
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 ?
Title: Re: How to find which extension provides a file
Post by: Rich on November 15, 2024, 02:32:29 PM
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: [Select]
# 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

}
Title: Re: How to find which extension provides a file
Post by: Rich on November 15, 2024, 02:49:40 PM
Hi polikuo
... Perhaps you could wrap up the syncing code into a shell function ?
Done. :)

Code: [Select]
#!/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
Title: Re: How to find which extension provides a file
Post by: patrikg on November 15, 2024, 03:00:14 PM
I think this script can also detect some fault tcz's, that the squashfs is making so you don't clobbing your own old files and directorys.

Here is the old thread talking about that.

https://forum.tinycorelinux.net/index.php/topic,25229.msg161137.html#msg161137 (https://forum.tinycorelinux.net/index.php/topic,25229.msg161137.html#msg161137)
Title: Re: How to find which extension provides a file
Post by: Rich on November 15, 2024, 04:21:23 PM
Hi patrikg
The version that is currently part of Tinycore is also capable
of that. The syntax for that version is a bit more cryptic:
Code: [Select]
provides.sh "\/usr_1\/"
Using the version attached to my last post would look like this:
Code: [Select]
provides.sh /usr_1/
Title: Re: How to find which extension provides a file
Post by: patrikg on November 16, 2024, 03:10:15 AM
Hi @Rich

Ok, thanks for that. Didn't know.
This is because my main dist is not TC.
I am just here to help out, because i want that TC survives.

I like it a lot with embedded systems like Raspberry PI (PICore).

And if somebody want to learn how Linux works i suggest to use TC, it's
not bloated with so many things.

It's not just the kernel(Core) that's are Tiny.





 
Title: Re: How to find which extension provides a file
Post by: mocore on November 17, 2024, 05:30:56 AM
ftr
a vaguely  related tangent ... expanding *generally (https://en.wikipedia.org/wiki/Abstract_nonsense)* on some themes in this topic
can be found hear https://forum.tinycorelinux.net/index.php/topic,27364.0.html
Title: Re: How to find which extension provides a file
Post by: Rich on November 18, 2024, 12:53:14 AM
Hi polikuo
Focusing on ease of use and some speed improvements, I did
some more work on provides.sh:
1. The default search mode is unchanged. Any programs/scripts calling provides.sh should see no breakage.
2. No backslashes to enter. The script adds them if needed.
3. No quoting of search term, even if it includes embedded spaces.
4. End of search term can be marked with an anchor ($).
5. Added a brief help message (-h, -help, or --help).
6. Added alias to /usr/local/bin/awk if it exists.
7. Added -nz switch to skip zsync for faster responses.
8. These features all work for both the command line and the GUI.

Regular search:
Code: [Select]
tc@E310:~/Scripting/Provides$ time ./provides.sh bin/grep$
grep.tcz
real    0m 1.36s
user    0m 0.49s
sys     0m 0.23s
tc@E310:~/Scripting/Provides$

Same search, no zsync:
Code: [Select]
tc@E310:~/Scripting/Provides$ time ./provides.sh -nz bin/grep$
grep.tcz
real    0m 0.22s
user    0m 0.19s
sys     0m 0.03s
tc@E310:~/Scripting/Provides$
-nz switch can be placed before or after the search term.
-nz switch also works in the Apps GUI Provides field.

Search, no zsync, embedded space:
Code: [Select]
tc@E310:~/Scripting/Provides$ time ./provides.sh Black Gnome -nz
volumeicon.tcz
real    0m 0.13s
user    0m 0.11s
sys     0m 0.02s
tc@E310:~/Scripting/Provides$
tc@E310:~/Scripting/Provides$ time ./provides.sh  Make Cocoa Headers.txt -nz
fpc-src.tcz
real    0m 0.13s
user    0m 0.10s
sys     0m 0.02s
tc@E310:~/Scripting/Provides$
No quotes required.

And the help message:
Code: [Select]
tc@E310:~/Scripting/Provides$ ./provides.sh --help

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

Usage:
   provides.sh [ -nz ] FileName

   -nz    Skip updating (zsync) the provides.db file. This speeds up
          the search, but might miss items if provides.db is outdated.

Examples:
   provides.sh cal           Finds cal anywhere in FileName

   provides.sh bin/cal       Finds bin/cal anywhere in FileName

   provides.sh bin/cal$      Finds FileName that ends in bin/cal

   provides.sh Black Gnome   Finds FileName with embedded spaces

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

tc@E310:~/Scripting/Provides$

I've attached a copy of this version. I would appreciate it if some forum
members download, test, and provide comments.

After downloading, run:
Code: [Select]
sudo cp provides.sh /usr/binThe new provides.sh will now be functional for both command line and Apps GUI.

I will be removing the attachments from previous posts.
Title: Re: How to find which extension provides a file
Post by: nick65go on November 18, 2024, 02:26:44 AM
@Rich: From my point of view, you did a really good job! I mean you kept compatibility for old i486 CPU and improved the search comfort, BOTH in GUI and cmd line. Using just awk and shell, the search time (for you) is less than 2 seconds!

The number of extensions (in the future) will NOT dramatically increase (because missing MORE significant number of developers).
My assumptions are that the SIZE of database will NOT increase too much (+20% maxim as GZIP-ed) and the database is LOCATED in RAM memory, plus the UPDATE of the database is not so often performed. So, it seams that by this path you will soon arrive at diminishing return (Pareto principle). All is just an intellectual challenge, which I like.

Even if you can achieve, lets say search in 1/1000 seconds, which is 2,000.00 time faster than 2 seconds, the practical implication could be exploited only in loops or pipes programs, but not in GUI interface.

I wonder what (old?) computer specification you used (CPU, HDD, RAM), if you want to share, for your test.
Title: Re: How to find which extension provides a file
Post by: polikuo on November 18, 2024, 06:27:00 AM
Hi, Rich.
Nice work !!
You don't mind if I tweak a little right ?
Cause I really like to skip the zsync routine.
Code: [Select]
--- provides.sh
+++ /usr/local/bin/provides.sh
@@ -32,6 +32,7 @@
 fi
 chmod g+rw "$DB"
 cd - > /dev/null
+touch $LIST
 }
 # --------------------------------------------------------------- #

@@ -85,7 +86,7 @@
 [ -z "$TARGET" ] && exit 1

 # Run zsync on the provides.db file if NZ equals 1.
-[ $NZ -eq 1 ] && UpdateProvidesDB
+[ $NZ -eq 1 ] || [ -n "$(find $LIST -mmin +15)" ] && UpdateProvidesDB

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

The idea here is similar to the sudo session.
On Ubuntu, for example, after typing the password in a sudo command, the system will not ask for password in the following 15 minutes.
The newer version of busybox supports -mmin option.
Code: [Select]
tc@pi4:/tmp$ uname -a
Linux pi4 6.6.47-piCore-v8 #21 SMP PREEMPT Sat Aug 31 15:08:05 EDT 2024 aarch64 GNU/Linux
tc@pi4:/tmp$ busybox find --help
BusyBox v1.36.1 (2024-04-07 15:54:04 UTC) multi-call binary.
...
...
        -mtime DAYS     mtime is greater than (+N), less than (-N),
                        or exactly N days in the past
        -mmin MINS      mtime is greater than (+N), less than (-N),
                        or exactly N minutes in the past
Title: Re: How to find which extension provides a file
Post by: Rich on November 18, 2024, 02:06:08 PM
Hi nick65go
... Using just awk and shell, the search time (for you) is less than 2 seconds! ...
Most of that time is consumed by zsync checking if the servers version of provides.db
is newer than the one on your computer.

If you use the -nz switch it skips the zsync check and the time drops to under 0.2 seconds.

Quote
... I wonder what (old?) computer specification you used (CPU, HDD, RAM), if you want to share, for your test.
I retired my old 800Mhz  i686 (coppermine), 512 Meg RAM, PATA drives system
a year or two ago. It was a good machine for testing speed impact of optimizations.

Now I mostly use this machine:
Code: [Select]
tc@E310:~$ sudo inxi -CfmDGnSM
System:
  Host: E310 Kernel: 4.19.10-tinycore i686 bits: 32 Desktop: FLWM
  Distro: TinyCore 10.1
Machine:
  Type: Desktop System: Dell product: Dell DV051 v: N/A serial: 9Q5X2B1
  Mobo: Dell model: 0JC474 serial: ..CN481115B2077P. BIOS: Dell v: A04
  date: 04/04/2006
Memory:
  RAM: total: 2.96 GiB used: 1.30 GiB (44.1%)
  Array-1: capacity: 4 GiB note: est. slots: 2 EC: Single-bit ECC
  Device-1: DIMM_1 size: 2 GiB speed: 533 MHz
  Device-2: DIMM_2 size: 2 GiB speed: 533 MHz
CPU:
  Topology: Single Core model: Intel Pentium 4 bits: 64 type: MT
  L2 cache: 1024 KiB
  Speed: 2793 MHz min/max: N/A Core speeds (MHz): 1: 2793 2: 2793
  Flags: acpi apic bts cid clflush cmov constant_tsc cpuid cx16 cx8 de
  ds_cpl dtes64 dts fpu fxsr ht lahf_lm lm mca mce mmx monitor msr mtrr pae
  pat pbe pebs pge pni pse pse36 sep ss sse sse2 tm tsc vme xtpr
Graphics:
  Device-1: Intel 82915G/GV/910GL Integrated Graphics driver: i915 v: kernel
  Display: server: X.Org 1.20.4 driver: intel resolution: 1920x1080~60Hz
  OpenGL: renderer: Mesa DRI Intel 915G x86/MMX/SSE2 v: 1.4 Mesa 19.2.3
Network:
  Device-1: Intel 82562ET/EZ/GT/GZ - PRO/100 VE Ethernet driver: e100
  IF: eth0 state: up speed: 100 Mbps duplex: full mac: 00:13:20:c4:4a:20
  IF-ID-1: dummy0 state: down mac: 6e:93:1c:04:f2:dc
  IF-ID-2: ip_vti0 state: down mac: 00:00:00:00
  IF-ID-3: tunl0 state: down mac: 00:00:00:00
Drives:
  Local Storage: total: 931.52 GiB used: 144.75 GiB (15.5%)
  ID-1: /dev/sda vendor: HP model: MB0500EBNCR size: 465.76 GiB
  ID-2: /dev/sdb vendor: HP model: MB0500EBNCR size: 465.76 GiB
tc@E310:~$
Title: Re: How to find which extension provides a file
Post by: Rich on November 18, 2024, 03:14:23 PM
Hi polikuo
... Cause I really like to skip the zsync routine. ...
I get the feeling you don't like waiting for results from your computer. ::)

This is the timing for busybox awk:
Code: [Select]
tc@E310:~/Scripting/Provides$ time ./provides.sh -nz glxinfo$
mesa-demos.tcz
real    0m 0.45s
user    0m 0.41s
sys     0m 0.04s
tc@E310:~/Scripting/Provides$ time ./provides.sh -nz glxinfo$
mesa-demos.tcz
real    0m 0.47s
user    0m 0.42s
sys     0m 0.04s
tc@E310:~/Scripting/Provides$

This is the timing when gawk.tcz is installed:
Code: [Select]
tc@E310:~/Scripting/Provides$ time ./provides.sh -nz glxinfo$
mesa-demos.tcz
real    0m 0.21s
user    0m 0.16s
sys     0m 0.04s
tc@E310:~/Scripting/Provides$ time ./provides.sh -nz glxinfo$
mesa-demos.tcz
real    0m 0.22s
user    0m 0.18s
sys     0m 0.03s
tc@E310:~/Scripting/Provides$
I override the busybox default and use GNU awk if I detect it's present.

Quote
... The newer version of busybox supports -mmin option. ...
The current version of busybox on TC10 x86 does too.

Quote
Code: [Select]
+[ $NZ -eq 1 ] || [ -n "$(find $LIST -mmin +15)" ] && UpdateProvidesDB
That's pretty clever, but was the intent to override the -nz command
line switch every 15 minutes?

Maybe it makes more sense to remove the -nz command line switch
and just let it run a check when the file is at least 15 minutes old.

I would like to hear some opinions on this please.
Title: Re: How to find which extension provides a file
Post by: CNK on November 18, 2024, 04:36:19 PM
Works fine here (TC15 x86_64).

Quote
Maybe it makes more sense to remove the -nz command line switch
and just let it run a check when the file is at least 15 minutes old.

I would like to hear some opinions on this please.

I like the switch - it would avoid trouble when internet access isn't working and connections are timing out. If someone really runs it on a 486 then there would be a performance advantage too.

I'd also suggest showing the help info if provides.sh is run with no arguments.
Title: Re: How to find which extension provides a file
Post by: Rich on November 18, 2024, 05:41:24 PM
Hi CNK
... I'd also suggest showing the help info if provides.sh is run with no arguments.
I initially did that, but then reverted back to  exit 1  because I didn't want to
risk changing a behavior that a program or script might be relying on.

Although you can now enter  usr/local/bin  without escaping the slashes, it will
still accept  usr\/local\/bin  so as not to break anything that might rely on that
behavior. The idea was to try to enhance the behavior without changing expected
behavior.
Title: Re: How to find which extension provides a file
Post by: polikuo on November 18, 2024, 10:10:11 PM
Quote
Code: [Select]
+[ $NZ -eq 1 ] || [ -n "$(find $LIST -mmin +15)" ] && UpdateProvidesDB
That's pretty clever, but was the intent to override the -nz command
line switch every 15 minutes?

Maybe it makes more sense to remove the -nz command line switch
and just let it run a check when the file is at least 15 minutes old.

I would like to hear some opinions on this please.
Hi, Rich.
In a way, yes.
Perhaps wrapping the "find condition" into the UpdateProvidesDB would be better, but they are equivalent.
The "overriding" only occur when "-nz" is not passed, making the default behavior of UpdateProvidesDB only to update if the interval is greater than 15 minutes.
Code: [Select]
--- provides.sh
+++ /usr/local/bin/provides.sh
@@ -18,6 +18,7 @@
 # --------------------------------------------------------------- #
 UpdateProvidesDB()
 {
+[ -n "$(find $LIST -mmin +15)" ] || return 0
 getMirror
 cd "$TCEDIR"
 if zsync -i "$LIST" -q "$MIRROR"/"$DB".zsync
@@ -32,6 +33,7 @@
 fi
 chmod g+rw "$DB"
 cd - > /dev/null
+touch $LIST
 }
 # --------------------------------------------------------------- #

As for the -nz switch, I incline to remove it cause extra code results in extra chance for unintentional behavior.
For instance, this would trigger the -nz switch:
Code: [Select]
time provides.sh stupid-filename-nz
real    0m 0.13s
user    0m 0.11s
sys     0m 0.02s

Are you planning to upload the newer version into the Github Core-scripts repo ?
Title: Re: How to find which extension provides a file
Post by: Rich on November 19, 2024, 12:32:58 AM
Hi polikuo
... For instance, this would trigger the -nz switch: ...
Actually, the way it's coded the -nz switch will not trigger:
Code: [Select]
tc@E310:~/Scripting/Provides$ time ./provides.sh stupid-filename-nz
NZ=1    TARGET=stupid-filename
real    0m 1.74s
user    0m 0.48s
sys     0m 0.22s
tc@E310:~/Scripting/Provides$
As you can see, NZ is still set to 1. However, because of the way -nz is
removed from the command, stupid-filename-nz is now 3 characters short.
I did figure out how to fix it:
Code: [Select]
tc@E310:~/Scripting/Provides$ time ./provides.sh -nzstupid-filename-nz
NZ=1    TARGET=-nzstupid-filename-nz
real    0m 1.19s
user    0m 0.41s
sys     0m 0.23s
tc@E310:~/Scripting/Provides$
Both a leading and trailing -nz in a filename are safe. And it can still
remove  -nz  from the command,

Quote
... As for the -nz switch, I incline to remove it cause extra code results in extra chance for unintentional behavior. ...
I'm leaning in that direction too. Adding the file age test to UpdateProvidesDB() is
looking more and more attractive.

Quote
... Are you planning to upload the newer version into the Github Core-scripts repo ?
I'd like it to be added to Core-scripts. We'll see what curaga has to say about it.
If curaga accepts it, I'll let someone who knows their way around github submit it.

I was also considering creating an extension to allow current and previous versions
of Tinycore to run an updated provides.sh.

I'd like to see if we get any more comments on this.
Title: Re: How to find which extension provides a file
Post by: polikuo on November 19, 2024, 01:30:16 AM
Hi, Rich.
Here's a loophole that I missed
Code: [Select]
--- provides.sh
+++ /usr/local/bin/provides.sh
@@ -18,6 +18,7 @@
 # --------------------------------------------------------------- #
 UpdateProvidesDB()
 {
+[ -f $LIST ] && [ -z "$(find $LIST -mmin +15)" ] && return 0
 getMirror
 cd "$TCEDIR"
 if zsync -i "$LIST" -q "$MIRROR"/"$DB".zsync
@@ -32,6 +33,7 @@
 fi
 chmod g+rw "$DB"
 cd - > /dev/null
+touch $LIST
 }
 # --------------------------------------------------------------- #

If the provides.db does not exist, the checker also returns.
The logic now should be OK.
Title: Re: How to find which extension provides a file
Post by: GNUser on November 19, 2024, 09:38:48 AM
I'd like to see if we get any more comments on this.
Hi Rich. I love your improvements to provides.sh. Thank you. I always thought that script could be easier to use.

My vote is to eliminate the  -nz  switch. I'd say if  provides.db  is older than 24 hours and there is a network connection, to go ahead and automatically update provides.db. Maybe provide a switch if user wants to update  provides.db  regardless of its age?

It would be nice for no arguments to cause help to be printed, but I understand your reason for not doing that. The  -h  and  --help  are working as expected.
Title: Re: How to find which extension provides a file
Post by: Rich on November 19, 2024, 12:11:53 PM
Hi polikuo
Here's a loophole that I missed ...
Looks like you plugged it, well done.
Title: Re: How to find which extension provides a file
Post by: Rich on November 19, 2024, 01:51:25 PM
Hi GNUser
Hi Rich. I love your improvements to provides.sh. Thank you. I always thought that script could be easier to use. ...
That was the plan:
  Use a / to mark the beginning of the search term if desired.
  Type in your search term.
  Use a $ sign to mark the end of the search term if desired.
  Hit enter.

Quote
... I'd say if  provides.db  is older than 24 hours and there is a network connection, to go ahead and automatically update provides.db. ...
I guess we could add something like this to the beginning of  UpdateProvidesDB():
Code: [Select]
ping -W 1 -c 1 8.8.8.8 2>&1 > /dev/null || return 1It doesn't check DNS, but it's fast and will stop you from trying to access
the Internet if there is no connectivity.

Quote
... Maybe provide a switch if user wants to update  provides.db  regardless of its age? ...
Instead of a command line switch, an environmental variable to override the
value used for  -mmin  in the find command would work, for example:
Never update:
Code: [Select]
PROVIDESUPDATE=-0
Update ASAP, file is older than 1 minute:
Code: [Select]
PROVIDESUPDATE=+0
Update if older than 24 hours:
Code: [Select]
PROVIDESUPDATE=+1440
Quote
... It would be nice for no arguments to cause help to be printed, but I understand your reason for not doing that. The  -h  and  --help  are working as expected.
I also include  -help  as an option. :P
Title: Re: How to find which extension provides a file
Post by: GNUser on November 19, 2024, 07:19:20 PM
I guess we could add something like this to the beginning of  UpdateProvidesDB():
Code: [Select]
ping -W 1 -c 1 8.8.8.8 2>&1 > /dev/null || return 1
Why not go with Robert's approach (which shows up in  tc-config  and  settime.sh ):
Code: [Select]
ifconfig | grep -q Bcast || return 1This way there are no hardcoded ip addresses, no calls to Google, no danger of breakage if firewall blocks icmp traffic. Plus, if Busybox's  ifconfig  someday changes Bcast to some other string, tc-config, settime.sh, and provides.sh would all need an identical repair.

I really like the PROVIDESUPDATE environmental variable approach. Great idea!
Title: Re: How to find which extension provides a file
Post by: Rich on November 19, 2024, 08:41:32 PM
Hi GNUser
... Why not go with Robert's approach (which shows up in  tc-config  and  settime.sh ):
Code: [Select]
ifconfig | grep -q Bcast || return 1 ...
Funny you should mention it. That will detect the NIC was brought up.
It will not detect an unplugged network cable or problems upstream.
Oh, and watch this:
Code: [Select]
tc@box:~$ ifconfig eth0 | grep Bcast
          inet addr:192.168.1.45  Bcast:192.168.1.255  Mask:255.255.255.0
tc@box:~$ sudo ifconfig eth0 down
tc@box:~$ ifconfig eth0 | grep Bcast
          inet addr:192.168.1.45  Bcast:192.168.1.255  Mask:255.255.255.0
tc@box:~$

I also considered using this:
Code: [Select]
ifconfig eth0 | grep RUNNINGThat will detect taking down the NIC and unplugging the network cable.
While it detects a disconnect between computer and network switch, it
won't detect a disconnect between network switch and router.
Title: Re: How to find which extension provides a file
Post by: GNUser on November 19, 2024, 09:48:08 PM
Hi Rich. Scratch the Bcast idea, then.

I also thought of this:

Code: [Select]
getMirror
DOMAIN="$(echo "$MIRROR" | busybox awk -F[/:] '{print $4}')"
nslookup "$DOMAIN" >/dev/null 2>&1 || return 1

However, maybe it's best not to test connectivity. If provides.db meets age criteria for updating, I think provides.sh should just go ahead and try to update it (with a reasonable timeout so that user isn't waiting forever if there is no network connectivity). If a failed connectivity check takes as long as a failed update attempt, the connectivity check does not seem to add any value.
Title: Re: How to find which extension provides a file
Post by: GNUser on November 19, 2024, 10:08:58 PM
Maybe Robert used ifconfig+grep to look for Bcast just as a fast screening test: If this screening test fails, there's definitely no working network connection so it's pointless to continue. If this test succeeds, there is a chance that there's a working network connection--the program can then attempt to perform a network task (if failure is acceptable) or proceed to a test that's more specific but more time-consuming (e.g., ping or nslookup).
Title: Re: How to find which extension provides a file
Post by: Rich on November 19, 2024, 10:22:17 PM
Hi GNUser
This timed out in 1 second with no connectivity:
Code: [Select]
ping -W 1 -c 1 8.8.8.8 2>&1 > /dev/nullIt returns instantly with connectivity.

This timed out in 12 seconds with no connectivity:
Code: [Select]
ping -W 1 -c 1 google.com 2>&1 > /dev/null
This timed out in 24 seconds with no connectivity:
Code: [Select]
nslookup "repo.tinycorelinux.net" 2>&1 > /dev/null
Title: Re: How to find which extension provides a file
Post by: GNUser on November 19, 2024, 10:38:22 PM
Hi GNUser
This timed out in 1 second with no connectivity:
Code: [Select]
ping -W 1 -c 1 8.8.8.8 2>&1 > /dev/nullIt returns instantly with connectivity.
Hi Rich. Thanks for the tests. Sounds like this one is both fast and reliable, so it gets my vote despite the two cosmetic issues (hardwired address, big company) I mentioned previously.
Title: Re: How to find which extension provides a file
Post by: CNK on November 19, 2024, 11:33:55 PM
Hi CNK
... I'd also suggest showing the help info if provides.sh is run with no arguments.
I initially did that, but then reverted back to  exit 1  because I didn't want to
risk changing a behavior that a program or script might be relying on.

Maybe only output the help info when called with no arguments if "[ -t 1 ]" is true, indicating that the output is to a terminal, not a pipe (more info here (https://stackoverflow.com/questions/911168/how-can-i-detect-if-my-shell-script-is-running-through-a-pipe))?

The 1s ping might be so short it gives false negatives sometimes, since the first ping response can be exceptionally delayed if networking hardware is in a sleep state or eg. WiFi signal is poor. The automatic retrieval in provides.sh has always annoyed me really. I prefer how "apt-file" on Debian has a separate update command that you run manually. But I guess I can always make my own version too.
Title: Re: How to find which extension provides a file
Post by: Rich on November 20, 2024, 12:36:55 AM
Hi CNK
... The 1s ping might be so short it gives false negatives sometimes, ... ... But I guess I can always make my own version too.
This is not cast in stone. That's why I requested comments.

If I change it to this:
Code: [Select]
ping -A -W 1 -c 2 8.8.8.8 2>&1 > /dev/nullIt timed out in 2 seconds with no connectivity and still
returned instantly with connectivity.

The -A flag made the instant response possible:
Code: [Select]
-A              Ping as soon as reply is recevied
I think that still sounds quite reasonable.
Title: Re: How to find which extension provides a file
Post by: Rich on November 20, 2024, 01:39:43 AM
Hi CNK
... Maybe only output the help info when called with no arguments if "[ -t 1 ]" is true, ...
Done.
Title: Re: How to find which extension provides a file
Post by: GNUser on November 20, 2024, 11:50:39 AM
Hi Rich. FWIW I like the idea of automatically updating provides.db and would be happy with whatever maximum age is default.

Splitting the update into its own command or special flag (à la apt-file) would mean that using provides.sh would be a 2-step process for every user, every time (first checking age +/- updating the db, then searching the db). No autoupdate would also be a gotcha for less experienced users.

P.S. Folks that want maximum speed or more granular control can use the environmental variable you proposed to stop db updates (and/or you could add a "no db update" flag).
Title: Re: How to find which extension provides a file
Post by: GNUser on November 20, 2024, 02:19:04 PM
Hi Rich. Can you please post/attach your current provides.sh? I'd love to start using it right away. Thanks :)
I hope it becomes part of the base system for TCL16.
Title: Re: How to find which extension provides a file
Post by: Rich on November 20, 2024, 02:54:48 PM
Hi GNUser
... No autoupdate would also be a gotcha for less experienced users. ...
There will be an auto update (zsync) by default for that very reason. I think
15 minutes is long enough for most users to not get in the way when doing
multiple searches.

Also, unless you're the unluckiest person in the world, and the item you are
searching for gets added to provides.db in between auto updates, is short
enough not to cause problems.

Quote
... P.S. Folks that want maximum speed or more granular control can use the environmental variable you proposed to stop db updates (and/or you could add a "no db update" flag).
That's already in place. Including  -nz  (no zsync) in the command tells
provides.sh not to call  UpdateProvidesDB().

Latest version attached. Give it a shake and let me know if any bugs fall out. ;D
I'll start looking into adding an environmental variable for advanced users.
Title: Re: How to find which extension provides a file
Post by: GNUser on November 20, 2024, 03:47:36 PM
Thanks, Rich. I'll give it a good shake  :)
Title: Re: How to find which extension provides a file
Post by: CNK on November 23, 2024, 10:15:15 PM
... The 1s ping might be so short it gives false negatives sometimes ...

If I change it to this:
Code: [Select]
ping -A -W 1 -c 2 8.8.8.8 2>&1 > /dev/null
It timed out in 2 seconds with no connectivity and still
returned instantly with connectivity.

The -A flag made the instant response possible:
Code: [Select]
-A              Ping as soon as reply is recevied

-A isn't supported by GNU Inetutils Ping. With inetutils.tcz loaded I get:
Code: [Select]
ping: invalid option -- 'A'

Ping (surprisingly) isn't covered by useBusybox(), so the script needs to use /bin/ping if using -A.
Title: Re: How to find which extension provides a file
Post by: Rich on November 23, 2024, 10:59:20 PM
Hi CNK
Thanks for the heads up. ping changed to /bin/ping.
Title: Re: How to find which extension provides a file
Post by: Rich on November 23, 2024, 11:16:54 PM
Hi GNUser
... Latest version attached. Give it a shake and let me know if any bugs fall out. ;D ...
I did some more testing and found it ignored + signs and choked on the [ character.
I did some work on sanitizing the input and it seems to handle almost everything:
Code: [Select]
tc@E310:~/Scripting/Provides$ ./provides.sh be@latin.strings
abiword.tcz
tc@E310:~/Scripting/Provides$ ./provides.sh corrupt_t1#P#p1.MYI
mariadb-10.3-test.tcz
mariadb-10.4-test.tcz
mariadb-test.tcz
tc@E310:~/Scripting/Provides$ ./provides.sh 100%_Opaque.myb
mypaint-brushes.tcz
mypaint.tcz
tc@E310:~/Scripting/Provides$ ./provides.sh 9vx-iso
9vx.tcz
tc@E310:~/Scripting/Provides$ ./provides.sh [
coreutils.tcz
tc@E310:~/Scripting/Provides$ ./provides.sh {972ce4c6-7e08-4474-a285-3208198ce6fd}
firefox-ESR.tcz
icecat.tcz
kompozer.tcz
minefield21.tcz
palemoon.tcz
seamonkey-noSSE2.tcz
seamonkey.tcz
thunderbird.tcz
tc@E310:~/Scripting/Provides$ ./provides.sh NetSNMP::ASN.3
net-snmp-dev.tcz
net-snmp-doc.tcz
tc@E310:~/Scripting/Provides$ ./provides.sh ISO_8859-1,GL.gz
glibc_i18n_locale.tcz
tc@E310:~/Scripting/Provides$ ./provides.sh avahi-ui-gtk3~1.desktop
avahi-ui-gtk3.tcz
tc@E310:~/Scripting/Provides$ ./provides.sh c++filt
binutils.tcz
tc@E310:~/Scripting/Provides$ ./provides.sh AAC_\(Nero_FAAC\).txt
deadbeef.tcz
tc@E310:~/Scripting/Provides$
As you can see, it handles  @ # % _ - [ { } : , ~  and  +.
It also handles  !  and should work with  ^ , the provides.db had no ^ characters.
I couldn't get it to accept ( or ), so those need to be escaped. i.e.  \(  and  \).
The $ sign can only be used at the end as an anchor, no where else.
The following should be avoided because the shell grabs them & | " ' ` < >.
You can try escaping them, but I'd hope none of the file names use them.

Edit: It also handles  ! ^ =  and  ].

Quote
... I'll start looking into adding an environmental variable for advanced users.
Accommodations for a  PROVIDESUPDATE  environmental variable have been added.
It's documented right in the beginning of the script if you want to use it.
Title: Re: How to find which extension provides a file
Post by: polikuo on November 24, 2024, 02:26:19 AM
Hi, Rich

How about escaping everything that's not alphanumeric into hex for pattern matching ?
awk script
Code: [Select]
tc@pi4:/mnt/mmcblk0p2/notes/Shell-Tricks$ cat hex.awk
#!/usr/bin/awk -f
function hex(h,_,r,cmd) {
  cmd = "printf \x27" h "\x27 | hexdump -e \x272/1 \x22%X\x22\x27"
  cmd | getline r
  close(cmd)
  return r
}
function qry2ptn(str,_,i,c,s) {
  for (i=1;i<=length(str);i++){
    c = substr(str,i,1)
    if(c ~ /[[:alnum:]]/) s = s c
    else {
      if(c ~ /\x27/) s = s "\x5Cx27" # single quote
      else if(c ~ /\x25/) s = s "\x5Cx25" # %
      else s = s "\x5Cx" hex(c)
    }
  }
  return s
}
{
  print qry2ptn($0)
}

Example run
Code: [Select]
$ printf '\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x3A\x3B\x3C\x3D\x3E\x3F\x40\x5B\x5C\x5D\x5E\x5F\x60\n' > ptn
$ cat ptn
!"#$%&'()*+,-./:;<=>?@[\]^_`
$ ./hex.awk ptn
\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x3A\x3B\x3C\x3D\x3E\x3F\x40\x5B\x5C\x5D\x5E\x5F\x60

The only thing that needs adjusting is the leading "^" and the trailing "$" that are defined by your script for special purpose.
Title: Re: How to find which extension provides a file
Post by: Rich on November 24, 2024, 01:43:50 PM
Hi polikuo
Thanks, but most non-alphanumeric don't even need to be escaped.

As I said in reply #36:
Quote
That was the plan:
  Use a / to mark the beginning of the search term if desired.
  Type in your search term.
  Use a $ sign to mark the end of the search term if desired.
  Hit enter.

I am:
1. Trying to simplify the search so you don't need to enter or learn
   escape or quoting techniques to find what you're looking for.
2. Make the search return a tiny list instead of hundreds of results.

This version of provides.sh can accept the following characters in the command line:
Code: [Select]
! # % + , - . / : = ? @ [ ] ^ _ { } ~  and blank spacesMost appear in various provides.db files.
All have been tested.

I have not found the following in any provides.db files:
Code: [Select]
< > & * ; ' " `The shell will try to act on them and may do bad things.

The shell will try to act on these but I found them in TC10 x86 provides.db file:
Code: [Select]
( )
It is a short list and you don't need ( or ) to find any of them:
Code: [Select]
tc@E310:~/Scripting/Provides$ grep '(' /etc/sysconfig/tcedir/provides.db
usr/local/share/gtk-doc/html/cairo/cairo-Quartz-(CGFont)-Fonts.html
/usr/local/lib/deadbeef/convpresets/AAC_(Nero_FAAC).txt
/usr/local/lib/deadbeef/convpresets/FLAC_(compression_level_5).txt
/usr/local/lib/deadbeef/convpresets/MP3_CBR_320_Kbps_(Lame).txt
/usr/local/lib/deadbeef/convpresets/MP3_VBR_192Kbps_(Lame).txt
/usr/local/lib/deadbeef/convpresets/Ogg_Vorbis_(-q_5).txt
/usr/local/share/vim/vim82/lang/menu_chinese(gb)_gb.936.vim
/usr/local/share/vim/vim82/lang/menu_chinese(taiwan)_taiwan.950.vim
/usr/local/share/mypaint-data/1.0/brushes/kaerhon_v1/smudge_ink(0.7)_sm.myb
/usr/local/share/mypaint-data/1.0/brushes/kaerhon_v1/smudge_ink(0.7)_sm_prev.png
/usr/local/lib/python3.6/site-packages/setuptools/script (dev).tmpl
/usr/local/lib/python2.7/site-packages/setuptools-39.0.1-py2.7.egg/setuptools/script (dev).tmpl
usr/local/share/vim/vim72/lang/menu_chinese(gb)_gb.936.vim
usr/local/share/vim/vim72/lang/menu_chinese(taiwan)_taiwan.950.vim
usr/local/share/gtk-doc/html/libxfce4panel-1.0/libxfce4panel-Panel-Plugin-Register-Macros-(4.6-Style).html
tc@E310:~/Scripting/Provides$

... The only thing that needs adjusting is the leading "^" ...
There is no leading ^ in this type of search. All filenames include a path, so
to anchor the beginning, just add a leading slash.

Let's say you're looking for program called apps:
Code: [Select]
tc@E310:~/Scripting/Provides$ ./provides.sh apps | wc -l
95
That's way too many. To narrow the search, you add an anchor:
Code: [Select]
tc@E310:~/Scripting/Provides$ ./provides.sh /apps | wc -l
78
That narrowed it some, but you know it's a program, so you extend the anchor:
Code: [Select]
tc@E310:~/Scripting/Provides$ ./provides.sh bin/apps
Xprogs.tcz
Title: Re: How to find which extension provides a file
Post by: GNUser on November 25, 2024, 09:44:10 AM
Hi Rich. Thanks for sharing this improved version. I have put it on all my TCL boxes :)

It would be helpful if the Usage() function also outputted provides.sh's version number and/or release date.
Title: Re: How to find which extension provides a file
Post by: Rich on November 25, 2024, 03:09:32 PM
Hi GNUser
I've been meaning to add that. Now that it's pretty much
stabilized, I'll do that.
Title: Re: How to find which extension provides a file
Post by: Rich on November 25, 2024, 05:13:31 PM
Hi GNUser
After further testing I added the period to the list of
punctuation I'm escaping.

Version information has also been added:
Code: [Select]
tc@E310:~/Scripting/Provides$ ./provides.sh

Version 0.3 Nov 25, 2024

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

Usage:
   provides.sh [ -nz ] FileName

   -nz    Skip updating (zsync) the provides.db file. This speeds up
          the search, but might miss items if provides.db is outdated.

Examples:
   provides.sh cal           Finds cal anywhere in FileName
   provides.sh bin/cal       Finds bin/cal anywhere in FileName
   provides.sh bin/cal$      Finds FileName that ends in bin/cal
   provides.sh Black Gnome   Finds FileName with embedded spaces

Searches are case sensitive.
The $ sign can only be used at the end of your search term.

tc@E310:~/Scripting/Provides$

The updated version is attached. The previous version will be removed.
Title: Re: How to find which extension provides a file
Post by: GNUser on November 25, 2024, 08:20:49 PM
Hi Rich. It looks great! Thanks again for adding all this polish to this script.
Title: Re: How to find which extension provides a file
Post by: patrikg on November 30, 2024, 05:17:22 AM
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.

Now when i looked at it again, i see that sqlite need the select command to stdin, so
if i make the command like this i get the performance with sqlite3.
I think i can be useful for another to see it can be done.

So the search.sh file should be like this:

Code: [Select]
echo "SELECT * FROM tcz_files WHERE file LIKE '%$1%';" | sqlite3 -cmd ".timer on" -box files.db
make.sh
Code: [Select]
cat << 'EOF' > make.sh
#!/bin/bash
sqlite3 files.db "CREATE TABLE tcz_files (name TEXT NOT NULL,file TEXT NOT NULL);"
for filename in $(ls *.tcz)
do
   tczfile=$filename;for file in $(unsquashfs -lc $tczfile); do sqlite3 files.db "INSERT INTO tcz_files ( NAME , FILE ) VALUES ( '$tczfile' , '$(basename $file)' );" ; done
done
EOF

chmod u+x make.sh

search.sh
Code: [Select]
cat << 'EOF' > search.sh
#!/bin/bash
echo "SELECT * FROM tcz_files WHERE file LIKE '%$1%';" | sqlite3 -cmd ".timer on" -box files.db
EOF

chmod u+x search.sh

PS, so I take back what I said before it's sqlite bug, I have to read RTFM, to get it :)

Happy coding !!! :)
Title: Re: How to find which extension provides a file
Post by: Rich on December 02, 2024, 02:59:07 PM
Hi GNUser
provides.tcz is now available in the TC15 repos for all architectures.
Code: [Select]
Title: provides.tcz
Description: Enhanced version of Tinycore provides.sh.
Version: 0.3
Author: Robert Shingledecker
Original-site: https://github.com/tinycorelinux/Core-scripts/blob/master/usr/bin/provides.sh
Copying-policy: GPLv2
Size: 4.0K
Extension_by: Rich
Tags: find filename extension
Comments: Run  provides.sh  for this help screen:

Version 0.3 Nov 25, 2024

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

Usage:
   provides.sh [ -nz ] FileName

   -nz    Skip updating (zsync) the provides.db file. This speeds up
          the search, but might miss items if provides.db is outdated.

Examples:
   provides.sh cal           Finds cal anywhere in FileName
   provides.sh bin/cal       Finds bin/cal anywhere in FileName
   provides.sh bin/cal$      Finds FileName that ends in bin/cal
   provides.sh Black Gnome   Finds FileName with embedded spaces

Searches are case sensitive.
The $ sign can only be used at the end of your search term.


-------------------------------------------------------------
Note: The default age of provides.db for checking if an update
is available is 15 minutes.

Environmental variable PROVIDESUPDATE can override this default.
Add  export PROVIDESUPDATE=+N  to .profile file.
log out, log in.

N must be an integer.
Set to -0 to block updates.
Set to +0 for frequent updates.
Set to +N where N is the update interval in minutes.
Ex.  PROVIDESUPDATE=+1440    for 24 hours.

Change-log: 2024/11/25 Original release v0.30
Current: 2024/11/25 Original release v0.30

This is also used by the  Apps  GUI program.

For those running a prior version of Tinycore, download one of
the TC15 versions, they're all the same:
http://tinycorelinux.net/15.x/x86/tcz/provides.tcz

I've tested under TC10 x86 and TC14 x86_64.
It failed under TC9:
    The parameter substitution couldn't handle the / character.
    Busybox ping does not support -A.

I think polikuo did some testing on TC15 aarch64 (piCore).
Title: Re: How to find which extension provides a file
Post by: GNUser on December 02, 2024, 09:14:01 PM
Hi Rich. That's great, but your version of provides.sh deserves to be in base system. I hope that's the plan for TCL16.
Title: Re: How to find which extension provides a file
Post by: polikuo on December 02, 2024, 09:42:23 PM
I hope that's the plan for TCL16.
Hi, GNUser.
A bit off topic, but...
Speaking of TCL16, I wish that /usr merge (https://forum.tinycorelinux.net/index.php/topic,26879.0.html) could be implemented as well.
Title: Re: How to find which extension provides a file
Post by: Rich on December 03, 2024, 01:14:31 AM
Hi GNUser
I'm hoping it gets incorporated into the base system too.

The extension just seemed like an easy way for users to
upgrade their provides.sh should they wish to.
Title: Re: How to find which extension provides a file
Post by: GNUser on December 03, 2024, 08:17:47 AM
Hi Rich. provides.sh is on github. May I submit a pull request on your behalf? If it gets accepted, it will make it into TCL16.

Hi polikuo. Yes, I'd love /usr merge in TCL16 (i.e., for /lib to simply be a symlink to /usr/lib. Same for /bin and /sbin). The change would affect only how the base system is organized. Extensions would continue to use /usr/local/lib, /usr/local/bin, and /usr/local/sbin and would be unaffected by the merge.

Curaga is okay with /usr merge but it seems final decision is Juanito's and he hasn't weighed in one way or the other:
https://forum.tinycorelinux.net/index.php/topic,27276.msg175480.html#msg175480

/usr merge would certainly make beautiful TCL even more beautiful. I can live without it but would sure like to have this.
Title: Re: How to find which extension provides a file
Post by: Rich on December 03, 2024, 10:08:08 AM
Hi GNUser
... May I submit a pull request on your behalf? ...
Yes, by all means, please.

Quote
... it seems final decision is Juanito's and he hasn't weighed in one way or the other ...
I thought he did:
I’d prefer to continue using /usr for everything in the base and /usr/local for extensions.
Title: Re: How to find which extension provides a file
Post by: GNUser on December 03, 2024, 10:34:57 AM
Hi Rich. Pull request has been made: https://github.com/tinycorelinux/Core-scripts/pull/70
Hopefully it will be accepted before TCL16 comes out :)

Quote
... it seems final decision is Juanito's and he hasn't weighed in one way or the other ...
I thought he did:
I’d prefer to continue using /usr for everything in the base and /usr/local for extensions.
Depending on how you read that, Juanito's preference would be best honored by implementing /usr merge.

Current situation: Base uses not only /usr/bin, /usr/lib, and /usr/sbin but also /bin, /lib, and /sbin. /usr/local used for extensions.

/usr merge situation: Base uses /usr for everything. /bin is just a link to /usr/bin, /lib is just a link to /usr/lib, /sbin is just a link to /usr/sbin. /usr/local for extensions.

P.S. Implementing /usr merge is not as radical as it sounds. I did it as a test and it was simple. It involves moving the few things in base that live in /bin into /usr/bin and creating a single symlink (ln -s /usr/bin /bin). Repeat for /lib and /sbin. Done.
Title: Re: How to find which extension provides a file
Post by: Rich on December 03, 2024, 11:09:55 AM
Hi GNUser
Pull request has been made: https://github.com/tinycorelinux/Core-scripts/pull/70 ...
Thank you.

Quote
... Current situation: Base uses not only /usr/bin, /usr/lib, and /usr/sbin but also /bin, /lib, and /sbin. /usr/local used for extensions.

/usr merge situation: Base uses /usr for everything. /bin is just a link to /usr/bin, /lib is just a link to /usr/lib, /sbin is just a link to /usr/sbin. /usr/local for extensions. ...
That reminded me of this:
sbin still has a purpose, root-requiring stuff, so not sure on that one.
Yes, at least sbin has a purpose. Debian/Devuan did not do sbin merge (i.e., kept /usr/sbin as a real directory).

    [Edit]: /usr merge comments continue here https://forum.tinycorelinux.net/index.php/topic,27276.msg176505.html#msg176505