qi: allow package cancelation in delete mode, e.g pressing Ctrl+C
[dragora.git] / qi / src / qi.in
bloba67ca87805b2052cf2bb62f72c89c139837537db
1 #! /bin/sh -
2 # Copyright (C) 2016-2018 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 " -x Extract packages for debugging purposes" \
44 "" \
45 "Common options:" \
46 " -L Print and exit default directory locations" \
47 " -N Don't read the configuration file" \
48 " -P <DIR> Package directory for (de)installations;" \
49 " only valid for -i, -d, or -u options" \
50 " -t <DIR> Target directory for symbolic links;" \
51 " only valid for -i, -d, or -u options" \
52 " -k Keep (don't delete) srcdir or destdir" \
53 " Keep (don't delete) package directory" \
54 " this is for -b, -d or -u options" \
55 " -p Prune conflicts on package (de)installation" \
56 " -r Use the named directory as the root directory" \
57 " for installing, deleting, or upgrading packages." \
58 " The target directory, package directory will be" \
59 " relative to this specific directory" \
60 " -v Be verbose (a 2nd -v gives more)" \
61 "" \
62 "Options for 'build' mode (-b):" \
63 " -O <DIR> Where the produced packages are written" \
64 " -W <DIR> Where archives, patches, and recipes are expected" \
65 " -Z <DIR> Where (compressed) sources will be found" \
66 " -a Architecture to use [detected]" \
67 " -f Force proceeding with a recipe" \
68 " -j Parallel jobs for the compiler" \
69 " -1 Increment release number (release + 1)" \
70 " -3 Resume skipping completed recipes option" \
71 " -n Don't create a .tlz package" \
72 "" \
73 "Other options:" \
74 " -h Display this help and exit" \
75 " -V Output version information" \
76 "" \
77 "Some influential environment variables:" \
78 " TMPDIR Temporary directory for sources" \
79 " QICFLAGS C compiler flags" \
80 " QICXXFLAGS C++ compiler flags" \
81 " QILDFLAGS Linker flags" \
82 "" \
83 "When FILE is -, read standard input." \
87 version()
89 printf "%s\n" \
90 "$PROGRAM @VERSION@" \
91 "Copyright (C) 2016-2018 Matias Andres Fonzo." \
92 "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>" \
93 "This is free software: you are free to change and redistribute it." \
94 "There is NO WARRANTY, to the extent permitted by law."
97 warn()
99 printf "%s\n" "$@" 1>&2
102 is_readable()
104 if test -e "$1"
105 then
106 if ! test -r "$1"
107 then
108 echo "${PROGRAM}: cannot read ${1}: Permission denied" 1>&2
109 return 1
111 else
112 echo "${PROGRAM}: cannot access ${1}: No such file or directory" 1>&2
113 return 1
117 # Portable alternative to the file operator -nt (among shells)
118 is_newer()
120 if test -n "$(find $1 -prune -newer $2 -print)"
121 then
122 return 0
124 return 1
127 # Determine whether $2 matches pattern $1
128 fnmatch()
130 case $2 in
132 return 0
135 return 1
137 esac
140 chkstatus_or_exit()
142 status=$?
144 if test $status -ne 0
145 then
146 echo "Return status = $status" 1>&2
147 exit ${1-2}; # If not given, defaults to 2
150 unset status
153 readconfig()
155 if test $RC = RC
156 then
157 is_readable "$HOME/.qirc" 2> /dev/null && RCFILE="$HOME/.qirc";
159 echo "Processing \`${RCFILE}' ..."
161 test -f "$RCFILE" || {
162 warn "${RCFILE} is not a regular file."
163 return 1
166 # Parse config file
167 while IFS='=' read -r variable value
169 case $variable in
170 \#* | "") # Ignore commented or blank lines
171 continue
173 esac
175 # Set variable name avoiding code execution
176 eval "$variable=\${value}"
177 done < "$RCFILE"
181 #### Mode functions
183 build_mode()
185 recipe=$1
187 # A recipe is any valid regular file, the current working directory
188 # has priority over the working tree (or where the recipes reside).
189 # The 'worktree' is the second place where to find a recipe. Also,
190 # we complete the possibility of using the directory name to invoke
191 # a recipe if it contains "recipe" as valid file name.
193 if test ! -f "$recipe"
194 then
195 if test -f "${recipe}/recipe"
196 then
197 recipe="${recipe}/recipe"
198 elif test -f "${worktree}/recipes/${recipe}/recipe"
199 then
200 recipe="${worktree}/recipes/${recipe}/recipe"
204 test -f "$recipe" || {
205 warn "\`${recipe}' is not a regular file."
206 return 4
209 # Complain if the file name is not "recipe"
211 if test "${recipe##*/}" = recipe
212 then
213 true
214 else
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 -r "${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_ignoreqsts" = opt_ignoreqsts
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_ignoreqsts=opt_ignoreqsts
386 readonly opt_ignoreqsts
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 echo "Running build() ..."
489 build ; set +e ; unset build
491 # Reset given signals
492 trap - EXIT HUP INT QUIT ABRT TERM
494 # If 'destdir' is empty, the package won't be created
495 if rmdir -- "$destdir" 2> /dev/null
496 then
497 warn "The package \"${full_pkgname}.tlz\" won't be created. 'destdir' is empty."
498 opt_nopkg=opt_nopkg
501 # Create (make) the package
503 if test "$opt_nopkg" != opt_nopkg
504 then
505 # Edit the recipe when 'release' is incremented
506 if test "$opt_incr_release" = opt_incr_release
507 then
508 echo ",s/^\(release\)=.*/\1=${release}/"$'\nw' | \
509 ed "${CWD}/$recipe" || chkstatus_or_exit
512 mkdir -p -- "${destdir}/var/lib/qi" || chkstatus_or_exit
514 # Include a recipe copy into the package
515 cp -p "${CWD}/$recipe" \
516 "${destdir}/var/lib/qi/${full_pkgname}.recipe" && \
517 chmod 644 "${destdir}/var/lib/qi/${full_pkgname}.recipe" || chkstatus_or_exit
519 # Detect post-install script for inclusion
521 if test -f "${CWD}/post-install"
522 then
523 echo "${CWD}/post-install: Detected."
524 cp -p "${CWD}/post-install" \
525 "${destdir}/var/lib/qi/${full_pkgname}.sh" && \
526 chmod 644 "${destdir}/var/lib/qi/${full_pkgname}.sh" || chkstatus_or_exit
529 # Detect declared package names for later replacement
531 if test -n "$replace"
532 then
533 warn \
534 "The following package names has been declared for replacement:" \
535 " $replace"
537 rm -f "${destdir}/var/lib/qi/${full_pkgname}.replace"
538 for item in $replace
540 echo "$replace" >> "${destdir}/var/lib/qi/${full_pkgname}.replace"
541 done
542 unset item
545 # Create meta file for the package information
546 echo "Creating meta file ${full_pkgname}.tlz.txt ..."
547 do_meta > "${outdir}/${full_pkgname}.tlz.txt" || chkstatus_or_exit
549 # Make a copy of it for the database
550 cp -p "${outdir}/${full_pkgname}.tlz.txt" \
551 "${destdir}/var/lib/qi/${full_pkgname}.txt" || chkstatus_or_exit
553 # Produce the package
554 cd -- "$destdir" && create_mode "${outdir}/${full_pkgname}.tlz"
557 # Back to the current working directory
558 cd -- "$CWD" || chkstatus_or_exit
560 # Delete 'srcdir' or 'destdir' if -k is not given
561 if test "$opt_keep" != opt_keep
562 then
563 echo "Deleting temporary directories ..."
565 srcdir="${srcdir%%/*}" # Directory name without parents.
567 if test -r "${TMPDIR}/$srcdir"
568 then
569 if test -z "$keep_srcdir"
570 then
571 rm -rf -- "${TMPDIR}/$srcdir" || chkstatus_or_exit
572 echo "removed directory: '${TMPDIR}/${srcdir}'"
573 else
574 warn "The variable 'keep_srcdir' is set:" \
575 "'${TMPDIR}/${srcdir}' will not be deleted."
579 if test -r "$destdir"
580 then
581 rm -rf -- "$destdir" || chkstatus_or_exit
582 echo "removed directory: '$destdir'"
586 # Install or update the package if -i or -u was passed
587 if test "$opt_nopkg" != opt_nopkg
588 then
589 if test "$opt_install" = opt_install
590 then
591 install_mode "${outdir}/${full_pkgname}.tlz"
592 elif test "$opt_update" = opt_update
593 then
594 upgrade_mode "${outdir}/${full_pkgname}.tlz"
598 echo ""
599 echo "All done for ${CWD}/${recipe}."
600 echo ""
603 create_mode()
605 directory=$(dirname -- "$1")
606 name=$(basename -- "$1")
608 # Perform sanity checks
610 is_readable "$directory" || exit 4
612 # Check again to find out if it is a valid directory
613 test -d "$directory" || {
614 warn "Package directory '$name' does not exist."
615 exit 4
618 test "$directory" = . && {
619 warn "Cannot create package on current working directory."
620 exit 4
623 test "$name" = "${name%.tlz}" && {
624 warn \
625 "Package format '$name' not supported." \
626 "It should be \`name-version-architecture+release.tlz'"
627 exit 4
630 echo "{#} Creating package $name at $directory ..."
632 ( umask 022 ; tar cvf - ./* | lzip -9cvv ) > "${directory}/$name"
633 chkstatus_or_exit 3
635 echo "Package created \"${directory}/${name}\"."
637 echo "${directory}/$name: Creating SHA256 checksum ..."
638 ( cd -- "$directory" && sha256sum $name > ${name}.sha256 )
640 # Remove used variables
641 unset directory name
644 delete_mode()
646 expunge="${1%%.tlz}"
648 echo "<<< Deleting package $rootdir${expunge} from $packagedir ..."
650 # If package is not a fully qualified directory,
651 # prepend it with the value of packagedir.
653 if ! fnmatch "${packagedir}*" "$expunge"
654 then
655 expunge="${packagedir}/$expunge"
658 # Complain if the package directory does not exist
660 test -d "$rootdir${expunge}" || {
661 warn "Package directory '$rootdir${expunge}' does not exist."
662 return 4
665 # Complain if the package directory cannot be well-read
667 is_readable "$rootdir${expunge}" || exit 4
669 # Ignore some signals up to completing the deinstallation
670 trap "" HUP QUIT ABRT TERM
672 # To catch and report on interruptions (e.g Ctrl+C),
673 # Note: SIGINT is allowed for cancel packages.
674 trap 'chkstatus_or_exit 2' INT
676 # Remove package from Graft control
678 # Scan for possible conflicts, stop if arise
679 if test "$opt_prune" != opt_prune
680 then
681 echo "Checking for possible conflicts ..."
682 if graft -d -n $graft_r -t "$targetdir" "$expunge" 2>&1 | \
683 grep ^CONFLICT
684 then
685 warn "" \
686 " A conflict occurred during uninstallation;" \
687 "Unless the -p option is given, this package will be PRESERVED."
688 return 6;
692 # Remove objects (files, links or directories) from the target
693 # directory that are in conflict with the package directory
695 echo "Pruning any conflict ..."
696 graft -p -D -u $graft_r -t "$targetdir" "$expunge"
697 chkstatus_or_exit 2
699 echo "Disabling links ..."
700 graft -d -D -u $graft_v $graft_r -t "$targetdir" "$expunge"
701 chkstatus_or_exit 2
703 # Delete package directory
704 if test "$opt_keep" != opt_keep
705 then
706 echo "Deleting package directory, if present ..."
708 if test -d "${rootdir}$expunge"
709 then
710 rm -rf -- "${rootdir}$expunge" || chkstatus_or_exit
711 echo "removed directory: '${rootdir}$expunge'"
715 # Reset given signals
716 trap - HUP INT QUIT ABRT TERM
718 # Remove used variables
719 unset expunge
722 install_mode()
724 # Complain if the package cannot be well-read
726 is_readable "$1" || exit 4
728 # Complain if the package does not end in .tlz
730 if ! fnmatch '*.tlz' "$1"
731 then
732 warn "\`${1}' does not end in .tlz"
733 return 4
736 # Make preparations to install the package
738 echo ">>> Installing package $1 ..."
740 # Get the filename
741 name=$(basename -- "$1" .tlz)
743 echo "Checking tarball integrity ..."
744 tar -tf "$1" > /dev/null
745 chkstatus_or_exit 3
747 # Create package directory using 'name'
748 if ! test -d "$rootdir${packagedir}/$name"
749 then
750 mkdir -p -- "$rootdir${packagedir}/$name" || chkstatus_or_exit
751 echo "mkdir: created directory '$rootdir${packagedir}/$name'"
754 # Scan for possible conflicts, stop if arise
755 if test "$opt_prune" != opt_prune
756 then
757 echo "Checking for possible conflicts ..."
758 if graft -i -n $graft_r -t "$targetdir" "${packagedir}/$name" 2>&1 | \
759 grep ^CONFLICT
760 then
761 warn "" \
762 " A conflict occurred during installation;" \
763 "Unless the -p option is given, this package won't be LINKED."
764 return 6;
768 # Ignore some signals up to completing the installation
769 trap "" HUP INT QUIT ABRT TERM
771 echo "Decompressing $1 ..."
772 ( cd -- "$rootdir${packagedir}/$name" && lzip -cd - | tar xpf - ) < "$1"
773 chkstatus_or_exit 3
775 # Transite package to Graft control
777 # Remove objects (files, links or directories) from the target
778 # directory that are in conflict with the package directory
779 echo "Pruning any conflict ..."
780 graft -p -D -u $graft_r -t "$targetdir" "${packagedir}/$name"
781 chkstatus_or_exit 2
783 echo "Enabling symbolic links ..."
784 graft -i -P $graft_v $graft_r -t "$targetdir" "${packagedir}/$name"
785 chkstatus_or_exit 2
787 # Avoid unnecessary runs coming from the upgrade_mode(),
788 # this is when the incoming package is **pre-installed**
790 if test "$_isUpgrade" != _isUpgrade.on
791 then
792 # Show package description
793 if test -r "$rootdir${packagedir}/${name}/var/lib/qi/${name}.txt"
794 then
795 grep '^#' "$rootdir${packagedir}/${name}/var/lib/qi/${name}.txt"
796 elif test -r "${1}.txt"
797 then
798 # From external meta file (current directory)
799 grep '^#' "${1}.txt"
800 else
801 warn "Description file not found for '$name'."
804 # Check and run the post-install script if exist
805 if test -r "$rootdir${packagedir}/${name}/var/lib/qi/${name}.sh"
806 then
807 echo "Running post-install script for \`${name}' ..."
809 # Rely on 'targetdir' if 'rootdir' is empty
810 cd -- "${rootdir:-$targetdir}"/ && \
811 . "$rootdir${packagedir}/${name}/var/lib/qi/${name}.sh"
815 # Check if there are declared packages for replacement
816 if test -r "$rootdir${packagedir}/${name}/var/lib/qi/${name}.replace"
817 then
818 while read -r line
820 for replace in "$rootdir${packagedir}/$(pkgbase $line)"-*
822 if ! test -e "$replace"
823 then
824 warn "${replace}: Declared package does not exist. (ignored)"
825 continue;
828 replace="${replace##*/}"
830 # The search for the package to be replaced cannot
831 # be the same to the incoming package, even to the
832 # temporary location coming from the upgrade_mode()
833 if test "$replace" = "$name" || test "$replace" = "${PRVLOC##*/}"
834 then
835 continue;
838 warn "WARNING: Replacing package \`${replace}' ..."
840 # Since the links belongs to the new package, only
841 # those which are not in conflict can be deleted.
842 # To complete, we will remove the package directory
844 graft -d -D -u $graft_r \
845 -t "$targetdir" "$replace" > /dev/null 2>&1
847 rm -rf -- "$rootdir${packagedir}/$replace"
848 done
849 done < "$rootdir${packagedir}/${name}/var/lib/qi/${name}.replace"
850 unset line
854 # Reset given signals
855 trap - HUP INT QUIT ABRT TERM
857 # Remove used variables
858 unset name
861 resolve_mode() {
862 # Complain if the file cannot be well-read
864 is_readable "$1" || exit 4
866 # Complain if the file does not end in .order
868 if ! fnmatch '*.order' "$1"
869 then
870 warn "\`${1}' does not end in .order"
871 return 4
874 # Get a clean list of the file while prints its content in reverse order,
875 # lines containing: colons, comments, parentheses, end of line, and blank
876 # lines, are removed. The parentheses are used to insert a reference.
877 # The last `awk' in the pipe: removes nonconsecutive lines, duplicate.
878 awk \
879 '{ gsub( /:|^#(.*)$|\([^)]*)|^$/,"" ); for( i=NF; i > 0; i-- ) print $i }' \
880 "$1" | awk '!s[$0]++'
883 upgrade_mode()
885 # Complain if the package is not a regular file
887 test -f "$1" || {
888 warn "\`${1}' is not a regular file."
889 return 4
892 # Get the filename
893 incoming=$(basename -- "$1" .tlz)
895 # Check for packages declared in the black list only for installation
897 for item in $blacklist
899 case $item in
900 ${incoming}*)
901 warn \
902 "*" \
903 "* The incoming package will be installed instead of being updated." \
904 "* This may be crucial for the correct functioning of \`${PROGRAM}'." \
906 opt_prune=opt_prune install_mode "$1"
907 return 0;;
908 esac
909 done
910 unset item
912 # Prepare the package to install it in a temporary location
914 # Set random directory using packagedir as prefix and 'incoming' as suffix
915 PRVLOC=$(mktemp -dp "$rootdir${packagedir}" ${incoming}.XXXXXXXXXXXX) || exit 2
917 # Pre-install the package in the custom 'packagedir'
919 save_packagedir="$packagedir"
920 packagedir="$PRVLOC"
922 echo "Pre-installing package using temporary location ..."
923 opt_prune=opt_prune # Turn on prune operation.
924 _isUpgrade=_isUpgrade.on install_mode "$1" > /dev/null
925 _isUpgrade=_isUpgrade.off
927 # Restore variable before looking for old packages
928 packagedir=$save_packagedir
929 unset save_packagedir
931 echo "Looking for installations under the same name ..."
932 for long_name in "$rootdir${packagedir}/$(pkgbase $incoming)"*
934 found="${long_name##*/}"
936 # The search for the package to be deleted
937 # cannot be the same to the temporary location
938 test "$long_name" = "$PRVLOC" && continue;
940 fnmatch "$(pkgbase $found)*" "$incoming" || continue;
941 echo "${long_name}: Detected."
943 # A package directory is preserved if -k is given
944 delete_mode "$found" > /dev/null
945 done
946 unset long_name found
948 # Re-install the package removing the temporary location
950 install_mode "$1"
951 opt_prune=opt_prune.off # Turn off prune operation.
953 echo "Deleting temporary location ..."
954 rm -rf -- "$PRVLOC" || chkstatus_or_exit
955 echo "removed directory: '$PRVLOC'"
957 echo ""
958 echo "Successful upgrade to '${incoming}'."
960 # Remove remaining variables
961 unset incoming PRVLOC
964 extract_mode()
966 # Perform sanity checks before package extraction
968 is_readable "$1" || exit 4
970 test -f "$1" || {
971 warn "\`${1}' is not a regular file."
972 return 4
975 # Preparations to extract the package
977 name=$(basename -- "$1" .tlz)
979 # Set random directory using 'name' as prefix
980 PRVDIR="${TMPDIR}/${name}.${RANDOM-0}$$"
982 # Trap to remove 'PRVDIR' on disruptions
983 trap "rm -rf -- $PRVDIR" HUP INT ABRT TERM
985 # Create 'PRVDIR' removing access for all but user
986 ( umask 077 ; mkdir -- $PRVDIR )
987 mkdir -p -m 700 -- $PRVDIR
989 echo "Extracting package $name ..."
990 ( umask 000 ; cd -- $PRVDIR && lzip -cd - | tar xpvf - ) < "$1"
991 if test $? -ne 0
992 then
993 # Try to remove (empty) 'PRVDIR' on failure
994 rmdir -- $PRVDIR
995 exit 3;
997 echo "$name has been extracted on $PRVDIR"
999 # Reset given signals
1000 trap - HUP INT ABRT TERM
1002 # Remove used variables
1003 unset name PRVDIR
1006 #### Extra functions used in the modes
1008 pkgbase()
1010 string=$(basename -- "$1" .tlz)
1012 # Take and count the dashes only
1014 dashes=$(echo "$string" | tr -cd '-')
1015 dashes="${#dashes}"
1017 # Match cases to print the package name
1018 case $dashes in
1020 # NAME-VERSION
1021 echo "${string%-*}"
1024 # NAME_[-LONG]-VERSION-ARCH+release
1025 echo "${string%-*-*}"
1027 esac
1029 unset string dashes
1032 unpack()
1034 for file in "$@"
1036 case $file in
1037 *.tar)
1038 tar tf "$file" > /dev/null && \
1039 tar xpf "$file"
1040 chkstatus_or_exit 3
1042 *.tar.gz | *.tgz | *.tar.Z )
1043 gzip -cd "$file" | tar tf - > /dev/null && \
1044 gzip -cd "$file" | tar xpf -
1045 chkstatus_or_exit 3
1047 *.tar.bz2 | *.tbz2 | *.tbz )
1048 bzip2 -cd "$file" | tar tf - > /dev/null && \
1049 bzip2 -cd "$file" | tar xpf -
1050 chkstatus_or_exit 3
1052 *.tar.lz | *.tlz )
1053 lzip -cd "$file" | tar tf - > /dev/null && \
1054 lzip -cd "$file" | tar xpf -
1055 chkstatus_or_exit 3
1057 *.tar.xz | *.txz )
1058 xz -cd "$file" | tar tf - > /dev/null && \
1059 xz -cd "$file" | tar xpf -
1060 chkstatus_or_exit 3
1062 *.zip | *.ZIP )
1063 unzip -t "$file" > /dev/null && \
1064 unzip "$file" > /dev/null
1065 chkstatus_or_exit 3
1067 *.gz)
1068 gzip -t "$file" && \
1069 gzip -cd "$file" > "$(basename -- $file .gz)"
1070 chkstatus_or_exit 3
1072 *.Z)
1073 gzip -t "$file" && \
1074 gzip -cd "$file" > "$(basename -- $file .Z)"
1075 chkstatus_or_exit 3
1077 *.bz2)
1078 bzip2 -t "$file" && \
1079 bzip2 -cd "$file" > "$(basename -- $file .bz2)"
1080 chkstatus_or_exit 3
1082 *.lz)
1083 lzip -t "$file" && \
1084 lzip -cd "$file" > "$(basename -- $file .lz)"
1085 chkstatus_or_exit 3
1087 *.xz)
1088 xz -t "$file" && \
1089 xz -cd "$file" > "$(basename -- $file .xz)"
1090 chkstatus_or_exit 3
1093 warn "${PROGRAM}: cannot unpack ${file}: Unsupported extension"
1094 exit 1
1095 esac
1096 done
1097 unset file
1100 do_meta()
1102 # Extract information from the recipe to create the meta file.
1104 # The package description is pre-formatted in 78 columns,
1105 # the '#' character and a space is added as prefix to conform
1106 # 80 columns in total
1108 cat << EOF
1109 $(echo "$description" | fold -w 78 | awk '$0="# " $0')
1111 QICFLAGS="$QICFLAGS"
1112 QICXXFLAGS="$QICXXFLAGS"
1113 QILDFLAGS="$QILDFLAGS"
1114 program=$program
1115 version=$version
1116 release=$release
1117 blurb="$(echo "$description" | sed -e '/^$/d;2q')"
1118 homepage="$homepage"
1119 license="$license"
1120 fetch="$fetch"
1121 replace="$replace"
1126 #### Default values
1128 PROGRAM="${0##*/}"
1129 packagedir=@PACKAGEDIR@
1130 targetdir=@TARGETDIR@
1131 blacklist="perl graft musl glibc"
1132 RC=RC
1133 RCFILE=@SYSCONFDIR@/qirc
1134 opt_install=opt_install.off
1135 opt_update=opt_update.off
1136 opt_force=opt_force.off
1137 opt_keep=opt_keep.off
1138 opt_incr_release=opt_incr_release.off
1139 opt_ignoreqsts=opt_ignoreqsts.off
1140 opt_nopkg=opt_nopkg.off
1141 opt_prune=opt_prune.off
1142 rootdir=""
1143 jobs=1
1144 mode=""
1145 verbose=0
1146 graft_v=-v
1147 graft_r=""
1148 _isUpgrade=_isUpgrade.off
1149 TMPDIR="${TMPDIR:=/usr/src/qi/build}"
1150 QICFLAGS="${QICFLAGS:=-g0 -Os}"
1151 QICXXFLAGS="${QICXXFLAGS:=$QICFLAGS}"
1152 QILDFLAGS="${QILDFLAGS:=-s}"
1153 worktree=/usr/src/qi
1154 tardir=${worktree}/sources
1155 outdir=/var/cache/qi/packages
1156 netget="wget -c -w1 -t3 --no-check-certificate"
1157 rsync="rsync -v -a -L -z -i --progress"
1158 configure_args="--prefix=@PREFIX@ --libexecdir=@LIBEXECDIR@ --bindir=@BINDIR@ --sbindir=@SBINDIR@ --sysconfdir=@SYSCONFDIR@ --localstatedir=@LOCALSTATEDIR@"
1159 infodir=@INFODIR@
1160 mandir=@MANDIR@
1161 docdir=@DOCDIR@
1163 # Store default locations
1164 QI_TARGETDIR=$targetdir
1165 QI_PACKAGEDIR=$packagedir
1166 QI_WORKTREE=$worktree
1167 QI_TARDIR=$tardir
1168 QI_OUTDIR=$outdir
1170 #### Parse options
1172 while getopts :bcdiouxLNP:t:fkvO:W:Z:a:j:13npr:hV name
1174 case $name in
1176 if test -z "$mode"
1177 then
1178 readconfig
1179 mode=build_mode
1183 mode=create_mode
1186 readconfig
1187 mode=delete_mode
1190 if test -z "$mode"
1191 then
1192 readconfig
1193 mode=install_mode
1195 if test "$mode" = build_mode
1196 then
1197 opt_install=opt_install
1201 mode=resolve_mode
1204 if test -z "$mode"
1205 then
1206 readconfig
1207 mode=upgrade_mode
1209 if test "$mode" = build_mode
1210 then
1211 opt_update=opt_update
1215 mode=extract_mode
1218 printf "%s\n" \
1219 "QI_TARGETDIR=$QI_TARGETDIR" \
1220 "QI_PACKAGEDIR=$QI_PACKAGEDIR" \
1221 "QI_WORKTREE=$QI_WORKTREE" \
1222 "QI_TARDIR=$QI_TARDIR" \
1223 "QI_OUTDIR=$QI_OUTDIR"
1224 exit 0
1227 RC=RC.off
1230 packagedir="$OPTARG"
1233 targetdir="$OPTARG"
1236 opt_force=opt_force
1239 opt_keep=opt_keep
1242 verbose=$(( verbose + 1 ))
1245 outdir="$OPTARG"
1248 worktree="$OPTARG"
1251 tardir="$OPTARG"
1254 arch="$OPTARG"
1257 jobs="$OPTARG"
1260 opt_incr_release=opt_incr_release
1263 opt_ignoreqsts=opt_ignoreqsts
1266 opt_nopkg=opt_nopkg
1269 opt_prune=opt_prune
1272 rootdir="$OPTARG"
1275 usage
1276 exit 0
1279 version
1280 exit 0
1283 warn "Option '-${OPTARG}' requires an argument"
1284 usage
1285 exit 1
1288 warn "Illegal option -- '-${OPTARG}'"
1289 usage
1290 exit 1
1292 esac
1293 done
1294 shift $(( OPTIND - 1 ))
1296 if test $# -eq 0
1297 then
1298 usage
1299 exit 1
1302 # Program sanity check
1303 for need in awk basename bzip2 chmod cp dirname find fold graft grep \
1304 lzip mkdir mktemp rm rmdir sed sha256sum stat tar tr
1306 if ! type $need 1> /dev/null 2> /dev/null
1307 then
1308 warn "${PROGRAM}: cannot operate without ${need}(1): Check your PATH"
1309 exit 2
1311 done
1312 unset need
1314 # Determine verbosity level/flag
1316 if test "$verbose" -gt 1
1317 then
1318 graft_v=-V
1321 # Read standard input if FILE is -, or when FILE
1322 # is not connected to a terminal.
1324 if test "$1" = - || test ! -t 0
1325 then
1326 # Unset positional parameters setting $# to zero
1327 set --
1329 # Assign remaining arguments to the positional parameters
1330 while read -r input
1332 set -- "$@" "$input"
1333 done
1336 # We need at least one operating mode
1337 if test -z "$mode"
1338 then
1339 usage
1340 exit 4
1343 umask 022; # Remove write permission for group and other.
1345 # Validate 'rootdir' directory
1347 if test -n "$rootdir"
1348 then
1349 if test -d "$rootdir" && test "$rootdir" != /
1350 then
1351 # Remove slash from the end
1352 rootdir="${rootdir%/}"
1354 # A workaround for graft-2.13+. The specified directory is
1355 # relative to the log file, we prepend it inside the rootdir
1357 eval "$(graft -L)" ; GRAFT_LOGFILE="${GRAFT_LOGFILE:=/var/log/graft}"
1358 mkdir -p -- "$rootdir$(dirname -- $GRAFT_LOGFILE)" || chkstatus_or_exit
1360 # Compose 'rootdir' and log file option to be used with graft(1)
1361 graft_r="-r $rootdir -l $GRAFT_LOGFILE"
1363 # Unset variables coming from eval
1364 unset GRAFT_PERL GRAFT_LOGFILE GRAFT_TARGETDIR GRAFT_PACKAGEDIR
1365 else
1366 warn "${PROGRAM}: \`${rootdir}' is not a qualified root directory"
1367 exit 4
1369 export rootdir
1372 # Ensure 'TMPDIR' creation to prefix temporary files
1374 if test ! -d "$TMPDIR"
1375 then
1376 mkdir -p -- "$TMPDIR" || chkstatus_or_exit
1378 readonly TMPDIR
1380 # Process each package or recipe provided on the command-line
1382 for package in "$@"
1384 $mode $package
1385 done