-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanLocalTreeAndCreateJSON.pl
More file actions
executable file
·556 lines (445 loc) · 13.9 KB
/
scanLocalTreeAndCreateJSON.pl
File metadata and controls
executable file
·556 lines (445 loc) · 13.9 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
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
#!/usr/bin/perl
#use English;
use FileHandle;
use strict;
use JSON;
use Cwd;
use File::stat;
use Fcntl ':mode';
my $g_GlobalIdCounter = 0;
my $g_rootItemBySize = createFileArrayObject();
$g_rootItemBySize->{name} = "ALL";
$g_rootItemBySize->{id} = "root";
$g_rootItemBySize->{depth} = 0;
$g_rootItemBySize->{type} = "directory";
my $g_rootItemByType = createFileArrayObject();
$g_rootItemByType->{name} = "ALL";
$g_rootItemByType->{id} = "root";
$g_rootItemByType->{depth} = 0;
$g_rootItemByType->{type} = "directory";
my $g_rootItemByDate = createFileArrayObject();
$g_rootItemByDate->{name} = "ALL";
$g_rootItemByDate->{id} = "root";
$g_rootItemByDate->{depth} = 0;
$g_rootItemByDate->{type} = "directory";
my $g_RootItemAllExtensions = {};
my %g_TypeReverseIndex = ();
# these hashes store summaries by path key
my %g_pathHash = ();
my %g_typeHash = ();
my %g_dateHash = ();
my $g_unique_id = 0;
# at each parent node add a string that represents the full path. eg "ALL/known types/media/images/rastor"
sub flatten_types($$) {
my($type_tree_node, $root_string) = @_;
# only add a node name if we have children
if($type_tree_node->{children})
{
if($root_string ne "") {
$type_tree_node->{levelPath} = $root_string . "/" . $type_tree_node->{name};
} else {
$type_tree_node->{levelPath} = $type_tree_node->{name};
}
foreach my $i (@{$type_tree_node->{children}}) {
# only recurse into nodes with children
if( ($i->{children}) != undef ) {
flatten_types( $i, $type_tree_node->{levelPath} );
}
}
}
}
# use recursion to create a flat index of all extensions pointing to their root object
sub create_type_reverse_index($) {
my($type_tree_node) = @_;
foreach my $i (@{$type_tree_node->{children}}) {
# extensions begin with "." and don't have children
if( ($i->{children}) == undef )
{
# point the index to the parent rather than the object itself
# this makes it easy to get the levelPath
$g_TypeReverseIndex{ $i->{name} } = $type_tree_node;
}
else
{
create_type_reverse_index( $i );
}
}
}
sub load_all_extensions() {
# read json file from local disk
open(FH, "exts/all_exts.json") or die "$!";
my $json_txt = do{local $/; <FH>;};
close FH;
#print($json_txt);
# parse json
$g_RootItemAllExtensions = JSON->new->utf8->decode ( $json_txt );
# add levelPath to all parent nodes using flatten
flatten_types($g_RootItemAllExtensions, "");
# create reverse index
create_type_reverse_index($g_RootItemAllExtensions);
#my $recode = JSON->new->utf8->pretty->encode ( $g_RootItemAllExtensions );
#print("JSON parsed: ");
#print($recode);
#print("\n");
}
sub main() {
my ($dirToScan, $outputFile) = @ARGV;
load_all_extensions();
# strip the trail slash if it's there
if ($#ARGV != 1 ) {
print "usage: SCRIPT_NAME [Directory to scan] [Output file for JSON Data]\n";
exit;
}
print "Scanning directory: " . $dirToScan . " and printing to file: " . $outputFile . "\n";
StartScanDirectory($dirToScan);
# now all the data is summarized by path, turn it into a tree
#debug print
#while (my ($k,$v)=each %g_pathHash){print "$k $v\n"}
while( my ($k, $v) = each %g_pathHash) {
my @pathArr = split('\/', $k);
loadFileItemInNestedFileArray(1, $g_rootItemBySize, $v, \@pathArr);
}
while( my ($k, $v) = each %g_typeHash) {
my @pathArr = split('\/', $k);
loadFileItemInNestedFileArray(1, $g_rootItemByType, $v, \@pathArr);
}
while( my ($k, $v) = each %g_dateHash) {
my @pathArr = split('\/', $k);
loadFileItemInNestedFileArray(1, $g_rootItemByDate, $v, \@pathArr);
}
# create a bogus root that can contain both root Items
my $fakeRoot = {};
#$fakeRoot->{SizeHash} = \%g_pathHash; #debug out
#$fakeRoot->{TypeHash} = \%g_typeHash; #debug out
#$fakeRoot->{DateHash} = \%g_dateHash; #debug out
$fakeRoot->{rootItemBySize} = $g_rootItemBySize;
$fakeRoot->{rootItemByType} = $g_rootItemByType;
$fakeRoot->{rootItemByDate} = $g_rootItemByDate;
open FILE, "> $outputFile" or die $!;
my $json_txt = JSON->new->pretty(1)->utf8->encode ($fakeRoot);
print FILE $json_txt . "\n";
}
# this function starts off the directory scan.
# It is different so that we always pass "." into the base of the recursive function
# Otherwise the paths generated have the fullCWD info
sub StartScanDirectory($) {
my ($newSubDir, $fullDir) = @_;
my($startdir) = &cwd; # keep track of where we began
chdir($newSubDir) or die "Unable to enter dir $newSubDir:$!\n";
ScanDirectory(".", "");
chdir($startdir) or die "Unable to change to dir $startdir:$!\n";
}
sub ScanDirectory($$) {
my ($newSubDir, $fullDir) = @_;
my($startdir) = &cwd; # keep track of where we began
chdir($newSubDir) or die "Unable to enter dir $newSubDir:$!\n";
opendir(DIR, ".") or die "Unable to open $newSubDir:$!\n";
my @names = readdir(DIR);
closedir(DIR);
if($fullDir eq "") {
if($newSubDir eq ".") {
$fullDir = "";
} else {
$fullDir = $newSubDir;
}
#$fullDir = $newSubDir;
} else {
$fullDir = $fullDir . "/" . $newSubDir;
}
foreach my $name (@names){
next if ($name eq ".");
next if ($name eq "..");
if (-d $name){ # is this a directory?
DirectoryFound($name, $fullDir);
&ScanDirectory($name, $fullDir);
next;
} else {
FileFound($name, $fullDir);
}
}
chdir($startdir) or die "Unable to change to dir $startdir:$!\n";
}
sub DirectoryFound($$) {
my($name, $path) = @_;
FileOrDirectoryFound($name, $path);
}
sub FileFound($$) {
my($name, $path) = @_;
FileOrDirectoryFound($name, $path);
}
sub FileOrDirectoryFound($$) {
my($filename, $path) = @_;
# attempt to read the stat entry for this file
my $sb;
eval { $sb = stat($filename) };
if(@@) {
warn "stat failed [$@]\n";
}
my $debug = 0;
if($debug) {
print "found a ";
if (-d $filename){ # is this a directory?
print " DIRECTORY ";
} else {
print " FILE ";
}
print "" . $path . " / " . $filename . " Size: " . $sb->size . "\n";
}
add_file_item ($sb, $filename, $path);
}
sub loadFileItemInHash($$$) {
my($rootHash, $fileItem, $path) = @_;
my $itemRef = $rootHash->{ $path };
my $isDirectory = 0;
if($fileItem->{type} eq "directory") {
$isDirectory = 1;
}
if($itemRef == undef)
{
# create item
my $hashItem = {};
$hashItem->{totalSizeAtLevel} = $fileItem->{st_size};
if($isDirectory == 1) {
$hashItem->{containerCountAtLevel}++;
} else {
$hashItem->{itemCountAtLevel} = 1;
}
$rootHash->{ $path } = $hashItem;
}
else
{
# increment since the item exists
$itemRef->{totalSizeAtLevel} += $fileItem->{st_size};
if($isDirectory == 1) {
$itemRef->{containerCountAtLevel}++;
} else {
$itemRef->{itemCountAtLevel}++;
}
}
}
# add_file_item(stat_info, filename, path);
sub add_file_item ($$$) {
my($stat_info, $filename, $path) = @_;
my($item);
if($stat_info->size == undef) {
$item->{st_size} = 0;
} else {
$item->{st_size} = $stat_info->size;
}
if($stat_info->mtime == undef) {
$item->{st_mtime} = 0;
} else {
$item->{st_mtime} = $stat_info->mtime;
}
# empty directories wont get counted correctly unless we use the whole name as the path
if (-d $filename){ # is this a directory?
$item->{type} = "directory";
if($path eq "") {
$path = $filename;
} else {
$path = $path . "/" . $filename;
}
} else {
$item->{type} = "file";
}
# create a path for types by looking up the type in the reverse index
# lookup the extension in the index and use the typeCategory to set it into the tree, instead of the dirName
my $dot = rindex($filename, '.');
my $typeCategory = "";
my $ext = "";
if($dot > -1)
{
$ext = substr($filename, $dot);
$ext = lc($ext);
my $ext_parent_node = $g_TypeReverseIndex{$ext};
if($ext_parent_node != undef)
{
$typeCategory = $ext_parent_node->{levelPath} . "/" . $ext;
}
}
if($typeCategory eq "") {
if($ext eq "") {
$ext = "No Extension";
}
$typeCategory = "uncatagorized/" . $ext;
}
# categorize by date
my $dateCategory = categorizeByDate($item);
#my @typePathArr = split('\/', $typeCategory);
#print "\nPATH: " . $path . "\n\n";
loadFileItemInHash(\%g_pathHash, $item, $path);
loadFileItemInHash(\%g_typeHash, $item, $typeCategory);
loadFileItemInHash(\%g_dateHash, $item, $dateCategory);
}
# take a File object and return a date category
sub categorizeByDate($) {
my( $manObj ) = @_;
my $todays_time_t = time;
my $hour = 60*60; # secs in an hour
my $day = $hour * 24;
my $week = $day * 7;
my $month = $day * 30;
my $year = $day * 365;
my $file_time_t = int($manObj->{st_mtime});
my $category;
if( $file_time_t == 0 ) {
$category = "unknown date";
} elsif( $file_time_t > $todays_time_t ) {
$category = "Incorrect future date";
} elsif( $file_time_t > $todays_time_t - $hour ) {
$category = "within 1 year/within 1 hour";
} elsif( $file_time_t > $todays_time_t - $day ) {
$category = "within 1 year/1 hour to 1 day";
} elsif( $file_time_t > $todays_time_t - $week ) {
$category = "within 1 year/1 day to 1 week";
} elsif( $file_time_t > $todays_time_t - $month ) {
$category = "within 1 year/1 week to 1 month";
} elsif( $file_time_t > $todays_time_t - $year ) {
$category = "within 1 year/1 month to 1 year";
} else {
my $diff = int(($todays_time_t - $file_time_t) / $year);
$category = "1 year and older/" . $diff . " to " . ($diff+1) . " years";
}
return $category;
}
sub parsePath ($) {
my( $fullPath ) = @_;
my $fileName;
my $dirName;
# parse out the directory name
my $index = rindex($fullPath, '/');
if($index > -1)
{
$dirName = substr($fullPath, 0, $index);
$fileName = substr($fullPath, $index+1);
}
else
{
$index = rindex($fullPath, '\\');
if($index > -1) {
$dirName = substr($fullPath, 0, $index);
$fileName = substr($fullPath, $index+1);
}
else
{
$dirName = "";
$fileName = $fullPath;
}
}
return ($fileName, $dirName);
}
# take a line of text comma delimited and turn it into an object
# line looks something like this:
# =DIR_NAME/FILENAME,acl_list=WINDOWS_ACLS,\
# attrs=8224&,st_ctime=1248231694,st_ino=56054,st_mode=100000,st_mtime=1226216052,st_nlink=3,\
# st_size=10526256,type=file,version=1,win_encrypted_size=0,_internal_record_crc=dc0e928c\
#
sub parseItem ($) {
my($line) = @_;
my @elements = split(',', $line);
my $fileItem = {};
my $association;
foreach $association ( @elements ) {
my ($name, $value) = split('=', $association, 2);
#my $equal = index($association, '=');
#my $name = substr($association, 0, $equal);
if($name eq "") # the first one has an empty name and is the name field
{
#my $value = substr($association, $equal+1);
if($value eq "header" || $value eq "trailer" || $value eq "") {
return undef;
}
my ($fileName, $path) = parsePath($value);
$fileItem->{name} = $fileName;
$fileItem->{path} = $path;
}
elsif ($name eq "st_size")
{
#my $value = substr($association, $equal+1);
$fileItem->{st_size} = int $value;
}
elsif ($name eq "st_mtime")
{
#my $value = substr($association, $equal+1);
$fileItem->{st_mtime} = int $value;
}
elsif ($name eq "type")
{
#my $value = substr($association, $equal+1);
$fileItem->{type} = $value;
}
}
return $fileItem;
}
sub createFileArrayObject() {
my $fileArray = {};
$fileArray->{id} = sprintf ("%d", ($g_GlobalIdCounter++));
$fileArray->{name} = "";
$fileArray->{totalSizeAtLevel} = 0;
$fileArray->{totalSizeWithChildren} = 0;
$fileArray->{itemCountAtLevel} = 0;
$fileArray->{itemCountWithChildren} = 0;
$fileArray->{containerCountWithChildren} = 0;
return $fileArray;
}
sub loadFileItemInNestedFileArray( $$$$ ) {
my($depth, $rootObj, $fileItem, $directoryNestingArray) = @_;
#print "Recurse level: " . $depth . " item name: " . $fileItem->{name} . "\n";
#print "dirNestingArray: " . @{$directoryNestingArray} . " d: " . scalar(@{$directoryNestingArray}) . "\n";
# The directoryNestingArray represents a progressive depth.
# When it gets empty, we are at the right depth
if(!$directoryNestingArray || $directoryNestingArray->[0])
{
# array is not empty
#
my $nextLevel = shift @{$directoryNestingArray}; # remove first element
#print "NextLevel: " . $nextLevel . "\n";
my $nextLevelObj;
# since we are not pushing files, we don't want to create a children node
# unless we are creating a directory
# find the directory item if it exists
if(defined($rootObj->{children}))
{
my $i;
foreach $i (@{$rootObj->{children}})
{
if($i->{name} eq $nextLevel)
{
$nextLevelObj = $i;
last; # perl "break"
}
}
}
else
{
# create array
$rootObj->{children} = [];
}
if( !defined($nextLevelObj) )
{
$nextLevelObj = createFileArrayObject();
$nextLevelObj->{name} = $nextLevel;
$nextLevelObj->{type} = "directory";
$nextLevelObj->{depth} = $depth;
# add to the beginning of the array
# This should be faster since the next path that comes through
# is likely to have the same sub path. Since it will be at the beginning of
# the list it will be found on the first compare instead of the last.
unshift(@{$rootObj->{children}}, $nextLevelObj);
}
$rootObj->{itemCountWithChildren} += $fileItem->{itemCountAtLevel};
$rootObj->{totalSizeWithChildren} += $fileItem->{totalSizeAtLevel};
$rootObj->{containerCountWithChildren} += $fileItem->{containerCountAtLevel};
loadFileItemInNestedFileArray($depth+1, $nextLevelObj, $fileItem, $directoryNestingArray);
}
else
{
$rootObj->{itemCountAtLevel} = $fileItem->{itemCountAtLevel};
$rootObj->{totalSizeAtLevel} = $fileItem->{totalSizeAtLevel};
$rootObj->{itemCountWithChildren} += $fileItem->{itemCountAtLevel};
$rootObj->{totalSizeWithChildren} += $fileItem->{totalSizeAtLevel};
$rootObj->{containerCountWithChildren} += $fileItem->{containerCountAtLevel};
}
}
main();
0;