Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Remove() for directories

Reply
Thread Tools

Remove() for directories

 
 
Quentin Pope
Guest
Posts: n/a
 
      08-25-2011
On Thu, 25 Aug 2011 19:41:24 +0000, Quentin Pope wrote:

> On Thu, 25 Aug 2011 10:52:17 -0600, Joe Pfeiffer wrote:
>
>> Quentin Pope <(E-Mail Removed)> writes:
>>
>>> Hi I am trying to delete a directory portably, using remove(). However
>>> it always returns EOF and doesn't delete the directory.
>>>
>>> Is there another function I should be using?

>>
>> Taking a quick look at remove()... it's returning -1, which is of
>> course the same value as EOF, but isn't EOF. It also sets errno when
>> an error happens, so you ought to be able to use perror() to find out
>> what went wrong.

>
> Hi perror() says :-
> remove: Is a directory
>
> This would suggest to me that remove cannot act on directories. My
> system is GNU/Linux.
>
> Cheers
> QP


Please ignore this post, it was posted twice in error.

Cheers
QP
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      08-25-2011
Quentin Pope <(E-Mail Removed)> writes:
> On Thu, 25 Aug 2011 10:52:17 -0600, Joe Pfeiffer wrote:
>> Quentin Pope <(E-Mail Removed)> writes:
>>> Hi I am trying to delete a directory portably, using remove(). However
>>> it always returns EOF and doesn't delete the directory.
>>>
>>> Is there another function I should be using?

>>
>> Taking a quick look at remove()... it's returning -1, which is of
>> course the same value as EOF, but isn't EOF. It also sets errno when an
>> error happens, so you ought to be able to use perror() to find out what
>> went wrong.

>
> Hi perror() says :-
> remove: Is a directory
>
> This would suggest to me that remove cannot act on directories. My system
> is GNU/Linux.


So is mine (Ubuntu 11.04), and it works for me. The Linux man page for
remove(3) says:

remove() deletes a name from the file system. It calls unlink(2)
for files, and rmdir(2) for directories.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      08-25-2011
Dr Nick <(E-Mail Removed)> writes:
> Quentin Pope <(E-Mail Removed)> writes:
>> Hi I am trying to delete a directory portably, using remove(). However it
>> always returns EOF and doesn't delete the directory.
>>
>> Is there another function I should be using?

>
> What does errno (or perror for that matter) show the actual problem to
> be?
>
> Is the directory empty?


Note that surprisingly few standard functions are required *by the C
standard* to set errno on failure. Of the <stdio.h> functions, only
fgetpos(), fsetpos(), and ftell() are explicitly required to do so, and
they all set it to an implementation-defined positive value (in
addition, I/O functions set errno to EILSEQ on an encoding error.

But on most systems, most or all <stdio.h> functions probably set errno
to a meaningful value on failure. POSIX has stronger requirements for
this than the C standard does.

Conclusion: You can probably count on a meaningful errno after an I/O
function has failed (assuming you set errno to 0 before the call), but
if you don't get one you probably can't blame the system for failing to
conform to the C standard.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Joe Pfeiffer
Guest
Posts: n/a
 
      08-25-2011
Quentin Pope <(E-Mail Removed)> writes:

> On Thu, 25 Aug 2011 10:52:17 -0600, Joe Pfeiffer wrote:
>
>> Quentin Pope <(E-Mail Removed)> writes:
>>
>>> Hi I am trying to delete a directory portably, using remove(). However
>>> it always returns EOF and doesn't delete the directory.
>>>
>>> Is there another function I should be using?

>>
>> Taking a quick look at remove()... it's returning -1, which is of
>> course the same value as EOF, but isn't EOF. It also sets errno when an
>> error happens, so you ought to be able to use perror() to find out what
>> went wrong.

>
> Hi perror() says :-
> remove: Is a directory
>
> This would suggest to me that remove cannot act on directories. My system
> is GNU/Linux.


Since it is documented as working on directories, this is very odd (to
say the least). You are deleting a straightforward directory, not a
symbolic link to a directory or something? That shouldn't matter, but
the only way I can imagine getting the error you are getting is somehow
remove() is being fooled into calling unlink() instead of rmdir(), and
your error is coming back from unlink().

Just to check, presumably you are setting errno to 0 before the call
(shouldn't be necessary, but it won't hurt), and your call to perror()
is immediately after the return?

Since rmdir() and remove() are called identically, it couldn't hurt to
try calling rmdir() directly and see what happens.
 
Reply With Quote
 
Alan Curry
Guest
Posts: n/a
 
      08-25-2011
In article <j368g6$b9e$(E-Mail Removed)>,
Quentin Pope <(E-Mail Removed)> wrote:
>On Thu, 25 Aug 2011 10:52:17 -0600, Joe Pfeiffer wrote:
>
>> Quentin Pope <(E-Mail Removed)> writes:
>>
>>> Hi I am trying to delete a directory portably, using remove(). However
>>> it always returns EOF and doesn't delete the directory.
>>>
>>> Is there another function I should be using?

>>
>> Taking a quick look at remove()... it's returning -1, which is of
>> course the same value as EOF, but isn't EOF. It also sets errno when an
>> error happens, so you ought to be able to use perror() to find out what
>> went wrong.

>
>Hi perror() says :-
>remove: Is a directory
>
>This would suggest to me that remove cannot act on directories. My system
>is GNU/Linux.


That's quite unexpected. Several questions occur to me:

Are you using glibc, or one of the alternative C libraries?

Does the directory still actually exist afterward?

Is it possible you've accidentally redefined "remove" elsewhere in your
program or in a third-party library?

Is the directory on a foreign or experimental filesystem, where imperfections
are to be expected?

strace it. The call to remove() should show an unlink attempt failing with
EISDIR, followed by a rmdir attempt that should succeed on an empty
directory. What do you actually get?

Because of the unlink attempt, it's quite normal for perror to say "Is a
directory" after a successful remove(). Be sure it actually returned nonzero
before you call perror.

--
Alan Curry
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Virtual Directories and Physical directories Paul M Fin MCAD 4 06-27-2008 07:50 PM
virtual directories/physical directories in IIS Paul F ASP .Net 4 06-25-2008 04:04 PM
Multiple bin-directories with virtual directories? =?Utf-8?B?TGFzc2UgTmlsc3Nvbg==?= ASP .Net 0 11-09-2004 05:49 PM
How to map Project directories to Production sub-directories Joel Finkel ASP .Net 0 09-12-2003 06:47 PM
Using virtual directories for common directories (scripts, images, styles, etc.) Jeffry van de Vuurst ASP .Net 2 07-30-2003 07:00 PM



Advertisments