lib/CrashTest/Storage/FileSystem.pm
author Vincent Tondellier <tonton+hg@team1664.org>
Tue, 20 Nov 2012 14:31:34 +0100
changeset 12 c98d3fa4a948
parent 11 0bef3b8087c1
child 19 300b902b5461
permissions -rw-r--r--
Add licence
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12
c98d3fa4a948 Add licence
Vincent Tondellier <tonton+hg@team1664.org>
parents: 11
diff changeset
     1
# This program is free software: you can redistribute it and/or modify
c98d3fa4a948 Add licence
Vincent Tondellier <tonton+hg@team1664.org>
parents: 11
diff changeset
     2
# it under the terms of the GNU General Public License as published by
c98d3fa4a948 Add licence
Vincent Tondellier <tonton+hg@team1664.org>
parents: 11
diff changeset
     3
# the Free Software Foundation, either version 3 of the License, or
c98d3fa4a948 Add licence
Vincent Tondellier <tonton+hg@team1664.org>
parents: 11
diff changeset
     4
# (at your option) any later version.
c98d3fa4a948 Add licence
Vincent Tondellier <tonton+hg@team1664.org>
parents: 11
diff changeset
     5
#
c98d3fa4a948 Add licence
Vincent Tondellier <tonton+hg@team1664.org>
parents: 11
diff changeset
     6
# This program is distributed in the hope that it will be useful,
c98d3fa4a948 Add licence
Vincent Tondellier <tonton+hg@team1664.org>
parents: 11
diff changeset
     7
# but WITHOUT ANY WARRANTY; without even the implied warranty of
c98d3fa4a948 Add licence
Vincent Tondellier <tonton+hg@team1664.org>
parents: 11
diff changeset
     8
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c98d3fa4a948 Add licence
Vincent Tondellier <tonton+hg@team1664.org>
parents: 11
diff changeset
     9
# GNU General Public License for more details.
c98d3fa4a948 Add licence
Vincent Tondellier <tonton+hg@team1664.org>
parents: 11
diff changeset
    10
#
c98d3fa4a948 Add licence
Vincent Tondellier <tonton+hg@team1664.org>
parents: 11
diff changeset
    11
# You should have received a copy of the GNU General Public License
c98d3fa4a948 Add licence
Vincent Tondellier <tonton+hg@team1664.org>
parents: 11
diff changeset
    12
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
c98d3fa4a948 Add licence
Vincent Tondellier <tonton+hg@team1664.org>
parents: 11
diff changeset
    13
11
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    14
package CrashTest::Storage::FileSystem;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    15
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    16
use strict;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    17
use warnings;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    18
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    19
sub new {
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    20
    my ($class, $data_path) = @_;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    21
    my $self  = {};
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    22
    $self->{data_path} = $data_path;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    23
    bless ($self, $class);
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    24
    return $self;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    25
}
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    26
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    27
sub index {
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    28
    my ($self) = @_;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    29
    my @files;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    30
    opendir my ($dh), $self->{data_path} or die $!;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    31
    my @allfiles = readdir $dh;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    32
    foreach(@allfiles)
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    33
    {
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    34
        if($_ =~ /(.*)\.json$/)
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    35
        {
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    36
            my $filename = File::Spec->catfile($self->{data_path}, $_);
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    37
            push @files, {
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    38
                file => $filename,
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    39
                uuid => $1,
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    40
                signature => $1,
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    41
                product => "",
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    42
                date => (stat $filename)[9],
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    43
            };
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    44
        }
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    45
    }
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    46
    closedir $dh;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    47
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    48
    my @sorted_files = ( sort { $b->{date} <=> $a->{date} } @files );
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    49
    @sorted_files = @sorted_files[0..19] if scalar(@sorted_files) > 20;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    50
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    51
    return \@sorted_files;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    52
}
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    53
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    54
sub get_processed_data {
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    55
    my ($self, $uuid) = @_;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    56
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    57
    open JSON, '<', File::Spec->catfile($self->{data_path}, $uuid . '.json') or die $!;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    58
    my @json_content_lines = <JSON>;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    59
    my $json_content = join('', @json_content_lines);
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    60
    close JSON;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    61
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    62
    my $json = Mojo::JSON->new;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    63
    my $processed_data = $json->decode($json_content);
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    64
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    65
    return $processed_data;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    66
}
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    67
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    68
sub store_processed_data {
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    69
    my ($self, $uuid, $pjson) = @_;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    70
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    71
    my $json = Mojo::JSON->new;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    72
    my $j = $json->encode($pjson);
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    73
    open JSON, '>', File::Spec->catfile($self->{data_path}, "$uuid.json") or die $!;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    74
    print JSON $j;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    75
    close JSON;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    76
}
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    77
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    78
sub store_dump {
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    79
    my ($self, $uuid, $file) = @_;
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    80
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    81
    my $dmp_file = File::Spec->catfile($self->{data_path}, "$uuid.dmp");
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    82
    $file->move_to($dmp_file);
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    83
}
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    84
0bef3b8087c1 Move filesystem access to lib/
Vincent Tondellier <tonton+hg@team1664.org>
parents:
diff changeset
    85
1;