Closed Bug 300692 Opened 17 years ago Closed 11 years ago

Win32 nsLocalFile::Create returns not found if no access and a directory hierarchy is needed


(Core :: XPCOM, defect)

Windows XP
Not set





(Reporter: robert.strong.bugs, Assigned: bbondy)




(1 file, 1 obsolete file)

Here it is:

This appears to affect nsIFile.createUnique in that if access is denied it
thinks a file already exists and then tries to create the file 10,000 times
before giving up.
This came to light in bug 300265
FWIW, I changed the return to use ConvertWinError, and createUnique started
behaving much more sensibly (though I found one odd side-effect[1]), but
obviously all call sites would need to be carefully checked for how they handle
the return code.

[1] If the "Access Privileges Test" file exists when createUnique tries to test
it, and the user has permission to delete it... it deletes it! I assume this is
because it got an unexpected error, and assumed it had worked, and that it was
ok to delete the file. Oops.
Assignee: file-handling → dougt
Component: File Handling → XPCOM
QA Contact: ian → xpcom
line 763 seams very broken.  Don't we want something like:

PRFileDesc* file = PR_Open(mResolvedPath.get(), PR_RDONLY | PR_CREATE_FILE |
PR_APPEND | PR_EXCL, attributes);

if (file) {
I would think we'd want the return value from ConvertWinError as is used when
creating a directory since this won't properly handle when access is denied. As
was pointed out it appears that not all call sites properly handle a return of a
mass reassigning to nobody.
Assignee: dougt → nobody
I tested this and the only time it returns that value is if the file does already exist. I think this is expected behaviour and this bug was fixed somewhere along the line. I'd like to nominate that we resolve this as invalid.
It was returning NS_ERROR_FILE_ALREADY_EXISTS when failing to create the file. For example, in bug 300265 (see comment #1) this was happening when trying to write to a read only share. If this has been fixed since this bug was filed then YAY!
ah ok I'll check, thanks for the info.
For writing a file to a read only share we return:

When we create a file with a directory hierarchy that doesn't exist though, we usually create the whole directory hierarchy.

For writing a file to a read only share that has a directory that doesn't exist, this returns NS_ERROR_FILE_NOT_FOUND.  So in that case NS_ERROR_FILE_ACCESS_DENIED should be used.
Assignee: nobody → netzen
Summary: Win32 nsIFile.create always returns NS_ERROR_FILE_ALREADY_EXISTS → Win32 nsLocalFile::Create returns already exists if no access and a directory hierarchy is needed
Depends on: 452217
Summary: Win32 nsLocalFile::Create returns already exists if no access and a directory hierarchy is needed → Win32 nsLocalFile::Create returns not found if no access and a directory hierarchy is needed
Depends on: 429484
No longer depends on: 452217
So the problem is, if you have an empty read only share:

And you try to do something like create a file inside a subfolder:

Then the first create directory returns access denied, which keeps going trying to create the other directories.
But the others return file not found and return at that point.
The real error is that access is denied though not that the directory doesn't exist.
Attachment #556265 - Flags: review?(benjamin)
Example code you can run to reproduce:

var file = Components.classes[';1'].createInstance(Components.interfaces.nsILocalFile);
file.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0777);
Blocks: 682571
Comment on attachment 556265 [details] [diff] [review]
Patch for wrong error code when access denied on Create operations

>diff --git a/xpcom/io/nsLocalFileWin.cpp b/xpcom/io/nsLocalFileWin.cpp

>+    nsresult directoryCreateError = 0;

This should be NS_OK;

>     if (slash)
>     {
>         // skip the first '\\'
>         ++slash;
>         slash = wcschr(slash, L'\\');
>         while (slash)
>         {
>             *slash = L'\0';
>             if (!::CreateDirectoryW(mResolvedPath.get(), NULL)) {
>                 rv = ConvertWinError(GetLastError());
>+                if (rv == NS_ERROR_FILE_NOT_FOUND && 
>+                    directoryCreateError == NS_ERROR_FILE_ACCESS_DENIED) {
>+                    // If a previous CreateDirectory failed due to access, return that.
>+                    return NS_ERROR_FILE_ACCESS_DENIED;
>+                }

As a style nit, it would be better to put the constant on the left side of the ==, i.e.:

    NS_ERROR_FILE_ACCESS_DENIED == directoryCreateError)

That way any accidentally mistyped "=" become compile errors. But since the surrounding code doesn't use that style currently, I won't insist on it.
Attachment #556265 - Flags: review?(benjamin) → review+
Fixed coding style on if conditions and NS_OK instead of 0.
Attachment #556265 - Attachment is obsolete: true
Attachment #557006 - Flags: review+
Closed: 11 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla9
You need to log in before you can comment on or make changes to this bug.