is closing an opened file required before unlinking it?

  • Follow


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

0
Reply martps100 (12) 5/3/2006 12:34:36 AM

"martin" <martps100@gmail.com> wrote:
> 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
0
Reply xhoster 5/3/2006 12:29:00 AM


martin wrote:
> 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

-1
Reply sanjeeb 5/3/2006 2:48:55 AM

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.


John
-- 
use Perl;
program
fulfillment
0
Reply John 5/3/2006 9:01:16 AM

John W. Krahn wrote:
> 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.
>
>
> 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

0
Reply sanjeeb 5/3/2006 11:45:44 AM

"sanjeeb" <sanjeeb25@gmail.com> wrote in
news:1146656744.304122.134010@e56g2000cwe.googlegroups.com: 

> 
> John W. Krahn wrote:
>> 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 <1usa@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

0
Reply A 5/3/2006 11:56:57 AM

A. Sinan Unur wrote:
> "sanjeeb" <sanjeeb25@gmail.com> wrote in
> news:1146656744.304122.134010@e56g2000cwe.googlegroups.com:
>
> >
> > John W. Krahn wrote:
> >> 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 <1usa@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



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

0
Reply sanjeeb 5/3/2006 12:07:13 PM

"sanjeeb" <sanjeeb25@gmail.com> wrote in news:1146658033.612943.295640
@i39g2000cwa.googlegroups.com:

> 
> A. Sinan Unur wrote:
>> "sanjeeb" <sanjeeb25@gmail.com> wrote in
>> news:1146656744.304122.134010@e56g2000cwe.googlegroups.com:

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

....

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

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


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

> 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
0
Reply A 5/3/2006 2:48:22 PM

A. Sinan Unur wrote:
> "sanjeeb" <sanjeeb25@gmail.com> wrote in news:1146658033.612943.295640
>
> > 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.

> (I think this is the fourth time  someone has mentioned this).

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

0
Reply Brian 5/3/2006 5:18:12 PM

sanjeeb wrote:

> 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
0
Reply Joe 5/4/2006 8:58:52 AM

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


0
Reply Dan 5/4/2006 11:00:42 PM

sanjeeb <sanjeeb25@gmail.com> wrote in comp.lang.perl.misc:
> 
> martin wrote:
> > 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.

> 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
-- 
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.
0
Reply anno4000 5/6/2006 12:22:47 PM

11 Replies
350 Views

(page loaded in 0.127 seconds)

Similiar Articles:


















7/21/2012 5:14:48 PM


Reply: