Closed Bug 300692 Opened 19 years ago Closed 13 years ago

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

Categories

(Core :: XPCOM, defect)

x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla9

People

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

References

Details

Attachments

(1 file, 1 obsolete file)

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
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) {
 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.
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:
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: 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:
\\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)
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);
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+
Fixed coding style on if conditions and NS_OK instead of 0.
Attachment #556265 - Attachment is obsolete: true
Attachment #557006 - Flags: review+
https://hg.mozilla.org/mozilla-central/rev/65f0640bf316
Status: NEW → RESOLVED
Closed: 13 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla9
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: