diff --git a/scripts/_common.sh b/scripts/_common.sh index 98be903..2a545e3 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -84,6 +84,102 @@ ynh_smart_mktemp () { echo "$(mktemp --directory --tmpdir="$tmpdir")" } +#================================================= + +# Set ownership 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 apply chown only if the permissions aren't correct. + # '\!' is used to have a negation on -user and -group. + # ' -d '\n' ' forces \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 apply chmod only if the permissions aren't correct. + # '\!' is used to have a negation on -perm. + # ' -d '\n' ' forces \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 51d8b0f..ba34521 100755 --- a/scripts/install +++ b/scripts/install @@ -305,10 +305,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 44f8384..7276aef 100755 --- a/scripts/restore +++ b/scripts/restore @@ -133,7 +133,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 205cc51..8a85b90 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -222,7 +222,9 @@ then last_major_version=${last_version%%.*} # Set write access for the following commands - 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 + ynh_chown --user=$app --file="$datadir" --recursive # Print the current version number of Nextcloud exec_occ -V @@ -307,7 +309,8 @@ EOF 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 @@ -417,11 +420,15 @@ exec_occ background:cron #================================================= # 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 +chown -R $app: "$final_path" +# Change of owner and group if files in datadir does not have the right permissions +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 +ynh_chmod --permissions=0640 --file="$datadir" --recursive --type=file +# Change permissions if directories in datadir does not have the right permissions +ynh_chmod --permissions=0750 --file="$datadir" --recursive --type=dir chmod 640 "$final_path/config/config.php" chmod 755 /home/yunohost.app