From 1f5ac3e1742b0aca9e6c3c7914f62aa4c9428f97 Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Tue, 28 May 2019 20:37:34 +0200 Subject: [PATCH] Use ynh_chmod and ynh_chown instead of find --- scripts/_common.sh | 96 ++++++++++++++++++++++++++++++++++++++++++++++ scripts/install | 8 ++-- scripts/restore | 3 +- scripts/upgrade | 19 ++++----- 4 files changed, 112 insertions(+), 14 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index 413f729..3c2f82f 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -339,6 +339,102 @@ ynh_smart_mktemp () { echo "$(sudo mktemp --directory --tmpdir="$tmpdir")" } +#================================================= + +# Set permissions on files and directories with chown +# +# Use find to apply permissions faster on very big directories. +# +# usage: ynh_chown --user=user [--group=group] --file="file_or_directory" [--recursive] +# | arg: -u, --user - Owner +# | arg: -g, --group - Owner group (Default same as --user) +# | arg: -f, --file - File or directory where permissions will be applied. +# | arg: -r, --recursive - Change permissions recursively +ynh_chown () { + # Declare an array to define the options of this helper. + local legacy_args=ugfr + declare -Ar args_array=( [u]=user= [g]=group= [f]=file= [r]=recursive ) + local user + local group + local file + local recursive + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + group="${group:-$user}" + recursive=${recursive:-0} + + if [ $recursive -eq 1 ] + then + local ending_slash="" + if [ -d "$file" ] + then + ending_slash=/ + fi + + # With very big directories, find is way faster than chown itself. + # Especially because find will check the permissions and applied chown only if the permissions aren't correct. + # '\!' is used to have a negation on -user and -group. + # ' -d '\n' ' force \n to be the delimiter of each entry instead of space. So xargs will handle correctly directories and files with spaces. + ynh_exec_warn_less "find \"$file$ending_slash\" \! -user $user -o \! -group $group | xargs --no-run-if-empty --delimiter='\n' chown --preserve-root $user:$group" + else + ynh_exec_warn_less chown $user:$group \"$file\" + fi +} + +# Set permissions on files and directories with chmod +# +# Use find to apply permissions faster on very big directories. +# +# usage: ynh_chmod --permissions=0755 --file="file_or_directory" [--recursive] [--type=file/dir] +# | arg: -p, --permissions - Permissions to apply with chmod. +# | arg: -f, --file - File or directory where permissions will be applied. +# | arg: -r, --recursive - Change permissions recursively +# | arg: -t, --type - Apply permissions only on regular files (file) or directories (dir) +ynh_chmod () { + # Declare an array to define the options of this helper. + local legacy_args=pfrt + declare -Ar args_array=( [p]=permissions= [f]=file= [r]=recursive [t]=type= ) + local permissions + local file + local recursive + local type + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + recursive=${recursive:-0} + type="${type:-}" + + if [ -n "$type" ] && [ "$type" != "file" ] && [ "$type" != "dir" ] + then + ynh_print_err --message="The value \"$type\" for --type is not recognized." + type="" + else + if [ "$type" == "file" ] + then + type="-type f" + elif [ "$type" == "dir" ] + then + type="-type d" + fi + fi + + if [ $recursive -eq 1 ] + then + local ending_slash="" + if [ -d "$file" ] + then + ending_slash=/ + fi + + # With very big directories, find is way faster than chmod itself. + # Especially because find will check the permissions and applied chmod only if the permissions aren't correct. + # '\!' is used to have a negation on -perm. + # ' -d '\n' ' force \n to be the delimiter of each entry instead of space. So xargs will handle correctly directories and files with spaces. + ynh_exec_warn_less "find \"$file$ending_slash\" $type \! -perm $permissions | xargs --no-run-if-empty --delimiter='\n' chmod --preserve-root $permissions" + else + ynh_exec_warn_less chmod $permissions \"$file\" + fi +} + #================================================= # FUTURE OFFICIAL HELPERS #================================================= diff --git a/scripts/install b/scripts/install index 7556b9d..cffcde4 100755 --- a/scripts/install +++ b/scripts/install @@ -303,10 +303,10 @@ ynh_multimedia_addaccess $app # Fix app ownerships & permissions chown -R $app: "$final_path" "$datadir" -find $final_path/ -type f -print0 | xargs -0 chmod 0644 -find $final_path/ -type d -print0 | xargs -0 chmod 0755 -find $datadir/ -type f -print0 | xargs -0 chmod 0640 -find $datadir/ -type d -print0 | xargs -0 chmod 0750 +ynh_chmod --permissions=0644 --file="$final_path" --recursive --type=file +ynh_chmod --permissions=0755 --file="$final_path" --recursive --type=dir +ynh_chmod --permissions=0640 --file="$datadir" --recursive --type=file +ynh_chmod --permissions=0750 --file="$datadir" --recursive --type=dir chmod 640 "$final_path/config/config.php" chmod 755 /home/yunohost.app diff --git a/scripts/restore b/scripts/restore index c6c8e7e..fc81779 100755 --- a/scripts/restore +++ b/scripts/restore @@ -124,7 +124,8 @@ mkdir -p "$datadir" #================================================= # Fix app ownerships & permissions -chown -R $app: "$final_path" "$datadir" +ynh_chown --user=$app --file="$final_path" --recursive +ynh_chown --user=$app --file="$datadir" --recursive chmod 640 "$final_path/config/config.php" chmod 755 /home/yunohost.app diff --git a/scripts/upgrade b/scripts/upgrade index 0dd4ea7..767fa84 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -194,9 +194,9 @@ then datadir="/home/yunohost.app/$app/data" # Set write access for the following commands - chown -R $app: "$final_path" + chown -R $app: "$final_path" # Change of owner and group if files in datadir does not have the right permissions - find "$datadir/" \! -user $app -o \! -group $app | xargs -d '\n' chown $app: + ynh_chown --user=$app --file="$datadir" --recursive # Print the current version number of nextcloud exec_occ -V @@ -251,7 +251,8 @@ then ynh_secure_remove --file="$tmpdir" # Set write access for the following commands - chown -R $app: "$final_path" "$datadir" + chown -R $app: "$final_path" + ynh_chown --user=$app --file="$datadir" --recursive # Upgrade Nextcloud (SUCCESS = 0, UP_TO_DATE = 3) exec_occ maintenance:mode --off @@ -390,15 +391,15 @@ ynh_multimedia_addaccess $app #================================================= # Fix app ownerships & permissions -chown -R $app: "$final_path" "$datadir" +chown -R $app: "$final_path" # Change of owner and group if files in datadir does not have the right permissions -find "$datadir/" \! -user $app -o \! -group $app | xargs -d '\n' chown $app: -find "$final_path/" -type f | xargs -d '\n' chmod 0644 -find "$final_path/" -type d | xargs -d '\n' chmod 0755 +ynh_chown --user=$app --file="$datadir" --recursive +ynh_chmod --permissions=0644 --file="$final_path" --recursive --type=file +ynh_chmod --permissions=0755 --file="$final_path" --recursive --type=dir # Change permissions if the file in datadir does not have the right permissions -find "$datadir/" -type f \! -perm 0640 | xargs -d '\n' chmod 0640 +ynh_chmod --permissions=0640 --file="$datadir" --recursive --type=file # Change permissions if directories in datadir does not have the right permissions -find "$datadir/" -type d \! -perm 0750 | xargs -d '\n' chmod 0750 +ynh_chmod --permissions=0750 --file="$datadir" --recursive --type=dir chmod 640 "$final_path/config/config.php" chmod 755 /home/yunohost.app