tazpkg view modules/mkdb @ rev 955

modules/search: allow search file with dash at start: tazpkg -sf "-spi"
author Aleksej Bobylev <al.bobylev@gmail.com>
date Fri Dec 22 00:02:54 2017 +0200 (2017-12-22)
parents 8a73a58ed3cb
children
line source
1 #!/bin/sh
2 # TazPkg - Tiny autonomous zone packages manager, hg.slitaz.org/tazpkg
3 # mkdb - TazPkg module
4 # Make TazPkg database for folder with *.tazpkg packages
7 # Input: $1 - path to folder contains *.tazpkg packages
8 # Output files in the $1 folder:
9 # packages.info
10 # packages.equiv
11 # descriptions.txt
12 # files.list.lzma
13 # IDs
14 # Do nothing if database already exists; force rebuild it with --forced option.
16 # DB format:
17 # ==========
19 # packages.info
20 # -------------
21 # Record is line; fields are tab-separated. Fields description:
22 # 1: package name
23 # 2: version with extra-version
24 # 3: category
25 # 4: short description
26 # 5: upstream web site
27 # 6: tags (space-separated)
28 # 7: packed and unpacked sizes (space-separated) in human readable format
29 # 8: depends
30 # 9: "release checksum"
31 #10: provide
33 # packages.equiv
34 # --------------
35 # This DB file used before package installation
36 # Record is line. Separator is "="
37 # Field 1 is package name to install (pkg1)
38 # Field 2 is space-separated list of items in the special format:
39 # a) pkg2:pkg3
40 # If pkg2 is installed, then install pkg3 instead of pkg1. Example:
41 # busybox=pam:busybox-pam
42 # If 'pam' is installed, then install 'busybox-pam' instead of 'busybox'
43 # b) pkg2
44 # If pkg2 already installed, then pkg1 will not be installed. Example:
45 # mysql=mariadb
46 # If 'mariadb' already installed, then 'mysql' will not be installed
47 # Complex rule example:
48 # ssh=pam:openssh-pam openssh pam:dropbear-pam dropbear
50 # descriptions.txt
51 # ----------------
52 # Field is line; record separator is empty line.
53 # First field is package name, rest - description itself.
54 # Empty lines in the description appended with space (" ") to avoid mess
55 # with end of record.
57 # files.list.lzma
58 # ---------------
59 # It is "files.list" compressed using lzma due to it's big size.
60 # Format of the files.list: record is line; field separator is ": ".
61 # First field is package name, second field is file path.
62 # There are DB records for all files installed with the package.
64 # packages.md5
65 #-------------
66 # Record is line; field separator is " ".
67 # First field is md5sum of the package file; second field is package file name.
68 # Actually, the type of checksum defined in variable $SUM and defaults to "md5".
71 # Connect function libraries
72 . /lib/libtaz.sh
74 # Get TazPkg working environment
75 . @@MODULES@@/getenv
80 # Exit if input folder not specified
81 [ -z "$1" ] && die 'Input folder not specified'
83 # Exit if input folder not exists
84 folder=$(realpath "$root$1") || exit 1
86 # Exit if folder is not writable
87 [ ! -w "$folder" ] && die 'You are not allowed to write to the folder "%s"' "$folder"
89 # Exit if input folder does not contain packages
90 [ -z "$(find "$folder" -maxdepth 1 -name '*.tazpkg')" ] && \
91 die 'Folder "%s" does not contain packages' "$folder"
94 # DB file names
95 DBi="$folder/packages.info"
96 DBe="$folder/packages.equiv"
97 DBm="$folder/packages.$SUM"
98 DBd="$folder/descriptions.txt"
99 DBf="$folder/files.list"
101 # Pre-remove DB if --forced and DB exists
102 if [ -n "$forced" ]; then
103 [ -e "$DBi" ] && rm "$DBi"
104 [ -e "$DBe" ] && rm "$DBe"
105 [ -e "$DBm" ] && rm "$DBm"
106 [ -e "$DBd" ] && rm "$DBd"
107 [ -e "$DBf.lzma" ] && rm "$DBf.lzma"
108 fi
110 if [ -s "$DBi" ]; then
111 _ 'Packages DB already exists.' >&2
112 exit 1
113 fi
115 # Random temporary folder
116 tempd="$(mktemp -d)"
118 # Make temporary list of packages checksum (usually md5sum)
119 _n 'Calculate %s...' "$CHECKSUM"
120 cd "$folder"; $CHECKSUM *.tazpkg | tee "$tempd/$SUM" > "$DBm"
121 status
123 cd "$tempd"
125 # Loop for every package
126 while read pkgsum pkgfile; do
127 # Current processed package
128 echo -n "$pkgfile"
130 # Extract $CHECKSUM, receipt, description.txt from package
131 # (description.txt may absent, no error produced)
132 cpio -F "$folder/$pkgfile" -i $CHECKSUM receipt description.txt >/dev/null 2>&1
134 # Make "release checksum"
135 cp $CHECKSUM rsum_file
136 md5sum receipt >> rsum_file
137 [ -e "description.txt" ] && md5sum description.txt >> rsum_file
138 rsum=$(md5sum rsum_file | awk '{print $1}')
140 # Unset variables that may absent in the receipt
141 unset EXTRAVERSION TAGS DEPENDS PROVIDE
142 # Get values
143 . ./receipt
145 # Clean
146 rm -f $CHECKSUM receipt description.txt rsum_file 2>/dev/null
148 # Make packages.info
149 echo -en "$PACKAGE\t$VERSION$EXTRAVERSION\t$CATEGORY\t" >> "$DBi"
150 echo -en "$SHORT_DESC\t$WEB_SITE\t$TAGS\t" >> "$DBi"
151 echo -en "$PACKED_SIZE $UNPACKED_SIZE\t" | sed 's|\.0||g' >> "$DBi"
152 echo -n $DEPENDS$'\t' >> "$DBi"
153 echo -e "$rsum\t$PROVIDE" >> "$DBi"
156 # Make packages.equiv
157 for i in $PROVIDE; do
158 # Example from busybox-pam package:
159 # PACKAGE="busybox-pam", PROVIDE="busybox:pam"
160 case $i in
161 # DEST="pam:"
162 *:*) DEST="${i#*:}:";;
163 *) DEST='';;
164 esac
165 # PKG="busybox"
166 PKG="${i%:*}"
167 if grep -qs ^$PKG= "$DBe"; then
168 # Append existing record
169 sed -i "s|^$PKG=|\0 $DEST$PACKAGE|" "$DBe"
170 else
171 # Add new record
172 echo "$PKG=$DEST$PACKAGE" >> "$DBe"
173 fi
174 done
177 # Make descriptions.txt
178 if cpio -F "$folder/$pkgfile" -t 2>/dev/null | fgrep -q 'description.txt'; then
179 # Extract description.txt from package
180 cpio -F "$folder/$pkgfile" -i description.txt >/dev/null 2>&1
181 # Append descriptions DB
182 echo "$PACKAGE" >> "$DBd"
183 cat description.txt | sed 's|^$| |' >> "$DBd"
184 echo >> "$DBd"
185 rm description.txt
186 fi
189 # Make files.list
190 if cpio -F "$folder/$pkgfile" -t 2>/dev/null | fgrep -q 'files.list'; then
191 # Extract files.list from package
192 cpio -F "$folder/$pkgfile" -i files.list >/dev/null 2>&1
193 # Append files list DB
194 sed "s|.*|$PACKAGE: \0|" files.list >> "$DBf"
195 rm files.list
196 fi
198 # End line with the status
199 status
200 done < "$tempd/$SUM"
203 # Sort DB alphabetically
204 sort -o "$tempd/pi" "$DBi"; mv -f "$tempd/pi" "$DBi"
206 # Create empty files if they not exists
207 touch "$DBi" "$DBe" "$DBd" "$DBf"
209 # Compress files.list using lzma
210 sort -k2 -o "$DBf.sorted" "$DBf"
211 lzma e "$DBf.sorted" "$DBf.lzma"
212 rm "$DBf" "$DBf.sorted"
214 # Make DB readable for all
215 chmod a+r "$DBi" "$DBe" "$DBd" "$DBf.lzma"
218 # Make files for DB recharge
219 # --------------------------
221 cd "$folder"
223 # Make IDs: md5 and timestamp
224 ( md5sum "$tempd/$SUM" | cut -d' ' -f1 | tr '\n' ' '; date -ur "$DBi" +%s ) > IDs
227 # Make files-list.md5: decide whether to download files.list.lzma or not
228 md5sum "$DBf.lzma" | cut -d' ' -f1 | tr -d $'\n' > files-list.md5
230 # Make bundle to fast recharge
231 [ -f 'bundle.tar.lzma' ] && rm 'bundle.tar.lzma'
232 tar -chaf bundle.tar.lzma \
233 files-list.md5 packages.info descriptions.txt packages.equiv
235 # Clean up
236 rm files-list.md5
237 rm -r "$tempd"