Bug 1529596 - part 2 - pull out code for creating files/directories; r=aklotz
authorNathan Froyd <froydnj@gmail.com>
Thu, 07 Mar 2019 20:43:04 +0000
changeset 521159 b55a2c1df7bb
parent 521158 db0f22709ad1
child 521160 de92c0248a0b
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaklotz
bugs1529596
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1529596 - part 2 - pull out code for creating files/directories; r=aklotz We eventually want to make the common path just attempt file creation, and only fall back to creating all the ancestor directories if the initial attempt failed. To do that in a reasonable way, we'll need re-usable code for the creation code, which is what this patch creates. Depends on D22359 Differential Revision: https://phabricator.services.mozilla.com/D22360
xpcom/io/nsLocalFileWin.cpp
--- a/xpcom/io/nsLocalFileWin.cpp
+++ b/xpcom/io/nsLocalFileWin.cpp
@@ -1129,16 +1129,44 @@ nsLocalFile::OpenANSIFileDesc(const char
   *aResult = _wfopen(mResolvedPath.get(), NS_ConvertASCIItoUTF16(aMode).get());
   if (*aResult) {
     return NS_OK;
   }
 
   return NS_ERROR_FAILURE;
 }
 
+static nsresult
+do_create(nsIFile* aFile, const nsString& aPath, uint32_t aAttributes) {
+  PRFileDesc* file;
+  nsresult rv = OpenFile(aPath,
+                         PR_RDONLY | PR_CREATE_FILE | PR_APPEND | PR_EXCL, aAttributes,
+                         false, &file);
+  if (file) {
+    PR_Close(file);
+  }
+
+  if (rv == NS_ERROR_FILE_ACCESS_DENIED) {
+    // need to return already-exists for directories (bug 452217)
+    bool isdir;
+    if (NS_SUCCEEDED(aFile->IsDirectory(&isdir)) && isdir) {
+      rv = NS_ERROR_FILE_ALREADY_EXISTS;
+    }
+  }
+  return rv;
+}
+
+static nsresult
+do_mkdir(nsIFile*, const nsString& aPath, uint32_t) {
+  if (!::CreateDirectoryW(aPath.get(), nullptr)) {
+    return ConvertWinError(GetLastError());
+  }
+  return NS_OK;
+}
+
 NS_IMETHODIMP
 nsLocalFile::Create(uint32_t aType, uint32_t aAttributes) {
   if (aType != NORMAL_FILE_TYPE && aType != DIRECTORY_TYPE) {
     return NS_ERROR_FILE_UNKNOWN_TYPE;
   }
 
   nsresult rv = Resolve();
   if (NS_FAILED(rv) && rv != NS_ERROR_FILE_NOT_FOUND) {
@@ -1205,39 +1233,21 @@ nsLocalFile::Create(uint32_t aType, uint
   }
 
   // If our last CreateDirectory failed due to access, return that.
   if (NS_ERROR_FILE_ACCESS_DENIED == directoryCreateError) {
     return directoryCreateError;
   }
 
   if (aType == NORMAL_FILE_TYPE) {
-    PRFileDesc* file;
-    rv = OpenFile(mResolvedPath,
-                  PR_RDONLY | PR_CREATE_FILE | PR_APPEND | PR_EXCL, aAttributes,
-                  false, &file);
-    if (file) {
-      PR_Close(file);
-    }
-
-    if (rv == NS_ERROR_FILE_ACCESS_DENIED) {
-      // need to return already-exists for directories (bug 452217)
-      bool isdir;
-      if (NS_SUCCEEDED(IsDirectory(&isdir)) && isdir) {
-        rv = NS_ERROR_FILE_ALREADY_EXISTS;
-      }
-    }
-    return rv;
+    return do_create(this, mResolvedPath, aAttributes);
   }
 
   if (aType == DIRECTORY_TYPE) {
-    if (!::CreateDirectoryW(mResolvedPath.get(), nullptr)) {
-      return ConvertWinError(GetLastError());
-    }
-    return NS_OK;
+    return do_mkdir(this, mResolvedPath, aAttributes);
   }
 
   return NS_ERROR_FILE_UNKNOWN_TYPE;
 }
 
 NS_IMETHODIMP
 nsLocalFile::Append(const nsAString& aNode) {
   // append this path, multiple components are not permitted