/usr/share/perl5/AnyData/Storage/File.pm is in libanydata-perl 0.12-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | package AnyData::Storage::File;
use strict;
use warnings;
use IO::File;
use Fcntl qw(:flock);
use File::Basename;
use constant HAS_FLOCK => eval { flock STDOUT, 0; 1 };
use constant HAS_FILE_SPEC => eval { require File::Spec };
use vars qw($DEBUG);
$DEBUG = 0;
sub new {
my $class = shift;
my $self = shift || {};
#$self->{f_dir} ||= './';
return bless $self, $class;
}
sub seek_first_record {
my $self = shift;
my $fh = $self->{fh};
my $start = $self->{first_row_pos};
$start
? $fh->seek($start,0) || die $!
: $fh->seek(0,0) || die $!;
}
sub get_pos { return shift->{fh}->tell }
sub go_pos { my($s,$pos)=@_; $s->{fh}->seek($pos,0); }
my $open_table_re =
HAS_FILE_SPEC ?
sprintf('(?:%s|%s|%s)',
quotemeta(File::Spec->curdir()),
quotemeta(File::Spec->updir()),
quotemeta(File::Spec->rootdir()))
: '(?:\.?\.)?\/';
sub open_local_file {
my( $self,$file, $open_mode ) = @_;
my $dir = $self->{f_dir} || './';
my($fname,$path) = fileparse($file);
my($foo2,$os_cur_dir) = fileparse('');
my $haspath = 1 if $path and $path ne $os_cur_dir;
if (!$haspath && $file !~ /^$open_table_re/o) {
$file = HAS_FILE_SPEC
? File::Spec->catfile($dir, $file)
: $dir . "/$file";
}
my $fh;
$open_mode ||= 'r';
my %valid_mode = (
r => q/read read an existing file, fail if already exists/,
u => q/update read & modify an existing file, fail if already exists/,
c => q/create create a new file, fail if it already exists/,
o => q/overwrite create a new file, overwrite if it already exists/,
);
my %mode = (
r => O_RDONLY,
u => O_RDWR,
c => O_CREAT | O_RDWR | O_EXCL,
o => O_CREAT | O_RDWR | O_TRUNC
);
my $help = qq(
r if file exists, get shared lock
u if file exists, get exclusive lock
c if file doesn't exist, get exclusive lock
o truncate if file exists, else create; get exclusive lock
);
if ( !$valid_mode{$open_mode} ) {
print "\nBad open_mode '$open_mode'\nValid modes are :\n";
for ('r','u','c','o'){
print " $_ = $valid_mode{$_}\n";
}
exit;
}
if ($open_mode eq 'c') {
if (-f $file) {
die "Cannot create '$file': Already exists";
}
}
if ($open_mode =~ /[co]/ ) {
if (!($fh = IO::File->new( $file, $mode{$open_mode} ))) {
die "Cannot open '$file': $!";
}
if (!$fh->seek(0, 0)) {
die " Error while seeking back: $!";
}
}
if ($open_mode =~ /[ru]/) {
die "Cannot read file '$file': doesn't exist!" unless -f $file;
if (!($fh = IO::File->new($file, $mode{$open_mode}))) {
die " Cannot open '$file': $!";
}
}
binmode($fh);
$fh->autoflush(1);
if ( HAS_FLOCK ) {
if ( $open_mode eq 'r') {
if (!flock($fh, LOCK_SH)) {
die "Cannot obtain shared lock on '$file': $!";
}
} else {
if (!flock($fh, LOCK_EX)) {
die " Cannot obtain exclusive lock on '$file': $!";
}
}
}
print "OPENING $file, mode = '$open_mode'\n" if $DEBUG;
return( $file, $fh, $open_mode) if wantarray;
return( $fh );
}
sub print_col_names {
my($self,$parser,$col_names) = @_;
my $fields = $col_names || $self->{col_names} || $parser->{col_names};
return undef unless scalar @$fields;
$self->{col_names} = $fields;
return $fields if $parser->{keep_first_line};
my $first_line = $self->get_record();
my $fh = $self->{fh};
$self->seek_first_record;
my $end = $parser->{record_sep} || "\n";
my $colStr = $parser->write_fields(@$fields);
$colStr = join( ',',@$fields) . $end if ref($parser) =~ /Fixed/;
$fh->write($colStr,length $colStr);
$self->{first_row_pos} = $fh->tell();
}
sub get_col_names {
my($self,$parser) = @_;
my @fields = ();
if ($parser->{keep_first_line}) {
my $cols = $parser->{col_names};
return undef unless $cols;
return $cols if ref $cols eq 'ARRAY';
@fields = split ',',$cols;
#die "@fields";
return scalar @fields
? \@fields
: undef;
}
my $fh = $self->{fh};
$fh->seek(0,0) if $fh;
my $first_line = $self->get_record($parser);
#print $first_line;
if ( $first_line ) {
@fields = ref($parser) =~ /Fixed/
? split /,/,$first_line
: $parser->read_fields($first_line);
}
# my @fields = $first_line
# ? $parser->read_fields($first_line)
# : ();
#print "<$_>" for @fields; print "\n";
return "CAN'T FIND COLUMN NAMES ON FIRST LINE OF '"
. $self->{file}
. "' : '@fields'" if "@fields" =~ /[^ a-zA-Z0-9_]/;
$parser->{col_names} = \@fields;
$self->{col_names} = \@fields;
$self->{col_nums} = $self->set_col_nums;
$self->{first_row_pos} = $fh->tell();
return( \@fields);
}
sub open_table {
my( $self, $parser, $file, $open_mode ) = @_;
my($newfile, $fh);
$file ||= '';
if ( $file =~ m'http://|ftp://' ) {
# die "wrong storage!";
$newfile = $file;
}
else {
($newfile,$fh) =
$self->open_local_file($file,$open_mode) if $file && !(ref $file);
}
$newfile ||= $file;
#die AnyData::dump($parser);
my $col_names = $parser->{col_names} || '';
# my @array = split(/,/,$col_names);
my @array;
@array = ref $col_names eq 'ARRAY'
? @$col_names
: split ',',$col_names;
my $pos = $fh->tell() if $fh;
my %table = (
file => $newfile,
open_mode => $open_mode,
fh => $fh,
col_nums => {},
col_names => \@array,
first_row_pos => $pos
);
for my $key(keys %table) {
$self->{$key}=$table{$key};
}
my $skip = $parser->init_parser($self);
if (!$skip && defined $newfile) {
$open_mode =~ /[co]/
? $self->print_col_names($parser)
: $self->get_col_names($parser);
}
$self->{col_nums} = $self->set_col_nums();
# use Data::Dumper; die Dumper $self;
}
sub get_file_handle { return shift->{fh} }
sub get_file_name { return shift->{file} }
sub get_file_open_mode { return shift->{open_mode} }
sub file2str { return shift->get_record(@_) }
sub get_record {
my($self,$parser)=@_;
local $/ = $parser->{record_sep} || "\n";
my $fh = $self->{fh} ;
my $record = $fh->getline || return undef;
$record =~ s/\015$//g;
$record =~ s/\012$//g;
return $record;
}
sub set_col_nums {
my $self = shift;
my $col_names = $self->{col_names};
return {} unless $col_names;
my $col_nums={}; my $i=0;
for (@$col_names) {
next unless $_;
$col_nums->{$col_names->[$i]} = $i;
$i++;
}
return $col_nums;
}
sub truncate {
my $self = shift;
if (!$self->{fh}->truncate($self->{fh}->tell())) {
die "Error while truncating " . $self->{file} . ": $!";
}
}
sub drop ($) {
my($self) = @_;
# We have to close the file before unlinking it: Some OS'es will
# refuse the unlink otherwise.
$self->{'fh'}->close() || die $!;
unlink($self->{'file'}) || die $!;
return 1;
}
sub close{ shift->{'fh'}->close() || die $!; }
sub push_row {
my $self = shift;
my $rec = shift;
my $fh = $self->{fh};
#####!!!! DON'T USE THIS #### $fh->seek(0,2) or die $!;
$fh->write($rec,length $rec)
|| die "Couldn't write to file: $!\n";
}
sub delete_record {
my $self = shift;
my $parser = shift || {};
my $fh = $self->{fh};
my $travel = length($parser->{record_sep}) || 0;
my $pos = $fh->tell - $travel;
$self->{deleted}->{$pos}++;
}
sub is_deleted {
my $self = shift;
my $parser = shift || {};
my $fh = $self->{fh};
my $travel = length($parser->{record_sep}) || 0;
my $pos = $fh->tell - $travel;
return $self->{deleted}->{$pos};
}
sub seek {
my($self, $pos, $whence) = @_;
if ($whence == 0 && $pos == 0) {
$pos = $self->{first_row_pos};
} elsif ($whence != 2 || $pos != 0) {
die "Illegal seek position: pos = $pos, whence = $whence";
}
if (!$self->{fh}->seek($pos, $whence)) {
die "Error while seeking in " . $self->{'file'} . ": $!";
}
#print "<$pos-$whence>";
}
sub DESTROY {
my $self = shift;
my $fh = $self->{fh};
print "CLOSING ", $self->get_file_name, "\n" if $fh && $DEBUG;
$fh->close if $fh;
}
__END__
|