"Fossies" - the Fresh Open Source Software Archive

Member "encfs-1.9.5/integration/common.pl" (27 Apr 2018, 3542 Bytes) of package /linux/misc/encfs-1.9.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "common.pl" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.9.4_vs_1.9.5.

    1 # Portable FUSE unmount
    2 # works on Linux AND OSX
    3 sub portable_unmount {
    4     my $crypt = shift;
    5     qx(./build/encfs -u "$crypt" >/dev/null);
    6 }
    7 
    8 # Helper function
    9 # Get the MD5 sum of the file open at the filehandle
   10 use Digest::MD5 qw(md5_hex);
   11 sub md5fh
   12 {
   13     my $fh_orig = shift;
   14     open(my $fh, "<&", $fh_orig); # Duplicate the file handle so the seek
   15     seek($fh, 0, 0);              # does not affect the caller
   16     my $md5 = Digest::MD5->new->addfile($fh)->hexdigest;
   17     close($fh);
   18     return $md5;
   19 }
   20 
   21 # Get the file size from stat() (by file handle or name)
   22 sub statSize
   23 {
   24     my $f = shift;
   25     my @s = stat($f) or die("stat on '$f' failed");
   26     return $s[7];
   27 }
   28 
   29 # Get the file size by read()ing the whole file
   30 sub readSize
   31 {
   32    my $fh = shift;
   33    seek($fh, 0, 0);
   34    my $block = 4*1024;
   35    my $s;
   36    my $data;
   37    my $sum = read($fh, $data, $block);
   38    while ( $s = read($fh, $data, $block) )
   39    {
   40         $sum += $s;
   41    }
   42    $data = "";
   43    return $sum;
   44 }
   45 
   46 # Verify that the size of the file passed by filehandle matches the target size s0
   47 # Checks both stat() and read()
   48 sub sizeVerify
   49 {
   50     my $ok = 1;
   51     my $fh = shift;
   52     my $s0 = shift;
   53     $ss = statSize($fh);
   54     if ($s0 != $ss) {
   55         $ok = 0;
   56         print("# stat size $ss, expected $s0\n");
   57     }
   58     $sr = readSize($fh);
   59     if ($s0 != $sr) {
   60         $ok = 0;
   61         print("# read size $sr, expected $s0\n");
   62     }
   63     return $ok;
   64 }
   65 
   66 # Wait for a file to appear
   67 use Time::HiRes qw(usleep);
   68 sub waitForFile
   69 {
   70     my $file = shift;
   71     my $timeout;
   72     $timeout = shift or $timeout = 5;
   73     for(my $i = $timeout*10; $i > 0; $i--)
   74     {
   75         -f $file and return 1;
   76         usleep(100000); # 0.1 seconds
   77     }
   78     print "# timeout waiting for '$file' to appear\n";
   79     return 0;
   80 }
   81 
   82 # writeZeroes($filename, $size)
   83 # Write zeroes of size $size to file $filename
   84 sub writeZeroes
   85 {
   86         my $filename = shift;
   87         my $size = shift;
   88         open(my $fh, ">", $filename);
   89         my $bs = 4096; # 4 KiB
   90         my $block = "\0" x $bs;
   91         my $remain;
   92         for($remain = $size; $remain >= $bs; $remain -= $bs)
   93         {
   94                 print($fh $block) or BAIL_OUT("Could not write to $filename: $!");
   95         }
   96         if($remain > 0)
   97         {
   98                 $block = "\0" x $remain;
   99                 print($fh $block) or BAIL_OUT("Could not write to $filename: $!");
  100         }
  101 }
  102 
  103 # Returns integer $milliseconds from float $seconds
  104 sub ms {
  105     my $seconds      = shift;
  106     my $milliseconds = int( $seconds * 1000 );
  107     return $milliseconds;
  108 }
  109 
  110 # stopwatch_start($name)
  111 # start the stopwatch for test "$name"
  112 sub stopwatch_start {
  113     stopwatch(1, shift);
  114 }
  115 
  116 # stopwatch_stop(\@results)
  117 # stop the stopwatch, save time into @results
  118 sub stopwatch_stop {
  119     stopwatch(0, shift);
  120 }
  121 
  122 # See stopwatch_{start,stop} above
  123 use feature 'state';
  124 use Time::HiRes qw( time );
  125 sub stopwatch {
  126     state $start_time;
  127     state $name;
  128     my $start = shift;
  129 
  130     if($start) {
  131         $name = shift;
  132         print("* $name... ");
  133         $start_time = time();
  134     } else {
  135         my $delta = ms(time() - $start_time);
  136         print("$delta ms\n");
  137         my $results = shift;
  138         push( @$results, [ $name, $delta, 'ms' ] );
  139     }
  140 }
  141 
  142 # Download linux-3.0.tar.gz unless it already exists ("-c" flag)
  143 sub dl_linuxgz {
  144     our $linuxgz = "/var/tmp/linux-3.0.tar.gz";
  145     if ( -e $linuxgz ) { return; }
  146     print "downloading linux-3.0.tar.gz (92MiB)... ";
  147     system("wget -nv -c https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.0.tar.gz -O $linuxgz");
  148     print "done\n";
  149 }
  150 
  151 # As this file will be require()'d, it needs to return true
  152 return 1;