-
Notifications
You must be signed in to change notification settings - Fork 3
/
listing2.html
executable file
·1596 lines (1320 loc) · 54.8 KB
/
listing2.html
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<!-- BEGIN META TAG INFO -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="home" href="http://developer.apple.com/">
<link rel="find" href="http://developer.apple.com/search/">
<link rel="stylesheet" type="text/css" href="../../documentation/css/adcstyle.css" title="fonts">
<script language="JavaScript" src="../../documentation/js/adc.js" type="text/javascript"></script>
<!-- END META TAG INFO -->
<!-- BEGIN TITLE -->
<title>FSMegaInfo - /BSD.c</title>
<!-- END TITLE -->
<script language="JavaScript">
function JumpToNewPage() {
window.location=document.scpopupmenu.gotop.value;
return true;
}
</script>
</head>
<!-- BEGIN BODY OPEN -->
<body>
<!--END BODY OPEN -->
<!-- START CENTER OPEN -->
<center>
<!-- END CENTER OPEN -->
<!-- BEGIN LOGO AND SEARCH -->
<!--#include virtual="/includes/adcnavbar"-->
<!-- END LOGO AND SEARCH -->
<!-- START BREADCRUMB -->
<div id="breadcrumb">
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td scope="row"><img width="340" height="10" src="images/1dot.gif" alt=""></td>
<td><img width="340" height="10" src="images/1dot.gif" alt=""></td>
</tr>
<tr valign="middle">
<td align="left" colspan="2">
<a href="http://developer.apple.com/">ADC Home</a> > <a href="../../referencelibrary/index.html">Reference Library</a> > <a href="../../samplecode/index.html">Sample Code</a> > <a href="../../samplecode/Darwin/index.html">Darwin</a> > <a href="../../samplecode/Darwin/idxFileManagement-date.html">File Management</a> > <A HREF="javascript:location.replace('index.html');">FSMegaInfo</A> >
</td>
</tr>
<tr>
<td colspan="2" scope="row"><img width="680" height="35" src="images/1dot.gif" alt=""></td>
</tr>
</table>
</div>
<!-- END BREADCRUMB -->
<!-- START MAIN CONTENT -->
<!-- START TITLE GRAPHIC AND INTRO-->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td><h1><div id="pagehead">FSMegaInfo</div></h1></td>
</tr>
</table>
<!-- END TITLE GRAPHIC AND INTRO -->
<!-- START WIDE COLUMN -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td id="scdetails">
<h2>/BSD.c</h2>
<form name="scpopupmenu" onSubmit="return false;" method=post>
<p><strong>View Source Code:</strong>
<select name="gotop" onChange="JumpToNewPage();" style="width:340px"><option selected value="ingnore">Select File</option>
<option value="listing1.html">/AliasManager.c</option>
<option value="listing2.html">/BSD.c</option>
<option value="listing3.html">/BSD.h</option>
<option value="listing4.html">/BSDAttrList.c</option>
<option value="listing5.html">/Command.c</option>
<option value="listing6.html">/Command.h</option>
<option value="listing7.html">/DiskArb.c</option>
<option value="listing8.html">/FieldPrinter.c</option>
<option value="listing9.html">/FieldPrinter.h</option>
<option value="listing10.html">/FileManager.c</option>
<option value="listing11.html">/FolderManager.c</option>
<option value="listing12.html">/FSMegaInfo.c</option>
<option value="listing13.html">/Read Me About FSMegaInfo.txt</option>
<option value="listing14.html">/Tests/ten-five-intel.txt</option>
<option value="listing15.html">/Tests/ten-five-one-ppc64.txt</option>
<option value="listing16.html">/Tests/ten-five-ppc.txt</option>
<option value="listing17.html">/Tests/ten-four-eleven-intel.txt</option>
<option value="listing18.html">/Tests/ten-four-eleven-ppc.txt</option></select>
</p>
</form>
<p><strong><a href="FSMegaInfo.zip">Download Sample</a></strong> (“FSMegaInfo.zip”, 522.7K)<BR>
<strong><a href="FSMegaInfo.dmg">Download Sample</a></strong> (“FSMegaInfo.dmg”, 920.2K)</p>
<!--
<p><strong><a href="#">Download Sample</a></strong> (“filename.sit”, 500K)</p>
-->
</td>
</tr>
<tr>
<td scope="row"><img width="680" height="10" src="images/1dot.gif" alt=""><br>
<img height="1" width="680" src="images/1dot_919699.gif" alt=""><br>
<img width="680" height="20" src="images/1dot.gif" alt=""></td>
</tr>
<tr>
<td scope="row">
<!--googleon: index -->
<pre class="sourcecodebox">/*
File: BSD.c
Contains: BSD command processing.
Written by: DTS
Copyright: Copyright (c) 2008 Apple Inc. All Rights Reserved.
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or
redistribution of this Apple software constitutes acceptance of
these terms. If you do not agree with these terms, please do
not use, install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following
terms, and subject to these terms, Apple grants you a personal,
non-exclusive license, under Apple's copyrights in this
original Apple software (the "Apple Software"), to use,
reproduce, modify and redistribute the Apple Software, with or
without modifications, in source and/or binary forms; provided
that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the
following text and disclaimers in all such redistributions of
the Apple Software. Neither the name, trademarks, service marks
or logos of Apple Inc. may be used to endorse or promote
products derived from the Apple Software without specific prior
written permission from Apple. Except as expressly stated in
this notice, no other rights or licenses, express or implied,
are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or
by other works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis.
APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING
THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT,
INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY
OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY
OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "BSD.h"
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/vnode.h>
#include <stdint.h>
#include <inttypes.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/xattr.h>
#include <fts.h>
#include "FieldPrinter.h"
#include "Command.h"
/////////////////////////////////////////////////////////////////
#pragma mark ****** Utilities
static int GetFSList(struct statfs **fsList, int *fsCountPtr)
// If fsBuf is too small to account for all volumes, getfsstat will
// silently truncate the returned information. Worse yet, it returns
// the number of volumes it passed back, not the number of volumes present,
// so you can't tell if the list was truncated.
//
// So, in order to get an accurate snapshot of the volume list, I call
// getfsstat with a NULL fsBuf to get a count (fsCountOrig), then allocate a
// buffer that holds (fsCountOrig + 1) items, then call getfsstat again with
// that buffer. If the list was silently truncated, the second count (fsCount)
// will be (fsCountOrig + 1), and we loop to try again.
{
int err;
int fsCountOrig;
int fsCount;
struct statfs * fsBuf;
bool done;
assert( fsList != NULL);
assert(*fsList == NULL);
assert(fsCountPtr != NULL);
fsBuf = NULL;
fsCount = 0;
done = false;
do {
// Get the initial count.
err = 0;
fsCountOrig = getfsstat(NULL, 0, MNT_WAIT);
if (fsCountOrig < 0) {
err = errno;
}
// Allocate a buffer for fsCountOrig + 1 items.
if (err == 0) {
if (fsBuf != NULL) {
free(fsBuf);
}
fsBuf = malloc((fsCountOrig + 1) * sizeof(*fsBuf));
if (fsBuf == NULL) {
err = ENOMEM;
}
}
// Get the list.
if (err == 0) {
fsCount = getfsstat(fsBuf, (int) ((fsCountOrig + 1) * sizeof(*fsBuf)), MNT_WAIT);
if (fsCount < 0) {
err = errno;
}
}
// We got the full list if the number of items returned by the kernel
// is strictly less than the buffer that we allocated (fsCountOrig + 1).
if (err == 0) {
if (fsCount <= fsCountOrig) {
done = true;
}
}
} while ( (err == 0) && ! done );
// Clean up.
if (err != 0) {
free(fsBuf);
fsBuf = NULL;
}
*fsList = fsBuf;
*fsCountPtr = fsCount;
assert( (err == 0) == (*fsList != NULL) );
return err;
}
static int MyLStatFS(const char *itemPath, struct statfs *sfsb)
// There is no lstatfs <rdar://problem/4154584>, so I have to emulate
// this myself. I do this by calling lstat on the path and
// then looking through the volume list calling lstat
// on the root directory of each entry; if the st_dev fields
// match, we're on the right file volume.
{
int err;
struct statfs * fsList;
int fsCount;
int fsIndex;
struct stat itemInfo;
struct stat fsRootInfo;
bool found;
assert(itemPath != NULL);
assert(sfsb != NULL);
fsList = NULL;
err = lstat(itemPath, &itemInfo);
if (err < 0) {
err = errno;
}
if (err == 0) {
err = GetFSList(&fsList, &fsCount);
}
if (err == 0) {
found = false;
for (fsIndex = 0; fsIndex < fsCount; fsIndex++) {
err = lstat(fsList[fsIndex].f_mntonname, &fsRootInfo);
if ( (err == 0) && (fsRootInfo.st_dev == itemInfo.st_dev) ) {
*sfsb = fsList[fsIndex];
found = true;
break;
}
}
// It's possible that one of the volumes in fsList could disappear
// while we're looking for a match. If so, we either swallow the
// error (if we did find a match) or return ENOENT (otherwise). The
// latter is what statfs would do if the itemPath was bogus.
if (found) {
err = 0;
} else {
err = ENOENT;
}
}
free(fsList);
return err;
}
/////////////////////////////////////////////////////////////////
#pragma mark ****** Commands
#pragma mark * stat/lstat
// Flag in st_flags and the ATTR_CMN_FLAGS attribute.
const FPFlagDesc kChFlagsFlags[] = {
{ UF_NODUMP, "UF_NODUMP" },
{ UF_IMMUTABLE, "UF_IMMUTABLE" },
{ UF_APPEND, "UF_APPEND" },
{ UF_OPAQUE, "UF_OPAQUE" },
{ SF_ARCHIVED, "SF_ARCHIVED" },
{ SF_IMMUTABLE, "SF_IMMUTABLE" },
{ SF_APPEND, "SF_APPEND" },
{ 0, NULL }
};
// Fields of (struct stat).
static const FPFieldDesc kStatFieldDesc[] = {
{"st_dev", offsetof(struct stat, st_dev), sizeof(dev_t), FPDevT, NULL},
{"st_ino", offsetof(struct stat, st_ino), sizeof(ino_t), FPUDec, NULL},
{"st_mode", offsetof(struct stat, st_mode), sizeof(mode_t), FPModeT, NULL},
{"st_nlink", offsetof(struct stat, st_nlink), sizeof(nlink_t), FPUDec, NULL},
{"st_uid", offsetof(struct stat, st_uid), sizeof(uid_t), FPUID, NULL},
{"st_gid", offsetof(struct stat, st_gid), sizeof(gid_t), FPGID, NULL},
{"st_rdev", offsetof(struct stat, st_rdev), sizeof(dev_t), FPDevT, NULL},
{"st_atime", offsetof(struct stat, st_atime), sizeof(struct timespec), FPTimeSpec, NULL},
{"st_mtime", offsetof(struct stat, st_mtime), sizeof(struct timespec), FPTimeSpec, NULL},
{"st_ctime", offsetof(struct stat, st_ctime), sizeof(struct timespec), FPTimeSpec, NULL},
{"st_size", offsetof(struct stat, st_size), sizeof(off_t), FPSize, NULL},
{"st_blocks", offsetof(struct stat, st_blocks), sizeof(blkcnt_t), FPUDec, NULL},
{"st_blksize", offsetof(struct stat, st_blksize), sizeof(blksize_t), FPUDec, NULL},
{"st_flags", offsetof(struct stat, st_flags), sizeof(__uint32_t), FPFlags, kChFlagsFlags},
{"st_gen", offsetof(struct stat, st_gen), sizeof(__uint32_t), FPUDec, NULL},
{"st_lspare", offsetof(struct stat, st_lspare), sizeof(__int32_t), FPNull, NULL},
{"st_qspare", offsetof(struct stat, st_qspare), sizeof(__int64_t), FPNull, NULL},
{NULL, 0, 0, NULL, NULL}
};
static CommandError PrintStatInfoCommon(CommandArgsRef args, uint32_t indent, uint32_t verbose, bool lStat)
// Uses lstat to get information about the specified item and
// prints the result.
//
// indent and verbose are as per the comments for FPPrinter.
{
int err;
struct stat sb;
const char * itemPath;
assert( CommandArgsValid(args) );
err = CommandArgsGetString(args, &itemPath);
if (err == 0) {
if (lStat) {
fprintf(stdout, "%*slstat '%s'\n", (int) indent, "", itemPath);
err = lstat(itemPath, &sb);
} else {
fprintf(stdout, "%*sstat '%s'\n", (int) indent, "", itemPath);
err = stat(itemPath, &sb);
}
if (err < 0) {
err = errno;
}
}
if (err == 0) {
FPPrintFields(kStatFieldDesc, &sb, sizeof(sb), indent + kStdIndent, verbose);
}
return CommandErrorMakeWithErrno(err);
}
static CommandError PrintStatInfo(CommandArgsRef args, uint32_t indent, uint32_t verbose)
{
assert( CommandArgsValid(args) );
return PrintStatInfoCommon(args, indent, verbose, false);
}
static CommandError PrintLStatInfo(CommandArgsRef args, uint32_t indent, uint32_t verbose)
{
assert( CommandArgsValid(args) );
return PrintStatInfoCommon(args, indent, verbose, true);
}
const CommandInfo kStatCommand = {
PrintStatInfo,
"stat",
"itemPath",
"Print information from stat.",
NULL
};
const CommandInfo kLStatCommand = {
PrintLStatInfo,
"lstat",
"itemPath",
"Print information from lstat.",
NULL
};
#pragma mark * access
static const FPFlagDesc kAccessOptions[] = {
{R_OK, "R_OK"},
{W_OK, "W_OK"},
{X_OK, "X_OK"},
{_READ_OK, "_READ_OK"},
{_WRITE_OK, "_WRITE_OK"},
{_EXECUTE_OK, "_EXECUTE_OK"},
{_DELETE_OK, "_DELETE_OK"},
{_APPEND_OK, "_APPEND_OK"},
{_RMFILE_OK, "_RMFILE_OK"},
{_RATTR_OK, "_RATTR_OK"},
{_WATTR_OK, "_WATTR_OK"},
{_REXT_OK, "_REXT_OK"},
{_WEXT_OK, "_WEXT_OK"},
{_RPERM_OK, "_RPERM_OK"},
{_WPERM_OK, "_WPERM_OK"},
{_CHOWN_OK, "_CHOWN_OK"},
{0, NULL}
};
static CommandError PrintAccessInfo(CommandArgsRef args, uint32_t indent, uint32_t verbose)
{
#pragma unused(indent)
#pragma unused(verbose)
int err;
int options;
const char * itemPath;
assert( CommandArgsValid(args) );
if (CommandArgsIsOption(args)) {
err = CommandArgsGetFlagListInt(args, kAccessOptions, &options);
} else {
options = R_OK | W_OK | X_OK;
err = 0;
}
// Get item path argument.
if (err == 0) {
err = CommandArgsGetString(args, &itemPath);
}
if (err == 0) {
// *** command summary should include the access requested
fprintf(stdout, "%*saccess '%s'\n", (int) indent, "", itemPath);
err = access(itemPath, options);
if (err < 0) {
err = errno;
}
if (err == 0) {
fprintf(stdout, "%*sOK\n", (int) indent, "");
}
}
return CommandErrorMakeWithErrno(err);
}
static const CommandHelpEntry kAccessCommandHelp[] = {
{CommandHelpString, "-options Access options; default is -R_OK,W_OK,X_OK"},
{CommandHelpFlags, kAccessOptions},
{NULL, NULL}
};
const CommandInfo kAccessCommand = {
PrintAccessInfo,
"access",
"[ -options ] itemPath",
"Print information from lstat.",
kAccessCommandHelp
};
#pragma mark * statfs
// Flags for the f_flags field of the statfs structure.
const FPFlagDesc kMountFlags[] = {
{MNT_RDONLY, "MNT_RDONLY"},
{MNT_SYNCHRONOUS, "MNT_SYNCHRONOUS"},
{MNT_NOEXEC, "MNT_NOEXEC"},
{MNT_NOSUID, "MNT_NOSUID"},
{MNT_NODEV, "MNT_NODEV"},
{MNT_UNION, "MNT_UNION"},
{MNT_ASYNC, "MNT_ASYNC"},
{MNT_DONTBROWSE, "MNT_DONTBROWSE"},
{MNT_IGNORE_OWNERSHIP, "MNT_IGNORE_OWNERSHIP"},
{MNT_AUTOMOUNTED, "MNT_AUTOMOUNTED"},
{MNT_JOURNALED, "MNT_JOURNALED"},
{MNT_NOUSERXATTR, "MNT_NOUSERXATTR"},
{MNT_DEFWRITE, "MNT_DEFWRITE"},
{MNT_MULTILABEL, "MNT_MULTILABEL"},
{MNT_NOATIME, "MNT_NOATIME"},
{MNT_EXPORTED, "MNT_EXPORTED"},
{MNT_LOCAL, "MNT_LOCAL"},
{MNT_QUOTA, "MNT_QUOTA"},
{MNT_ROOTFS, "MNT_ROOTFS"},
{MNT_DOVOLFS, "MNT_DOVOLFS"},
{0, NULL}
};
// The following mount flags were removed from the 10.4 headers. Rather
// than try to do clever things to display them correctly on old systems,
// I just decided to remove them.
//
// {MNT_EXRDONLY, "MNT_EXRDONLY"},
// {MNT_DEFEXPORTED, "MNT_DEFEXPORTED"},
// {MNT_EXPORTANON, "MNT_EXPORTANON"},
// {MNT_EXKERB, "MNT_EXKERB"},
// {MNT_FIXEDSCRIPTENCODING, "MNT_FIXEDSCRIPTENCODING"},
// Size multipliers for fields in the statfs structure. See
// FPSize for a description of what this is about.
static const FPSizeMultiplier kStatFSBlocksMultiplier = {
offsetof(struct statfs, f_bsize) - offsetof(struct statfs, f_blocks),
sizeof(long)
};
static const FPSizeMultiplier kStatFSFreeBlocksMultiplier = {
offsetof(struct statfs, f_bsize) - offsetof(struct statfs, f_bfree),
sizeof(long)
};
static const FPSizeMultiplier kStatFSAvailBlocksMultiplier = {
offsetof(struct statfs, f_bsize) - offsetof(struct statfs, f_bavail),
sizeof(long)
};
// Known constants for f_type field of statfs, from Darwin source
// (xnu/bsd/vfs/vfs_conf.c).
const FPEnumDesc kFSTypeEnums[] = {
{ 17, "HFS/HFS Plus" },
{ 1, "UFS" },
{ 14, "ISO 9660" },
{ 3, "Memory File System" },
{ 2, "NFS" },
{ 13, "AndrewFS" },
{ 9, "Loopback (nullfs)" },
{ 15, "Union" },
{ 7, "File Descriptor" },
{ 18, "Volume ID (volfs)" },
{ 19, "Device (devfs)" },
{ 0, NULL },
};
// The fields of the statfs structure.
static const FPFieldDesc kStatFSFieldDesc[] = {
{"f_bsize", offsetof(struct statfs, f_bsize), sizeof(long), FPSize, NULL},
{"f_iosize", offsetof(struct statfs, f_iosize), sizeof(long), FPSize, NULL},
{"f_blocks", offsetof(struct statfs, f_blocks), sizeof(long), FPSize, &kStatFSBlocksMultiplier},
{"f_bfree", offsetof(struct statfs, f_bfree), sizeof(long), FPSize, &kStatFSFreeBlocksMultiplier},
{"f_bavail", offsetof(struct statfs, f_bavail), sizeof(long), FPSize, &kStatFSAvailBlocksMultiplier},
{"f_files", offsetof(struct statfs, f_files), sizeof(long), FPSDec, NULL},
{"f_ffree", offsetof(struct statfs, f_ffree), sizeof(long), FPSDec, NULL},
{"f_fsid.val[0]", offsetof(struct statfs, f_fsid.val[0]), sizeof(int32_t),FPHex, NULL},
{"f_fsid.val[1]", offsetof(struct statfs, f_fsid.val[1]), sizeof(int32_t),FPHex, NULL},
{"f_owner", offsetof(struct statfs, f_owner), sizeof(uid_t), FPUID, NULL},
{"f_reserved1", offsetof(struct statfs, f_reserved1), sizeof(short), FPSDec, NULL}, // file system subtype on 10.4 and later
{"f_type", offsetof(struct statfs, f_type), sizeof(short), FPEnum, kFSTypeEnums},
{"f_flags", offsetof(struct statfs, f_flags), sizeof(long), FPFlags, kMountFlags},
{"f_fstypename", offsetof(struct statfs, f_fstypename), MFSNAMELEN, FPCString, NULL},
{"f_mntonname", offsetof(struct statfs, f_mntonname), MNAMELEN, FPCString, NULL},
{"f_mntfromname", offsetof(struct statfs, f_mntfromname), MNAMELEN, FPCString, NULL},
{NULL, 0, 0, NULL, NULL}
};
static CommandError PrintStatFSInfoCommon(CommandArgsRef args, uint32_t indent, uint32_t verbose, bool lStat)
// Uses statfs to get information about the specified volume and
// prints the result.
//
// indent and verbose are as per the comments for FPPrinter.
{
int err;
struct statfs sfsb;
const char * itemPath;
assert( CommandArgsValid(args) );
err = CommandArgsGetString(args, &itemPath);
if (err == 0) {
if ( lStat ) {
fprintf(stdout, "%*slstatfs '%s'\n", (int) indent, "", itemPath);
err = MyLStatFS(itemPath, &sfsb);
} else {
fprintf(stdout, "%*sstatfs '%s'\n", (int) indent, "", itemPath);
err = statfs(itemPath, &sfsb);
if (err < 0) {
err = errno;
}
}
}
if (err == 0) {
FPPrintFields(kStatFSFieldDesc, &sfsb, sizeof(sfsb), indent + kStdIndent, verbose);
}
return CommandErrorMakeWithErrno(err);
}
static CommandError PrintStatFSInfo(CommandArgsRef args, uint32_t indent, uint32_t verbose)
{
assert( CommandArgsValid(args) );
return PrintStatFSInfoCommon(args, indent, verbose, false);
}
static CommandError PrintLStatFSInfo(CommandArgsRef args, uint32_t indent, uint32_t verbose)
{
assert( CommandArgsValid(args) );
return PrintStatFSInfoCommon(args, indent, verbose, true);
}
const CommandInfo kStatFSCommand = {
PrintStatFSInfo,
"statfs",
"itemPath",
"Print information from statfs.",
NULL
};
const CommandInfo kLStatFSCommand = {
PrintLStatFSInfo,
"lstatfs",
"itemPath",
"Print information from lstatfs compatibility code.",
NULL
};
#pragma mark * getfsstat
static CommandError PrintGetFSStatInfo(CommandArgsRef args, uint32_t indent, uint32_t verbose)
{
int err;
int fsCount;
int fsIndex;
struct statfs * fsBuf;
bool raw;
assert( CommandArgsValid(args) );
fsBuf = NULL;
// Get the list of mounted file systems.
raw = CommandArgsGetOptionalConstantString(args, "-r");
if ( raw ) {
fprintf(stdout, "%*sgetfsstat (single call)\n", (int) indent, "");
// In raw mode, just call getfsstat once.
fsCount = 64;
fsBuf = malloc( sizeof(*fsBuf) * fsCount );
err = 0;
if (fsBuf == NULL) {
err = ENOMEM;
}
if (err == 0) {
fsCount = getfsstat(fsBuf, (int) (fsCount * sizeof(*fsBuf)), MNT_WAIT);
if (fsCount < 0) {
err = errno;
}
}
} else {
fprintf(stdout, "%*sgetfsstat\n", (int) indent, "");
// By default, use complex code to guarantee that we can
// a complete snapshot of the file system list.
err = GetFSList(&fsBuf, &fsCount);
}
// Print information about each file system.
if (err == 0) {
for (fsIndex = 0; fsIndex < fsCount; fsIndex++) {
fprintf(stdout, "%*s%s\n", (int) (indent + kStdIndent), "", fsBuf[fsIndex].f_mntonname);
if (verbose > 0) {
FPPrintFields(kStatFSFieldDesc, &fsBuf[fsIndex], sizeof(fsBuf[fsIndex]), indent + 2 * kStdIndent, verbose - 1);
}
}
}
// Clean up.
free(fsBuf);
return CommandErrorMakeWithErrno(err);
}
static const CommandHelpEntry kGetFSStatCommandHelp[] = {
{CommandHelpString, "-r Raw; use a single call to getfsstat"},
{NULL, NULL}
};
const CommandInfo kGetFSStatCommand = {
PrintGetFSStatInfo,
"getfsstat",
"[ -r ]",
"Print file system list from getfsstat.",
kGetFSStatCommandHelp
};
#pragma mark * getdirentries
static const FPEnumDesc kDirTypes[] = {
{ DT_UNKNOWN, "DT_UNKNOWN" },
{ DT_FIFO, "DT_FIFO" },
{ DT_CHR, "DT_CHR" },
{ DT_DIR, "DT_DIR" },
{ DT_BLK, "DT_BLK" },
{ DT_REG, "DT_REG" },
{ DT_LNK, "DT_LNK" },
{ DT_SOCK, "DT_SOCK" },
{ DT_WHT, "DT_WHT" },
{ 0, NULL }
};
static const char kDirEntFieldSpacer[] = "bytesRead";
static const FPFieldDesc kDirEntFieldDesc[] = {
{"d_ino", offsetof(struct dirent, d_ino), sizeof(ino_t), FPUDec, NULL},
{"d_reclen", offsetof(struct dirent, d_reclen), sizeof(__uint16_t), FPUDec, NULL},
{"d_type", offsetof(struct dirent, d_type), sizeof(__uint8_t), FPEnum, kDirTypes},
{"d_namlen", offsetof(struct dirent, d_namlen), sizeof(__uint8_t), FPUDec, NULL},
{"d_name", offsetof(struct dirent, d_name), __DARWIN_MAXNAMLEN + 1, FPCString, NULL},
{kDirEntFieldSpacer, 0, 0, FPNull, NULL}, // present to pad out field widths
{NULL, 0, 0, NULL, NULL}
};
static void PrintDirEnt(bool firstCall, off_t fileOffset, const char *offsetFieldName, struct dirent *ent, uint32_t indent, uint32_t verbose, bool raw)
// Prints a (struct dirent). Can be called by the getdirentries and readdir
// commands.
{
char tmp[32];
const char * typeStr;
size_t enumIndex;
assert(offsetFieldName != NULL);
assert(ent != NULL);
if (verbose == 0) {
// Print as columns.
if ( raw || (ent->d_ino != 0) ) {
enumIndex = FPFindEnumByValue(kDirTypes, ent->d_type);
if (enumIndex == kFPNotFound) {
snprintf(tmp, sizeof(tmp), "%u", (unsigned int) ent->d_type);
typeStr = tmp;
} else {
typeStr = kDirTypes[enumIndex].enumName;
}
fprintf(
stdout,
"%*s%*s %8llu '%s'\n",
(int) indent, "",
- (int) strlen("DT_UNKNOWN"), typeStr,
(unsigned long long) ent->d_ino,
ent->d_name
);
}
} else {
// Print as individual records.
if ( ! firstCall ) {
fprintf(stdout, "\n");
}
FPUDec(offsetFieldName, sizeof(fileOffset), &fileOffset, indent, strlen(kDirEntFieldSpacer), verbose, NULL);
FPPrintFields(kDirEntFieldDesc, ent, sizeof(*ent), indent, verbose);
}
}
static CommandError PrintGetDirEntriesInfo(CommandArgsRef args, uint32_t indent, uint32_t verbose)
// Implements the getdirentries command.
{
int err;
int junk;
int bufSize;
const char * dirPath;
char * buf;
int dirFD;
bool raw;
assert( CommandArgsValid(args) );
buf = NULL;
dirFD = -1;
// Process -r argument.
raw = CommandArgsGetOptionalConstantString(args, "-r");
// Process optional -bufSize argument.
// st_blksize is a blksize_t; getdirentries's nbytes arg is an int;
// so, I've chosen to use int to represent the buffer size.
err = 0;
bufSize = 0; // indicates to use stat to get the buffer size
if ( CommandArgsGetOptionalConstantString(args, "-bufSize") ) {
err = CommandArgsGetInt(args, &bufSize);
if ( (err == 0) && (bufSize <= 0) ) {
err = EUSAGE;
}
}
// Get directory path.
if (err == 0) {
err = CommandArgsGetString(args, &dirPath);
}
// If we're using the default buffer size, get it from stat.
if ( (err == 0) && (bufSize == 0) ) {
struct stat sb;
err = stat(dirPath, &sb);
if (err < 0) {
err = errno;
}
if (err == 0) {
bufSize = (int) sb.st_blksize;
assert( ((blksize_t) bufSize) == sb.st_blksize);
}
}
// Allocate the buffer.
if (err == 0) {
buf = (char *) malloc(bufSize);
if (buf == NULL) {
err = ENOMEM;
}
}
// Open the directory.
if (err == 0) {
dirFD = open(dirPath, O_RDONLY);
if (dirFD < 0) {
err = errno;
}
}
// Loop until we run out of entries.
if (err == 0) {
int bytesRead;
long base;
off_t fileOffset;
bool first;
first = true;
fileOffset = 0;
do {
bytesRead = getdirentries(dirFD, buf, bufSize, &base);
if (bytesRead < 0) {
err = errno;
} else if (bytesRead > 0) {
char * cursor;
char * limit;
struct dirent * thisEnt;
// Print each entry in the buffer.
if (verbose > 1) {
if ( ! first ) {
fprintf(stdout, "\n");
}
first = false;
FPSDec("bytesRead", sizeof(bytesRead), &bytesRead, indent + kStdIndent, strlen(kDirEntFieldSpacer), verbose, NULL);
FPHex("base", sizeof(base), &base, indent + kStdIndent, strlen(kDirEntFieldSpacer), verbose, NULL);
}
limit = buf + bytesRead;
cursor = buf;
do {
// Check for expected termination.
if (cursor == limit) {
// Exactly at end buffer, end of this block of dirents.
// This used to be a >= test, but there's no point doing
// that because a) if this is the first iteration, the
// check can't apply, and b) if this is a subsequent iteration,
// the next check (that the dirent is entirely contained
// within the buffer) would have stopped us on the previous
// iteration.
break;
}
// Check for unexpected termination, that is, running off the
// end of the buffer. There are two checks here. The first
// checks that we have enough buffer space to read a meaningful
// thisEnt->d_reclen. The second checks that, given that record
// length, the entire record fits in the buffer.
thisEnt = (struct dirent *) cursor;
if ( ((cursor + offsetof(struct dirent, d_reclen) + sizeof(thisEnt->d_reclen)) > limit)
|| ((cursor + thisEnt->d_reclen) > limit) ) {
fprintf(stderr, "dirent not fully contained within buffer\n");
err = EINVAL;
break;
}
// readdir checks that each entry starts at a multiple of
// 4 bytes. We implement roughly the same check by checking that
// each entry is a multiple of 4 bytes long.
if ( (thisEnt->d_reclen & 3) != 0) {
static bool sPrinted;
if ( ! sPrinted ) {
fprintf(stderr, "d_reclen is not a multiple of 4; readdir will be unhappy.\n");
sPrinted = true;
}
}
// Print the entry.
PrintDirEnt(first, fileOffset, "offset", thisEnt, indent + kStdIndent, verbose, raw);
first = false;
fileOffset += thisEnt->d_reclen;
cursor += thisEnt->d_reclen;
} while (true);
}
} while ( (err == 0) && (bytesRead != 0) );
}
// Clean up.
if (dirFD != -1) {
junk = close(dirFD);
assert(junk == 0);
}
free(buf);
return CommandErrorMakeWithErrno(err);
}
static const CommandHelpEntry kGetDirEntriesCommandHelp[] = {
{CommandHelpString, "-r Raw; print entries with a 0 inode number"},
{CommandHelpString, "-bufSize Use a buffer size of N bytes; default is the device block size"},
{NULL, NULL}
};
const CommandInfo kGetDirEntriesCommand = {
PrintGetDirEntriesInfo,
"getdirentries",
"[ -r ] [ -bufSize N ] dirPath",
"Lists a directory using getdirentries.",
kGetDirEntriesCommandHelp
};
// -r implies print directory entries even if they have a 0 inode number
#pragma mark * readdir
static CommandError PrintReadDirInfo(CommandArgsRef args, uint32_t indent, uint32_t verbose)
// Implements the readdir command.
{
int err;
int junk;
const char * dirPath;
DIR * dirp;
bool raw;
assert( CommandArgsValid(args) );
dirp = NULL;
// Process -r argument.