Quellcode durchsuchen

Refactor directory ownership check into a separate function

Vinicius Teshima vor 10 Monaten
Ursprung
Commit
a393f35cc8
1 geänderte Dateien mit 15 neuen und 28 gelöschten Zeilen
  1. 15 28
      luks.sh

+ 15 - 28
luks.sh

@@ -31,6 +31,19 @@ _LUKS_OPEN () {
 	echo "Succesfully Opened Encrypted file: ${1} as ${2}"
 }
 
+_CHECK_DIR_PERM () {
+	dir_perm="$(stat -c '%u:%g' "${1}")"
+	cur_perm="${cur_uid}:${cur_gid}"
+	test "$dir_perm" = "$cur_perm" || return 0
+	echo -n "Directory ${1} is not owned by current user. Want to change direcory owner? (Y/n) "
+	read choose
+	case "$choose" in
+		N|n) return 0 ;;
+	esac
+	echo "Changing ownership of ${1} from ${dir_perm} to ${cur_perm}."
+	TRY_AND_RETRY "${sudo_prog} chown ${cur_perm} ${1}" || ERR "Failed to change permission of directory ${1} from ${dir_perm} to ${cur_perm}." 1
+}
+
 file_path="$1"
 test -n "$file_path" || { echo 'No file given'; exit 1; }
 file="$(basename "$file_path")"
@@ -108,20 +121,7 @@ else
 
 	_MOUNT "$block_dev" "./${name}"
 
-	dir_perm="$(stat -c '%u:%g' "$file_path")"
-	cur_perm="${cur_uid}:${cur_gid}"
-	if ! test "$dir_perm" = "$cur_perm"
-	then
-		(
-			echo -n "Directory ./${name} not owned by current user. Want to change direcory owner? (Y/n) "
-			read choose
-			case "$choose" in
-				N|n) exit 0 ;;
-			esac
-			echo "Changing ownership of ./${name} from ${dir_perm} to ${cur_perm}."
-			$sudo_prog chown "$cur_perm" ./"$name" || ERR "Failed to change permission of directory ./${name} from ${dir_perm} to ${cur_perm}." 1
-		) || exit 1
-	fi
+	_CHECK_DIR_PERM "./${name}"
 
 	echo "Succesfully Created and Opened file: ${file_path} into ./${name}"
 
@@ -132,20 +132,7 @@ _LUKS_OPEN "$file_path" "$_name"
 
 _MOUNT "$block_dev" "./${name}"
 
-dir_perm="$(stat -c '%u:%g' "$file_path")"
-cur_perm="${cur_uid}:${cur_gid}"
-if ! test "$dir_perm" = "$cur_perm"
-then
-	(
-		echo -n "Directory ./${name} not owned by current user. Want to change direcory owner? (Y/n) "
-		read choose
-		case "$choose" in
-			N|n) exit 0 ;;
-		esac
-		echo "Changing ownership of ./${name} from ${dir_perm} to ${cur_perm}."
-		$sudo_prog chown "$cur_perm" ./"$name" || ERR "Failed to change permission of directory ./${name} from ${dir_perm} to ${cur_perm}." 1
-	) || exit 1
-fi
+_CHECK_DIR_PERM "./${name}"
 
 echo "Opened ${file_path} on ./${name}"