forked from cppvik/teleperl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cborpack.pl
131 lines (109 loc) · 3.81 KB
/
cborpack.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
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
use Modern::Perl;
use CBOR::XS;
use Data::Dumper;
use Teleperl::Util qw(unlock_hashref_recurse);
use Telegram::ObjTable;
use MTProto::ObjTable;
use File::Find;
use Storable qw(dclone);
use Getopt::Long::Descriptive;
require $_ for map { $_->{file} } values %Telegram::ObjTable::tl_type;
require $_ for map { $_->{file} } values %MTProto::ObjTable::tl_type;
sub option_spec {
[ 'daily=s' => 'regexp, $1_$2.cbor to $1.cbor, e.g. 24 hour files to one per-day' ],
[ 'mtime=i' => 'analogous to find -mtime, for use with --daily' ],
[ 'prefix|p=s' => 'prefix path for output files' ],
[ 'dry|n' => 'do no action, just print file names' ],
[ 'verbose|v:+' => 'more twitting about actions', { default => 0} ],
}
### initialization
my ($opts, $usage);
eval { ($opts, $usage) = describe_options( '%c %o ...', option_spec() ) };
die "Invalid opts: $@\nUsage: $usage\n" if $@;
### pack sub
my $cbor_data;
my $cbor = CBOR::XS->new;
my $cborp = CBOR::XS->new->pack_strings(1);
my ($rec, $octets, @all);
sub output_file {
my $outfname = shift;
say "starting output file $outfname";
while (my $infname = shift) {
say "\tinput file $infname" if $opts->verbose;
next if $opts->dry;
my $mtime = (stat($infname))[9];
push @all, { infname => $infname, mtime => $mtime };
open FH, "<", $infname
or die "can't open '$infname': $!";
binmode FH;
# slurp all file at once :)
{
local $/ = undef;
$cbor_data = <FH>;
close FH;
}
while (my $left = length $cbor_data) {
last if $left == 3 and $cbor_data eq $CBOR::XS::MAGIC;
($rec, $octets) = $cbor->decode_prefix ($cbor_data);
substr($cbor_data, 0, $octets) = '';
my $one_rec = sub {
my $rec = shift;
my $clone = dclone $rec;
if (exists $clone->{schema}) {
$clone->{marktime} = delete $clone->{time};
}
push @all, unlock_hashref_recurse($clone);
};
$one_rec->($_) for (ref $rec eq 'HASH' ? $rec : @$rec);
}
}
return if $opts->dry;
my $packed = $CBOR::XS::MAGIC
. $cborp->encode({ # what version decoder should use
packtime => time,
schema => $Telegram::ObjTable::GENERATED_FROM,
})
. $cborp->encode(\@all);
open OUT, ">", $outfname
or die "can't open '$outfname': $!";
binmode OUT;
$\ = undef;
print OUT $packed;
close OUT;
@all = ();
}
## main work
$opts->{verbose}++ if $opts->dry;
$opts->{prefix} = "." unless $opts->prefix;
$opts->{prefix} .= "/" unless $opts->{prefix} =~ /\/$/;
if (my $pat = $opts->daily) {
die "need directory arg" unless -d $ARGV[0];
my %list;
my $mtime = $opts->mtime || 0;
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev);
say "daily: start searching" if $opts->verbose;
find(sub {
my $match = 0;
say "find wanted on $_" if $opts->verbose > 1;
/$pat/s &&
(($dev, $ino, $mode, $nlink, $uid, $gid, $rdev) = lstat($_)) &&
($match = 1);
if ($mtime) {
$match = 0 unless int(-M _) > $mtime;
}
return unless $match;
# final action
$list{"$1.cbor"} = [] unless exists $list{"$1.cbor"};
push @{ $list{"$1.cbor"} }, $File::Find::name ;
},
$ARGV[0],
);
say "file list built, beginning to pack" if $opts->verbose or $opts->dry;
output_file($opts->{prefix}.$_, sort @{ $list{$_} }) for sort keys %list;
}
else {
my $fname = "$ARGV[0]";
$fname =~ s/cbor$/packcbor/
or $fname = "$ARGV[0].pack";
output_file($fname, $ARGV[0]);
}