WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: How to find which extension provides a file  (Read 4407 times)

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11703
Re: How to find which extension provides a file
« Reply #30 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.

Offline polikuo

  • Hero Member
  • *****
  • Posts: 758
Re: How to find which extension provides a file
« Reply #31 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 ?

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11703
Re: How to find which extension provides a file
« Reply #32 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.

Offline polikuo

  • Hero Member
  • *****
  • Posts: 758
Re: How to find which extension provides a file
« Reply #33 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.

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1530
Re: How to find which extension provides a file
« Reply #34 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.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11703
Re: How to find which extension provides a file
« Reply #35 on: November 19, 2024, 12:11:53 PM »
Hi polikuo
Here's a loophole that I missed ...
Looks like you plugged it, well done.

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11703
Re: How to find which extension provides a file
« Reply #36 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

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1530
Re: How to find which extension provides a file
« Reply #37 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!
« Last Edit: November 19, 2024, 07:22:55 PM by GNUser »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11703
Re: How to find which extension provides a file
« Reply #38 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.

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1530
Re: How to find which extension provides a file
« Reply #39 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.

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1530
Re: How to find which extension provides a file
« Reply #40 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).
« Last Edit: November 19, 2024, 10:21:39 PM by GNUser »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11703
Re: How to find which extension provides a file
« Reply #41 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

Offline GNUser

  • Wiki Author
  • Hero Member
  • *****
  • Posts: 1530
Re: How to find which extension provides a file
« Reply #42 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.
« Last Edit: November 19, 2024, 10:41:00 PM by GNUser »

Offline CNK

  • Wiki Author
  • Sr. Member
  • *****
  • Posts: 292
Re: How to find which extension provides a file
« Reply #43 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)?

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.
« Last Edit: November 19, 2024, 11:36:15 PM by CNK »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11703
Re: How to find which extension provides a file
« Reply #44 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.