mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
52 lines
1.5 KiB
Text
52 lines
1.5 KiB
Text
CAN_BIND=${CAN_BIND:-1}
|
|
|
|
# Bind a directory or copy it on error
|
|
#
|
|
# usage: ynh_bind_or_cp srcdir destdir as_root
|
|
# | arg: srcdir - directory to bind or copy
|
|
# | arg: destdir - mountpoint or destination directory
|
|
# | arg: as_root - 1 to execute commands as root
|
|
ynh_bind_or_cp() {
|
|
local SRCDIR=$1
|
|
local DESTDIR=$2
|
|
local SUDO_CMD=
|
|
[[ "${3}" = "1" ]] && SUDO_CMD="sudo"
|
|
|
|
[[ ! -f "${DESTDIR}" ]] || {
|
|
echo "Destination directory '${DESTDIR}' already exists" >&2
|
|
return 1
|
|
}
|
|
|
|
# attempt to bind mounting the directory
|
|
if [[ "${CAN_BIND}" = "1" ]]; then
|
|
eval $SUDO_CMD mkdir -p "${DESTDIR}"
|
|
|
|
if sudo mount --rbind "${SRCDIR}" "${DESTDIR}"; then
|
|
return 0
|
|
else
|
|
CAN_BIND=0
|
|
echo "Bind mounting seems to be disabled on your system."
|
|
echo "You have maybe to check your apparmor configuration."
|
|
fi
|
|
|
|
# delete mountpoint directory safely
|
|
mountpoint -q "${DESTDIR}" && sudo umount -R "${DESTDIR}"
|
|
eval $SUDO_CMD rm -rf "${DESTDIR}"
|
|
fi
|
|
|
|
# ... or just copy the directory
|
|
eval $SUDO_CMD mkdir -p $(dirname "${DESTDIR}")
|
|
eval $SUDO_CMD cp -a "${SRCDIR}" "${DESTDIR}"
|
|
}
|
|
|
|
# Create a directory under /tmp
|
|
#
|
|
# usage: ynh_mkdir_tmp
|
|
# | ret: the created directory path
|
|
ynh_mkdir_tmp() {
|
|
TMPDIR="/tmp/$(ynh_string_random 6)"
|
|
while [ -d $TMPDIR ]; do
|
|
TMPDIR="/tmp/$(ynh_string_random 6)"
|
|
done
|
|
mkdir -p "$TMPDIR" && echo "$TMPDIR"
|
|
}
|