Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.96k stars 555 forks source link

PerlIO::scalar doesn't know how to truncate #8572

Open p5pRT opened 18 years ago

p5pRT commented 18 years ago

Migrated from rt.perl.org#40241 (status was 'open')

Searchable as RT40241$

p5pRT commented 18 years ago

From blgl@hagernas.com

Created by blgl@hagernas.com

Truncating a string isn't an expensive operation\, so is this really too much to ask?

#! /usr/local/bin/perl use strict; open(my $fh\,'+>'\,\my $buf)   or die "open​: $!\n"; print {$fh} "Hello\, World!\n"   or die "print​: $!\n"; truncate($fh\,5)   or warn "truncate​: $!\n"; # outputs "truncate​: Bad file
descriptor" print 'length​: '.length($buf)."\n"; # outputs "length​: 14"

Perl Info ``` Flags: category=core severity=low Site configuration information for perl v5.8.8: Configured by blgl at Fri Jun 9 19:26:53 CEST 2006. Summary of my perl5 (revision 5 version 8 subversion 8) configuration: Platform: osname=darwin, osvers=8.6.0, archname=darwin-thread- multi-64int-2level uname='darwin sunwukung.sth.ownit.se 8.6.0 darwin kernel version 8.6.0: tue mar 7 16:58:48 pst 2006; root:xnu-792.6.70.obj~1release_ppc power macintosh powerpc ' config_args='' hint=recommended, useposix=true, d_sigaction=define usethreads=define use5005threads=undef useithreads=define usemultiplicity=define useperlio=define d_sfio=undef uselargefiles=define usesocks=undef use64bitint=define use64bitall=undef uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='cc', ccflags ='-fno-common -DPERL_DARWIN -no-cpp-precomp - fno-strict-aliasing -pipe -Wdeclaration-after-statement -I/usr/local/ include', optimize='-Os', cppflags='-no-cpp-precomp -fno-common -DPERL_DARWIN -no-cpp- precomp -fno-strict-aliasing -pipe -Wdeclaration-after-statement -I/ usr/local/include' ccversion='', gccversion='4.0.1 (Apple Computer, Inc. build 5247)', gccosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=87654321 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16 ivtype='long long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8 alignbytes=8, prototype=define Linker and Libraries: ld='env MACOSX_DEPLOYMENT_TARGET=10.3 cc', ldflags =' -L/usr/ local/lib' libpth=/usr/local/lib /usr/lib libs=-lc perllibs=-lc libc=/usr/lib/libc.dylib, so=dylib, useshrplib=false, libperl=libperl.a gnulibc_version='' Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=bundle, d_dlsymun=undef, ccdlflags=' ' cccdlflags=' ', lddlflags=' -bundle -undefined dynamic_lookup -L/ usr/local/lib' Locally applied patches: @INC for perl v5.8.8: /usr/local/lib/perl5/5.8.8/darwin-thread-multi-64int-2level /usr/local/lib/perl5/5.8.8 /usr/local/lib/perl5/site_perl/5.8.8/darwin-thread- multi-64int-2level /usr/local/lib/perl5/site_perl/5.8.8 /usr/local/lib/perl5/site_perl . Environment for perl v5.8.8: DYLD_LIBRARY_PATH (unset) HOME=/Users/blgl LANG (unset) LANGUAGE (unset) LD_LIBRARY_PATH (unset) LOGDIR (unset) PATH=/Users/blgl/bin:/Users/blgl/bin/powerpc-apple-darwin:/usr/ local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin PERL_BADLANG (unset) SHELL=/bin/tcsh ```
p5pRT commented 18 years ago

From @rgarcia

On 26/08/06\, via RT Bo Lindbergh \perlbug\-followup@​perl\.org wrote​:

Truncating a string isn't an expensive operation\, so is this really too much to ask?

A quick look at the implementation suggests that truncate() only works for filehandles currently.

p5pRT commented 18 years ago

The RT System itself - Status changed from 'new' to 'open'

p5pRT commented 18 years ago

From nick@ing-simmons.net

Rafael Garcia-Suarez \rgarciasuarez@​gmail\.com writes​:

On 26/08/06\, via RT Bo Lindbergh \perlbug\-followup@​perl\.org wrote​:

Truncating a string isn't an expensive operation\, so is this really too much to ask?

A quick look at the implementation suggests that truncate() only works for filehandles currently.

But a PerlIO​::Scalar "is" a file handle :-(

That said we there isn't a ftruncate() hook in PerlIO virtualization so I don't know what we do...

p5pRT commented 18 years ago

From blgl@hagernas.com

In article \20060901184007\.24952\.7@​llama\.ing\-simmons\.net\, nick@​ing-simmons.net (Nick Ing-Simmons) wrote​:

Rafael Garcia-Suarez \rgarciasuarez@​gmail\.com writes​:

On 26/08/06\, via RT Bo Lindbergh \perlbug\-followup@​perl\.org wrote​:

Truncating a string isn't an expensive operation\, so is this really too much to ask?

A quick look at the implementation suggests that truncate() only works for filehandles currently.

But a PerlIO​::Scalar "is" a file handle :-(

That said we there isn't a ftruncate() hook in PerlIO virtualization so I don't know what we do...

Adding _only_ ftruncate to PerlIO is not the Right Thing to do\, because the next bug report would be "Why can't you use the -s operator to get the length of the scalar used by a PerlIO​::Scalar handle?" And once you have both fstat and ftruncate\, you might as well complete the set and add fchown\, fchmod\, and futimes too.

/Bo Lindbergh

p5pRT commented 18 years ago

From nick@ing-simmons.net

Bo Lindbergh \blgl@​hagernas\.com writes​:

In article \20060901184007\.24952\.7@​llama\.ing\-simmons\.net\, nick@​ing-simmons.net (Nick Ing-Simmons) wrote​:

Rafael Garcia-Suarez \rgarciasuarez@​gmail\.com writes​:

On 26/08/06\, via RT Bo Lindbergh \perlbug\-followup@​perl\.org wrote​:

Truncating a string isn't an expensive operation\, so is this really too much to ask?

A quick look at the implementation suggests that truncate() only works for filehandles currently.

But a PerlIO​::Scalar "is" a file handle :-(

That said we there isn't a ftruncate() hook in PerlIO virtualization so I don't know what we do...

Adding _only_ ftruncate to PerlIO is not the Right Thing to do\, because the next bug report would be "Why can't you use the -s operator to get the length of the scalar used by a PerlIO​::Scalar handle?"

And I would agree with that. But then we have a bunch of other fields in stat that we would have to lie about or fake by tracking last time touched etc.

And once you have both fstat and ftruncate\, you might as well complete the set and add fchown\, fchmod\, and futimes too.

If we fake times in our fake stat then fake futimes makes sense. But chown an chmod make little sense to me - only the current process can access the scalar so current UID is owner (and can't exec it).

We could accept the calls and set errno to nearest code that made sense\, or perhaps just set the mode so that fake stat saw it.

Have we missed any more fd related calls? If there anything else that we can do to filehandles via perl interface that we missed?

p5pRT commented 18 years ago

From blgl@hagernas.com

In article \20060902095359\.24952\.8@​llama\.ing\-simmons\.net\, nick@​ing-simmons.net (Nick Ing-Simmons) wrote​:

If we fake times in our fake stat then fake futimes makes sense. But chown an chmod make little sense to me - only the current process can access the scalar so current UID is owner (and can't exec it).

That's true for scalar i/o\, but they would be useful for other kinds of virtual file. An SFTP client comes to mind.

Have we missed any more fd related calls?

ioctl and fcntl. I don't know about the former\, but a virtual O_NONBLOCK flag might be useful.

/Bo Lindbergh

p5pRT commented 11 years ago

From @leonerd

I find this bug still present\, and affecting a unit test of mine (wherein I construct scalar filehandles to pass to the function under test to avoid touching the real FS)​:

  $ perlsh   eval​: open my $fh\, ">"\, \my $content;   '1'

  eval​: $fh->print( "AAAAAAAAAA" );   '1'

  eval​: $fh->seek(0\,0); $fh->truncate(0)   undef

  eval​: $fh->print( "BB" )   '1'

  eval​: $content   'BBAAAAAAAA'

This is on​:

$ perl -v

This is perl 5\, version 18\, subversion 1 (v5.18.1) built for x86_64-linux-gnu-thread-multi(with 46 registered patches\, see perl -V for more detail)

While I await a fix - any known workarounds? E.g. a Tie module?

-- Paul "LeoNerd" Evans

leonerd@​leonerd.org.uk ICQ# 4135350 | Registered Linux# 179460 http​://www.leonerd.org.uk/