Subject: | grep() can truncate files |
Date: | Thu, 06 Jan 2011 07:47:10 +1100 |
To: | bug-File-Find-Rule [...] rt.cpan.org |
From: | Kevin Ryde <user42 [...] zip.com.au> |
With File::Find::Rule and recent debian i386 perl 5.10.1, the program
foo.pl below truncates file /tmp/zzz/foo to zero length, where I hoped a
File::Find::Rule grep() would be read-only.
I think the filename ">foo" in the directory causes
open FILE, $_ or return;
to overwrite the "foo" file.
I suppose only the 3-arg open treats all weird filenames properly. If
staying 2-arg for older perl compatibility then a "<$filename", or a
"<./$filename" if not an absolute filename already, might help avoid the
worst.
Either way it could be worth showing 3-arg in the POD sample exec()
under the "Further examples", to keep cut-and-paste users out of this
kind of trouble.
# create two files /tmp/zzz/foo and /tmp/zzz/>foo
system "
rm -r /tmp/zzz
mkdir /tmp/zzz
echo abc def >/tmp/zzz/foo
echo abc def >'/tmp/zzz/>foo'
ls -l /tmp/zzz
";
# grep them ...
use File::Find::Rule;
my @files = File::Find::Rule->grep('abc')->in('/tmp/zzz');
# /tmp/zzz/foo has been truncated to 0 length ...
system ("ls -l /tmp/zzz");