lib: nove lock functions to util-msg

This commit is contained in:
udeved 2017-04-19 08:50:08 +02:00
parent c6bd1d3a5e
commit f3bbc3b96f
2 changed files with 61 additions and 71 deletions

View file

@ -72,6 +72,35 @@ stat_done() {
printf "${BOLD}done${ALL_OFF}\n" >&2 printf "${BOLD}done${ALL_OFF}\n" >&2
} }
lock_close() {
local fd=$1
exec {fd}>&-
}
lock() {
if ! [[ "/dev/fd/$1" -ef "$2" ]]; then
mkdir -p -- "$(dirname -- "$2")"
eval "exec $1>"'"$2"'
fi
if ! flock -n $1; then
stat_busy "$3"
flock $1
stat_done
fi
}
slock() {
if ! [[ "/dev/fd/$1" -ef "$2" ]]; then
mkdir -p -- "$(dirname -- "$2")"
eval "exec $1>"'"$2"'
fi
if ! flock -sn $1; then
stat_busy "$3"
flock -s $1
stat_done
fi
}
cleanup() { cleanup() {
exit ${1:-0} exit ${1:-0}
} }

View file

@ -123,7 +123,6 @@ get_timer(){
echo $(date +%s) echo $(date +%s)
} }
# $1: start timer # $1: start timer
elapsed_time(){ elapsed_time(){
echo $(echo $1 $(get_timer) | awk '{ printf "%0.2f",($2-$1)/60 }') echo $(echo $1 $(get_timer) | awk '{ printf "%0.2f",($2-$1)/60 }')
@ -133,35 +132,6 @@ show_elapsed_time(){
info "Time %s: %s minutes" "$1" "$(elapsed_time $2)" info "Time %s: %s minutes" "$1" "$(elapsed_time $2)"
} }
lock_close() {
local fd=$1
exec {fd}>&-
}
lock() {
if ! [[ "/dev/fd/$1" -ef "$2" ]]; then
mkdir -p -- "$(dirname -- "$2")"
eval "exec $1>"'"$2"'
fi
if ! flock -n $1; then
stat_busy "$3"
flock $1
stat_done
fi
}
slock() {
if ! [[ "/dev/fd/$1" -ef "$2" ]]; then
mkdir -p -- "$(dirname -- "$2")"
eval "exec $1>"'"$2"'
fi
if ! flock -sn $1; then
stat_busy "$3"
flock -s $1
stat_done
fi
}
copy_mirrorlist(){ copy_mirrorlist(){
cp -a /etc/pacman.d/mirrorlist "$1/etc/pacman.d/" cp -a /etc/pacman.d/mirrorlist "$1/etc/pacman.d/"
} }
@ -692,29 +662,6 @@ show_config(){
fi fi
} }
# $1: chroot
# kill_chroot_process(){
# # enable to have more debug info
# #msg "machine-id (etc): $(cat $1/etc/machine-id)"
# #[[ -e $1/var/lib/dbus/machine-id ]] && msg "machine-id (lib): $(cat $1/var/lib/dbus/machine-id)"
# #msg "running processes: "
# #lsof | grep $1
#
# local prefix="$1" flink pid name
# for root_dir in /proc/*/root; do
# flink=$(readlink $root_dir)
# if [ "x$flink" != "x" ]; then
# if [ "x${flink:0:${#prefix}}" = "x$prefix" ]; then
# # this process is in the chroot...
# pid=$(basename $(dirname "$root_dir"))
# name=$(ps -p $pid -o comm=)
# info "Killing chroot process: %s (%s)" "$name" "$pid"
# kill -9 "$pid"
# fi
# fi
# done
# }
create_min_fs(){ create_min_fs(){
msg "Creating install root at %s" "$1" msg "Creating install root at %s" "$1"
mkdir -m 0755 -p $1/var/{cache/pacman/pkg,lib/pacman,log} $1/{dev,run,etc} mkdir -m 0755 -p $1/var/{cache/pacman/pkg,lib/pacman,log} $1/{dev,run,etc}
@ -769,30 +716,44 @@ check_root() {
fi fi
} }
##
# usage : is_btrfs( $path )
# return : whether $path is on a btrfs
##
is_btrfs() { is_btrfs() {
[[ -e "$1" && "$(stat -f -c %T "$1")" == btrfs ]] [[ -e "$1" && "$(stat -f -c %T "$1")" == btrfs ]]
} }
##
# usage : subvolume_delete_recursive( $path )
#
# Find all btrfs subvolumes under and including $path and delete them.
##
subvolume_delete_recursive() { subvolume_delete_recursive() {
local subvol local subvol
is_btrfs "$1" || return 0 is_btrfs "$1" || return 0
while IFS= read -d $'\0' -r subvol; do while IFS= read -d $'\0' -r subvol; do
if ! btrfs subvolume delete "$subvol" &>/dev/null; then if ! btrfs subvolume delete "$subvol" &>/dev/null; then
error "Unable to delete subvolume %s" "$subvol" error "Unable to delete subvolume %s" "$subvol"
return 1 return 1
fi fi
done < <(find "$1" -xdev -depth -inum 256 -print0) done < <(find "$1" -xdev -depth -inum 256 -print0)
return 0 return 0
} }
# $1: chroot
# kill_chroot_process(){
# # enable to have more debug info
# #msg "machine-id (etc): $(cat $1/etc/machine-id)"
# #[[ -e $1/var/lib/dbus/machine-id ]] && msg "machine-id (lib): $(cat $1/var/lib/dbus/machine-id)"
# #msg "running processes: "
# #lsof | grep $1
#
# local prefix="$1" flink pid name
# for root_dir in /proc/*/root; do
# flink=$(readlink $root_dir)
# if [ "x$flink" != "x" ]; then
# if [ "x${flink:0:${#prefix}}" = "x$prefix" ]; then
# # this process is in the chroot...
# pid=$(basename $(dirname "$root_dir"))
# name=$(ps -p $pid -o comm=)
# info "Killing chroot process: %s (%s)" "$name" "$pid"
# kill -9 "$pid"
# fi
# fi
# done
# }