-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacFinderInfo
More file actions
executable file
·400 lines (317 loc) · 10.8 KB
/
macFinderInfo
File metadata and controls
executable file
·400 lines (317 loc) · 10.8 KB
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
#!/usr/bin/env perl -w
#
# macFinderInfo: Extract and diplay Mac 'finderInfo', etc.
# 2011-04-05: Written by Steven J. DeRose.
#
use strict;
use Getopt::Long;
use Encode;
use XmlTuples;
use sjdUtils;
our %metadata = (
'title' => "macFinderInfo",
'description' => "Extract and diplay Mac 'finderInfo', etc.",
'rightsHolder' => "Steven J. DeRose",
'creator' => "http://viaf.org/viaf/50334488",
'type' => "http://purl.org/dc/dcmitype/Software",
'language' => "Perl 5.18",
'created' => "2011-04-05",
'modified' => "2021-06-14",
'publisher' => "http://github.com/sderose",
'license' => "https://creativecommons.org/licenses/by-sa/3.0/"
);
our $VERSION_DATE = $metadata{'modified'};
use Cwd 'abs_path';
(my $thisPath = abs_path($0)) =~ s|/[^/]*$||;
=pod
=head1 Usage
finderInfo [options] files
Extract and diplay the 'finderInfo' for a given file, mostly using C<xattr>.
Intended to be more readable than the usual.
Also shows UUID for the partition, volume, and host.
=head1 Options
(prefix 'no' to negate where applicable)
=over
=item * B<--listKeys>
Show a list of common information items. For more details, see C<spotlite> or
L[https://developer.apple.com/documentation/coreservices/file_metadata/mditem/file_system_metadata_attribute_keys].
=item * B<--quiet> OR B<-q>
Suppress most messages.
=item * B<--toHex>
Try to make hex values more readable.
=item * B<--verbose> OR B<-v>
Add more messages (repeatable).
=item * B<--version>
Show version info and exit.
=item * B<--visible>
Translate unusual characters to visible representation.
=back
=head1 Known Bugs and Limitations
Doesn't split out and report individual finderFlags yet (though info is here).
=head1 Related Commands and Files
PyPI package `xattr` gives direct access to get and set these attrs.
F<macOsxSpotlightKeys.xsv>, C<XmlTuples.pm>.
C<whereFrom>, C<pdfinfo>, C<file>, C<xattr>.
=head1 To do
Finish adding PartitionID, VolumeID, getHostID, inode, etc.
Option to get *all* xattr data, and/or to check kMDItemWhereFroms (see xattr -l,
but chop trailing nulls, decode dates, etc.
Do something smarter with (Unicode?) strings.
Parse the internals.
See e.g. https://eclecticlight.co/2017/12/19/xattr-com-apple-finderinfo-information-for-the-finder/
"It’s a FileInfo structure followed by a ExtendedFileInfo structure. These are
located in the CoreServices Framework (CarbonCore>Finder.h>)."
https://opensource.apple.com/source/CarbonHeaders/CarbonHeaders-8A428/Finder.h
struct FileInfo {
OSType fileType; /* The type of the file (4 bytes) */
OSType fileCreator; /* The file's creator */
UInt16 finderFlags; /* ex: kHasBundle, kIsInvisible... */
Point location; /* File's location in the folder */
/* If set to {0, 0}, the Finder will place the item automatically */
UInt16 reservedField; /* (set to 0) */
};
typedef struct FileInfo FileInfo;
struct FolderInfo {
Rect windowBounds; /* The position and dimension of the folder's window */
UInt16 finderFlags; /* ex. kIsInvisible, kNameLocked, etc.*/
Point location; /* Folder's location in the parent folder */
/* If set to {0, 0}, the Finder will place the item automatically */
UInt16 reservedField; /* (set to 0) */
};
typedef struct FolderInfo FolderInfo;
struct ExtendedFileInfo {
SInt16 reserved1[4]; /* Reserved (set to 0) */
UInt16 extendedFinderFlags; /* Extended flags (custom badge, routing info...) */
SInt16 reserved2; /* Reserved (set to 0). Comment ID if high-bit is clear */
SInt32 putAwayFolderID; /* Put away folder ID */
};
typedef struct ExtendedFileInfo ExtendedFileInfo;
struct ExtendedFolderInfo {
Point scrollPosition; /* Scroll position (for icon views) */
SInt32 reserved1; /* Reserved (set to 0) */
UInt16 extendedFinderFlags; /* Extended flags (custom badge, routing info...) */
SInt16 reserved2; /* Reserved (set to 0). Comment ID if high-bit is clear */
SInt32 putAwayFolderID; /* Put away folder ID */
};
/* Finder flags and Extended flags -- see below */
=head1 History
2011-04-05: Written by Steven J. DeRose.
2018-09-20: Handles paths containing spaces etc.
2020-07-13: New layout.
2020-08-11: Fix decoding/display of utf-8 value, ";"-delimited list values.
2021-06-14: Add --visible.
=head1 Ownership
Copyright 2011-04-05 by Steven J. DeRose. This work is licensed under a
Creative Commons Attribution-Share Alike 3.0 Unported License.
For further information on this license, see
L<https://creativecommons.org/licenses/by-sa/3.0>.
For the most recent version, see L<http://www.derose.net/steve/utilities> or
L<https://github.com/sderose>.
=cut
###############################################################################
#
my %finderFlags = (
kIsOnDesk => 0x0001,
kColor => 0x000E,
kIsShared => 0x0040,
kHasNoINITs => 0x0080,
kHasBeenInited => 0x0100,
kHasCustomIcon => 0x0400,
kIsStationery => 0x0800,
kNameLocked => 0x1000,
kHasBundle => 0x2000,
kIsInvisible => 0x4000,
kIsAlias => 0x8000,
);
my %extendedFinderFlags = (
kExtendedFlagsAreInvalid => 0x8000,
kExtendedFlagHasCustomBadge => 0x0100,
kExtendedFlagObjectIsBusy => 0x0080,
kExtendedFlagHasRoutingInfo => 0x0004,
);
my @defaultColors = (
"none", "red", "orange", "yellow", "green", "blue", "purple", "gray"
);
my %cAliasFile = (
cAliasFile => 'alia',
cApplicationFile => 'appf',
cControlPanelFile => 'ccdv',
cDeskAccessoryFile => 'dafi',
cDocumentFile => 'docf',
cFontFile => 'fntf',
cSoundFile => 'sndf',
cClippingFile => 'clpf',
cContainer => 'ctnr',
cDesktop => 'cdsk',
cSharableContainer => 'sctr',
cDisk => 'cdis',
cFolder => 'cfol',
cSuitcase => 'stcs',
cAccessorySuitcase => 'dsut',
cFontSuitcase => 'fsut',
cTrash => 'ctrs',
cDesktopPrinter => 'dskp',
cPackage => 'pack',
cContentSpace => 'dwnd',
cContainerWindow => 'cwnd',
cInfoWindow => 'iwnd',
cSharingWindow => 'swnd',
cStatusWindow => 'qwnd',
cClippingWindow => 'lwnd',
cPreferencesWindow => 'pwnd',
cDTPWindow => 'dtpw',
cProcess => 'prcs',
cAccessoryProcess => 'pcda',
cApplicationProcess => 'pcap',
cGroup => 'sgrp',
cUser => 'cuse',
cSharingPrivileges => 'priv',
cPreferences => 'cprf',
cLabel => 'clbl',
cSound => 'snd ',
cAliasList => 'alst',
cSpecialFolders => 'spfl',
cOnlineDisk => 'cods',
cOnlineLocalDisk => 'clds',
cOnlineRemoteDisk => 'crds',
cEntireContents => 'ects',
cIconFamily => 'ifam',
);
# These appear as 'com.apple.metadata:'...
my %others = (
kMDItemDownloadedDate => 1,
kMDItemWhereFroms => 1,
);
my $fmt = " %-16s = '%s'\n";
###############################################################################
# Options
#
my $quiet = 0;
my $keyFile = "$thisPath/macOsxSpotLightKeys.xsv";
my $listKeys = 0;
my $toHex = 0;
my $verbose = 0;
my $visible = 1;
Getopt::Long::Configure ("ignore_case");
my $result = GetOptions(
"h|help" => sub { system "perldoc $0"; exit; },
"listKeys!" => \$listKeys,
"q!" => \$quiet,
"toHex!" => \$toHex,
"v+" => \$verbose,
"version" => sub {
die "Version of $VERSION_DATE, by Steven J. DeRose.\n";
},
"visible!" => \$visible,
);
($result) || die "Bad options.\n";
###############################################################################
# Main
#
if ($listKeys) {
listKeys();
exit;
}
(scalar(@ARGV)) || die "No file(s) specified.\n";
while (my $file = shift) {
(!$quiet) && warn "\n*** Starting file '$file'\n";
doOneFile($file);
}
exit;
###############################################################################
#
sub listKeys {
my $foo = new XmlTuples();
$foo->open($keyFile) || die
"Couldn't open list of Spotlight keys from '$keyFile'.\n"; # FIX
my $keysRef = $foo->getAllAsHash("Key"); # FIX
warn("\nKey names start with 'kMDItem' (not shown below). Key file: \n");
warn(" $keyFile\n");
for my $k (sort(keys(%{$keysRef}))) {
(my $shortKey = $k) =~ s/^kMDItem//;
warn(sprintf("%-24s", $shortKey) .
" " . $keysRef->{$k}->{"Typ"} .
"\n" . wrap($keysRef->{$k}->{"Dsc"}, 4));
}
warn("Total keys known: " . scalar(keys(%{$keysRef})) . ".\n");
}
sub doOneFile {
my ($file) = @_;
my @attrNames = `xattr '$file'`; # Typically returns 'com.apple.FinderInfo'
for my $name (@attrNames) {
#($verbose) && warn "Fetching xattr '$name'.\n";
chomp $name;
my $cmd = "xattr -p $name '$file'";
my $val = `$cmd`;
print " '$name'\n";
if ($val =~ m/;.*;/) {
print " =(array?): [\n";
chomp $val;
my @entries = split /;/, $val;
print " " . join(";\n ", @entries) . "\n ]\n";
}
elsif (" $val" =~ m/^(\s[0-9A-F][0-9A-F])+\s*$/s) {
print " =(raw string): ###$val###\n";
(length($val)/3 == 32) || warn
sprintf("Length is %d, not 32.\n", length($val)/3);
print " =(hex string): '" . x2str($val) . "'\n";
}
else {
print " =(raw string): ###$val###\n";
}
}
print("Partition UUID: %s", getPartitionID($file));
print("Volume UUID: %s", getVolumeID($file));
print("Host UUID: %s", getHostID($file));
}
sub getPartitionID {
my ($file) = @_;
return "???";
}
sub getVolumeID {
my ($file) = @_;
return "???";
}
sub getHostID {
my ($file) = @_;
return "???";
}
###############################################################################
#
BEGIN {
my $theString = undef;
my $theOffset = 0;
sub setHexString {
my ($s) = @_;
$theString = $s;
$theOffset = 0;
}
sub nextN {
my ($n) = @_;
my $buf = substr($theString, $theOffset, $n);
$theOffset += $n;
return($buf);
}
} # END
# Convert a string like this to characters:
# 00 00 00 00 00 00 00 00 40 00 00 00...
# Doesn't handle utf8 or ucs2.
#
sub x2str {
my ($s) = @_;
my $buf = "";
Encode::decode('utf8', $s);
if ($toHex) {
while ($s =~ m/\s*([0-9a-f][0-9a-f])/ig) {
my $x = hex("0x".$1);
my $c = chr($x);
$buf .= $c;
}
}
if ($visible) { $buf = sjdUtils::showInvisibles($buf); }
return($buf);
}
sub x2int {
my ($s) = @_;
return(hex("0x".$s));
}