tazpkg view modules/recharge @ rev 846
Remove "busybox" "prefixes" (thanks llev)
We used "busybox wget", etc. to be sure we called Busybox's "wget", not any other "wget". Workaround already done in "getenv" module.
We used "busybox wget", etc. to be sure we called Busybox's "wget", not any other "wget". Workaround already done in "getenv" module.
author | Aleksej Bobylev <al.bobylev@gmail.com> |
---|---|
date | Fri Oct 09 13:14:01 2015 +0300 (2015-10-09) |
parents | a02e36d44d06 |
children | d62dc010e0ee |
line source
1 #!/bin/sh
2 # TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
3 # recharge - TazPkg module
4 # Recharge packages databases from a mirror
7 # Options:
8 # [main|<repository>] Repository name to recharge (all if empty)
10 # Environment variables:
11 # root Root of the packages DB
12 # UA User Agent string ("TazPkg-<version>")
15 # Connect function libraries
16 . /lib/libtaz.sh
18 # Get TazPkg working environment
19 . @@MODULES@@/getenv
24 # Functions
25 # ---------
27 # Download a file from specified mirror
29 get_from_mirror() {
30 case "$mirror" in
31 http://* | https://* | ftp://*)
32 wget -c -q -T 30 -U "$UA" "$mirror$1" 2>/dev/null;;
33 *)
34 ln -sf "$mirror$1" .;;
35 esac
36 status
37 }
40 # When recharging errors occur
42 recharging_failed() {
43 # Restore database from bak files
44 action 'Restoring database files...'
45 [ -e 'ID' -a ! -e 'ID.bak' ] && rm ID
46 [ -e 'IDs' -a ! -e 'IDs.bak' ] && rm IDs
47 for file in $(ls "$1"/*.bak); do
48 mv -f "$file" "${file%.bak}"
49 done
50 status
52 footer "$(colorize 31 "$(_ 'Recharging failed')")"
53 }
58 REPO="$1"
60 # What to recharge: main, or all, or selected undigest
61 case "$REPO" in
62 main) repo_to_recharge="$PKGS_DB";;
63 '') repo_to_recharge="$PKGS_DB $PKGS_DB/undigest/*";;
64 *) repo_to_recharge="$PKGS_DB/undigest/$REPO"
65 if [ ! -d "$repo_to_recharge" ]; then
66 _ "Repository \"%s\" doesn't exist." "$repo_to_recharge" >&2
67 exit 1
68 fi
69 ;;
70 esac
72 for path in $repo_to_recharge; do
73 [ ! -s "$path/mirror" ] && continue # skip
74 cd "$path"
75 # Mirror URL will have a trailing slash
76 mirror=$(cat mirror); mirror="${mirror%/}/"
78 # Repository name
79 if [ "$path" == "$PKGS_DB" ]; then
80 repo_name='Main'
81 else
82 repo_name="$(_n 'Undigest %s' "$(basename "$path")")"
83 fi
85 title 'Recharging repository "%s"' "$repo_name"
87 # Don't let ID be a symlink when using local repository.
88 if [ -h ID ]; then mv -f ID ID.lnk; cat ID.lnk > ID; rm ID.lnk; fi
89 if [ -h IDs ]; then mv -f IDs IDs.lnk; cat IDs.lnk > IDs; rm IDs.lnk; fi
91 [ -f ID ] && mv ID ID.bak # Compatibility with "old" ID
92 [ -f IDs ] && mv IDs IDs.bak
93 action 'Checking...'
94 get_from_mirror IDs
96 [ -e 'IDs' ] && awk '{print $1}' IDs > ID # Compatibility with "old" ID
97 [ -e 'IDs' ] && _ 'Database timestamp: %s' "$(date -d "@$(awk '{print $2}' IDs)" "+%x %R")"
99 # Check if recharging is needed
100 if [ -f 'IDs' ] && [ -f 'packages.info' ] && [ -f 'files.list.lzma' ] && \
101 cmp -s IDs IDs.bak; then
102 footer "$(_ 'Repository "%s" is up to date.' "$repo_name")"
103 rm IDs.bak ID.bak
104 continue
105 fi
106 rm IDs.bak ID.bak 2>/dev/null
108 action 'Creating backup of the last packages list...'
109 for i in packages.desc packages.$SUM packages.txt packages.list \
110 packages.equiv files.list.lzma extra.list mirrors packages.info; do
111 [ -f "$i" ] && mv -f $i $i.bak 2>/dev/null
112 done
113 # Always "[ Done ]"
114 :; status
116 # Download and extract bundle: extra.list, mirrors, files-list.md5,
117 # packages.{info,desc,md5,txt,list,equiv}
118 bundle='bundle.tar.lzma'
119 action 'Getting "%s"...' "$bundle"
120 get_from_mirror "$bundle"
121 if [ -f "$bundle" ]; then
122 tar -xaf "$bundle"; rm "$bundle"
123 else
124 recharging_failed "$path"; continue
125 fi
127 # Download files.list.lzma
128 files_local='files.list.lzma'; files_remote='files-list.lzma'
129 if [ -e "$files_local.bak" ]; then
130 md5sum "$files_local.bak" | awk '{printf $1}' > files-list.md5.bak
131 if cmp -s files-list.md5 files-list.md5.bak; then
132 mv "$files_local.bak" "$files_remote"
133 else
134 action 'Getting "%s"...' "$files_remote"
135 get_from_mirror "$files_remote"
136 fi
137 else
138 action 'Getting "%s"...' "$files_remote"
139 get_from_mirror "$files_remote"
140 fi
142 if [ ! -e "$files_remote" ]; then
143 recharging_failed "$path"; continue
144 fi
145 mv -f "$files_remote" "$files_local"
147 # Remove old database files (but packages.list.bak, extra.list.bak)
148 for i in packages.desc packages.$SUM packages.txt packages.equiv \
149 files.list.lzma mirrors packages.info files-list.md5; do
150 [ -f "$i.bak" ] && rm $i.bak 2>/dev/null
151 done
153 footer "$(_ 'Last database is ready to use.')"
155 # Check diff
156 if [ -f 'packages.list.bak' ]; then
157 diff -u packages.list.bak packages.list | grep ^+[a-z] > packages.diff
158 rm packages.list.bak
159 if [ -f 'extra.list.bak' ]; then
160 if [ -f 'extra.list' ]; then
161 awk -F'|' '{print $1 " (extra)"}' extra.list > extra.list1
162 awk -F'|' '{print $1 " (extra)"}' extra.list.bak > extra.list1.bak
163 diff -u extra.list1.bak extra.list1 | grep ^+[a-z] >> packages.diff
164 rm extra.list.bak extra.list1 extra.list1.bak
165 else
166 mv extra.list.bak extra.list
167 fi
168 fi
169 sed -i s/+// packages.diff
171 new_pkgs=$(wc -l < packages.diff)
172 if [ "$new_pkgs" -gt 0 ]; then
173 title 'Mirrored packages diff'
174 cat packages.diff
175 footer "$(emsg "$(_p \
176 '%s new package on the mirror.' \
177 '%s new packages on the mirror.' "$new_pkgs" \
178 "<c 32>$new_pkgs</c>")")"
179 fi
180 else
181 longline "$(_ "Note that next time you recharge the list, a list of \
182 differences will be displayed to show new and upgradeable packages.")"
183 fi
184 done
185 newline
187 # Close tazpkg-notify
188 echo "quit" > /tmp/tazpkg-notify.fifo