Bug 1433982 - Make nsAtomicFileOutputStream::DoOpen() fail if the file is read-only. r=glandium
authorNicholas Nethercote <nnethercote@mozilla.com>
Wed, 31 Jan 2018 10:01:26 +1100
changeset 401653 5ada933db7e2887646d64b44525da5daf5f44b85
parent 401652 03c14ea47865f40b1da9a30191d9948ee07e49aa
child 401654 05f077d8f612a29db4495988e5b1fbafc8d12263
push id33351
push userbtara@mozilla.com
push dateWed, 31 Jan 2018 10:00:45 +0000
treeherdermozilla-central@7b46ef2ae141 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersglandium
bugs1433982
milestone60.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 1433982 - Make nsAtomicFileOutputStream::DoOpen() fail if the file is read-only. r=glandium This means we don't leave behind prefs-<n>.js files when prefs.js is read-only. MozReview-Commit-ID: H6KKnoYGdhH
netwerk/base/nsFileStreams.cpp
--- a/netwerk/base/nsFileStreams.cpp
+++ b/netwerk/base/nsFileStreams.cpp
@@ -800,21 +800,28 @@ nsAtomicFileOutputStream::DoOpen()
 
         // XP_UNIX ignores SetFollowLinks(), so we have to normalize.
         if (mTargetFileExists) {
             tempResult->Normalize();
         }
     }
 
     if (NS_SUCCEEDED(rv) && mTargetFileExists) {
+        // Abort if |file| is not writable; it won't work as an output stream.
+        bool isWritable;
+        if (NS_SUCCEEDED(file->IsWritable(&isWritable)) && !isWritable) {
+            return NS_ERROR_FILE_ACCESS_DENIED;
+        }
+
         uint32_t origPerm;
         if (NS_FAILED(file->GetPermissions(&origPerm))) {
             NS_ERROR("Can't get permissions of target file");
             origPerm = mOpenParams.perm;
         }
+
         // XXX What if |perm| is more restrictive then |origPerm|?
         // This leaves the user supplied permissions as they were.
         rv = tempResult->CreateUnique(nsIFile::NORMAL_FILE_TYPE, origPerm);
     }
     if (NS_SUCCEEDED(rv)) {
         // nsFileOutputStream::DoOpen will work on the temporary file, so we
         // prepare it and place it in mOpenParams.localFile.
         mOpenParams.localFile = tempResult;