-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist-protected
executable file
·129 lines (124 loc) · 3.53 KB
/
list-protected
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/zsh
# shellcheck shell=bash
# list-protected
# v0.9.11
# macOS
#
# List Protected Files - part of Trash Tools
# Copyright (c) 2021 Joss Brown (pseud.)
# License: MIT / place of jurisdiction: Berlin, Germany / German laws apply
#
# keyboard shortcut: - (command-line only)
#
# list all currently protected files on all volumes with indexing enabled
#
# examples:
#
# (1) raw mdfind list (filepaths of protected files)
# list-protected
#
# (2) list protected files with extended attribute prefixed
# list-protected -v
#
# (3) sort protected files (most recent first)
# list-protected -v 2>/dev/null | sort -n -t ';' -r -k2
#
# (4) sort protected files (most recent first, filenames only & sorted)
# list-protected -v 2>/dev/null | sort -n -t ';' -r -k2 | awk -F":" '{print substr($0, index($0,$2))}' | sort
export LANG=en_US.UTF-8
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/local/bin:/opt/homebrew/bin:/opt/sw/bin:"$HOME"/.local/bin:"$HOME"/bin:"$HOME"/local/bin
usertag="Protected"
xastr="local.lcars.fpsr#PS"
_protectlist () {
protectlist=""
if [[ $1 == "ckvol" ]] ; then
mountpoint=$(df "$volume" | tail -n +2 | awk '{print substr($0, index($0,$9))}')
! [[ $mountpoint ]] && mountpoint="/"
if mdutil -s "$mountpoint" | grep -q "Indexing enabled\." &>/dev/null ; then
protectlist=$(mdfind -onlyin "$volume" "kMDItemUserTags == $usertag" 2>/dev/null)
else
echo "mdutil: indexing not enabled or status unknown - using regular find instead: please wait..." >&2
protectlist=$(find "$volume" -xattrname "$xastr" 2>/dev/null)
fi
else
if mdutil -s "$volume" | grep -q "Indexing enabled\." &>/dev/null ; then
protectlist=$(mdfind -onlyin "$volume" "kMDItemUserTags == $usertag" 2>/dev/null)
else
echo "mdutil: indexing not enabled - using regular find instead: please wait..." >&2
protectlist=$(find "$volume" -xattrname "$xastr" 2>/dev/null)
fi
fi
if ! [[ $protectlist ]] ; then
echo "No results" >&2
else
if ! $verbose ; then
echo "$protectlist"
else
while read -r protectpath
do
protxa=$(xattr -ps "$xastr" "$protectpath" 2>/dev/null)
! [[ $protxa ]] && protxa="-"
echo "$protxa:$protectpath"
done < <(echo "$protectlist")
fi
fi
}
verbose=false
if [[ $1 =~ ^(-v|--verbose)$ ]] ; then
verbose=true
shift
fi
if ! [[ $* ]] ; then
allvols=$(df | tail -n +2 | grep -v -e "/$" -e "/dev$" -e "/home$" -e "/System/Volumes/" | awk '{print substr($0, index($0,$9))}')
finalvols="$allvols"
while read -r volume
do
finalvols=$(echo "$finalvols" | grep -v "^$volume - Data$" 2>/dev/null)
done < <(echo "$allvols")
echo -e "--------\nAccessing: /" >&2
volume="/"
_protectlist
if [[ $allvols ]] ; then
while read -r volume
do
echo -e "--------\nAccessing: $volume" >&2
volume=$(echo "$volume/" | sed 's-//$-/-')
_protectlist
done < <(echo "$finalvols")
fi
else
for filepath in "$@"
do
if ! [[ -e "$filepath" ]] && ! [[ -h "$filepath" ]] ; then
continue
fi
if ! [[ -d "$filepath" ]] ; then
echo "--------" >&2
protxa=$(xattr -ps "$xastr" "$filepath" 2>/dev/null)
if [[ $protxa ]] ; then
if $verbose ; then
echo "$protxa:$filepath"
else
echo "$filepath"
fi
else
echo "-:-" >&2
fi
else
echo -e "--------\nAccessing: $filepath" >&2
protxa=$(xattr -ps "$xastr" "$filepath" 2>/dev/null)
if [[ $protxa ]] ; then
if $verbose ; then
echo "$protxa:$filepath"
else
echo "$filepath"
fi
fi
volume=$(echo "$filepath/" | sed 's-//$-/-')
volume="$filepath"
_protectlist ckvol
fi
done
fi
echo "--------" >&2
exit