1if [ ! "$_PACKAGES_INDEX_SUBR" ]; then _PACKAGES_INDEX_SUBR=1
2#
3# Copyright (c) 2013 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
--- 10 unchanged lines hidden (view full) ---
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD: head/usr.sbin/bsdconfig/share/packages/index.subr 257795 2013-11-07 12:40:42Z dteske $
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." packages/index.subr
34f_include $BSDCFG_SHARE/device.subr
35f_include $BSDCFG_SHARE/media/common.subr
36f_include $BSDCFG_SHARE/strings.subr
37
38BSDCFG_LIBE="/usr/libexec/bsdconfig"
39f_include_lang $BSDCFG_LIBE/include/messages.subr
40
41############################################################ GLOBALS
42
43PACKAGE_INDEX=
44_INDEX_INITTED=
45
46#
47# Default path to pkg(8) repo-packagesite.sqlite database
48#
49SQLITE_REPO="/var/db/pkg/repo-packagesite.sqlite"
50
51#
52# Default path to on-disk cache INDEX file
53#
54PACKAGES_INDEX_CACHEFILE="/var/run/bsdconfig/packages_INDEX.cache"
55
56#
57# INDEX format for FreeBSD-6.0 or higher:
58#
59# package|port-origin|install-prefix|comment|port-desc-file|maintainer|
60# categories|build-deps|run-deps|www-site|reserve|reserve|reserve|disc
61#
62INDEX_FORMAT="%n-%v" # package
63INDEX_FORMAT="$INDEX_FORMAT|/usr/ports/%o" # port-origin
64INDEX_FORMAT="$INDEX_FORMAT|%p" # install-prefix
65INDEX_FORMAT="$INDEX_FORMAT|%c" # comment
66INDEX_FORMAT="$INDEX_FORMAT|/usr/ports/%o/pkg-descr" # port-desc-file
67INDEX_FORMAT="$INDEX_FORMAT|%m" # maintainer
68INDEX_FORMAT="$INDEX_FORMAT|@CATEGORIES@" # place-holder
69INDEX_FORMAT="$INDEX_FORMAT|" # build-deps
70INDEX_FORMAT="$INDEX_FORMAT|@RUNDEPS@" # place-holder
71INDEX_FORMAT="$INDEX_FORMAT|%w" # www-site
72INDEX_FORMAT="$INDEX_FORMAT|" # reserved
73INDEX_FORMAT="$INDEX_FORMAT|" # reserved
74INDEX_FORMAT="$INDEX_FORMAT|" # reserved
75INDEX_FORMAT="$INDEX_FORMAT|" # disc
76
77############################################################ FUNCTIONS
78
79# f_index_initialize [$var_to_set]
80#
81# Read and initialize the global index. Returns success unless media cannot be
82# initialized for any reason (e.g. user cancels media selection dialog or an
83# error occurs). The index is sorted before being loaded into $var_to_set.
84#
85# NOTE: The index is processed with f_index_read() [below] after being loaded.
86#
87f_index_initialize()
88{
89 local __var_to_set="${2:-PACKAGE_INDEX}"
90
91 [ "$_INDEX_INITTED" ] && return $SUCCESS
92
93 # Got any media?
94 f_media_verify || return $FAILURE
95
96 # Does it move when you kick it?
97 f_device_init media || return $FAILURE
98
99 f_show_info "$msg_attempting_to_update_repository_catalogue"
100
101 #
102 # Generate $PACKAGESITE variable for pkg(8) based on media type
103 #
104 local __type __data __site
105 device_media get type __type
106 device_media get private __data
107 case "$__type" in
108 $DEVICE_TYPE_UFS|$DEVICE_TYPE_DISK) __site="file://$MOUNTPOINT" ;;
109 $DEVICE_TYPE_DIRECTORY) __site="file://$__data" ;;
110 $DEVICE_TYPE_FLOPPY) __site="file://${__data:-$MOUNTPOINT}" ;;
111 $DEVICE_TYPE_FTP) f_getvar $VAR_FTP_PATH __site ;;
112 $DEVICE_TYPE_HTTP_PROXY) f_getvar $VAR_HTTP_PROXY_PATH __site ;;
113 $DEVICE_TYPE_HTTP) f_getvar $VAR_HTTP_PATH __site ;;
114 $DEVICE_TYPE_CDROM) __site="file://$MOUNTPOINT" ;;
115 $DEVICE_TYPE_USB) __site="file://$MOUNTPOINT" ;;
116 $DEVICE_TYPE_DOS) __site="file://$MOUNTPOINT" ;;
117 $DEVICE_TYPE_NFS) __site="file://$MOUNTPOINT" ;;
118 esac
119
120 if ! PACKAGESITE="$__site" f_quietly pkg update; then
121 f_show_err "$msg_unable_to_update_pkg_from_selected_media"
122 f_device_shutdown media
123 return $FAILURE
124 fi
125
126 #
127 # Try to get contents from validated on-disk cache
128 #
129
130 #
131 # Calculate digest used to determine if the on-disk persistant cache
132 # INDEX (containing this digest on the first line) is valid and can be
133 # used to quickly populate the environment.
134 #
135 local __sqlite_digest
136 if ! __sqlite_digest=$( md5 < "$SQLITE_REPO" 2> /dev/null ); then
137 f_show_err "$msg_no_pkg_database_found"
138 f_device_shutdown media
139 return $FAILURE
140 fi
141
142 #
143 # Check to see if the persistant cache INDEX file exists
144 #
145 if [ -f "$PACKAGES_INDEX_CACHEFILE" ]; then
146 #
147 # Attempt to populate the environment with the (soon to be)
148 # validated on-disk cache. If validation fails, fall-back to
149 # generating a fresh cache.
150 #
151 if eval $__var_to_set='$(
152 ( # Get digest as the first word on first line
153 read digest rest_ignored
154
155 #
156 # If the stored digest matches the calculated-
157 # one populate the environment from the on-disk
158 # cache and provide success exit status.
159 #
160 if [ "$digest" = "$__sqlite_digest" ]; then
161 cat
162 exit $SUCCESS
163 else
164 # Otherwise, return the current value
165 eval echo \"\$__var_to_set\"
166 exit $FAILURE
167 fi
168 ) < "$PACKAGES_INDEX_CACHEFILE" 2> /dev/null
169 )'; then
170 f_show_info \
171 "$msg_located_index_now_reading_package_data_from_it"
172 if ! f_index_read "$__var_to_set"; then
173 f_show_err \
174 "$msg_io_or_format_error_on_index_file"
175 return $FAILURE
176 fi
177 _INDEX_INITTED=1
178 return $SUCCESS
179 fi
180 # Otherwise, fall-thru to create a fresh cache from scratch
181 fi
182
183 #
184 # If we reach this point, we need to generate the data from scratch
185 #
186
187 f_show_info "$msg_getting_package_categories_via_pkg_rquery"
188 if ! eval "$( pkg rquery "%n-%v %C" | awk '
189 { categories[$1] = categories[$1] " " $2 }
190 END {
191 for (package in categories)
192 {
193 cats = categories[package]
194 sub(/^ /, "", cats)
195 gsub(/[^[:alnum:]_]/, "_", package)
196 printf "local _%s_categories=\"%s\";\n", package, cats
197 }
198 }' )"; then
199 f_show_err "$msg_unable_to_pkg_rquery_package_dependeices"
200 f_device_shutdown media
201 return $FAILURE
202 fi
203
204 f_show_info "$msg_getting_package_dependencies_via_pkg_rquery"
205 if ! eval "$( pkg rquery "%n-%v %dn-%dv" | awk '
206 { rundeps[$1] = rundeps[$1] " " $2 }
207 END {
208 for (package in rundeps)
209 {
210 deps = rundeps[package]
211 sub(/^ /, "", deps)
212 gsub(/[^[:alnum:]_]/, "_", package)
213 printf "local _%s_rundeps=\"%s\";\n", package, deps
214 }
215 }' )"; then
216 f_show_err "$msg_unable_to_pkg_rquery_package_dependeices"
217 f_device_shutdown media
218 return $FAILURE
219 fi
220
221 f_show_info "$msg_generating_index_from_pkg_database"
222 eval "$__var_to_set"='$( pkg rquery "$INDEX_FORMAT" |
223 while read LINE; do
224 package="${LINE%%|*}";
225 f_str2varname "$package" varpkg;
226 eval f_replaceall \"\$LINE\" \"\|@CATEGORIES@\|\" \
227 \"\|\$_${varpkg}_categories\|\" LINE
228 eval f_replaceall \"\$LINE\" \"\|@RUNDEPS@\|\" \
229 \"\|\$_${varpkg}_rundeps\|\" LINE
230 echo "$LINE"
231 done
232 )' # always returns true (status of last item in pipe-chain)
233 eval "$__var_to_set"='$( debug= f_getvar "$__var_to_set" | sort )'
234
235 #
236 # Attempt to create the persistant on-disk cache
237 #
238
239 # Create a new temporary file to write to
240 local __tmpfile="$( mktemp -t "$pgm" )"
241 if [ "$__tmpfile" ]; then
242 # Write the temporary file contents
243 echo "$__sqlite_digest" > "$__tmpfile"
244 debug= f_getvar "$__var_to_set" >> "$__tmpfile"
245
246 # Finally, move the temporary file into place
247 case "$PACKAGES_INDEX_CACHEFILE" in
248 */*) f_quietly mkdir -p "${PACKAGES_INDEX_CACHEFILE%/*}"
249 esac
250 f_quietly mv -f "$__tmpfile" "$PACKAGES_INDEX_CACHEFILE"
251 fi
252
253 f_show_info "$msg_located_index_now_reading_package_data_from_it"
254 if ! f_index_read "$__var_to_set"; then
255 f_show_err "$msg_io_or_format_error_on_index_file"
256 return $FAILURE
257 fi
258
259 _INDEX_INITTED=1
260 return $SUCCESS
261}
262
263# f_index_read [$var_to_get]
--- 195 unchanged lines hidden ---
2#
3# Copyright (c) 2013 Devin Teske
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
--- 10 unchanged lines hidden (view full) ---
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD: head/usr.sbin/bsdconfig/share/packages/index.subr 257795 2013-11-07 12:40:42Z dteske $
28#
29############################################################ INCLUDES
30
31BSDCFG_SHARE="/usr/share/bsdconfig"
32. $BSDCFG_SHARE/common.subr || exit 1
33f_dprintf "%s: loading includes..." packages/index.subr
34f_include $BSDCFG_SHARE/device.subr
35f_include $BSDCFG_SHARE/media/common.subr
36f_include $BSDCFG_SHARE/strings.subr
37
38BSDCFG_LIBE="/usr/libexec/bsdconfig"
39f_include_lang $BSDCFG_LIBE/include/messages.subr
40
41############################################################ GLOBALS
42
43PACKAGE_INDEX=
44_INDEX_INITTED=
45
46#
47# Default path to pkg(8) repo-packagesite.sqlite database
48#
49SQLITE_REPO="/var/db/pkg/repo-packagesite.sqlite"
50
51#
52# Default path to on-disk cache INDEX file
53#
54PACKAGES_INDEX_CACHEFILE="/var/run/bsdconfig/packages_INDEX.cache"
55
56#
57# INDEX format for FreeBSD-6.0 or higher:
58#
59# package|port-origin|install-prefix|comment|port-desc-file|maintainer|
60# categories|build-deps|run-deps|www-site|reserve|reserve|reserve|disc
61#
62INDEX_FORMAT="%n-%v" # package
63INDEX_FORMAT="$INDEX_FORMAT|/usr/ports/%o" # port-origin
64INDEX_FORMAT="$INDEX_FORMAT|%p" # install-prefix
65INDEX_FORMAT="$INDEX_FORMAT|%c" # comment
66INDEX_FORMAT="$INDEX_FORMAT|/usr/ports/%o/pkg-descr" # port-desc-file
67INDEX_FORMAT="$INDEX_FORMAT|%m" # maintainer
68INDEX_FORMAT="$INDEX_FORMAT|@CATEGORIES@" # place-holder
69INDEX_FORMAT="$INDEX_FORMAT|" # build-deps
70INDEX_FORMAT="$INDEX_FORMAT|@RUNDEPS@" # place-holder
71INDEX_FORMAT="$INDEX_FORMAT|%w" # www-site
72INDEX_FORMAT="$INDEX_FORMAT|" # reserved
73INDEX_FORMAT="$INDEX_FORMAT|" # reserved
74INDEX_FORMAT="$INDEX_FORMAT|" # reserved
75INDEX_FORMAT="$INDEX_FORMAT|" # disc
76
77############################################################ FUNCTIONS
78
79# f_index_initialize [$var_to_set]
80#
81# Read and initialize the global index. Returns success unless media cannot be
82# initialized for any reason (e.g. user cancels media selection dialog or an
83# error occurs). The index is sorted before being loaded into $var_to_set.
84#
85# NOTE: The index is processed with f_index_read() [below] after being loaded.
86#
87f_index_initialize()
88{
89 local __var_to_set="${2:-PACKAGE_INDEX}"
90
91 [ "$_INDEX_INITTED" ] && return $SUCCESS
92
93 # Got any media?
94 f_media_verify || return $FAILURE
95
96 # Does it move when you kick it?
97 f_device_init media || return $FAILURE
98
99 f_show_info "$msg_attempting_to_update_repository_catalogue"
100
101 #
102 # Generate $PACKAGESITE variable for pkg(8) based on media type
103 #
104 local __type __data __site
105 device_media get type __type
106 device_media get private __data
107 case "$__type" in
108 $DEVICE_TYPE_UFS|$DEVICE_TYPE_DISK) __site="file://$MOUNTPOINT" ;;
109 $DEVICE_TYPE_DIRECTORY) __site="file://$__data" ;;
110 $DEVICE_TYPE_FLOPPY) __site="file://${__data:-$MOUNTPOINT}" ;;
111 $DEVICE_TYPE_FTP) f_getvar $VAR_FTP_PATH __site ;;
112 $DEVICE_TYPE_HTTP_PROXY) f_getvar $VAR_HTTP_PROXY_PATH __site ;;
113 $DEVICE_TYPE_HTTP) f_getvar $VAR_HTTP_PATH __site ;;
114 $DEVICE_TYPE_CDROM) __site="file://$MOUNTPOINT" ;;
115 $DEVICE_TYPE_USB) __site="file://$MOUNTPOINT" ;;
116 $DEVICE_TYPE_DOS) __site="file://$MOUNTPOINT" ;;
117 $DEVICE_TYPE_NFS) __site="file://$MOUNTPOINT" ;;
118 esac
119
120 if ! PACKAGESITE="$__site" f_quietly pkg update; then
121 f_show_err "$msg_unable_to_update_pkg_from_selected_media"
122 f_device_shutdown media
123 return $FAILURE
124 fi
125
126 #
127 # Try to get contents from validated on-disk cache
128 #
129
130 #
131 # Calculate digest used to determine if the on-disk persistant cache
132 # INDEX (containing this digest on the first line) is valid and can be
133 # used to quickly populate the environment.
134 #
135 local __sqlite_digest
136 if ! __sqlite_digest=$( md5 < "$SQLITE_REPO" 2> /dev/null ); then
137 f_show_err "$msg_no_pkg_database_found"
138 f_device_shutdown media
139 return $FAILURE
140 fi
141
142 #
143 # Check to see if the persistant cache INDEX file exists
144 #
145 if [ -f "$PACKAGES_INDEX_CACHEFILE" ]; then
146 #
147 # Attempt to populate the environment with the (soon to be)
148 # validated on-disk cache. If validation fails, fall-back to
149 # generating a fresh cache.
150 #
151 if eval $__var_to_set='$(
152 ( # Get digest as the first word on first line
153 read digest rest_ignored
154
155 #
156 # If the stored digest matches the calculated-
157 # one populate the environment from the on-disk
158 # cache and provide success exit status.
159 #
160 if [ "$digest" = "$__sqlite_digest" ]; then
161 cat
162 exit $SUCCESS
163 else
164 # Otherwise, return the current value
165 eval echo \"\$__var_to_set\"
166 exit $FAILURE
167 fi
168 ) < "$PACKAGES_INDEX_CACHEFILE" 2> /dev/null
169 )'; then
170 f_show_info \
171 "$msg_located_index_now_reading_package_data_from_it"
172 if ! f_index_read "$__var_to_set"; then
173 f_show_err \
174 "$msg_io_or_format_error_on_index_file"
175 return $FAILURE
176 fi
177 _INDEX_INITTED=1
178 return $SUCCESS
179 fi
180 # Otherwise, fall-thru to create a fresh cache from scratch
181 fi
182
183 #
184 # If we reach this point, we need to generate the data from scratch
185 #
186
187 f_show_info "$msg_getting_package_categories_via_pkg_rquery"
188 if ! eval "$( pkg rquery "%n-%v %C" | awk '
189 { categories[$1] = categories[$1] " " $2 }
190 END {
191 for (package in categories)
192 {
193 cats = categories[package]
194 sub(/^ /, "", cats)
195 gsub(/[^[:alnum:]_]/, "_", package)
196 printf "local _%s_categories=\"%s\";\n", package, cats
197 }
198 }' )"; then
199 f_show_err "$msg_unable_to_pkg_rquery_package_dependeices"
200 f_device_shutdown media
201 return $FAILURE
202 fi
203
204 f_show_info "$msg_getting_package_dependencies_via_pkg_rquery"
205 if ! eval "$( pkg rquery "%n-%v %dn-%dv" | awk '
206 { rundeps[$1] = rundeps[$1] " " $2 }
207 END {
208 for (package in rundeps)
209 {
210 deps = rundeps[package]
211 sub(/^ /, "", deps)
212 gsub(/[^[:alnum:]_]/, "_", package)
213 printf "local _%s_rundeps=\"%s\";\n", package, deps
214 }
215 }' )"; then
216 f_show_err "$msg_unable_to_pkg_rquery_package_dependeices"
217 f_device_shutdown media
218 return $FAILURE
219 fi
220
221 f_show_info "$msg_generating_index_from_pkg_database"
222 eval "$__var_to_set"='$( pkg rquery "$INDEX_FORMAT" |
223 while read LINE; do
224 package="${LINE%%|*}";
225 f_str2varname "$package" varpkg;
226 eval f_replaceall \"\$LINE\" \"\|@CATEGORIES@\|\" \
227 \"\|\$_${varpkg}_categories\|\" LINE
228 eval f_replaceall \"\$LINE\" \"\|@RUNDEPS@\|\" \
229 \"\|\$_${varpkg}_rundeps\|\" LINE
230 echo "$LINE"
231 done
232 )' # always returns true (status of last item in pipe-chain)
233 eval "$__var_to_set"='$( debug= f_getvar "$__var_to_set" | sort )'
234
235 #
236 # Attempt to create the persistant on-disk cache
237 #
238
239 # Create a new temporary file to write to
240 local __tmpfile="$( mktemp -t "$pgm" )"
241 if [ "$__tmpfile" ]; then
242 # Write the temporary file contents
243 echo "$__sqlite_digest" > "$__tmpfile"
244 debug= f_getvar "$__var_to_set" >> "$__tmpfile"
245
246 # Finally, move the temporary file into place
247 case "$PACKAGES_INDEX_CACHEFILE" in
248 */*) f_quietly mkdir -p "${PACKAGES_INDEX_CACHEFILE%/*}"
249 esac
250 f_quietly mv -f "$__tmpfile" "$PACKAGES_INDEX_CACHEFILE"
251 fi
252
253 f_show_info "$msg_located_index_now_reading_package_data_from_it"
254 if ! f_index_read "$__var_to_set"; then
255 f_show_err "$msg_io_or_format_error_on_index_file"
256 return $FAILURE
257 fi
258
259 _INDEX_INITTED=1
260 return $SUCCESS
261}
262
263# f_index_read [$var_to_get]
--- 195 unchanged lines hidden ---