A small percentage of users reporting issue where `pkg install` shows that
package is not available, though it is present in our apt repository. This
happens when sources.list has been changed without subsequent `apt update`.
Now `pkg` will force run `apt update` if sources.list was modified after
packages list update but cache still not expired.
Before this, we had only '--connect-timeout 5' option specified but unfortunately
it doesn't work in cases when remote host is behind some proxy (e.g. CloudFlare)
but is not connected to Internet.
To prevent curl from hanging for unspecified amount of time, we need to introduce
external timeout here.
* Weight-based mirror selection: do not give each mirror same usage ratio as hosts have different properties.
* Rotate mirrors after 6h: pkg now checks usability of all mirrors before selecting one, this takes time but we may want to reduce delays before the actual `apt` operation will be started.
* Update apt cache after 20min: reduce amount of necessary package list queries. We even can go with 1h, but since we prefer origin repository over mirrors as download source, 20 minutes should be fine.
* Avoid long lines.
* Add missing info for some commands.
* Do not show deb cache size if its size cannot be determined, for example if cache dir was deleted.
See https://github.com/termux/termux-packages/issues/5620.
Implements automatic mirror selection:
* Pick a random URL of top 4 mirrors of Termux main repository and use it during 30 minutes limit, then rotate to a new one. Distribute traffic accross multiple mirrors, including origin Bintray repository.
* During 30 minutes limit, let `pkg` to check whether mirror is accessible on each run. If mirror is not accessible, use a new one. Mirrors are not guaranteed to be 100% accessible, so let pkg to check which one is working before using `apt`.
* Skip rotating if using `.cn` mirrors - Chinese users will not want to use something else due to great firewall and download speed issues.
Usage of package manager as root has certain bad effects in Termux such as
messed up SELinux contexts and ownership. Root checks done in 'pkg' wrapper
are not reliable because one can execute 'apt' directly or with third-party
script downloaded from the Internet.
This commit adds user id check and if it found that uid is 0, apt will
refuse to do any work in root session. These checks done in such way so
they cannot be bypassed in any way unless command is executed as non-root
user.
Those who use Termux via ADB root shell should be able to switch to Termux
user id with command 'su' in order to have package manager working.
---
This change also affects the 'termux-info' utility:
* It will no longer use 'apt policy' to detect subscribed repositories. Each
source will be checked by script manually.
* Information will be copied to clipboard only if 'termux-api' is installed.
* Syntax error in timeout command is fixed: 'timeout' doesn't understand the
argument '-t'.
* Minor information entries reordering.
When installing or searching for packages, the `apt update` will run only if it
was not executed within the last 5 minutes. Command `pkg upgrade` will
always execute `apt update` to ensure that latest package versions were
picked during upgrade.