Discussion:
is closing an opened file required before unlinking it?
(too old to reply)
martin
2006-05-03 00:34:36 UTC
Permalink
Hi I have a question regarding deleting files and appreciate any input.

Do I need to close an open file, before unlinking it or can I simply
unlink (delete( it without worring to close it.

case 1) file has been opened for reading
case 2) file has been opened for writing.


thanks in advance.

martin
x***@gmail.com
2006-05-03 00:29:00 UTC
Permalink
Post by martin
Hi I have a question regarding deleting files and appreciate any input.
Do I need to close an open file, before unlinking it or can I simply
unlink (delete( it without worring to close it.
That is up to your OS.

Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
sanjeeb
2006-05-03 02:48:55 UTC
Permalink
Post by martin
Hi I have a question regarding deleting files and appreciate any input.
Do I need to close an open file, before unlinking it or can I simply
unlink (delete( it without worring to close it.
case 1) file has been opened for reading
case 2) file has been opened for writing.
thanks in advance.
martin
Hello,
I think you can't delete a file while it's opened.
To be safe close the handle and unlink the file, because in perl unlink
will not give error , it just return the numbers of files deleted. So
if you didnt close the handle and wants to delete then it will return
0.
I think it helps.

With regards
sanjeeb
John W. Krahn
2006-05-03 09:01:16 UTC
Permalink
Post by sanjeeb
Post by martin
Do I need to close an open file, before unlinking it or can I simply
unlink (delete( it without worring to close it.
case 1) file has been opened for reading
case 2) file has been opened for writing.
I think you can't delete a file while it's opened.
To be safe close the handle and unlink the file, because in perl unlink
will not give error
Yes it does "give error":

$ perl -e' unlink q[DoesNotExist] or die "unlink: $!" '
unlink: No such file or directory at -e line 1.


John
--
use Perl;
program
fulfillment
sanjeeb
2006-05-03 11:45:44 UTC
Permalink
Post by John W. Krahn
Post by sanjeeb
Post by martin
Do I need to close an open file, before unlinking it or can I simply
unlink (delete( it without worring to close it.
case 1) file has been opened for reading
case 2) file has been opened for writing.
I think you can't delete a file while it's opened.
To be safe close the handle and unlink the file, because in perl unlink
will not give error
$ perl -e' unlink q[DoesNotExist] or die "unlink: $!" '
unlink: No such file or directory at -e line 1.
John
--
use Perl;
program
fulfillment
Prog 1:
###################
open(handle,">asdf");
close handle;
unlink asdf;


O/p
It will delete the file .

prog 2
###################
open(handle,">asdf");
unlink asdf;


It will not delete the file and doesnt give an error also.


If you record the return value from the unlink in first case it will
record 1(means it deleted one file , u can put a list if file and test
also) and in the last it will show 0(since the handle is not closed ).


With regards
Sanjeeb
A. Sinan Unur
2006-05-03 11:56:57 UTC
Permalink
Post by sanjeeb
Post by John W. Krahn
Post by sanjeeb
Post by martin
Do I need to close an open file, before unlinking it or can I
simply unlink (delete( it without worring to close it.
case 1) file has been opened for reading
case 2) file has been opened for writing.
I think you can't delete a file while it's opened.
To be safe close the handle and unlink the file, because in perl
unlink will not give error
$ perl -e' unlink q[DoesNotExist] or die "unlink: $!" '
unlink: No such file or directory at -e line 1.
...
Post by sanjeeb
###################
open(handle,">asdf");
close handle;
unlink asdf;
O/p
It will delete the file .
prog 2
###################
open(handle,">asdf");
unlink asdf;
It will not delete the file and doesnt give an error also.
By your reasoning, no call in Perl returns an error.

For example, if the open call failed above, you would not get a message
either (unless you have Fatal'ized it).

You have to check if the call succeded. If the call failed, $! will
contain the error. You can then choose to report it or not.

That is what John was demonstrating.

As a side note, unlinking an open file is possible (and advisable in
certain situations) on some operating systems other than Windows.

Sinan
--
A. Sinan Unur <***@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
sanjeeb
2006-05-03 12:07:13 UTC
Permalink
Post by A. Sinan Unur
Post by sanjeeb
Post by John W. Krahn
Post by sanjeeb
Post by martin
Do I need to close an open file, before unlinking it or can I
simply unlink (delete( it without worring to close it.
case 1) file has been opened for reading
case 2) file has been opened for writing.
I think you can't delete a file while it's opened.
To be safe close the handle and unlink the file, because in perl
unlink will not give error
$ perl -e' unlink q[DoesNotExist] or die "unlink: $!" '
unlink: No such file or directory at -e line 1.
...
Post by sanjeeb
###################
open(handle,">asdf");
close handle;
unlink asdf;
O/p
It will delete the file .
prog 2
###################
open(handle,">asdf");
unlink asdf;
It will not delete the file and doesnt give an error also.
By your reasoning, no call in Perl returns an error.
For example, if the open call failed above, you would not get a message
either (unless you have Fatal'ized it).
You have to check if the call succeded. If the call failed, $! will
contain the error. You can then choose to report it or not.
That is what John was demonstrating.
As a side note, unlinking an open file is possible (and advisable in
certain situations) on some operating systems other than Windows.
Sinan
--
(remove .invalid and reverse each component for email address)
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
Ya i do agree. Your view is right.
And my view was to clarify whether the file will be deleted or not
while the handle isnt closed.
Can you please tell me in which situation it is advisable to delete the
file while its open? May be i was wrong. I was testing in WinXp, But
its advisable to delete the file after closing the handle. can you
please clarify my doubt.

With regards
Sanjeeb
A. Sinan Unur
2006-05-03 14:48:22 UTC
Permalink
Post by sanjeeb
Post by A. Sinan Unur
Post by sanjeeb
###################
open(handle,">asdf");
close handle;
unlink asdf;
O/p
It will delete the file .
prog 2
###################
open(handle,">asdf");
unlink asdf;
It will not delete the file and doesnt give an error also.
...
Post by sanjeeb
Post by A. Sinan Unur
You have to check if the call succeded. If the call failed, $! will
contain the error. You can then choose to report it or not.
[ Please do not quote signatures ]
Post by sanjeeb
Ya i do agree. Your view is right.
And my view was to clarify whether the file will be deleted or not
while the handle isnt closed.
It depends on the operating system (I think this is the fourth time
someone has mentioned this).
Post by sanjeeb
Post by A. Sinan Unur
As a side note, unlinking an open file is possible (and advisable in
certain situations) on some operating systems other than Windows.
Can you please tell me in which situation it is advisable to delete the
file while its open?
For security. If you need to work with a temporary file, and make it
harder for others to interfere with that, you can create and unlink. You
will still be able to work with it, though, because you have a handle to
it.
Post by sanjeeb
May be i was wrong.
You were wrong about your claim that unlink does not give an error when
it fails.
Post by sanjeeb
I was testing in WinXp,
You are correct that you cannot delete an open file on a Windows XP
system.
Post by sanjeeb
But its advisable to delete the file after closing the handle.
I said *in certain situations*.

Read what you are responding to.

Sinan
Brian McCauley
2006-05-03 17:18:12 UTC
Permalink
Post by A. Sinan Unur
Post by sanjeeb
And my view was to clarify whether the file will be deleted or not
while the handle isnt closed.
It depends on the operating system
And the filesystem. ISTR on some network file systems on POSIX-like
OSs unlink() will delete a file even if there are open handles. Any
attempt thereafter to use those handles gives ESTALE.
Post by A. Sinan Unur
(I think this is the fourth time someone has mentioned this).
Has anyone mentioned: _this_ _has_ _nothing_ _to_ _do_ _with_ _Perl_?
Joe Smith
2006-05-04 08:58:52 UTC
Permalink
Post by sanjeeb
prog 2
###################
open(handle,">asdf");
unlink asdf;
It will not delete the file and doesnt give an error also.
That statement is not correct. Observe:

linux% cat test.pl
#!/usr/bin/perl
$file='asdf';
open HANDLE,">$file";
print "File exists\n" if -f $file;
unlink $file or warn "Failed: unlink($file) = $!\n";
print "File has been removed from the directory\n" if not -f $file;
system 'uname -a';

linux% ./test.pl
File exists
File has been removed from the directory
Linux mathras 2.6.15-1.1831_FC4 #1 Tue Feb 7 13:37:42 EST 2006 i686 GNU/Linux

cygwin-on-XP% ./test.pl
File exists
File has been removed from the directory
CYGWIN_NT-5.1 PREZZY 1.5.19(0.150/4/2) 2006-01-20 13:28 i686 Cygwin

-Joe
Dan Mercer
2006-05-04 23:00:42 UTC
Permalink
On UNIXish systems, an unlink removes the corresponding entry from
its directory and decrements the inode's link count. If the link count
is zero and the file is closed the inode is deleted. If the link count
is zero and the file is open, the inode will be deleted when the file
is closed.

Experienced UNIX coders rely on this feature when implementing temp
files - you don't have to worry about various race conditions or
having to remember to remove the files later (or worry about
interruptions or crashes leaving behind files). Indeed, that's how
modern shells implement here docs (the old SV5R1 Bourne shell would
slowly fill up /tmp with .sh$$ files).

I first learned this lesson in 1985 with a 200 Meg hard drive NCR Tower.
We got 16 meg dump files from the mainframe over an RJE line, but we
were always 16 meg short. Finally figured they were dumping the files
to a temp file which was then copied and unlinked, but never closed until
the next file came through. Changed the mainframe job to send through a
100 byte dummy file after every transmission.

Dan Mercer
Anno Siegel
2006-05-06 12:22:47 UTC
Permalink
Post by sanjeeb
Post by martin
Hi I have a question regarding deleting files and appreciate any input.
Do I need to close an open file, before unlinking it or can I simply
unlink (delete( it without worring to close it.
case 1) file has been opened for reading
case 2) file has been opened for writing.
[...]

The answers depend on the OS you are running under. For a Unix
system, all three of your claims are wrong.
Post by sanjeeb
I think you can't delete a file while it's opened.
You can.
Post by sanjeeb
To be safe close the handle and unlink the file, because in perl unlink
will not give error , it just return the numbers of files deleted.
If an error occurs on deletion, $! will be set accordingly.
Post by sanjeeb
So
if you didnt close the handle and wants to delete then it will return
0.
No. It will delete open files and include the count in the result.
Post by sanjeeb
I think it helps.
I don't think so.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
Loading...