The default bug view has changed. See this FAQ.

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

RESOLVED FIXED in mozilla9

Status

()

Core
XPCOM
RESOLVED FIXED
12 years ago
6 years ago

People

(Reporter: rstrong, Assigned: bbondy)

Tracking

Trunk
mozilla9
x86
Windows XP
Points:
---
Dependency tree / graph

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(1 attachment, 1 obsolete attachment)

Here it is:
http://lxr.mozilla.org/seamonkey/source/xpcom/io/nsLocalFileWin.cpp#762

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

Comment 2

12 years ago
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

Comment 3

12 years ago
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) {
 PR_Close(file);
 return NS_ERROR_FILE_ALREADY_EXISTS;
}
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
value other than NS_ERROR_FILE_ALREADY_EXISTS.

Comment 5

10 years ago
mass reassigning to nobody.
Assignee: dougt → nobody
(Assignee)

Comment 6

6 years ago
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!
(Assignee)

Comment 8

6 years ago
ah ok I'll check, thanks for the info.
(Assignee)

Comment 9

6 years ago
For writing a file to a read only share we return:
NS_ERROR_FILE_ACCESS_DENIED which is correct.

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)

Updated

6 years ago
Assignee: nobody → netzen
(Assignee)

Updated

6 years ago
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
(Assignee)

Updated

6 years ago
Depends on: 452217
(Assignee)

Updated

6 years ago
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
(Assignee)

Updated

6 years ago
Depends on: 429484
No longer depends on: 452217
(Assignee)

Comment 10

6 years ago
Created attachment 556265 [details] [diff] [review]
Patch for wrong error code when access denied on Create operations

So the problem is, if you have an empty read only share:
\\computername\myshare

And you try to do something like create a file inside a subfolder:
\\computername\myshare\1\2\3\file.txt

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)
(Assignee)

Comment 11

6 years ago
Example code you can run to reproduce:

var file = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("\\\\rhesus\\readonlyshare\\1\\2\\3\\test.txt"); 
file.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0777);
(Assignee)

Updated

6 years ago
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.:

if (NS_ERROR_FILE_NOT_FOUND == rv &&
    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+
(Assignee)

Comment 13

6 years ago
Created attachment 557006 [details] [diff] [review]
Patch for wrong error code when access denied on Create operations v2.

Fixed coding style on if conditions and NS_OK instead of 0.
Attachment #556265 - Attachment is obsolete: true
(Assignee)

Updated

6 years ago
Attachment #557006 - Flags: review+
(Assignee)

Comment 14

6 years ago
Pushed to try: 
https://tbpl.mozilla.org/?tree=Try&usebuildbot=1&rev=ed47da6511a5
(Assignee)

Comment 15

6 years ago
Pushed to inbound:
http://hg.mozilla.org/integration/mozilla-inbound/rev/65f0640bf316
https://hg.mozilla.org/mozilla-central/rev/65f0640bf316
Status: NEW → RESOLVED
Last Resolved: 6 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla9
You need to log in before you can comment on or make changes to this bug.