Skip to content

Commit 750fd74

Browse files
committed
performance optimisation
reversed the order of the prefilter and tag
1 parent 4b376c5 commit 750fd74

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

extended-attributes-and-tags/listtags

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
if (( $# == 0 )) ; then
33
echo "usage: $(basename "$0") [-a] [-l] path [...]" >&2
44
echo " -a also descend into *-/ directories (normally filtered out)" >&2
5+
echo "output: prints a list of files with their Finder tags" >&2
6+
echo " line format: path<tab>tag[,tag2...]" >&2
57
echo "note: $(basename "$0") lists the tags in the order they are stored in the filesystem, unlike the output of" >&2
68
echo " jdberry's Python tag script, which sorts the tags. preserving this order is important for the Finder" >&2
79
echo " label colours (i.e. showing Purple on top of Green or Green on top of Purple)." >&2
@@ -20,17 +22,17 @@ for n in "$@" ; do
2022
n="$(dirname "$n")/@eaDir/$(basename "$n")@SynoEAStream"
2123
fi
2224
grep -rlF "com.apple.metadata:_kMDItemUserTags" "$n" --include='*@SynoEAStream' | while read f ; do # filter only on SynoEAStream files that have the _kMDItemUserTags metadata field
25+
# note: there may be hard linked files with a label (=colour tag) that have no @SynoEAStream file - on the mac side, these files
26+
# appear to have a tag (label) but do not show up here. the labels are linked under water by the cnid db in afpd. on the
27+
# syno side, the tag (label) will be picked up on the original file, which _does_ have the @SynoEAStream file
28+
# note: (nested) directories with tag(s) within directories with links (marked by directory-/ e.g. path/genres-/Drama, or path/genres-/Sets/James Bond)
29+
# must not be filtered, only the files within those, i.e. skip files _within_ "Drama", "Sets" or "James Bond", not these directories themselves, which may have a useful tag.
30+
# note: this is equivalent to filtering the grep output through | grep -v -- '-/.*\.[^ ]' | sed -e 's#/@eaDir/#/#' -e 's#@SynoEAStream##', but that doesn't do the -all
31+
# and is only marginally faster than the below (calling 'tag' is the bottleneck)
2332
i=${f/@eaDir\/} ; i=${i%@SynoEAStream}
24-
t=$(tag "$f" | tr '\n' ',') ; t=${t%,}
25-
if [[ -n $t ]] ; then
26-
# note: there may be hard linked files with a label (=colour tag) that have no @SynoEAStream file - on the mac side, these files
27-
# appear to have a tag (label) but do not show up here. the labels are linked under water by the cnid db in afpd. on the
28-
# syno side, the tag (label) will be picked up on the original file, which _does_ have the @SynoEAStream file
29-
# note: (nested) directories with tag(s) within directories with links (marked by directory-/ e.g. path/genres-/Drama, or path/genres-/Sets/James Bond)
30-
# must not be filtered, only the files within those, i.e. skip files _within_ "Drama", "Sets" or "James Bond", not these directories themselves, which may have a useful tag.
31-
# note: the grep -a is to prevent grep interpreting the file name as binary "(Binary file (standard input) matches)" on file names with broken UTF-8 codes
32-
# output result: path<tab>tag[,tag2...]
33-
if [[ -n $all || -d $i || ( -f $i && $i != *-/* ) ]] ; then # exclude files with tag(s) found within linked directories (directories whose name end with -)
33+
if [[ -n $all || -d $i || ( -f $i && $i != *-/* ) ]] ; then # exclude files with tag(s) found within linked directories (directories whose name end with -)
34+
t=$(tag "$f" | tr '\n' ',') ; t=${t%,}
35+
if [[ -n $t ]] ; then
3436
echo -e "$i\t$t"
3537
fi
3638
fi

0 commit comments

Comments
 (0)