-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcleanup_SynoFiles
132 lines (123 loc) · 5.81 KB
/
cleanup_SynoFiles
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
130
131
132
#!/bin/bash
# check parameters
if [[ $1 == -l ]] ; then lst=y; shift ; fi
if (( $# == 0 )) ; then err=y ; fi
# make sure $@ only contains directories or find will create havoc if you pass -exec in $@ by accident
for d in "$@" ; do if [[ ! -d $d ]] ; then echo "$d: not a directory" >&2 ; err=y ; fi ; done
xat=xattrs.lst
if [[ ! -f $xat ]] ; then echo "$xat not found - don't know which xattrs are not bogus" >&2 ; exit 1 ; fi
if [[ -n $err ]] ; then
echo "usage: $0 [-l] dir [dir ...]" >&2
echo " delete or list (-l) in specified directories:" >&2
echo " - the 'stray' (extraneous) @SynoEAStream and @SynoResource files" >&2
echo " - any subdirectories inside the @eaDir directories (which are used by Synology to store picture previews" >&2
echo " (Photo Station), SYNO_DTIME delete timestamps (when using the Trash), and other paraphernalia)" >&2
echo " - the 'bogus' @SynoEAStream and @SynoResource files ('bogus' means the files don't have any of the meaningful xattrs listed in $xat)" >&2
echo " - when deleting, it also cleans up any empty @eaDir directories that are left behind" >&2
echo " prints found files and directories on stdout; prints messages on stderr" >&2
exit 1
fi
# find stray @SynoEAStream files, i.e. @SynoEAStream files that don't have the parent file it is supposed to be associated with
echo "finding stray @SynoEAStream files" >&2
find "$@" -type f -name '*@SynoEAStream' -print | while read f ; do
p=${f/@eaDir\/} ; p=${p%@SynoEAStream}
if [[ ! -e "$p" ]] ; then
if [[ -n $lst ]] ; then echo "$f" ; else rm -v "$f" ; fi
fi
done
# find stray @SynoResource files, i.e. @SynoResource files that don't have the parent file it is supposed to be associated with
echo "finding stray @SynoResource files" >&2
find "$@" -type f -name '*@SynoResource' -print | while read f ; do
p=${f/@eaDir\/} ; p=${p%@SynoResource}
if [[ ! -e "$p" ]] ; then
if [[ -n $lst ]] ; then echo "$f" ; else rm -v "$f" ; fi
fi
done
# find @eaDir subdirectories, but skip the #recycle bin because that contains useful SYNO_DTIME timestamps
echo "finding @eaDir subdirectories (*/@eaDir/*/)" >&2
find "$@" -depth -type d -path '*/@eaDir/*' ! -path '*/#recycle/*' -print | while read d ; do
if [[ -n $lst ]] ; then echo "$d/" ; else rm -rfv "$d" ; fi
done
# now scan the #recycle bin and find any _stray_ @eaDir subdirectories - these are no good
find "$@" -depth -type d -path '*/#recycle/@eaDir/*' -print | while read d ; do
p=${d/@eaDir\/}
if [[ ! -e "$p" ]] ; then
if [[ -n $lst ]] ; then echo "$d/" ; else rm -rfv "$d" ; fi
fi
done
# find @SynoEAStream files that don't have any of the relevant (non-bogus) xattrs inside
echo "finding bogus @SynoEAStream files (and their @SynoResource siblings)" >&2
# use find followed by ! grep -qF -f because grep -vqFr -f doesn't produce the same result
find "$@" -type f -name '*@SynoEAStream' -print | while read f ; do
if ! grep -qF -f $xat "$f" ; then
if true ; then
# final paranoia check
for i in $(cat $xat) ; do
if [[ -n $(get_attr -x -q "$i" "$f") ]] ; then
echo "error: $f has a $i xattr - there's something wrong - exit" >&2 ; exit 1
fi
done
fi
# if this @SynoEAStream is bogus, and happens to have a @SynoResource at its side, it's not necessary either (can be considered bogus as well)
s="${f%@SynoEAStream}@SynoResource"
if [[ -n $lst ]] ; then
echo "$f"
if [[ -f "$s" ]] ; then echo "$s" ; fi
else
rm -v "$f"
if [[ -f "$s" ]] ; then rm -v "$s" ; fi
fi
fi
done
# find remaining @SynoEAStream files that do have any of the relevant (non-bogus) xattrs inside, but they all have an empty value
echo "finding more bogus @SynoEAStream files (and their @SynoResource siblings)" >&2
grep -rlF -f $xat "$@" --include='*@SynoEAStream' | while read f ; do
# this filters all files with one or more of the (non-bogus) xattrs, but they could be empty (all having an empty value)
found=n
# visit all non-bogus($xat) attributes and check on their values, if one non-empty attr is found, the metadata (and as a result, the file as a whole) is not bogus
for i in $(cat $xat) ; do
val=$(get_attr -x -q "$i" "$f")
# on an empty xattr, the value could still be either empty or an empty bplist in case of com.apple.metadata:_kMDItemUserTags
if [[ -n $val && $val != "62706c6973743030a0080000000000000101000000000000000100000000000000000000000000000009" ]] ; then
found=y # found (at least one) non-empty non-bogus xattr
fi
done
# check that none of the xat attributes had meaningful data in the value
if [[ $found == "n" ]] ; then
if true ; then
# final paranoia check
if [[ -n $(tag "$f") ]] ; then
echo "error: $f has a custom tag - there's something wrong - exit" >&2 ; exit 1
fi
for i in $(cat $xat | grep -v com.apple) ; do
if [[ -n $(get_attr -x -q "$i" "$f") ]] ; then
echo "error: $f has a $i xattr - there's something wrong - exit" >&2 ; exit 1
fi
done
fi
# this @SynoEAStream is bogus, and if it happens to have a @SynoResource at its side, it's not necessary either (can be considered bogus as well)
s="${f%@SynoEAStream}@SynoResource"
if [[ -n $lst ]] ; then
echo "$f"
if [[ -f "$s" ]] ; then echo "$s" ; fi
else
rm -v "$f"
if [[ -f "$s" ]] ; then rm -v "$s" ; fi
fi
fi
done
# find any remaining @SynoResource files that don't have a @SynoEAStream file at their side
echo "finding bogus @SynoResource files" >&2
find "$@" -type f -name '*@SynoResource' -print | while read f ; do
# if this @SynoResource is on its own, it's not necessary either (can be considered bogus as well)
s="${f%@SynoResource}@SynoEAStream"
if [[ ! -f "$s" ]] ; then
if [[ -n $lst ]] ; then echo "$f" ; else rm -v "$f" ; fi
fi
done
# find leftover @eaDir directories
if [[ ! -n $lst ]] ; then
echo "finding leftover (empty) @eaDir directories" >&2
find "$@" -depth -type d -name '@eaDir' -empty -exec rmdir -v {} \;
fi
#EOF