nix/home/program/bash/module/Own.nix

41 lines
891 B
Nix
Raw Normal View History

2024-11-04 04:37:29 +03:00
{ ... }: {
text = ''
# Change file ownership to specified user id and restrict access to him.
# Root user by default. This directory recursively by default.
# Usage: own [USER] [FILES]
function own() {
local IFS=$'\n'
local files=("''${@:2}")
local user="''${1}"
local group="''${1}"
2024-04-06 03:03:58 +03:00
2024-11-04 04:37:29 +03:00
# default to current dir.
if [ "''${files[*]}" = "" ]; then
files=(".")
fi
2024-04-06 03:03:58 +03:00
2024-11-04 04:37:29 +03:00
# default to current user.
if [ "''${user}" = "" ]; then
user="''${UID}"
fi
2024-04-06 03:03:58 +03:00
2024-11-04 04:37:29 +03:00
# If not root, default to users group.
[[ "''${user}" = 0 ]] && group="0" || group="100"
2024-04-06 03:03:58 +03:00
2024-11-04 04:37:29 +03:00
for file in "''${files[@]}"; do
# set ownership.
chown "''${user}":"''${group}" -R "''${file}" &> /dev/null
2024-04-06 03:03:58 +03:00
2024-11-04 04:37:29 +03:00
# remove access from group and others.
chmod -077 -R "''${file}"
done
}
2024-04-06 03:03:58 +03:00
2024-11-04 04:37:29 +03:00
function _complete_own() {
2024-12-12 05:27:20 +03:00
_autocomplete $(_get_users)
2024-11-04 04:37:29 +03:00
}
2024-04-06 03:03:58 +03:00
2024-11-04 04:37:29 +03:00
complete -F _complete_own own
'';
2024-04-06 03:03:58 +03:00
}