1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/nextcloud_ynh.git synced 2024-09-03 19:55:57 +02:00

Use ynh_chmod and ynh_chown instead of find

This commit is contained in:
Maniack Crudelis 2019-05-28 20:37:34 +02:00
parent 38e7a2886c
commit 1f5ac3e174
4 changed files with 112 additions and 14 deletions

View file

@ -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
#=================================================

View file

@ -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

View file

@ -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

View file

@ -196,7 +196,7 @@ then
# 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 | 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