source: ocsinventory-agent/trunk/fuentes/lib/Ocsinventory/Compress.pm @ 468

Last change on this file since 468 was 468, checked in by mabarracus, 4 years ago

Copyt trusty code

File size: 2.5 KB
Line 
1package Ocsinventory::Compress;
2use strict;
3
4use File::Temp qw/ tempdir tempfile /;
5
6sub new {
7  my (undef, $params) = @_;
8
9  my $self = {};
10
11  my $logger = $self->{logger} = $params->{logger};
12
13
14  eval{require Compress::Zlib;};
15  $self->{mode} = 'natif' unless $@;
16
17  chomp(my $gzippath=`which gzip 2>/dev/null`);
18  if ($self->{mode} eq 'natif') {
19    $logger->debug ('Compress::Zlib is available.');
20  } elsif (-x $gzippath) {
21    $logger->debug (
22        'Compress::Zlib is not available! The data will be compressed with
23        gzip instead but won\'t be accepted by server prior 1.02');
24      $self->{mode} = 'gzip';
25      $self->{tmpdir} = tempdir( CLEANUP => 1 );
26      mkdir $self->{tmpdir};
27      if ( ! -d $self->{tmpdir} ) {
28        $logger->fault("Failed to create the temp dir `$self->{tmpdir}'");
29      }
30  } else {
31    $self->{mode} = 'deflated';
32    $logger->debug ('I need the Compress::Zlib library or the gzip'.
33    ' command to compress the data - The data will be sent uncompressed
34    but won\'t be accepted by server prior 1.02');
35  }
36
37  bless $self;
38}
39
40sub compress {
41  my ($self, $content) = @_;
42  my $logger = $self->{logger};
43
44# native mode (zlib)
45  if ($self->{mode} eq 'natif') {
46    return Compress::Zlib::compress($content);
47  }
48# gzip mode
49  elsif($self->{mode} eq 'gzip'){
50    my ($fh, $filename) = tempfile( DIR => $self->{tmpdir} );
51    print $fh $content;
52    close $fh;
53
54    system ("gzip --best $filename > /dev/null");
55
56#  print "filename ".$filename."\n";
57
58    my $ret;
59    open FILE, "<$filename.gz";
60    $ret .= $_ foreach (<FILE>);
61    close FILE;
62    if ( ! unlink "$filename.gz" ) {
63      $logger->debug("Failed to remove `$filename.gz'");
64    }
65    return $ret;
66  }
67# No compression available
68  elsif($self->{mode} eq 'deflated'){
69    return $content;
70  }
71}
72
73sub uncompress {
74  my ($self,$data) = @_;
75  my $logger = $self->{logger};
76# Native mode
77  if ($self->{mode} eq 'natif') {
78    return Compress::Zlib::uncompress($data);
79  }
80  elsif($self->{mode} eq 'gzip'){
81# Gzip mode
82    my ($fh, $filename) = tempfile( DIR => $self->{tmpdir}, SUFFIX => '.gz' );
83   
84    print $fh $data;
85    close $fh;
86 
87    system ("gzip -d $filename");
88    my ($uncompressed_filename) = $filename =~ /(.*)\.gz$/;
89 
90    my $ret;
91    open FILE, "<$uncompressed_filename";
92    $ret .= $_ foreach (<FILE>);
93    close FILE;
94    if ( ! unlink "$uncompressed_filename" ) {
95      $logger->debug("Failed to remove `$uncompressed_filename'");
96    }
97    return $ret;
98  }
99# No compression available
100  elsif($self->{mode} eq 'deflated'){
101        return $data;
102  }
103}
1041;
Note: See TracBrowser for help on using the repository browser.