is closing an opened file required before unlinking it?

X

xhoster

martin said:
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
 
M

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
 
S

sanjeeb

martin said:
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
 
J

John W. Krahn

sanjeeb said:
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
 
S

sanjeeb

John said:
sanjeeb said:
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



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

A. Sinan Unur

sanjeeb said:
martin wrote:

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.
....

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.

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 <[email protected]>
(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
 
S

sanjeeb

A. Sinan Unur said:
sanjeeb wrote:
martin wrote:

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.
...

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.

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 <[email protected]>
(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



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

A. Sinan Unur



[ Please do not quote signatures ]
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).

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.
May be i was wrong.

You were wrong about your claim that unlink does not give an error when
it fails.
I was testing in WinXp,

You are correct that you cannot delete an open file on a Windows XP
system.
But its advisable to delete the file after closing the handle.

I said *in certain situations*.

Read what you are responding to.

Sinan
 
B

Brian McCauley

A. Sinan Unur said:
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.
(I think this is the fourth time someone has mentioned this).

Has anyone mentioned: _this_ _has_ _nothing_ _to_ _do_ _with_ _Perl_?
 
J

Joe Smith

sanjeeb said:
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
 
A

Anno Siegel

[...]

The answers depend on the OS you are running under. For a Unix
system, all three of your claims are wrong.
I think you can't delete a file while it's opened.

You can.
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.
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.
I think it helps.

I don't think so.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top