-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage1.pl
44 lines (35 loc) · 889 Bytes
/
stage1.pl
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
#!/usr/bin/perl
# stage 1: takes a bunch of paths as argv, walks them with find, stats the
# individual files for their size and then stores the size in the sqlite
# database.
use strict;
use warnings;
use DBI;
use File::stat;
use File::Spec;
my $dbconf = "dbi:SQLite:dbname=/var/tmp/files.db";
my $dbh;
$dbh = DBI->connect($dbconf,"","") or die $dbh->errstr;
my $insert = $dbh->prepare(<<SQL);
INSERT INTO files(basename, dirname, size) VALUES(?,?,?);
SQL
sub do_file {
my $file = shift;
my $stat = stat $file;
my $size = $stat->size;
return unless $size > 1024*1024; # ignore files <1M
my (undef, $dirname, $basename) = File::Spec->splitpath($file);
$insert->execute($basename, $dirname, $size);
}
sub do_find {
local $/ = "\0";
my $fh = shift;
while (<$fh>) {
do_file $_
}
}
foreach (@ARGV) {
open my $fh, "find $_ -type f -print0|";
do_find $fh;
close $fh;
}