From f923c68a3e183a0080bac11797380d6d8003b1aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Martin?= Date: Thu, 7 Mar 2019 10:21:13 +0100 Subject: [PATCH 1/6] speedup chown and chmod limit write operations. --- scripts/upgrade | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/scripts/upgrade b/scripts/upgrade index d58f4b0..35c1259 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -158,7 +158,8 @@ last_version=$next_version datadir="/home/yunohost.app/${app}/data" # Set write access for the following commands -chown -R $app: "$final_path" "$datadir" +chown -R $app: "$final_path" +find ${datadir}/ \! -user $app -o \! -group $app -exec chown $app: {} \; # Print the current version number of nextcloud exec_occ -V @@ -213,7 +214,8 @@ do ynh_secure_remove "$tmpdir" # Set write access for the following commands - chown -R $app: "$final_path" "$datadir" + chown -R $app: "$final_path" + find ${datadir}/ \! -user $app -o \! -group $app -exec chown $app: {} \; # Upgrade Nextcloud (SUCCESS = 0, UP_TO_DATE = 3) exec_occ maintenance:mode --off @@ -329,11 +331,12 @@ ynh_multimedia_addaccess $app #================================================= # Fix app ownerships & permissions -chown -R $app: "$final_path" "$datadir" +chown -R $app: "$final_path" +find ${datadir}/ \! -user $app -o \! -group $app -exec chown $app: {} \; 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 +find ${datadir}/ -type f \! -perm 0640 -print0 | xargs -0 chmod 0640 +find ${datadir}/ -type d \! -perm 0750 -print0 | xargs -0 chmod 0750 chmod 640 "${final_path}/config/config.php" chmod 755 /home/yunohost.app From ee4873fa230db6de0863eba946142a082cd018cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Martin?= Date: Thu, 7 Mar 2019 11:10:05 +0100 Subject: [PATCH 2/6] add comments --- scripts/upgrade | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/scripts/upgrade b/scripts/upgrade index 35c1259..94f977e 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -159,6 +159,7 @@ datadir="/home/yunohost.app/${app}/data" # Set write access for the following commands 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 -exec chown $app: {} \; # Print the current version number of nextcloud @@ -215,6 +216,7 @@ do # Set write access for the following commands 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 -exec chown $app: {} \; # Upgrade Nextcloud (SUCCESS = 0, UP_TO_DATE = 3) @@ -332,10 +334,13 @@ ynh_multimedia_addaccess $app # Fix app ownerships & permissions 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 -exec chown $app: {} \; find ${final_path}/ -type f -print0 | xargs -0 chmod 0644 find ${final_path}/ -type d -print0 | xargs -0 chmod 0755 +# Change permissions if the file in datadir does not have the right permissions find ${datadir}/ -type f \! -perm 0640 -print0 | xargs -0 chmod 0640 +# Change permissions if directories in datadir does not have the right permissions find ${datadir}/ -type d \! -perm 0750 -print0 | xargs -0 chmod 0750 chmod 640 "${final_path}/config/config.php" chmod 755 /home/yunohost.app From 5acd1f871bef7a572860e8b0c439e488284f17aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Martin?= Date: Thu, 7 Mar 2019 11:56:29 +0100 Subject: [PATCH 3/6] add double quote to protect datadir and final_path --- scripts/upgrade | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scripts/upgrade b/scripts/upgrade index 94f977e..eb21538 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -160,7 +160,7 @@ datadir="/home/yunohost.app/${app}/data" # Set write access for the following commands 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 -exec chown $app: {} \; +find "${datadir}/" \! -user $app -o \! -group $app -exec chown $app: {} \; # Print the current version number of nextcloud exec_occ -V @@ -217,7 +217,7 @@ do # Set write access for the following commands 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 -exec chown $app: {} \; + find "${datadir}/" \! -user $app -o \! -group $app -exec chown $app: {} \; # Upgrade Nextcloud (SUCCESS = 0, UP_TO_DATE = 3) exec_occ maintenance:mode --off @@ -335,13 +335,13 @@ ynh_multimedia_addaccess $app # Fix app ownerships & permissions 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 -exec chown $app: {} \; -find ${final_path}/ -type f -print0 | xargs -0 chmod 0644 -find ${final_path}/ -type d -print0 | xargs -0 chmod 0755 +find "${datadir}/" \! -user $app -o \! -group $app -exec chown $app: {} \; +find "${final_path}/" -type f -print0 | xargs -0 chmod 0644 +find "${final_path}/" -type d -print0 | xargs -0 chmod 0755 # Change permissions if the file in datadir does not have the right permissions -find ${datadir}/ -type f \! -perm 0640 -print0 | xargs -0 chmod 0640 +find "${datadir}/" -type f \! -perm 0640 -print0 | xargs -0 chmod 0640 # Change permissions if directories in datadir does not have the right permissions -find ${datadir}/ -type d \! -perm 0750 -print0 | xargs -0 chmod 0750 +find "${datadir}/" -type d \! -perm 0750 -print0 | xargs -0 chmod 0750 chmod 640 "${final_path}/config/config.php" chmod 755 /home/yunohost.app From 5e3ddb41c7ea9faf543e30347ace87a4efc35dae Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Thu, 7 Mar 2019 13:32:32 +0100 Subject: [PATCH 4/6] Fix find for files containing spaces --- scripts/upgrade | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/scripts/upgrade b/scripts/upgrade index eb21538..9b4e8ea 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -160,7 +160,7 @@ datadir="/home/yunohost.app/${app}/data" # Set write access for the following commands 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 -exec chown $app: {} \; +find "$datadir/" \! -user $app -o \! -group $app | xargs -d '\n' chown $app: # Print the current version number of nextcloud exec_occ -V @@ -217,7 +217,7 @@ do # Set write access for the following commands 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 -exec chown $app: {} \; + find "$datadir/" \! -user $app -o \! -group $app | xargs -d '\n' chown $app: # Upgrade Nextcloud (SUCCESS = 0, UP_TO_DATE = 3) exec_occ maintenance:mode --off @@ -335,13 +335,13 @@ ynh_multimedia_addaccess $app # Fix app ownerships & permissions 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 -exec chown $app: {} \; -find "${final_path}/" -type f -print0 | xargs -0 chmod 0644 -find "${final_path}/" -type d -print0 | xargs -0 chmod 0755 +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 # Change permissions if the file in datadir does not have the right permissions -find "${datadir}/" -type f \! -perm 0640 -print0 | xargs -0 chmod 0640 +find "$datadir/" -type f \! -perm 0640 | xargs -d '\n' chmod 0640 # Change permissions if directories in datadir does not have the right permissions -find "${datadir}/" -type d \! -perm 0750 -print0 | xargs -0 chmod 0750 +find "$datadir/" -type d \! -perm 0750 | xargs -d '\n' chmod 0750 chmod 640 "${final_path}/config/config.php" chmod 755 /home/yunohost.app From 1f5ac3e1742b0aca9e6c3c7914f62aa4c9428f97 Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Tue, 28 May 2019 20:37:34 +0200 Subject: [PATCH 5/6] 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 From cf88a5d0d00ae9cc310a561ee2d28171f6c244a4 Mon Sep 17 00:00:00 2001 From: JimboJoe Date: Wed, 29 May 2019 17:04:51 +0200 Subject: [PATCH 6/6] Typo fixes --- scripts/_common.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/_common.sh b/scripts/_common.sh index 3c2f82f..9d8cdca 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -341,7 +341,7 @@ ynh_smart_mktemp () { #================================================= -# Set permissions on files and directories with chown +# Set ownership on files and directories with chown # # Use find to apply permissions faster on very big directories. # @@ -372,9 +372,9 @@ ynh_chown () { 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. + # 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' ' force \n to be the delimiter of each entry instead of space. So xargs will handle correctly directories and files with spaces. + # ' -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\" @@ -426,9 +426,9 @@ ynh_chmod () { 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. + # 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' ' force \n to be the delimiter of each entry instead of space. So xargs will handle correctly directories and files with spaces. + # ' -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\"