File::Storage::Stat - Storage manager of minimum size


File-Storage-Stat documentation Contained in the File-Storage-Stat distribution.

Index


Code Index:

NAME

Top

File::Storage::Stat - Storage manager of minimum size

SYNOPSIS

Top

  use File::Storage::Stat;

  my $fss = File::Storage::Stat->new({FilePath => 'filepath'});
  $fss->set(100, 1000);
  my($a, $b) = $fss->get;#max 4byte int.

  my $fss = File::Storage::Stat->new({FilePath => 'filepath', Type => 'char'});
  $fss->set('hoge', 'test');# max 4byte char.
  my($a, $b) = $fss->get;




DESCRIPTION

Top

small data is stored in atime and mtime of file.

METHODS

Top

set(data1, data2)

data is set. the data of less than 4byte can be preserved respectively.

get()

the preserved is loaded.

AUTHOR

Top

Kazuhiro Osawa <ko@yappo.ne.jp>

COPYRIGHT AND LICENSE

Top


File-Storage-Stat documentation Contained in the File-Storage-Stat distribution.

package File::Storage::Stat;

use strict;
use Carp;

require Exporter;

our @ISA = qw(Exporter);
our $VERSION = '0.02';

sub new {
    my $class = shift;
    my $options = shift;

    croak('FilePath is not set.')
	unless $options->{FilePath};

    croak('Ther is no (' . $options->{FilePath} . ').')
	unless -e $options->{FilePath};

    if ($options->{Type}) {
	$options->{Type} ='int'
	    unless $options->{Type} eq 'char';
    } else {
	$options->{Type} = 'int';
    }

    my $self = {
	FilePath => $options->{FilePath},
	Type => $options->{Type},
    };

    return bless $self, $class;
}

sub set {
    my $self = shift;
    my @times = (shift, shift);

    if ($self->{Type} eq 'char') {
	foreach (0...1) {
	    if (length($times[$_]) > 4) {
		$times[$_] = 0;
	    } else {
		my $t = 0;
		foreach (split('', $times[$_])) {
		    $t <<= 8;
		    $t += ord($_);
		}
		$times[$_] = $t;
	    }
	}
    }
    
    @times = map {
	my $v = $_;
	if ($v < 0) {
	    $v = 0;
	} elsif ($v >= ((1 << 31) * 2)) {
	    $v = 0;
	}
	$v;
    } @times;

    return utime($times[0], $times[1], $self->{FilePath});
}

sub get {
    my $self = shift;

    my @times = map {
	my $v = $_;
	if ($v < 0) {
	    $v = $v + ((1 << 31) * 2);
	}
	$v;
    } (stat $self->{FilePath})[8,9,10];

    if ($self->{Type} eq 'char') {
	foreach (0...1) {
	    if ($times[$_]) {
		my $t = '';
		while (1) {
		    $t = chr($times[$_] % 256) . $t;
		    $times[$_] >>= 8;
		    last unless $times[$_];
		}
		$times[$_] = $t;
	    } else {
		$times[$_] = '';
	    }
	}
    }

    return @times;
}

1;
__END__