-
Notifications
You must be signed in to change notification settings - Fork 95
/
Tag.m
1021 lines (833 loc) · 33.4 KB
/
Tag.m
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Tag.m
// Tag
//
// Created by James Berry on 10/25/13.
//
// The MIT License (MIT)
//
// Copyright (c) 2013-2019 James Berry
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
/*
FUTURE POTENTIALS:
Potential simple boolean tag query:
foo OR bar
foo,bar -- comma same as AND
foo AND bar
NOT foo
foo,bar AND baz
foo,bar OR baz
foo,bar AND NOT biz,baz
* -- Some tag
<empty expr> -- No tag
support glob patterns?
support queries for both match and find?
Use NSPredicate for both find and match?
*/
#import "Tag.h"
#import "TagName.h"
#import <getopt.h>
NSString* const version = @"0.10.0";
// This constant doesn't seem to be defined in MDItem.h, so we define it here
NSString* const kMDItemUserTags = @"kMDItemUserTags";
@interface Tag ()
@end
@implementation Tag
static void FPrintf(FILE* f, NSString* fmt, ...) __attribute__ ((format(__NSString__, 2, 3)));
static void Printf(NSString* fmt, ...) __attribute__ ((format(__NSString__, 1, 2)));
static void FPrintf(FILE* f, NSString* fmt, ...)
{
va_list ap;
va_start (ap, fmt);
NSString *output = [[NSString alloc] initWithFormat:fmt arguments:ap];
va_end (ap);
fprintf(f, "%s", [output UTF8String]);
}
static void Printf(NSString* fmt, ...)
{
va_list ap;
va_start (ap, fmt);
NSString *output = [[NSString alloc] initWithFormat:fmt arguments:ap];
va_end (ap);
printf("%s", [output UTF8String]);
}
- (OutputFlags)outputFlagsForMode:(OperationMode)mode
{
OutputFlags result = 0;
switch (mode)
{
case OperationModeMatch:
case OperationModeFind:
result = OutputFlagsName;
break;
case OperationModeList:
result = OutputFlagsName | OutputFlagsTags;
break;
default:
break;
}
return result;
}
typedef NS_ENUM(int, CommandCode) {
CommandCodeHome = 1000,
CommandCodeLocal,
CommandCodeNetwork
};
- (void)parseCommandLineArgv:(char * const *)argv argc:(int)argc
{
static struct option options[] = {
// Operations
{ "set", required_argument, 0, OperationModeSet },
{ "add", required_argument, 0, OperationModeAdd },
{ "remove", required_argument, 0, OperationModeRemove },
{ "match", required_argument, 0, OperationModeMatch },
{ "find", required_argument, 0, OperationModeFind },
{ "usage", optional_argument, 0, OperationModeUsage },
{ "list", no_argument, 0, OperationModeList },
// Directory Enumeration Options
{ "all", no_argument, 0, 'A' }, // Display all (hidden files)
{ "enter", no_argument, 0, 'e' }, // Enter/enumerate directories: enumerate contents of provided directories
{ "recursive", no_argument, 0, 'R' }, // Recursively process any directory we encounter
{ "descend", no_argument, 0, 'd' }, // Recursively process any directory we encounter (alias for backwards compatibility)
// Format options
{ "name", no_argument, 0, 'n' },
{ "no-name", no_argument, 0, 'N' },
{ "tags", no_argument, 0, 't' },
{ "no-tags", no_argument, 0, 'T' },
{ "garrulous", no_argument, 0, 'g' },
{ "no-garrulous", no_argument, 0, 'G' },
{ "color", no_argument, 0, 'c' },
{ "slash", no_argument, 0, 'p' }, // Write a slash ('/') after is each filename if that file is a directory
{ "nul", no_argument, 0, '0' },
// Search Scope
{ "home", no_argument, 0, CommandCodeHome },
{ "local", no_argument, 0, CommandCodeLocal },
{ "network", no_argument, 0, CommandCodeNetwork },
// other
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'v' },
{ 0, 0, 0, 0 }
};
// Initialize to a known state
self.operationMode = OperationModeUnknown;
self.outputFlags = 0;
self.searchScope = SearchScopeNone;
self.displayAllFiles = NO;
self.recurseDirectories = NO;
self.enterDirectories = NO;
self.tags = nil;
self.URLs = nil;
self.tagColors = nil;
int name_flag = 0;
int tags_flag = 0;
int garrulous_flag = 0;
BOOL slash = NO;
BOOL color = NO;
BOOL nulTerminate = NO;
// Parse Options
int option_char;
int option_index;
while ((option_char = getopt_long(argc, argv, "s:a:r:m:f:u::lAeRdnNtTgGcp0hv", options, &option_index)) != -1)
{
switch (option_char)
{
case OperationModeSet:
case OperationModeAdd:
case OperationModeRemove:
case OperationModeMatch:
case OperationModeFind:
case OperationModeUsage:
case OperationModeList:
{
if (self.operationMode)
{
FPrintf(stderr, @"%@: Operation mode cannot be respecified\n", [self programName]);
exit(1);
}
self.operationMode = option_char;
if (optarg == nil && self.operationMode == OperationModeUsage)
optarg = "*";
if (optarg != nil)
[self parseTagsArgument:[NSString stringWithUTF8String:optarg]];
break;
}
case 'A':
_displayAllFiles = YES;
break;
case 'e':
_enterDirectories = YES;
break;
case 'R':
case 'd': // -d is a backward compatibility alias for -R
_recurseDirectories = YES;
break;
case 'n':
name_flag = 2;
break;
case 'N':
name_flag = 1;
break;
case 't':
tags_flag = 2;
break;
case 'T':
tags_flag = 1;
break;
case 'g':
garrulous_flag = 2;
break;
case 'G':
garrulous_flag = 1;
break;
case CommandCodeHome:
_searchScope = SearchScopeHome;
break;
case CommandCodeLocal:
_searchScope = SearchScopeLocal;
break;
case CommandCodeNetwork:
_searchScope = SearchScopeNetwork;
break;
case 'c':
color = YES;
break;
case 'p':
slash = YES;
break;
case '0':
nulTerminate = YES;
break;
case '?':
case ':':
case 'h':
_operationMode = OperationModeNone;
[self displayHelp];
break;
case 'v':
_operationMode = OperationModeNone;
[self displayVersion];
break;
}
}
// If the operation wasn't set, default to list
if (self.operationMode == OperationModeUnknown)
self.operationMode = OperationModeList;
// Set default output flags for the chosen operation
_outputFlags = [self outputFlagsForMode:self.operationMode];
// Override the output flags if they were explicitly set on command line
if (name_flag)
_outputFlags = (_outputFlags & ~OutputFlagsName) | ((name_flag - 1) * OutputFlagsName);
if (tags_flag)
_outputFlags = (_outputFlags & ~OutputFlagsTags) | ((tags_flag - 1) * OutputFlagsTags);
if (garrulous_flag)
_outputFlags = (_outputFlags & ~OutputFlagsGarrulous) | ((garrulous_flag - 1) * OutputFlagsGarrulous);
// Set additional output flags
if (slash)
_outputFlags |= OutputFlagsSlashDirectory;
if (nulTerminate)
_outputFlags |= OutputFlagsNulTerminate;
// Get colors if we're able to use them. If tagColors is nil, we won't try to emit color escapes
if (color && isatty(fileno(stdout)))
self.tagColors = [self getTagColors];
// Process any remaining arguments as pathnames, converting into URLs
[self parseFilenameArguments:&argv[optind] argc:argc - optind];
}
- (void)parseFilenameArguments:(char * const *)argv argc:(int)argc
{
NSMutableArray* URLs = [NSMutableArray new];
for (int arg = 0; arg < argc; ++arg)
{
// Get the path, ignoring empty paths
NSString* path = [NSString stringWithUTF8String:argv[arg]];
if (![path length])
continue;
// Add the URL to our array of URLs to process
NSURL* URL = [NSURL fileURLWithPath:path];
if (!URL)
{
FPrintf(stderr, @"%@: Can't form a URL from path %@\n", [self programName], path);
exit(3);
}
[URLs addObject:URL];
}
self.URLs = URLs;
}
- (void)parseTagsArgument:(NSString*)arg
{
// The tags arg is a comma-separated list of tags
NSArray* components = [arg componentsSeparatedByString:@","];
// Form the unique set of tags
NSMutableSet* uniqueTags = [NSMutableSet new];
for (NSString* component in components)
{
NSString* trimmed = [component stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ([trimmed length])
[uniqueTags addObject:[[TagName alloc] initWithTag:trimmed]];
}
self.tags = uniqueTags;
}
- (NSString*)programName
{
return [NSProcessInfo processInfo].processName;
}
- (void)displayVersion
{
Printf(@"%@ v%@\n", [self programName], version);
}
// show, display, enumerate, discover, all, usage
- (void)displayHelp
{
Printf(@"%@ - %@", [self programName], @"A tool for manipulating and querying file tags.\n"
" usage:\n"
" tag -a | --add <tags> <path>... Add tags to file\n"
" tag -r | --remove <tags> <path>... Remove tags from file\n"
" tag -s | --set <tags> <path>... Set tags on file\n"
" tag -m | --match <tags> <path>... Display files with matching tags\n"
" tag -f | --find <tags> <path>... Find all files with tags (-A, -e, -R ignored)\n"
" tag -u | --usage <tags> <path>... Display tags used, with usage counts\n"
" tag -l | --list <path>... List the tags on file\n"
" <tags> is a comma-separated list of tag names; use * to match/find any tag.\n"
" additional options:\n"
" -v | --version Display version\n"
" -h | --help Display this help\n"
" -A | --all Display invisible files while enumerating\n"
" -e | --enter Enter and enumerate directories provided\n"
" -R | --recursive Recursively process directories\n"
" -n | --name Turn on filename display in output (default)\n"
" -N | --no-name Turn off filename display in output (list, find, match)\n"
" -t | --tags Turn on tags display in output (find, match)\n"
" -T | --no-tags Turn off tags display in output (list)\n"
" -g | --garrulous Display tags each on own line (list, find, match)\n"
" -G | --no-garrulous Display tags comma-separated after filename (default)\n"
" -c | --color Display tags in color\n"
" -p | --slash Terminate each directory name with a slash\n"
" -0 | --nul Terminate lines with NUL (\\0) for use with xargs -0\n"
" --home Find tagged files in user home directory\n"
" --local Find tagged files in home + local filesystems\n"
" --network Find tagged files in home + local + network filesystems\n"
);
}
#define COLORS_ESCAPE @"\033["
#define COLORS_NONE COLORS_ESCAPE @"m"
#define COLORS_GRAY COLORS_ESCAPE @"48;5;241m"
#define COLORS_GREEN COLORS_ESCAPE @"42m"
#define COLORS_PURPLE COLORS_ESCAPE @"48;5;129m"
#define COLORS_BLUE COLORS_ESCAPE @"44m"
#define COLORS_YELLOW COLORS_ESCAPE @"43m"
#define COLORS_RED COLORS_ESCAPE @"41m"
#define COLORS_ORANGE COLORS_ESCAPE @"48;5;208m"
- (NSDictionary*)getTagColors
{
// Get the tag colors
//
// Since this is using private finder data structures, it may not always continue to work.
// We make a best effort attempt and try to bail if we don't find what we expect to find there
NSError* error;
NSString* homeDir = NSHomeDirectory();
NSString* finderPlistPath = [homeDir stringByAppendingString: @"/Library/SyncedPreferences/com.apple.finder.plist"];
NSURL* url = [NSURL fileURLWithPath:finderPlistPath];
NSData* data = [NSData dataWithContentsOfURL:url];
if (!data)
return nil;
id properties = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:nil error:&error];
if (!properties)
return nil;
NSArray* tagsArray = [properties valueForKeyPath:@"values.FinderTagDict.value.FinderTags"];
if (![tagsArray isKindOfClass:[NSArray class]])
return nil;
// Form a map from tag name to color escape sequence for that tag
NSUInteger tagCount = [tagsArray count];
NSMutableDictionary *colors = [NSMutableDictionary dictionaryWithCapacity:tagCount];
for (NSDictionary* tagEntry in tagsArray)
{
if (![tagEntry isKindOfClass:[NSDictionary class]])
return nil;
NSString* tag = tagEntry[@"n"];
NSNumber* colorCode = tagEntry[@"l"];
if (tag == nil || colorCode == nil)
continue;
NSString* colorSequence = nil;
switch ([colorCode intValue])
{
case 1:
colorSequence = COLORS_GRAY;
break;
case 2:
colorSequence = COLORS_GREEN;
break;
case 3:
colorSequence = COLORS_PURPLE;
break;
case 4:
colorSequence = COLORS_BLUE;
break;
case 5:
colorSequence = COLORS_YELLOW;
break;
case 6:
colorSequence = COLORS_RED;
break;
case 7:
colorSequence = COLORS_ORANGE;
break;
}
if (colorSequence != nil)
[colors setObject:colorSequence forKey:[[TagName alloc] initWithTag:tag]];
}
return [colors copy];
}
- (void)performOperation
{
switch (self.operationMode)
{
case OperationModeSet:
[self doSet];
break;
case OperationModeAdd:
[self doAdd];
break;
case OperationModeRemove:
[self doRemove];
break;
case OperationModeMatch:
[self doMatch];
break;
case OperationModeFind:
[self doFind];
break;
case OperationModeUsage:
[self doUsage];
break;
case OperationModeList:
[self doList];
break;
case OperationModeNone:
case OperationModeUnknown:
break;
}
}
- (void)reportFatalError:(NSError*)error onURL:(NSURL*)URL
{
FPrintf(stderr, @"%@: %@\n", [self programName], error.localizedDescription);
exit(2);
}
- (NSString*)string:(NSString*)s paddedToMinimumLength:(int)minLength
{
NSInteger length = [s length];
if (length >= minLength)
return s;
return [s stringByPaddingToLength:minLength withString:@" " startingAtIndex:0];
}
- (NSString*)displayStringForTag:(NSString*)tag
{
NSString* result = nil;
NSString* colorSequence = (self.tagColors != nil) ? _tagColors[[[TagName alloc] initWithTag:tag]] : nil;
if (colorSequence != nil)
result = [NSString stringWithFormat:@"%@%@%@", colorSequence, tag, COLORS_NONE];
else
result = tag;
return result;
}
- (void)emitURL:(NSURL*)URL tags:(NSArray*)tagArray
{
NSString* fileName = nil;
if (_outputFlags & OutputFlagsName)
{
NSString* suffix = @"";
if (_outputFlags & OutputFlagsSlashDirectory)
{
NSNumber* isDir = nil;
[URL getResourceValue:&isDir forKey:NSURLIsDirectoryKey error:nil];
if ([isDir boolValue])
suffix = @"/";
}
fileName = [NSString stringWithFormat:@"%@%@", [URL relativePath], suffix];
}
BOOL tagsOnSeparateLines = !!(_outputFlags & OutputFlagsGarrulous);
BOOL printTags = (_outputFlags & OutputFlagsTags) && [tagArray count];
char lineTerminator = (_outputFlags & OutputFlagsNulTerminate) ? '\0' : '\n';
if (fileName)
{
BOOL padFileField = printTags && !tagsOnSeparateLines;
NSString* fileField = padFileField ? [self string:fileName paddedToMinimumLength:31] : fileName;
Printf(@"%@", fileField);
}
if (printTags)
{
BOOL needLineTerm = NO;
NSArray* sortedTags = [tagArray sortedArrayUsingSelector:@selector(compare:)];
NSString* tagSeparator;
NSString* startingSeparator;
if (tagsOnSeparateLines)
{
needLineTerm = !!fileName;
tagSeparator = fileName ? @" " : @"";
startingSeparator = tagSeparator;
}
else
{
tagSeparator = @",";
startingSeparator = fileName ? @"\t" : @"";
}
NSString* sep = startingSeparator;
for (NSString* tag in sortedTags)
{
if (needLineTerm)
putc(lineTerminator, stdout);
Printf(@"%@%@", sep, [self displayStringForTag:tag]);
sep = tagSeparator;
needLineTerm = tagsOnSeparateLines;
}
}
if (fileName || printTags)
putc(lineTerminator, stdout);
}
- (BOOL)wildcardInTagSet:(NSSet*)set
{
TagName* wildcard = [[TagName alloc] initWithTag:@"*"];
return [set containsObject:wildcard];
}
- (NSMutableSet*)tagSetFromTagArray:(NSArray*)tagArray
{
NSMutableSet* set = [[NSMutableSet alloc] initWithCapacity:[tagArray count]];
for (NSString* tag in tagArray)
[set addObject:[[TagName alloc] initWithTag:tag]];
return set;
}
- (NSArray*)tagArrayFromTagSet:(NSSet*)tagSet
{
NSMutableArray* array = [[NSMutableArray alloc] initWithCapacity:[tagSet count]];
for (TagName* tag in tagSet)
[array addObject:tag.visibleName];
return array;
}
- (void)enumerateDirectory:(NSURL*)directoryURL withBlock:(void (^)(NSURL *URL))block
{
NSURL* baseURL = directoryURL;
NSInteger enumerationOptions = 0;
if (!_displayAllFiles)
enumerationOptions |= NSDirectoryEnumerationSkipsHiddenFiles;
if (!_recurseDirectories)
enumerationOptions |= NSDirectoryEnumerationSkipsSubdirectoryDescendants;
NSFileManager* fileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator* enumerator = [fileManager enumeratorAtURL:baseURL
includingPropertiesForKeys:@[NSURLTagNamesKey]
options:enumerationOptions
errorHandler:nil];
NSString* baseURLString = [baseURL absoluteString];
for (NSObject* obj in enumerator)
{
@autoreleasepool {
NSURL* fullURL = (NSURL*)obj;
// The directory enumerator returns full URLs, not partial URLs, which are what we really want.
// So remake the URL as a partial URL if possible
NSURL* URL = fullURL;
NSString* fullURLString = [fullURL absoluteString];
if ([fullURLString hasPrefix:baseURLString])
{
NSString* relativePart = [fullURLString substringFromIndex:[baseURLString length]];
URL = [NSURL URLWithString:relativePart relativeToURL:baseURL];
}
block(URL);
}
}
}
- (void)enumerateURLsWithBlock:(void (^)(NSURL *URL))block
{
if (!block)
return;
NSFileManager* fileManager = [NSFileManager defaultManager];
if ([self.URLs count] == 0)
{
// No URLs were provided on the command line; enumerate the current directory
NSURL* currentDirectoryURL = [NSURL fileURLWithPath:[fileManager currentDirectoryPath]];
[self enumerateDirectory:currentDirectoryURL withBlock:block];
}
else
{
// Process URLs provided on the command line
for (NSURL* URL in self.URLs)
{
@autoreleasepool {
// Invoke the block
block(URL);
// If we want to enter or recurse directories then do so
// if we have a directory
if (_enterDirectories || _recurseDirectories)
{
NSNumber* isDir = nil;
[URL getResourceValue:&isDir forKey:NSURLIsDirectoryKey error:nil];
if ([isDir boolValue])
[self enumerateDirectory:URL withBlock:block];
}
}
}
}
}
- (void)doSet
{
// Only perform set on specified URLs
// (we don't implicitly enumerate the current directory)
if ([self.URLs count] == 0)
return;
// Enumerate the provided URLs, setting tags on each
// --all, --enter, and --recursive apply
NSArray* tagArray = [self tagArrayFromTagSet:self.tags];
[self enumerateURLsWithBlock:^(NSURL *URL) {
NSError* error;
if (![URL setResourceValue:tagArray forKey:NSURLTagNamesKey error:&error])
[self reportFatalError:error onURL:URL];
}];
}
- (void)doAdd
{
// If there are no tags to add, we're done
if (![self.tags count])
return;
// Only perform add on specified URLs
// (we don't implicitly enumerate the current directory)
if ([self.URLs count] == 0)
return;
// Enumerate the provided URLs, adding tags to each
// --all, --enter, and --recursive apply
[self enumerateURLsWithBlock:^(NSURL *URL) {
NSError* error;
// Get the existing tags
NSArray* existingTags;
if (![URL getResourceValue:&existingTags forKey:NSURLTagNamesKey error:&error])
[self reportFatalError:error onURL:URL];
// Form the union of the existing tags + new tags.
NSMutableSet* tagSet = [self tagSetFromTagArray:existingTags];
[tagSet unionSet:self.tags];
// Set all the new tags onto the item
if (![URL setResourceValue:[self tagArrayFromTagSet:tagSet] forKey:NSURLTagNamesKey error:&error])
[self reportFatalError:error onURL:URL];
}];
}
- (void)doRemove
{
// If there are no tags to remove, we're done
if (![self.tags count])
return;
// Only perform remove on specified URLs
// (we don't implicitly enumerate the current directory)
if ([self.URLs count] == 0)
return;
BOOL matchAny = [self wildcardInTagSet:self.tags];
// Enumerate the provided URLs, removing tags from each
// --all, --enter, and --recursive apply
[self enumerateURLsWithBlock:^(NSURL *URL) {
NSError* error;
// Get existing tags from the URL
NSArray* existingTags;
if (![URL getResourceValue:&existingTags forKey:NSURLTagNamesKey error:&error])
[self reportFatalError:error onURL:URL];
// Form the revised array of tags
NSArray* revisedTags;
if (matchAny)
{
// We matched the wildcard, so remove all tags from the item
revisedTags = [[NSArray alloc] init];
}
else
{
// Existing tags minus tags to remove
NSMutableSet* tagSet = [self tagSetFromTagArray:existingTags];
[tagSet minusSet:self.tags];
revisedTags = [self tagArrayFromTagSet:tagSet];
}
// Set the revised tags onto the item
if (![URL setResourceValue:revisedTags forKey:NSURLTagNamesKey error:&error])
[self reportFatalError:error onURL:URL];
}];
}
- (void)doMatch
{
BOOL matchAny = [self wildcardInTagSet:self.tags];
BOOL matchNone = [self.tags count] == 0;
// Enumerate the provided URLs or current directory, listing all paths that match the specified tags
// --all, --enter, and --recursive apply
[self enumerateURLsWithBlock:^(NSURL *URL) {
NSError* error;
// Get the tags on the URL
NSArray* tagArray;
if (![URL getResourceValue:&tagArray forKey:NSURLTagNamesKey error:&error])
[self reportFatalError:error onURL:URL];
NSUInteger tagCount = [tagArray count];
// If the set of existing tags contains all of the required
// tags then emit
if ( (matchAny && tagCount > 0)
|| (matchNone && tagCount == 0)
|| (!matchNone && [self.tags isSubsetOfSet:[self tagSetFromTagArray:tagArray]])
)
[self emitURL:URL tags:tagArray];
}];
}
- (void)doList
{
// Enumerate the provided URLs or current directory, listing the tags for each path
// --all, --enter, and --recursive apply
[self enumerateURLsWithBlock:^(NSURL* URL) {
// Get the tags
NSError* error;
NSArray* tagArray;
if (![URL getResourceValue:&tagArray forKey:NSURLTagNamesKey error:&error])
[self reportFatalError:error onURL:URL];
// Emit
[self emitURL:URL tags:tagArray];
}];
}
- (void)doFind
{
[self findGutsWithUsage:NO];
}
- (void)doUsage
{
[self findGutsWithUsage:YES];
}
- (void)findGutsWithUsage:(BOOL)usageMode
{
// Start a metadata search for files containing all of the given tags
NSMetadataQuery* metadataQuery = [self performMetadataSearchForTags:self.tags usageMode:usageMode];
// Emit the results of the query, either for tags or for usage
if (usageMode)
{
// Print the statistics, ignoring the general query results
NSDictionary* valueLists = [metadataQuery valueLists];
NSArray* tagTuples = valueLists[kMDItemUserTags];
for (NSMetadataQueryAttributeValueTuple* tuple in tagTuples)
{
NSString* tag = (tuple.value == [NSNull null]) ? @"<no_tag>" : tuple.value;
Printf(@"%ld\t%@\n", (long)tuple.count, [self displayStringForTag:tag]);
}
}
else
{
// Print the query results
[metadataQuery enumerateResultsUsingBlock:^(NSMetadataItem* theResult, NSUInteger idx, BOOL * _Nonnull stop) {
@autoreleasepool {
NSString* path = [theResult valueForAttribute:(NSString *)kMDItemPath];
if (path)
{
NSURL* URL = [NSURL fileURLWithPath:path];
NSArray* tagArray = [theResult valueForAttribute:kMDItemUserTags];
[self emitURL:URL tags:tagArray];
}
}
}];
}
}
- (NSPredicate*)formQueryPredicateForTags:(NSSet*)tagSet
{
BOOL matchAny = [self wildcardInTagSet:tagSet];
BOOL matchNone = [tagSet count] == 0;
NSPredicate* result;
if (matchAny)
{
result = [NSPredicate predicateWithFormat:@"%K LIKE '*'", kMDItemUserTags];
}
else if (matchNone)
{
result = [NSPredicate predicateWithFormat:@"NOT %K LIKE '*'", kMDItemUserTags];
}
else if ([tagSet count] == 1)
{
result = [NSPredicate predicateWithFormat:@"%K ==[c] %@", kMDItemUserTags, ((TagName*)tagSet.anyObject).visibleName];
}
else // if tagSet count > 0
{
NSMutableArray* subpredicates = [NSMutableArray new];
for (TagName* tag in tagSet)
[subpredicates addObject:[NSPredicate predicateWithFormat:@"%K ==[c] %@", kMDItemUserTags, tag.visibleName]];
result = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];
}
return result;
}
- (NSArray*)searchScopesFromSearchScope:(SearchScope)scope
{
NSMutableArray* result = [[NSMutableArray alloc] init];
// Add URLs in which to explicitly search
if ([self.URLs count])
[result addObjectsFromArray:self.URLs];
// Add any specified search scopes
switch (scope)
{
case SearchScopeNone:
break;
case SearchScopeHome:
[result addObject:NSMetadataQueryUserHomeScope];
break;
case SearchScopeLocal:
[result addObject:NSMetadataQueryLocalComputerScope];
break;
case SearchScopeNetwork:
[result addObjectsFromArray:@[NSMetadataQueryLocalComputerScope,NSMetadataQueryNetworkScope]];
break;
}
// In the absence of any scope, the search is not scoped
return result;
}
- (NSMetadataQuery*)performMetadataSearchForTags:(NSSet*)tagSet usageMode:(BOOL)usageMode
{
// Create the metadata query
NSMetadataQuery* metadataQuery = [[NSMetadataQuery alloc] init];
// Register the notifications for batch and completion updates
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(queryComplete:)
name:NSMetadataQueryDidFinishGatheringNotification
object:metadataQuery];
// Configure the search predicate
NSPredicate *searchPredicate = [self formQueryPredicateForTags:tagSet];
[metadataQuery setPredicate:searchPredicate];
// Set the search scope
NSArray *searchScopes = [self searchScopesFromSearchScope:self.searchScope];
[metadataQuery setSearchScopes:searchScopes];
// Configure the sorting of the results
// (note that the query can't sort by the item path, which makes sorting less useful)
NSSortDescriptor *sortKeys = [[NSSortDescriptor alloc] initWithKey:(id)kMDItemDisplayName
ascending:YES];
[metadataQuery setSortDescriptors:[NSArray arrayWithObject:sortKeys]];
// If we're collecting usage stats, request that values be saved for tags
if (usageMode)
[metadataQuery setValueListAttributes:@[kMDItemUserTags]];
// Ask the query to send notifications on the main thread, which will
// ensure we process them on the main thread, and will also ensure that our
// main thread is kicked so that the run loop will iterate and thus complete.
[metadataQuery setOperationQueue:[NSOperationQueue mainQueue]];
// Begin the asynchronous query
[metadataQuery startQuery];
// Enter the run loop, exiting only when the query is done
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
while (!metadataQuery.stopped && [runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]])