Skip to content

Commit

Permalink
Prevent [import] from using a temporary file for inline data.
Browse files Browse the repository at this point in the history
This also implicitly fixes the UTF-8 problems reported in #339.
  • Loading branch information
Rok Ružič authored and racke committed Apr 20, 2010
1 parent 9026f0b commit fe4fa39
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
7 changes: 4 additions & 3 deletions lib/Vend/Data.pm
Expand Up @@ -306,8 +306,9 @@ sub import_text {
or die ::errmsg("No absolute file names like '%s' allowed.\n", $fn);
}
else {
Vend::Util::writefile($fn, $text)
or die ("Cannot write temporary import file $fn: $!\n");
# data is already in memory, do not create a temporary file
$options->{scalar_ref} = 1;
$fn = \$text;
}

my $save = $/;
Expand All @@ -319,7 +320,7 @@ sub import_text {
Vend::Table::Common::import_ascii_delimited($fn, $options);

$/ = $save;
unlink $fn unless $options->{'file'};
unlink $fn unless $options->{'file'} or $options->{scalar_ref};
return 1;
}

Expand Down
24 changes: 18 additions & 6 deletions lib/Vend/Table/Common.pm
Expand Up @@ -1046,7 +1046,9 @@ sub import_ascii_delimited {

my $realfile;
if($options->{PRELOAD}) {
if (-f $infile and $options->{PRELOAD_EMPTY_ONLY}) {
# do not preload if $infile is a scalar reference
if ($options->{scalar_ref} or
(-f $infile and $options->{PRELOAD_EMPTY_ONLY})) {
# Do nothing, no preload
}
else {
Expand All @@ -1058,10 +1060,18 @@ sub import_ascii_delimited {
}

if(! defined $realfile) {
open(IN, "+<$infile")
or die errmsg("%s %s: %s\n", errmsg("open read/write"), $infile, $!);
lockfile(\*IN, 1, 1)
or die errmsg("%s %s: %s\n", errmsg("lock"), $infile, $!);
if($options->{scalar_ref}){
open(IN, '+<', $infile)
or die errmsg("%s %s: %s\n", errmsg("open scalar reference"), *$infile, $!);
# locking of scalar reference filehandles in unsupported
}
else{
open(IN, "+<$infile")
or die errmsg("%s %s: %s\n", errmsg("open read/write"), $infile, $!);
lockfile(\*IN, 1, 1)
or die errmsg("%s %s: %s\n", errmsg("lock"), $infile, $!);
}

}
else {
open(IN, "<$infile")
Expand Down Expand Up @@ -1474,7 +1484,9 @@ EndOfRoutine
}
delete $out->[$CONFIG]{Clean_start};
delete $out->[$CONFIG]{_Dirty};
unlockfile(\*IN) or die "unlock\n";
unless($options->{scalar_ref}){
unlockfile(\*IN) or die "unlock\n";
}
close(IN);
my $dot = $out->[$CONFIG]{HIDE_AUTO_FILES} ? '.' : '';
if($numeric_guess) {
Expand Down

0 comments on commit fe4fa39

Please sign in to comment.