bugGNUstep - Bugs: bug #35671, Wrong error from -[NSFileManager...

Group
 
 

bug #35671: Wrong error from -[NSFileManager createDirectoryAtPath:...]

Submitter:  Jens Alfke <snej>
Submitted:  Wed 29 Feb 2012 08:07:47 PM UTC
   
 
Category:  Base/Foundation Severity:  2 - Minor
Item Group:  Change Request Status:  None
Privacy:  Public Assigned to:  None
Open/Closed:  Open
* Mandatory Fields

Add a New Comment Rich Markup
   

Fri 20 Dec 2013 04:44:40 PM UTC, comment #2: 

While I personally have no use for NSError, it would be nice to have exact compatibility with OSX errors.  I'm sure createDirectoryAtPath: is not the only problem.

What would be good is contribution of patches to add specific errors for a class (or just one method) at a time, along with regression test cases (to go in the Tests/base/... directories) we could use to test exact behavior on OSX and ensure that gnustep behavior matches it.

Richard Frith-Macdonald <CaS>
Group Member
Mon 29 Apr 2013 03:51:36 PM UTC, comment #1: 

I don't know how precise compatibility with Mac OS X/Cocoa should be for this bug, but the behaviour proposed in the patch doesn't match it:

'Wrong error Error Domain=NSCocoaErrorDomain Code=516 "The file “foobar” couldn’t be saved in the folder “tmp” because a file with the same name already exists." UserInfo=0x7f85d940ab80 {NSFilePath=/tmp/foobar, NSUnderlyingError=0x7f85d940a9d0 "The operation couldn’t be completed. File exists"}'

but error code 516 is not EEXIST and the error is in the Cocoa error domain. The underlying error is in NSPOSIXErrorDomain and has code 17 (EEXIST). The matching Foundation error (from Foundation/FoundationErrors.h) is this:

    NSFileWriteFileExistsError NS_ENUM_AVAILABLE(10_7, 5_0) = 516,          // Write error (file exists)

which is what NSFileManager returns in Foundation. I think it'd be reasonable for GNUstep-base to follow that example.

Graham Lee <leeg>
Wed 29 Feb 2012 08:07:47 PM UTC, original submission:  

Calling -[NSFileManager createDirectoryAtPath: withIntermediateDirectories: attributes: error:] with an already-existing directory whose path is (at least?) two levels deep, returns the wrong error code. The output NSError has code 2 (ENOENT), when it should be EEXIST. (It may be possible for other incorrect error codes to be returned too.)

STEPS TO REPRODUCE

$ mkdir /tmp/foobar

NSError* error = nil;
[[NSFIleManager defaultManager] createDirectoryAtPath: @"/tmp/foobar" withIntermediateDirectories: NO attributes: nil error: &error];
NSAssert(error.code == EEXIST, @"Wrong error %@", error);

DIAGNOSIS
The above method calls the older method -createDirectoryAtPath:attributes:, and if that fails it calls -[NSError _lastError] to generate the NSError to be returned. This assumes the relevant error code is contained in errno.

However, in the case where the directory already exists, the older method returns NO after a successful stat() call, so errno never actually gets set to EEXIST. Instead the value from the previous error (whatever that was) is left behind.

The best fix appears to be to manually set errno=EEXIST in this situation. (See patch below.)

There may be other code paths in that method that have similar issues; I haven't checked.

CONFIGURATION
URL: http://svn.gna.org/svn/gnustep/libs/base/trunk/Source
Repository Root: http://svn.gna.org/svn/gnustep
Repository UUID: 72102866-910b-0410-8b05-ffd578937521
Revision: 34837
My OS is Ubuntu 11 (current according to software update).
I'm compiling with Clang 3.1 (trunk 151546).

PATCH

Index: NSFileManager.m
===================================================================
--- NSFileManager.m (revision 34837)
+++ NSFileManager.m (working copy)
@@ -854,6 +854,7 @@
      {
        ASSIGN(_lastError,
  @"Could not create directory - already exists");
+       errno = EEXIST;
        return NO;
      }
  }

Jens Alfke <snej>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by CaS (Posted a comment)
  • -email is unavailable- added by FredKiefer (Updated the item)
  • -email is unavailable- added by leeg (Posted a comment)
  • -email is unavailable- added by snej (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follow 6 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2017-04-09 CaS CategoryMakefiles Base/Foundation
    2017-04-09 CaS CategoryBase/Foundation Makefiles
    2013-12-20 CaS Severity3 - Normal 2 - Minor
        Item GroupBug Change Request
    2013-12-07 FredKiefer CategoryNone Base/Foundation
        Item GroupNone Bug

    Back to the top

    Powered by Savane 3.13-4448.
    Corresponding source code