-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.m
630 lines (507 loc) · 20.6 KB
/
main.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
/*
Copyright (c) 2017-2020, Sveinbjorn Thordarson <[email protected]>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#import <Foundation/Foundation.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <err.h>
#include <string.h>
#include <unistd.h>
#include <sysexits.h>
#include <getopt.h>
#include <sys/attr.h>
#include <sys/param.h>
#include <sys/attr.h>
#include <sys/vnode.h>
#include <sys/fsgetpath.h>
#include <sys/mount.h>
struct packed_name_attr {
u_int32_t size; // Of the remaining fields
struct attrreference ref; // Offset/length of name itself
char name[PATH_MAX];
};
struct packed_attr_ref {
u_int32_t size; // Of the remaining fields
struct attrreference ref; // Offset/length of attr itself
};
struct packed_result {
u_int32_t size; // Including size field itself
struct fsid fs_id;
struct fsobj_id obj_id;
};
typedef struct packed_result packed_result;
typedef struct packed_result *packed_result_p;
#define MAX_MATCHES 20
#define MAX_EBUSY_RETRIES 5
#define DEFAULT_VOLUME @"/"
// Prototypes
static void do_searchfs_search(const char *volpath, const char *match_string);
static BOOL filter_result(const char *path, const char *match_string);
static BOOL has_fsgetpath(void);
static ssize_t fsgetpath_compat(char * buf, size_t buflen, fsid_t * fsid, uint64_t obj_id);
static ssize_t fsgetpath_legacy(char *buf, size_t buflen, fsid_t *fsid, uint64_t obj_id);
static NSString *dev_to_mount_path(NSString *devPath);
static BOOL is_mount_path(NSString *path);
static BOOL vol_supports_searchfs(NSString *path);
static void list_volumes(void);
static void print_usage(void);
static void print_version(void);
static const float program_version = 0.3f;
static const char optstring[] = "v:dfespixnm:loh";
static struct option long_options[] = {
{"volume", required_argument, 0, 'v'},
{"dirs-only", no_argument, 0, 'd'},
{"files-only", no_argument, 0, 'f'},
{"exact-match", no_argument, 0, 'e'},
{"case-sensitive", no_argument, 0, 's'},
{"skip-packages", no_argument, 0, 'p'},
{"skip-invisibles", no_argument, 0, 'i'},
{"skip-inappropriate", no_argument, 0, 'x'},
{"negate-params", no_argument, 0, 'n'},
{"limit", required_argument, 0, 'm'},
{"list", no_argument, 0, 'l'},
{"version", no_argument, 0, 'o'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
static BOOL fsgetpathAvailable = NO;
static BOOL dirsOnly = NO;
static BOOL filesOnly = NO;
static BOOL exactMatchOnly = NO;
static BOOL caseSensitive = NO;
static BOOL skipPackages = NO;
static BOOL skipInvisibles = NO;
static BOOL skipInappropriate = NO;
static BOOL negateParams = NO;
static NSUInteger limit = 0;
static BOOL startMatchOnly = NO;
static BOOL endMatchOnly = NO;
#pragma mark -
int main(int argc, const char *argv[]) {
NSString *volumePath = DEFAULT_VOLUME;
// Check if running macOS 10.13 or later
fsgetpathAvailable = has_fsgetpath();
// Line-buffered output
setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
// Parse options
int optch;
int long_index = 0;
while ((optch = getopt_long(argc, (char *const *)argv, optstring, long_options, &long_index)) != -1) {
switch (optch) {
case 'l':
list_volumes();
exit(EX_OK);
break;
case 'v':
volumePath = dev_to_mount_path([@(optarg) stringByResolvingSymlinksInPath]);
break;
case 'd':
dirsOnly = YES;
break;
case 'f':
filesOnly = YES;
break;
case 'e':
exactMatchOnly = YES;
break;
case 's':
caseSensitive = YES;
break;
case 'p':
skipPackages = YES;
break;
case 'i':
skipInvisibles = YES;
break;
case 'x':
skipInappropriate = YES;
break;
case 'n':
negateParams = YES;
break;
case 'm':
limit = [@(optarg) integerValue];
break;
case 'o':
print_version();
exit(EX_OK);
break;
case 'h':
default:
{
print_usage();
exit(EX_OK);
}
break;
}
}
if (dirsOnly && filesOnly) {
fprintf(stderr, "-d and -f flags are mutually exclusive.\n");
print_usage();
exit(EX_USAGE);
}
// Verify that path is the mount path for a file system
if (![volumePath isEqualToString:DEFAULT_VOLUME] && !is_mount_path(volumePath)) {
fprintf(stderr, "Not a volume mount point: %s\n", [volumePath cStringUsingEncoding:NSUTF8StringEncoding]);
print_usage();
exit(EX_USAGE);
}
// Verify that volume supports catalog search
if (!vol_supports_searchfs(volumePath)) {
fprintf(stderr, "Voume does not support catalog search: %s\n", [volumePath cStringUsingEncoding:NSUTF8StringEncoding]);
exit(EX_UNAVAILABLE);
}
if (optind >= argc) {
print_usage();
exit(EX_USAGE);
}
NSString *searchStr = @(argv[optind]);
// Empty search string will match 0 results
// if ([searchStr length] == 0 || [searchStr length] > PATH_MAX) {
// fprintf(stderr, "Error: Invalid search string.\n");
// exit(EX_USAGE);
// }
// Parse ^ and $ modifiers
if ([searchStr hasPrefix:@"^"]) {
startMatchOnly = YES;
searchStr = [searchStr substringWithRange:NSMakeRange(1, [searchStr length] - 1)];
}
if ([searchStr hasSuffix:@"$"]) {
endMatchOnly = YES;
searchStr = [searchStr substringWithRange:NSMakeRange(0, [searchStr length] - 1)];
}
if (startMatchOnly && endMatchOnly) {
exactMatchOnly = YES;
}
// Do search
do_searchfs_search([volumePath cStringUsingEncoding:NSUTF8StringEncoding],
[searchStr cStringUsingEncoding:NSUTF8StringEncoding]);
return EX_OK;
}
#pragma mark - search
static void do_searchfs_search(const char *volpath, const char *match_string) {
// See "man 2 searchfs" for further details
int err = 0;
int items_found = 0;
int ebusy_count = 0;
unsigned long matches;
unsigned int search_options;
struct fssearchblock search_blk;
struct attrlist return_list;
struct searchstate search_state;
struct packed_name_attr info1;
struct packed_attr_ref info2;
packed_result result_buffer[MAX_MATCHES];
catalog_changed:
items_found = 0; // Set this here in case we're completely restarting
search_blk.searchattrs.bitmapcount = ATTR_BIT_MAP_COUNT;
search_blk.searchattrs.reserved = 0;
search_blk.searchattrs.commonattr = ATTR_CMN_NAME;
search_blk.searchattrs.volattr = 0;
search_blk.searchattrs.dirattr = 0;
search_blk.searchattrs.fileattr = 0;
search_blk.searchattrs.forkattr = 0;
// Set up the attributes we want for all returned matches.
search_blk.returnattrs = &return_list;
return_list.bitmapcount = ATTR_BIT_MAP_COUNT;
return_list.reserved = 0;
return_list.commonattr = ATTR_CMN_FSID | ATTR_CMN_OBJID;
return_list.volattr = 0;
return_list.dirattr = 0;
return_list.fileattr = 0;
return_list.forkattr = 0;
// Allocate a buffer for returned matches
search_blk.returnbuffer = result_buffer;
search_blk.returnbuffersize = sizeof(result_buffer);
// Pack the searchparams1 into a buffer
// NOTE: A name appears only in searchparams1
strcpy(info1.name, match_string);
info1.ref.attr_dataoffset = sizeof(struct attrreference);
info1.ref.attr_length = (u_int32_t)strlen(info1.name) + 1;
info1.size = sizeof(struct attrreference) + info1.ref.attr_length;
search_blk.searchparams1 = &info1;
search_blk.sizeofsearchparams1 = info1.size + sizeof(u_int32_t);
// Pack the searchparams2 into a buffer
info2.size = sizeof(struct attrreference);
info2.ref.attr_dataoffset = sizeof(struct attrreference);
info2.ref.attr_length = 0;
search_blk.searchparams2 = &info2;
search_blk.sizeofsearchparams2 = sizeof(info2);
// Maximum number of matches we want
search_blk.maxmatches = MAX_MATCHES;
// Maximum time to search, per call
search_blk.timelimit.tv_sec = 1;
search_blk.timelimit.tv_usec = 0;
// Configure search options
search_options = SRCHFS_START;
if (!dirsOnly) {
search_options |= SRCHFS_MATCHFILES;
}
if (!filesOnly) {
search_options |= SRCHFS_MATCHDIRS;
}
if (!exactMatchOnly) {
search_options |= SRCHFS_MATCHPARTIALNAMES;
}
if (skipPackages) {
search_options |= SRCHFS_SKIPPACKAGES;
}
if (skipInvisibles) {
search_options |= SRCHFS_SKIPINVISIBLE;
}
if (skipInappropriate) {
search_options |= SRCHFS_SKIPINAPPROPRIATE;
}
if (negateParams) {
search_options |= SRCHFS_NEGATEPARAMS;
}
unsigned int match_cnt = 0;
// Start searching
do {
err = searchfs(volpath, &search_blk, &matches, 0, search_options, &search_state);
if (err == -1) {
err = errno;
}
if ((err == 0 || err == EAGAIN) && matches > 0) {
// Unpack the results
char *ptr = (char *)&result_buffer[0];
char *end_ptr = (ptr + sizeof(result_buffer));
for (int i = 0; i < matches; ++i) {
packed_result_p result_p = (packed_result_p)ptr;
items_found++;
// Call private SPI fsgetpath to get path string for file system object ID
char path_buf[PATH_MAX];
ssize_t size = fsgetpath_compat((char *)&path_buf,
sizeof(path_buf),
&result_p->fs_id,
(uint64_t)result_p->obj_id.fid_objno |
((uint64_t)result_p->obj_id.fid_generation << 32));
if (size > -1) {
if (strlen(match_string) > 0 && !filter_result(path_buf, match_string)) {
fprintf(stdout, "%s\n", path_buf);
match_cnt++;
if (limit && match_cnt >= limit) {
return;
}
}
} else {
// Getting path failed. This may be because the file system object
// was deleted in the interval between being found and path lookup.
// Fail silently.
// fprintf(stderr, "Unable to get path for object ID: %d\n", result_p->obj_id.fid_objno);
}
ptr = (ptr + result_p->size);
if (ptr > end_ptr) {
break;
}
}
}
// EBUSY indicates catalog change; retry a few times.
if ((err == EBUSY) && (ebusy_count++ < MAX_EBUSY_RETRIES)) {
//fprintf(stderr, "EBUSY, trying again");
goto catalog_changed;
}
if (err != 0 && err != EAGAIN) {
fprintf(stderr, "searchfs() function failed with error %d - \"%s\"\n", err, strerror(err));
}
search_options &= ~SRCHFS_START;
} while (err == EAGAIN);
}
#pragma mark - post-processing
BOOL filter_result(const char *path, const char *match_string) {
if (!caseSensitive && !startMatchOnly && !endMatchOnly) {
return NO;
}
NSString *pathStr = @(path);
NSString *matchStr = @(match_string);
if (caseSensitive) {
NSString *escMatch = [NSRegularExpression escapedTemplateForString:matchStr];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:escMatch
options:0
error:nil];
NSTextCheckingResult *res = [regex firstMatchInString:pathStr
options:0
range:NSMakeRange(0, [pathStr length])];
if (res == nil) {
return YES;
}
}
if (!exactMatchOnly) {
if (startMatchOnly) {
if (![[pathStr lastPathComponent] hasPrefix:matchStr]) {
return YES;
}
}
if (endMatchOnly) {
if (![[pathStr lastPathComponent] hasSuffix:matchStr]) {
return YES;
}
}
}
return NO;
}
#pragma mark - fsgetpath compatibility shim
// Use deprecated Carbon API to get OS version.
// This is the only reliable way on OS versions prior to 10.10
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
static BOOL has_fsgetpath(void) {
SInt32 maj, min;
Gestalt(gestaltSystemVersionMajor, &maj);
Gestalt(gestaltSystemVersionMinor, &min);
return (maj > 10 || (maj == 10 && min >= 13));
}
#pragma GCC diagnostic pop
// fsgetpath was introduced in macOS 10.13. To support older systems, we use a
// compatibility shim that relies on the volfs support in older versions of the OS
// See https://forums.developer.apple.com/thread/103162
static ssize_t fsgetpath_compat(char *buf, size_t buflen, fsid_t *fsid, uint64_t obj_id) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpartial-availability"
if (fsgetpathAvailable) {
ssize_t size = fsgetpath(buf, buflen, fsid, obj_id);
if (size > -1) {
return size;
}
}
#pragma clang diagnastic pop
return fsgetpath_legacy(buf, buflen, fsid, obj_id);
}
static ssize_t fsgetpath_legacy(char *buf, size_t buflen, fsid_t *fsid, uint64_t obj_id) {
char volfsPath[64]; // 8 for `/.vol//\0`, 10 for `fsid->val[0]`, 20 for `obj_id`, rounded up for paranoia
snprintf(volfsPath, sizeof(volfsPath), "/.vol/%ld/%llu", (long)fsid->val[0], (unsigned long long)obj_id);
struct {
uint32_t length;
attrreference_t pathRef;
char buffer[MAXPATHLEN];
} __attribute__((aligned(4), packed)) attrBuf;
struct attrlist attrList;
memset(&attrList, 0, sizeof(attrList));
attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
attrList.commonattr = ATTR_CMN_FULLPATH;
int success = getattrlist(volfsPath, &attrList, &attrBuf, sizeof(attrBuf), 0) == 0;
if (!success) {
return -1;
}
if (attrBuf.pathRef.attr_length > buflen) {
errno = ENOSPC;
return -1;
}
strlcpy(buf, ((const char *)&attrBuf.pathRef) + attrBuf.pathRef.attr_dataoffset, buflen);
return attrBuf.pathRef.attr_length;
}
#pragma mark - util
static NSString *dev_to_mount_path(NSString *devPath) {
// Check if string is a device path. If so, look up
// the mount point for the device
if ([[NSFileManager defaultManager] fileExistsAtPath:devPath] && ![devPath hasPrefix:@"/dev/"]) {
return devPath;
}
if (![devPath hasPrefix:@"/"]) {
// Could be a device name
devPath = [NSString stringWithFormat:@"/dev/%@", devPath];
}
// Make sure it exists
if (![[NSFileManager defaultManager] fileExistsAtPath:devPath]) {
fprintf(stderr, "No such device: %s\n",
[devPath cStringUsingEncoding:NSUTF8StringEncoding]);
exit(EX_UNAVAILABLE);
}
// Look up mount point for device
int fs_count = getfsstat(NULL, 0, MNT_NOWAIT);
if (fs_count == -1) {
fprintf(stderr, "Error: %d\n", errno);
return nil;
}
struct statfs buf[256];
getfsstat(buf, fs_count * sizeof(buf[0]), MNT_NOWAIT);
for (int i = 0; i < fs_count; ++i) {
if ([@(buf[i].f_mntfromname) isEqualToString:devPath]) {
return @(buf[i].f_mntonname);
}
}
return nil;
}
static BOOL is_mount_path(NSString *path) {
NSArray *mountPaths = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:nil options:0];
for (NSURL *mountPathURL in mountPaths) {
if ([path isEqualToString:[mountPathURL path]]) {
return YES;
}
}
return NO;
}
static BOOL vol_supports_searchfs(NSString *path) {
struct vol_attr_buf {
u_int32_t size;
vol_capabilities_attr_t vol_capabilities;
} __attribute__((aligned(4), packed));
const char *p = [path cStringUsingEncoding:NSUTF8StringEncoding];
struct attrlist attrList;
memset(&attrList, 0, sizeof(attrList));
attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
attrList.volattr = (ATTR_VOL_INFO | ATTR_VOL_CAPABILITIES);
struct vol_attr_buf attrBuf;
memset(&attrBuf, 0, sizeof(attrBuf));
int err = getattrlist(p, &attrList, &attrBuf, sizeof(attrBuf), 0);
if (err != 0) {
err = errno;
fprintf(stderr, "Error %d getting attrs for volume %s\n", err, p);
return NO;
}
assert(attrBuf.size == sizeof(attrBuf));
if ((attrBuf.vol_capabilities.valid[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_SEARCHFS) == VOL_CAP_INT_SEARCHFS) {
if ((attrBuf.vol_capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_SEARCHFS) == VOL_CAP_INT_SEARCHFS) {
return YES;
}
}
return NO;
}
static void list_volumes(void) {
int fs_count = getfsstat(NULL, 0, MNT_NOWAIT);
if (fs_count == -1) {
fprintf(stderr, "Error: %d\n", errno);
return;
}
struct statfs buf[256];
getfsstat(buf, fs_count * sizeof(buf[0]), MNT_NOWAIT);
for (int i = 0; i < fs_count; ++i) {
if (!vol_supports_searchfs(@(buf[i].f_mntonname))) {
continue;
}
printf("%s (%-4s): %s\n",
buf[i].f_mntfromname,
buf[i].f_fstypename,
buf[i].f_mntonname);
}
}
#pragma mark -
static void print_usage(void) {
fprintf(stderr, "usage: searchfs [-ldfespixnmoh] [-v mount_point] search_term\n");
}
static void print_version(void) {
printf("searchfs version %.2f\n", program_version);
}