qi: improve warning mode for packages
[dragora.git] / qi / src / qi.in
bloba3a27e86f3972a9aecb8b650e162c9416aff7c9d
1 #! /bin/sh -
2 # Copyright (C) 2016-2019 Matias Fonzo <selk@dragora.org>
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # EXIT STATUS
18 # 0 = Successful completion
19 # 1 = Minor common errors (e.g: help usage, support not available)
20 # 2 = Command execution error
21 # 3 = Integrity check error for compressed files
22 # 4 = File empty, not regular, or expected
23 # 5 = Empty or not defined variable
24 # 6 = Package already installed
25 # 10 = Network manager error
27 #### Functions
29 usage()
31 printf "%s\n" \
32 "Qi - A practical and user-friendly package manager." \
33 "" \
34 "Usage: $PROGRAM [OPTION...] [FILE]..." \
35 "" \
36 "Operation mode:" \
37 " -b Build packages using recipes" \
38 " -c Create .tlz package from directory" \
39 " -d Delete packages" \
40 " -i Install packages" \
41 " -o Resolve build order through .order files" \
42 " -u Update packages (implies -i, -d with -p)" \
43 " -w Warn about files that will be installed" \
44 " -x Extract packages for debugging purposes" \
45 "" \
46 "Common options:" \
47 " -L Print default directory locations" \
48 " -N Don't read the configuration file" \
49 " -P <DIR> Package directory for (de)installations;" \
50 " (only valid for -i, -d, or -u options)" \
51 " -f Force package upgrade" \
52 " Force build a recipe" \
53 " -t <DIR> Target directory for symbolic links" \
54 " (only valid for -i, -d, or -u options)" \
55 " -k Keep (don't delete) srcdir or destdir" \
56 " Keep (don't delete) package directory" \
57 " (only valid for -b, -d or -u options)" \
58 " -p Prune conflicts on package (de)installation" \
59 " -r Use the named directory as the root directory" \
60 " for installing, deleting, or upgrading packages." \
61 " The target directory, package directory will be" \
62 " relative to this specific directory" \
63 " -v Be verbose (a 2nd -v gives more)" \
64 "" \
65 "Options for 'build' mode (-b):" \
66 " -O <DIR> Where the produced packages are written" \
67 " -W <DIR> Where archives, patches, and recipes are expected" \
68 " -Z <DIR> Where (compressed) sources will be found" \
69 " -a Architecture to use [detected]" \
70 " -j Parallel jobs for the compiler" \
71 " -1 Increment release number (release + 1)" \
72 " -n Don't create a .tlz package" \
73 " -S Selects the option to skip completed recipes" \
74 "" \
75 "Informative options:" \
76 " -h Display this help and exit" \
77 " -V Output version information" \
78 "" \
79 "Some influential environment variables:" \
80 " TMPDIR Temporary directory for sources" \
81 " QICFLAGS C compiler flags" \
82 " QICXXFLAGS C++ compiler flags" \
83 " QILDFLAGS Linker flags" \
84 "" \
85 "When FILE is -, read standard input." \
89 version()
91 printf "%s\n" \
92 "$PROGRAM @VERSION@" \
93 "Copyright (C) 2016-2019 Matias Andres Fonzo." \
94 "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>" \
95 "This is free software: you are free to change and redistribute it." \
96 "There is NO WARRANTY, to the extent permitted by law."
99 warn()
101 printf "%s\n" "$@" 1>&2
104 is_readable()
106 if test -e "$1"
107 then
108 if ! test -r "$1"
109 then
110 echo "${PROGRAM}: cannot read ${1}: Permission denied" 1>&2
111 return 1
113 else
114 echo "${PROGRAM}: cannot access ${1}: No such file or directory" 1>&2
115 return 1
119 # Portable alternative to the file operator -nt (among shells)
120 is_newer()
122 if test -n "$(find $1 -prune -newer $2 -print)"
123 then
124 return 0
126 return 1
129 # Determine whether $2 matches pattern $1
130 fnmatch()
132 case $2 in
134 return 0
137 return 1
139 esac
142 chkstatus_or_exit()
144 status=$?
146 if test $status -ne 0
147 then
148 echo "Return status = $status" 1>&2
149 exit ${1-2}; # If not given, defaults to 2
152 unset status
155 readconfig()
157 if test $RC = RC
158 then
159 is_readable "$HOME/.qirc" 2> /dev/null && RCFILE="$HOME/.qirc";
161 echo "Processing \`${RCFILE}' ..."
163 test -f "$RCFILE" || {
164 warn "${RCFILE} is not a regular file."
165 return 1
168 # Parse config file
169 while IFS='=' read -r variable value
171 case $variable in
172 \#* | "") # Ignore commented or blank lines
173 continue
175 esac
177 # Set variable name avoiding code execution
178 eval "$variable=\${value}"
179 done < "$RCFILE"
183 #### Mode functions
185 build_mode()
187 recipe=$1
189 # A recipe is any valid regular file, the current working directory
190 # has priority over the working tree (or where the recipes reside).
191 # The 'worktree' is the second place where to find a recipe. Also,
192 # we complete the possibility of using the directory name to invoke
193 # a recipe if it contains "recipe" as valid file name.
195 if test ! -f "$recipe"
196 then
197 if test -f "${recipe}/recipe"
198 then
199 recipe="${recipe}/recipe"
200 elif test -f "${worktree}/recipes/${recipe}/recipe"
201 then
202 recipe="${worktree}/recipes/${recipe}/recipe"
206 test -f "$recipe" || {
207 warn "\`${recipe}' is not a regular file."
208 return 4
211 # Complain if the file name is not "recipe"
213 if test "${recipe##*/}" != recipe
214 then
215 warn "\`${recipe}' is not a valid recipe name."
216 return 4
219 # Start preparations to import the recipe
221 # Separate the directory name from the file name,
222 # getting its absolute path and base name
224 CWD=$(CDPATH= cd -P -- $(dirname -- "$recipe") && printf "$PWD")
225 recipe=$(basename -- "$recipe")
227 # Check readability for load the recipe on success
229 is_readable "${CWD}/$recipe" || exit 4
231 # Find target architecture if 'arch' is not set
232 if test -z "$arch"
233 then
234 arch=$(${CC:-cc} -dumpmachine 2> /dev/null) || arch=unknown
235 arch="${arch%%-*}" # Get the rid of target triplet.
238 # Re-create external directories
239 mkdir -p -- "${worktree}/archive" \
240 "${worktree}/patches" \
241 "${worktree}/recipes" \
242 "$tardir"
244 # Variables treatment for the current and next recipe.
246 # Unset special variables that can only be predefined on
247 # the recipe and not coming from ${sysconfdir}/qirc
249 unset srcdir destdir pkgname pkgversion program version release \
250 fetch description homepage license replace full_pkgname \
251 CFLAGS CXXFLAGS LDFLAGS
253 # The following variables must be saved and restored
254 save_arch="${save_arch:=$arch}"
255 save_jobs="${save_jobs:=$jobs}"
256 save_outdir="${save_outdir:=$outdir}"
257 save_opt_nopkg="${save_opt_nopkg:=$opt_nopkg}"
259 # Reset variable values in case of return
260 arch=$save_arch
261 jobs=$save_jobs
262 outdir=$save_outdir
263 opt_nopkg=$save_opt_nopkg
265 # The following variables cannot be redefined on the recipe
266 readonly worktree netget rsync
268 # Import the recipe
270 echo "{@} Building from ${CWD}/$recipe ..."
271 . "${CWD}/$recipe"
273 # Check for required variables
274 if test -z "$program"
275 then
276 warn "${recipe}: The variable 'program' is not defined."
277 exit 5
279 if test -z "$version"
280 then
281 warn "${recipe}: The variable 'version' is not defined."
282 exit 5
284 if test -z "$release"
285 then
286 warn "${recipe}: The variable 'release' is not defined."
287 exit 5
290 # Pre-settings before to start building
292 # Increment the release number if the option was given
293 if test "$opt_incr_release" = opt_incr_release
294 then
295 release=$(( release + 1 ))
298 # Allow the dot as definition for 'tardir'
299 if test "$tardir" = .
300 then
301 tardir="$CWD"
304 # Set default values for the following special variables
306 pkgname="${pkgname:=$program}"
307 pkgversion="${pkgversion:=$version}"
308 srcdir="${srcdir:=${program}-$version}"
309 destdir="${destdir:=${TMPDIR}/package-$pkgname}"
311 # Complete package name adding 'pkgversion-arch+release'
312 full_pkgname="${full_pkgname:=$pkgname-${pkgversion}-${arch}+${release}}"
314 # If a package is going to be created, the existence of a
315 # previous build will be detected and reported. Under normal
316 # conditions the recipe is built as long as it is newer than
317 # the produced package, if not, we warn to the user about it.
318 # Rebuilding the package is possible (through the force ;-)
320 if test "$opt_nopkg" != opt_nopkg && \
321 { test "$opt_force" != opt_force && \
322 test -e "${outdir}/${full_pkgname}.tlz" ; }
323 then
324 if is_newer "${CWD}/$recipe" "${outdir}/${full_pkgname}.tlz"
325 then
326 warn \
327 "" \
328 "The recipe is more RECENT than the detected package:" \
329 "" \
330 "$( stat -c "%y %n" "${CWD}/$recipe" )" \
331 "$( stat -c "%y %n" "${outdir}/${full_pkgname}.tlz" )" \
332 "" \
333 " This recipe will be processed ..." \
335 elif test -e "${CWD}/post-install" && \
336 is_newer "${CWD}/post-install" "${CWD}/$recipe"
337 then
338 warn \
339 "" \
340 "The post-install script is more RECENT than the recipe:" \
341 "" \
342 "$( stat -c "%y %n" "${CWD}/post-install" )" \
343 "$( stat -c "%y %n" "${CWD}/$recipe" )" \
344 "" \
345 " The recipe will be re-processed ..." \
347 touch "${CWD}/$recipe"
348 elif test "$opt_skipqsts" = opt_skipqsts
349 then
350 warn "Recipe for '${full_pkgname}.tlz': Ignored." ""
351 return 0
352 else
353 warn \
354 "" \
355 "This recipe ALREADY produced a package:" \
356 "$( stat -c "%y %n" "${outdir}/${full_pkgname}.tlz" )" \
357 "" \
358 "The recipe is still OLDER than the produced package:" \
359 "$( stat -c "%y %n" "${CWD}/$recipe" )" \
360 "" \
361 " Probably nothing has changed." \
364 # In non-interactive mode, the user is asked about
365 # rebuilding the package. In interactive mode,
366 # the user need to pass the option explicitly
367 if test ! -t 0
368 then
369 printf "%b" \
370 "Do you want to rebuild this package?\n" \
371 "1) Yes, built it\n" \
372 "2) No, skipt it [default]\n" \
373 "3) Resume, skipping completed recipes\n" \
374 "Choose an option number: " > /dev/tty
375 IFS= read -r ANSWER < /dev/tty || exit 2;
376 case $ANSWER in
378 echo "$ANSWER" > /dev/tty
379 unset ANSWER
382 unset ANSWER
383 echo "Building UNPROCESSED/MODIFIED recipes ..." > /dev/tty
384 echo ""
385 opt_skipqsts=opt_skipqsts
386 readonly opt_skipqsts
387 return 0
390 unset ANSWER
391 echo "Recipe for '${full_pkgname}.tlz': Cancelled." > /dev/tty
392 echo ""
393 return 0
395 esac
396 else
397 warn "Use the -f option to re-process ${CWD}/$recipe." ""
398 return 6;
403 # Fetch remote sources
405 echo "Fetching remote sources if needed ..."
406 if test -n "$fetch"
407 then
408 for origin in $fetch
410 _source="${origin##*/}"
412 echo "Looking for \"$_source\" ..."
414 echo "Verifying checksum file \"${_source}.sha256\" from '${tardir}'"
415 if test -e "${tardir}/${_source}.sha256"
416 then
417 ( cd -- "$tardir" && sha256sum - ) < "${tardir}/${_source}.sha256"
418 chkstatus_or_exit
419 continue;
420 else
421 warn "${_source}.sha256: Checksum file does not exist, yet."
424 # Download source or resume if allowed
426 if test ! -e "${tardir}/$_source"
427 then
428 warn " Can't find $_source in ${tardir};" \
429 "attempting to get it from ${origin%/*} ..."
432 case $origin in
433 rsync://*)
435 cd -- "$tardir" && $rsync $origin || exit $?
436 sha256sum $_source > ${_source}.sha256
437 ); chkstatus_or_exit 10
439 *://*)
441 cd -- "$tardir" && $netget $origin || exit $?
442 sha256sum $_source > ${_source}.sha256
443 ); chkstatus_or_exit 10
446 warn "${PROGRAM}: Unrecognized protocol for ${origin}."
447 exit 4
448 esac
449 done
450 unset origin _source
451 else
452 warn "The variable 'fetch' is empty."
455 # Prepare special directories for build the source,
456 # the destination and the output of the package
458 echo "Preparing directories ..."
460 if test -d "${TMPDIR}/$srcdir" && test -z "$keep_srcdir"
461 then
462 rm -rf -- "${TMPDIR}/$srcdir" || chkstatus_or_exit
463 echo "removed directory: '${TMPDIR}/$srcdir'"
466 if test -d "$destdir"
467 then
468 rm -rf -- "$destdir" || chkstatus_or_exit
469 echo "removed directory: '$destdir'"
471 mkdir -p -- "$destdir" || chkstatus_or_exit
472 echo "mkdir: created directory '$destdir'"
474 if test ! -d "$outdir"
475 then
476 mkdir -p -- "$outdir" || chkstatus_or_exit
477 echo "mkdir: created directory '$outdir'"
480 echo "Entering to 'TMPDIR': $TMPDIR ..."
481 cd -- "$TMPDIR" || chkstatus_or_exit
483 # Set trap before to run the build() function in order
484 # to catch the return status, exit code 2 if fails
486 trap 'chkstatus_or_exit 2' EXIT HUP INT QUIT ABRT TERM
488 # Determine if the debugging indicators of the shell should be
489 # retained, assuming that it has been previously passed
490 case $- in *x*)
491 _xtrace_flag_is_set=xtrace_flag_is_set ;;
492 esac
494 echo "Running build() ..."
495 build
496 unset build
498 # Turn off possible shell flags coming from the recipe
500 set +e
501 if test "$_xtrace_flag_is_set" != xtrace_flag_is_set
502 then
503 set +x
506 # Reset given signals
507 trap - EXIT HUP INT QUIT ABRT TERM
509 # If 'destdir' is empty, the package won't be created
510 if rmdir -- "$destdir" 2> /dev/null
511 then
512 warn "The package \"${full_pkgname}.tlz\" won't be created. 'destdir' is empty."
513 opt_nopkg=opt_nopkg
516 # Create (make) the package
518 if test "$opt_nopkg" != opt_nopkg
519 then
520 # Edit the recipe when 'release' is incremented
521 if test "$opt_incr_release" = opt_incr_release
522 then
523 echo ",s/^\(release\)=.*/\1=${release}/"$'\nw' | \
524 ed "${CWD}/$recipe" || chkstatus_or_exit
527 mkdir -p -- "${destdir}/var/lib/qi" || chkstatus_or_exit
529 # Include a recipe copy into the package
530 cp -p "${CWD}/$recipe" \
531 "${destdir}/var/lib/qi/${full_pkgname}.recipe" && \
532 chmod 644 "${destdir}/var/lib/qi/${full_pkgname}.recipe" || chkstatus_or_exit
534 # Detect post-install script for inclusion
536 if test -f "${CWD}/post-install"
537 then
538 echo "${CWD}/post-install: Detected."
539 cp -p "${CWD}/post-install" \
540 "${destdir}/var/lib/qi/${full_pkgname}.sh" && \
541 chmod 644 "${destdir}/var/lib/qi/${full_pkgname}.sh" || chkstatus_or_exit
544 # Detect declared package names for later replacement
546 if test -n "$replace"
547 then
548 warn \
549 "The following package names has been declared for replacement:" \
550 " $replace"
552 rm -f "${destdir}/var/lib/qi/${full_pkgname}.replace"
553 for item in $replace
555 echo "$replace" >> "${destdir}/var/lib/qi/${full_pkgname}.replace"
556 done
557 unset item
560 # Create meta file for the package information
561 echo "Creating meta file ${full_pkgname}.tlz.txt ..."
562 do_meta > "${outdir}/${full_pkgname}.tlz.txt" || chkstatus_or_exit
564 # Make a copy of it for the database
565 cp -p "${outdir}/${full_pkgname}.tlz.txt" \
566 "${destdir}/var/lib/qi/${full_pkgname}.txt" || chkstatus_or_exit
568 # Produce the package
569 cd -- "$destdir" && create_mode "${outdir}/${full_pkgname}.tlz"
572 # Back to the current working directory
573 cd -- "$CWD" || chkstatus_or_exit
575 # Delete 'srcdir' or 'destdir' if -k was not given
576 if test "$opt_keep" != opt_keep
577 then
578 echo "Deleting temporary directories ..."
580 srcdir="${srcdir%%/*}" # Directory name without parents.
582 if test -r "${TMPDIR}/$srcdir"
583 then
584 if test -z "$keep_srcdir"
585 then
587 cd -- "$TMPDIR" && rm -rf -- "$srcdir" && \
588 echo "removed directory: '${TMPDIR}/${srcdir}'"
589 ); chkstatus_or_exit
590 else
591 warn "The variable 'keep_srcdir' has been set;" \
592 "'${TMPDIR}/${srcdir}' will not be deleted."
596 if test -r "$destdir"
597 then
598 rm -rf -- "$destdir" || chkstatus_or_exit
599 echo "removed directory: '$destdir'"
603 # Install or update the package if -i or -u was passed
604 if test "$opt_nopkg" != opt_nopkg
605 then
606 if test "$opt_install" = opt_install
607 then
608 install_mode "${outdir}/${full_pkgname}.tlz"
609 elif test "$opt_update" = opt_update
610 then
611 upgrade_mode "${outdir}/${full_pkgname}.tlz"
615 echo ""
616 echo "All done for ${CWD}/${recipe}."
617 echo ""
620 create_mode()
622 directory=$(dirname -- "$1")
623 name=$(basename -- "$1")
625 # Perform sanity checks
627 is_readable "$directory" || exit 4
629 # Check again to find out if it is a valid directory
630 test -d "$directory" || {
631 warn "Package directory '$name' does not exist."
632 exit 4
635 test "$directory" = . && {
636 warn "Cannot create package on current working directory."
637 exit 4
640 test "$name" = "${name%.tlz}" && {
641 warn \
642 "Package format '$name' not supported." \
643 "It should be \`name-version-architecture+release.tlz'"
644 exit 4
647 echo "{#} Creating package $name ..."
649 ( umask 022 ; tarlz --solid -9 -cvf - * ) > "${directory}/$name"
650 chkstatus_or_exit 3
652 ( cd -- "$directory" && sha256sum $name > ${name}.sha256 )
653 chkstatus_or_exit 4
655 echo "Package created \`${directory}/${name}'."
656 echo ""
658 # Remove used variables
659 unset directory name
662 delete_mode()
664 expunge="${packagedir}/$(basename -- $1 .tlz)"
666 echo "{<} Deleting package $rootdir${expunge} ..."
668 # Complain if the package directory does not exist
670 test -d "$rootdir${expunge}" || {
671 warn "Package directory '$rootdir${expunge}' does not exist."
672 return 4
675 # Complain if the package directory cannot be well-read
677 is_readable "$rootdir${expunge}" || exit 4
679 # Remove package from Graft control
681 # Scan for possible conflicts, stop if arise
682 if test "$opt_prune" != opt_prune
683 then
684 echo "Checking for possible conflicts ..."
685 if graft -d -n $graft_r -t "$targetdir" "$expunge" 2>&1 | \
686 grep ^CONFLICT
687 then
688 warn "" \
689 " A conflict occurred during uninstallation;" \
690 "Unless the -p option is given, this package will be PRESERVED."
691 return 6;
695 # Ignore some signals up to completing the deinstallation
696 trap "" HUP INT QUIT ABRT TERM
698 # Remove objects (files, links or directories) from the target
699 # directory that are in conflict with the package directory
701 echo "Pruning any conflict ..."
702 graft -p -D -u $graft_r -t "$targetdir" "$expunge"
703 chkstatus_or_exit 2
705 echo "Disabling links ..."
706 graft -d -D -u $graft_v $graft_r -t "$targetdir" "$expunge"
707 chkstatus_or_exit 2
709 # Delete package directory
710 if test "$opt_keep" != opt_keep
711 then
712 echo "Deleting package directory, if it exists as such ..."
714 if test -d "${rootdir}$expunge"
715 then
716 rm -rf -- "${rootdir}$expunge" || chkstatus_or_exit
717 echo "removed directory: '${rootdir}$expunge'"
721 # Reset given signals
722 trap - HUP INT QUIT ABRT TERM
724 # Remove used variables
725 unset expunge
728 install_mode()
730 # Complain if the package cannot be well-read
732 is_readable "$1" || exit 4
734 # Complain if the package does not end in .tlz
736 if ! fnmatch '*.tlz' "$1"
737 then
738 warn "\`${1}' does not end in .tlz"
739 return 4
742 # Make preparations to install the package
744 echo "{>} Installing package $1 ..."
746 # Get the filename
747 name=$(basename -- "$1" .tlz)
749 echo "Checking tarball integrity ..."
750 tarlz -tf "$1" > /dev/null
751 chkstatus_or_exit 3
753 # Create package directory using 'name'
754 if ! test -d "$rootdir${packagedir}/$name"
755 then
756 mkdir -p -- "$rootdir${packagedir}/$name" || chkstatus_or_exit
757 echo "mkdir: created directory '$rootdir${packagedir}/$name'"
760 # Scan for possible conflicts, stop if arise
761 if test "$opt_prune" != opt_prune
762 then
763 echo "Checking for possible conflicts ..."
764 if graft -i -n $graft_r -t "$targetdir" "${packagedir}/$name" 2>&1 | \
765 grep ^CONFLICT
766 then
767 warn "" \
768 " A conflict occurred during installation;" \
769 "Unless the -p option is given, this package won't be LINKED."
770 return 6;
774 # Ignore some signals up to completing the installation
775 trap "" HUP INT QUIT ABRT TERM
777 echo "Decompressing $1 ..."
778 ( cd -- "$rootdir${packagedir}/$name" && tarlz -xf - ) < "$1"
779 chkstatus_or_exit 3
781 # Transite package to Graft control
783 # Remove objects (files, links or directories) from the target
784 # directory that are in conflict with the package directory
785 echo "Pruning any conflict ..."
786 graft -p -D -u $graft_r -t "$targetdir" "${packagedir}/$name"
787 chkstatus_or_exit 2
789 echo "Enabling symbolic links ..."
790 graft -i -P $graft_v $graft_r -t "$targetdir" "${packagedir}/$name"
791 chkstatus_or_exit 2
793 # Avoid unnecessary runs coming from the upgrade_mode(),
794 # this is when the incoming package is **pre-installed**
796 if test "$_isUpgrade" != _isUpgrade.on
797 then
798 # Show package description
799 if test -r "$rootdir${packagedir}/${name}/var/lib/qi/${name}.txt"
800 then
801 grep '^#' "$rootdir${packagedir}/${name}/var/lib/qi/${name}.txt"
802 elif test -r "${1}.txt"
803 then
804 # From external meta file (current directory)
805 grep '^#' "${1}.txt"
806 else
807 warn "Description file not found for '$name'."
810 # Check and run the post-install script if exist
811 if test -r "$rootdir${packagedir}/${name}/var/lib/qi/${name}.sh"
812 then
813 echo "Running post-install script for \`${name}' ..."
815 # Rely on 'targetdir' if 'rootdir' is empty
816 cd -- "${rootdir:-$targetdir}"/ && \
817 . "$rootdir${packagedir}/${name}/var/lib/qi/${name}.sh"
821 # Check if there are declared packages for replacement
822 if test -r "$rootdir${packagedir}/${name}/var/lib/qi/${name}.replace"
823 then
824 while read -r line
826 for replace in "$rootdir${packagedir}/$(pkgbase $line)"-*
828 if ! test -e "$replace"
829 then
830 warn "${replace}: Declared package does not exist. (ignored)"
831 continue;
834 replace="${replace##*/}"
836 # The search for the package to be replaced cannot
837 # be the same to the incoming package, even to the
838 # temporary location coming from the upgrade_mode()
839 if test "$replace" = "$name" || test "$replace" = "${PRVLOC##*/}"
840 then
841 continue;
844 warn "WARNING: Replacing package \`${replace}' ..."
846 # Since the links belongs to the new package, only
847 # those which are not in conflict can be deleted.
848 # To complete, we will remove the package directory
850 graft -d -D -u $graft_r \
851 -t "$targetdir" "$replace" > /dev/null 2>&1
853 rm -rf -- "$rootdir${packagedir}/$replace"
854 done
855 done < "$rootdir${packagedir}/${name}/var/lib/qi/${name}.replace"
856 unset line
860 # Reset given signals
861 trap - HUP INT QUIT ABRT TERM
863 # Remove used variables
864 unset name
867 resolve_mode() {
868 # Complain if the file cannot be well-read
870 is_readable "$1" || exit 4
872 # Complain if the file does not end in .order
874 if ! fnmatch '*.order' "$1"
875 then
876 warn "\`${1}' does not end in .order"
877 return 4
880 # Get a clean list of the file while prints its content in reverse order,
881 # lines containing: colons, comments, parentheses, end of line, and blank
882 # lines, are removed. The parentheses are used to insert a reference.
883 # The last `awk' in the pipe: removes nonconsecutive lines, duplicate.
884 awk \
885 '{ gsub( /:|^#(.*)$|\([^)]*)|^$/,"" ); for( i=NF; i > 0; i-- ) print $i }' \
886 "$1" | awk '!s[$0]++'
889 upgrade_mode()
891 # Complain if the package does not end in .tlz
893 if ! fnmatch '*.tlz' "$1"
894 then
895 warn "\`${1}' does not end in .tlz"
896 return 4
899 # Get the filename
900 incoming=$(basename -- "$1" .tlz)
902 echo "{^} Upgrading to $incoming ..."
904 # Check packages in the blacklist for installation
906 echo "Checking blacklist ..."
907 for item in $blacklist
909 case $item in
910 ${incoming}*)
911 if test ! -e "$rootdir${packagedir}/$incoming"
912 then
913 warn \
914 "" \
915 " The package declared in the blacklist will be" \
916 "installed instead of being updated ..." \
918 opt_prune=opt_prune install_mode "$1"
919 return 0
922 if is_newer "$1" "$rootdir${packagedir}/$incoming"
923 then
924 warn \
925 "" \
926 "Incoming package is more RECENT than the installed package:" \
927 "" \
928 "$( stat -c "%y %n" "$1" )" \
929 "$( stat -c "%y %n" "$rootdir${packagedir}/$incoming" )" \
930 "" \
931 " The package declared in the blacklist will be" \
932 " installed as part of the upgrade process ..." \
934 opt_prune=opt_prune install_mode "$1"
935 touch "$rootdir${packagedir}/$incoming"
936 return 0
937 else
938 warn \
939 "" \
940 "Blacklisted package is already up-to-date:" \
941 "" \
942 "$( stat -c "%y %n" "$rootdir${packagedir}/$incoming" )" \
943 "$( stat -c "%y %n" "$1" )" \
945 return 6
948 esac
949 done
950 unset item
952 # Check package pre-existence
953 if test "$opt_force" != opt_force && \
954 test -e "$rootdir${packagedir}/$incoming"
955 then
956 warn \
957 "" \
958 " The package to be updated already exist;" \
959 "Unless the -f option is given, this package won't be UPGRADED."
960 return 6;
963 # Prepare the package to install it in a temporary location
965 # Set random directory using packagedir as prefix and 'incoming' as suffix
966 PRVLOC=$(mktemp -dp "$rootdir${packagedir}" ${incoming}.XXXXXXXXXXXX) || exit 2
968 # Pre-install the package in the custom 'packagedir'
970 save_packagedir="$rootdir${packagedir}"
971 packagedir="$PRVLOC"
973 echo "Pre-installing package in a temporary location ..."
974 opt_prune=opt_prune # Turn on prune operation.
975 _isUpgrade=_isUpgrade.on install_mode "$1" > /dev/null
976 _isUpgrade=_isUpgrade.off
978 # Restore variable before looking for old packages
979 packagedir=$save_packagedir
980 unset save_packagedir
982 echo "Looking for installations under the same name ..."
983 for long_name in "$rootdir${packagedir}/$(pkgbase $incoming)"*
985 found="${long_name##*/}"
987 # The search for the package to be deleted
988 # cannot be the same to the temporary location
989 test "$long_name" = "$PRVLOC" && continue;
991 fnmatch "$(pkgbase $found)*" "$incoming" || continue;
992 echo "${long_name}: Detected."
994 # A package directory is preserved if -k is given
995 delete_mode "$found" > /dev/null
996 done
997 unset long_name found
999 # Re-install the package removing the temporary location
1001 install_mode "$1"
1002 opt_prune=opt_prune.off # Turn off prune operation.
1004 echo "Deleting temporary location ..."
1005 rm -rf -- "$PRVLOC" || chkstatus_or_exit
1006 echo "removed directory: '$PRVLOC'"
1008 echo ""
1009 echo "Successful upgrade to '${incoming}'."
1011 # Remove remaining variables
1012 unset incoming PRVLOC
1015 warn_mode()
1017 # Complain if the package cannot be well-read
1019 is_readable "$1" || exit 4
1021 # Complain if the package does not end in .tlz
1023 if ! fnmatch '*.tlz' "$1"
1024 then
1025 warn "\`${1}' does not end in .tlz"
1026 return 4
1029 # List content of files excluding directories
1030 while test -f "$1"
1032 tarlz -tvvf "$1" | awk '!/^drwx/'
1033 chkstatus_or_exit 3
1034 shift;
1035 done
1036 return 0
1039 extract_mode()
1041 # Perform sanity checks before package extraction
1043 is_readable "$1" || exit 4
1045 test -f "$1" || {
1046 warn "\`${1}' is not a regular file."
1047 return 4
1050 # Preparations to extract the package
1052 name=$(basename -- "$1" .tlz)
1054 # Set random directory using 'name' as prefix
1055 PRVDIR="${TMPDIR}/${name}.${RANDOM-0}$$"
1057 # Trap to remove 'PRVDIR' on disruptions
1058 trap "rm -rf -- $PRVDIR" HUP INT ABRT TERM
1060 # Create 'PRVDIR' removing access for all but user
1061 ( umask 077 ; mkdir -- $PRVDIR )
1062 mkdir -p -m 700 -- $PRVDIR
1064 echo "Extracting package $name ..."
1065 ( umask 000 ; cd -- $PRVDIR && tarlz -xvf - ) < "$1"
1066 if test $? -ne 0
1067 then
1068 # Try to remove (empty) 'PRVDIR' on failure
1069 rmdir -- $PRVDIR
1070 exit 3;
1072 echo "$name has been extracted on $PRVDIR"
1074 # Reset given signals
1075 trap - HUP INT ABRT TERM
1077 # Remove used variables
1078 unset name PRVDIR
1081 #### Extra functions used in the modes
1083 pkgbase()
1085 string=$(basename -- "$1" .tlz)
1087 # Match cases to print the package name.
1089 # We will take into account the four segments removing
1090 # the last two to print the package (long) name
1091 case $string in
1092 *-*-*+*)
1093 echo "${string%-*-*}"
1096 echo "$string"
1098 esac
1100 unset string
1103 unpack()
1105 for file in "$@"
1107 case $file in
1108 *.tar)
1109 tar -tf "$file" > /dev/null && \
1110 tar -xpf "$file"
1111 chkstatus_or_exit 3
1113 *.tar.gz | *.tgz | *.tar.Z )
1114 gzip -cd "$file" | tar -tf - > /dev/null && \
1115 gzip -cd "$file" | tar -xpf -
1116 chkstatus_or_exit 3
1118 *.tar.bz2 | *.tbz2 | *.tbz )
1119 bzip2 -cd "$file" | tar -tf - > /dev/null && \
1120 bzip2 -cd "$file" | tar -xpf -
1121 chkstatus_or_exit 3
1123 *.tar.lz | *.tlz )
1124 tarlz -tf "$file" > /dev/null && \
1125 tarlz -xf "$file"
1126 chkstatus_or_exit 3
1128 *.tar.xz | *.txz )
1129 xz -cd "$file" | tar -tf - > /dev/null && \
1130 xz -cd "$file" | tar -xpf -
1131 chkstatus_or_exit 3
1133 *.zip | *.ZIP )
1134 unzip -t "$file" > /dev/null && \
1135 unzip "$file" > /dev/null
1136 chkstatus_or_exit 3
1138 *.gz)
1139 gzip -t "$file" && \
1140 gzip -cd "$file" > "$(basename -- $file .gz)"
1141 chkstatus_or_exit 3
1143 *.Z)
1144 gzip -t "$file" && \
1145 gzip -cd "$file" > "$(basename -- $file .Z)"
1146 chkstatus_or_exit 3
1148 *.bz2)
1149 bzip2 -t "$file" && \
1150 bzip2 -cd "$file" > "$(basename -- $file .bz2)"
1151 chkstatus_or_exit 3
1153 *.lz)
1154 lzip -t "$file" && \
1155 lzip -cd "$file" > "$(basename -- $file .lz)"
1156 chkstatus_or_exit 3
1158 *.xz)
1159 xz -t "$file" && \
1160 xz -cd "$file" > "$(basename -- $file .xz)"
1161 chkstatus_or_exit 3
1164 warn "${PROGRAM}: cannot unpack ${file}: Unsupported extension"
1165 exit 1
1166 esac
1167 done
1168 unset file
1171 do_meta()
1173 # Extract information from the recipe to create the meta file.
1175 # The package description is pre-formatted in 78 columns,
1176 # the '#' character and a space is added as prefix to conform
1177 # 80 columns in total
1179 cat << EOF
1180 $(echo "$description" | fold -w 78 | awk '$0="# " $0')
1182 QICFLAGS="$QICFLAGS"
1183 QICXXFLAGS="$QICXXFLAGS"
1184 QILDFLAGS="$QILDFLAGS"
1185 program=$program
1186 version=$version
1187 release=$release
1188 blurb="$(echo "$description" | sed -e '/^$/d;2q')"
1189 homepage="$homepage"
1190 license="$license"
1191 fetch="$fetch"
1192 replace="$replace"
1197 #### Default values
1199 PROGRAM="${0##*/}"
1200 packagedir=@PACKAGEDIR@
1201 targetdir=@TARGETDIR@
1202 blacklist="perl graft tarlz plzip musl glibc"
1203 RC=RC
1204 RCFILE=@SYSCONFDIR@/qirc
1205 opt_install=opt_install.off
1206 opt_update=opt_update.off
1207 opt_force=opt_force.off
1208 opt_keep=opt_keep.off
1209 opt_incr_release=opt_incr_release.off
1210 opt_skipqsts=opt_skipqsts.off
1211 opt_nopkg=opt_nopkg.off
1212 opt_prune=opt_prune.off
1213 rootdir=""
1214 jobs=1
1215 mode=""
1216 verbose=0
1217 graft_v=-v
1218 graft_r=""
1219 _isUpgrade=_isUpgrade.off
1220 TMPDIR="${TMPDIR:=/usr/src/qi/build}"
1221 QICFLAGS="${QICFLAGS:=-g0 -Os}"
1222 QICXXFLAGS="${QICXXFLAGS:=$QICFLAGS}"
1223 QILDFLAGS="${QILDFLAGS:=-s}"
1224 worktree=/usr/src/qi
1225 tardir=${worktree}/sources
1226 outdir=/var/cache/qi/packages
1227 netget="wget -c -w1 -t3 --no-check-certificate"
1228 rsync="rsync -v -a -L -z -i --progress"
1229 configure_args="--prefix=@PREFIX@ --libexecdir=@LIBEXECDIR@ --bindir=@BINDIR@ --sbindir=@SBINDIR@ --sysconfdir=@SYSCONFDIR@ --localstatedir=@LOCALSTATEDIR@"
1230 infodir=@INFODIR@
1231 mandir=@MANDIR@
1232 docdir=@DOCDIR@
1234 # Store (default) directory locations
1235 QI_TARGETDIR=$targetdir
1236 QI_PACKAGEDIR=$packagedir
1237 QI_WORKTREE=$worktree
1238 QI_TARDIR=$tardir
1239 QI_OUTDIR=$outdir
1241 #### Parse options
1243 while getopts :bcdiouwxLNP:t:fkvO:W:Z:a:j:1nSpr:hV name
1245 case $name in
1247 if test -z "$mode"
1248 then
1249 readconfig
1250 mode=build_mode
1254 mode=create_mode
1257 readconfig
1258 mode=delete_mode
1261 if test -z "$mode"
1262 then
1263 readconfig
1264 mode=install_mode
1266 if test "$mode" = build_mode
1267 then
1268 opt_install=opt_install
1272 mode=resolve_mode
1275 if test -z "$mode"
1276 then
1277 readconfig
1278 mode=upgrade_mode
1280 if test "$mode" = build_mode
1281 then
1282 opt_update=opt_update
1286 mode=warn_mode
1289 mode=extract_mode
1292 printf "%s\n" \
1293 "QI_TARGETDIR=$QI_TARGETDIR" \
1294 "QI_PACKAGEDIR=$QI_PACKAGEDIR" \
1295 "QI_WORKTREE=$QI_WORKTREE" \
1296 "QI_TARDIR=$QI_TARDIR" \
1297 "QI_OUTDIR=$QI_OUTDIR"
1298 exit 0
1301 RC=RC.off
1304 packagedir="$OPTARG"
1307 targetdir="$OPTARG"
1310 opt_force=opt_force
1313 opt_keep=opt_keep
1316 verbose=$(( verbose + 1 ))
1319 outdir="$OPTARG"
1322 worktree="$OPTARG"
1325 tardir="$OPTARG"
1328 arch="$OPTARG"
1331 jobs="$OPTARG"
1334 opt_incr_release=opt_incr_release
1337 opt_nopkg=opt_nopkg
1340 opt_skipqsts=opt_skipqsts
1343 opt_prune=opt_prune
1346 rootdir="$OPTARG"
1349 usage
1350 exit 0
1353 version
1354 exit 0
1357 warn "Option '-${OPTARG}' requires an argument"
1358 usage
1359 exit 1
1362 warn "Illegal option -- '-${OPTARG}'"
1363 usage
1364 exit 1
1366 esac
1367 done
1368 shift $(( OPTIND - 1 ))
1370 if test $# -eq 0
1371 then
1372 usage
1373 exit 1
1376 # Program sanity check
1377 for need in awk basename chmod cp dirname find fold graft grep \
1378 mkdir mktemp rm rmdir sed sha256sum stat tarlz
1380 if ! type $need 1> /dev/null 2> /dev/null
1381 then
1382 warn "${PROGRAM}: cannot operate without ${need}(1): Check your PATH"
1383 exit 2
1385 done
1386 unset need
1388 # Determine verbosity level/flag
1390 if test "$verbose" -gt 1
1391 then
1392 graft_v=-V
1395 # Read standard input if FILE is -, or when FILE
1396 # is not connected to a terminal.
1398 if test "$1" = - || test ! -t 0
1399 then
1400 # Unset positional parameters setting $# to zero
1401 set --
1403 # Assign remaining arguments to the positional parameters
1404 while read -r input
1406 set -- "$@" "$input"
1407 done
1410 # We need at least one operating mode
1411 if test -z "$mode"
1412 then
1413 usage
1414 exit 4
1417 umask 022; # Remove write permission for group and other.
1419 # Validate 'rootdir' directory
1421 if test -n "$rootdir"
1422 then
1423 if test -d "$rootdir" && test "$rootdir" != /
1424 then
1425 # Remove slash from the end
1426 rootdir="${rootdir%/}"
1428 # A workaround for graft-2.13+. The specified directory is
1429 # relative to the log file, we prepend it inside the rootdir
1431 eval "$(graft -L)" ; GRAFT_LOGFILE="${GRAFT_LOGFILE:=/var/log/graft}"
1432 mkdir -p -- "$rootdir$(dirname -- $GRAFT_LOGFILE)" || chkstatus_or_exit
1434 # Compose 'rootdir' and log file option to be used with graft(1)
1435 graft_r="-r $rootdir -l $GRAFT_LOGFILE"
1437 # Unset variables coming from eval
1438 unset GRAFT_PERL GRAFT_LOGFILE GRAFT_TARGETDIR GRAFT_PACKAGEDIR
1439 else
1440 warn "${PROGRAM}: \`${rootdir}' is not a qualified root directory"
1441 exit 4
1443 export rootdir
1446 # Ensure 'TMPDIR' creation to prefix temporary files
1448 if test ! -d "$TMPDIR"
1449 then
1450 mkdir -p -- "$TMPDIR" || chkstatus_or_exit
1452 readonly TMPDIR
1454 # Process each package or recipe provided on the command-line
1456 for package in "$@"
1458 $mode $package
1459 done