aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xlinks-chmod98
-rw-r--r--src/db.sh14
-rwxr-xr-xtest/test.sh40
3 files changed, 152 insertions, 0 deletions
diff --git a/links-chmod b/links-chmod
new file mode 100755
index 0000000..517ce1f
--- /dev/null
+++ b/links-chmod
@@ -0,0 +1,98 @@
+#!/usr/bin/env bash
+
+# Copyright (c) 2021 Egor Tensin <Egor.Tensin@gmail.com>
+# This file is part of the "Config file sharing" project.
+# For details, see https://github.com/egor-tensin/config-links.
+# Distributed under the MIT License.
+
+# usage: ./links-chmod [-h|--help] [-d|--database PATH] [-s|--shared-dir DIR] [-m|--mode MODE] [-n|--dry-run]
+
+set -o errexit
+set -o nounset
+set -o pipefail
+
+script_name="$( basename -- "${BASH_SOURCE[0]}" )"
+readonly script_name
+script_path="$( realpath --canonicalize-existing -- "${BASH_SOURCE[0]}" )"
+readonly script_path
+script_dir="$( dirname -- "$script_path" )"
+readonly script_dir
+src_dir="$( cd -- "$script_dir/src" && pwd )"
+readonly src_dir
+
+. "$src_dir/common.sh"
+. "$src_dir/os.sh"
+. "$src_dir/path.sh"
+. "$src_dir/vars.sh"
+. "$src_dir/db.sh"
+
+script_usage() {
+ local msg
+ for msg; do
+ echo "$script_name: $msg"
+ done
+
+ echo "usage: $script_name [-h|--help] [-d|--database PATH] [-s|--shared-dir DIR] [-n|--dry-run]
+ -h,--help show this message and exit
+ -d,--database set database file path
+ -s,--shared-dir set top-level shared directory path
+ (current working directory by default)
+ -n,--dry-run don't actually do anything intrusive
+ -m,--mode shared files mode (as in chmod)"
+}
+
+parse_script_options() {
+ while [ "$#" -gt 0 ]; do
+ local key="$1"
+ shift
+
+ case "$key" in
+ -h|--help)
+ script_usage
+ exit 0
+ ;;
+ -n|--dry-run)
+ set_dry_run
+ continue
+ ;;
+ -d|--database|-s|--shared-dir|-m|--mode)
+ ;;
+ *)
+ script_usage "unrecognized parameter: $key" >&2
+ exit 1
+ ;;
+ esac
+
+ if [ "$#" -eq 0 ]; then
+ script_usage "missing argument for parameter: $key" >&2
+ exit 1
+ fi
+
+ local value="$1"
+ shift
+
+ case "$key" in
+ -d|--database)
+ update_database_path "$value"
+ ;;
+ -s|--shared-dir)
+ update_shared_dir "$value"
+ ;;
+ -m|--mode)
+ mode="$value"
+ ;;
+ *)
+ script_usage "unrecognized parameter: $key" >&2
+ exit 1
+ ;;
+ esac
+ done
+}
+
+main() {
+ parse_script_options "$@"
+ read_database
+ chmod_entries "$mode"
+}
+
+main "$@"
diff --git a/src/db.sh b/src/db.sh
index 6145c6d..0022ab9 100644
--- a/src/db.sh
+++ b/src/db.sh
@@ -262,3 +262,17 @@ unlink_obsolete_entries() {
fi
done
}
+
+chmod_entries() {
+ if [ "$#" -ne 1 ]; then
+ echo "usage: ${FUNCNAME[0]} MODE" >&2
+ return 1
+ fi
+
+ local mode="$1"
+ shift
+
+ if [ "${#shared_paths[@]}" -ne 0 ]; then
+ is_dry_run || chmod -- "$mode" ${shared_paths[@]+"${shared_paths[@]}"}
+ fi
+}
diff --git a/test/test.sh b/test/test.sh
index 37167ec..db094ad 100755
--- a/test/test.sh
+++ b/test/test.sh
@@ -65,6 +65,10 @@ call_unlink() {
call_bin_script "$script_dir/../links-remove"
}
+call_chmod() {
+ call_bin_script "$script_dir/../links-chmod" "$@"
+}
+
verify_output() {
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
echo "usage: ${FUNCNAME[0]} EXPECTED_OUTPUT [DEST_DIR]" >&2
@@ -308,6 +312,40 @@ test_symlink_unlink_works() {
test "$copy_content" = '3'
}
+test_chmod_works() {
+ # Test that links-chmod works.
+ new_test
+ call_update
+
+ local expected_output="$test_dest_dir->
+$test_dest_dir/1.txt->$test_src_dir/%DEST%/1.txt
+$test_dest_dir/bar->
+$test_dest_dir/bar/3.txt->$test_src_dir/%DEST%/bar/3.txt
+$test_dest_dir/bar/baz->
+$test_dest_dir/bar/baz/4.txt->$test_src_dir/%DEST%/bar/baz/4.txt
+$test_dest_dir/foo->
+$test_dest_dir/foo/2.txt->$test_src_dir/%DEST%/foo/2.txt"
+ verify_output "$expected_output"
+
+ echo
+ echo 'Verifying 1.txt (the shared file) permissions...'
+
+ local mode
+
+ mode="$( stat -c '%a' -- "$test_src_dir/%DEST%/1.txt" )"
+ echo "Actual permissions: $mode"
+ test "$mode" = '644'
+
+ call_chmod --mode 0600
+
+ echo
+ echo 'Verifying 1.txt (the shared file) permissions have changed...'
+
+ mode="$( stat -c '%a' -- "$test_src_dir/%DEST%/1.txt" )"
+ echo "Actual permissions: $mode"
+ test "$mode" = '600'
+}
+
main() {
test_update_works
test_unlink_works
@@ -320,6 +358,8 @@ main() {
test_symlink_update_works
test_symlink_unlink_works
+
+ test_chmod_works
}
main