Bug 277367. Trying to open a file in MODE_CREATE via an XPCOM file stream in a directory that does not exist should crete the directory too, not just the file. r=bzbarsky
authorBen <bchau3440@gmail.com>
Mon, 19 Jun 2017 14:50:16 -0400
changeset 596772 17af61e163bdede0d8801c044e3c81fd117786ae
parent 596771 2d2ee0638f59a24c5e6ea43be55cc41776c9c51b
child 596773 1b3720381302435070d415720dccaaff15caa48c
child 596939 50c745be6bd11865f02115cab01d817192e529b8
push id64751
push userbmo:rbarker@mozilla.com
push dateMon, 19 Jun 2017 19:08:32 +0000
reviewersbzbarsky
bugs277367
milestone56.0a1
Bug 277367. Trying to open a file in MODE_CREATE via an XPCOM file stream in a directory that does not exist should crete the directory too, not just the file. r=bzbarsky
netwerk/base/nsFileStreams.cpp
--- a/netwerk/base/nsFileStreams.cpp
+++ b/netwerk/base/nsFileStreams.cpp
@@ -320,16 +320,27 @@ nsFileStreamBase::DoOpen()
     MOZ_ASSERT(mState == eDeferredOpen || mState == eUnitialized ||
                mState == eClosed);
     NS_ASSERTION(!mFD, "Already have a file descriptor!");
     NS_ASSERTION(mOpenParams.localFile, "Must have a file to open");
 
     PRFileDesc* fd;
     nsresult rv;
 
+    if (mOpenParams.ioFlags & PR_CREATE_FILE) {
+        nsCOMPtr<nsIFile> parent;
+        mOpenParams.localFile->GetParent(getter_AddRefs(parent));
+
+        // Result doesn't need to be checked. If the file's parent path does not
+        // exist, make it. If it does exist, do nothing.
+        if (parent) {
+            Unused << parent->Create(nsIFile::DIRECTORY_TYPE, 0644);
+        }
+    }
+
 #ifdef XP_WIN
     if (mBehaviorFlags & nsIFileInputStream::SHARE_DELETE) {
       nsCOMPtr<nsILocalFileWin> file = do_QueryInterface(mOpenParams.localFile);
       MOZ_ASSERT(file);
 
       rv = file->OpenNSPRFileDescShareDelete(mOpenParams.ioFlags,
                                              mOpenParams.perm,
                                              &fd);