xpcom/base/nsGZFileWriter.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "nsGZFileWriter.h"
michael@0 8 #include "nsIFile.h"
michael@0 9 #include "nsString.h"
michael@0 10 #include "zlib.h"
michael@0 11
michael@0 12 #ifdef XP_WIN
michael@0 13 #include <io.h>
michael@0 14 #define _dup dup
michael@0 15 #else
michael@0 16 #include <unistd.h>
michael@0 17 #endif
michael@0 18
michael@0 19 NS_IMPL_ISUPPORTS(nsGZFileWriter, nsIGZFileWriter)
michael@0 20
michael@0 21 nsGZFileWriter::nsGZFileWriter()
michael@0 22 : mInitialized(false)
michael@0 23 , mFinished(false)
michael@0 24 {}
michael@0 25
michael@0 26 nsGZFileWriter::~nsGZFileWriter()
michael@0 27 {
michael@0 28 if (mInitialized && !mFinished) {
michael@0 29 Finish();
michael@0 30 }
michael@0 31 }
michael@0 32
michael@0 33 NS_IMETHODIMP
michael@0 34 nsGZFileWriter::Init(nsIFile* aFile)
michael@0 35 {
michael@0 36 if (NS_WARN_IF(mInitialized) ||
michael@0 37 NS_WARN_IF(mFinished))
michael@0 38 return NS_ERROR_FAILURE;
michael@0 39
michael@0 40 // Get a FILE out of our nsIFile. Convert that into a file descriptor which
michael@0 41 // gzip can own. Then close our FILE, leaving only gzip's fd open.
michael@0 42
michael@0 43 FILE* file;
michael@0 44 nsresult rv = aFile->OpenANSIFileDesc("wb", &file);
michael@0 45 if (NS_WARN_IF(NS_FAILED(rv)))
michael@0 46 return rv;
michael@0 47
michael@0 48 mGZFile = gzdopen(dup(fileno(file)), "wb");
michael@0 49 fclose(file);
michael@0 50
michael@0 51 // gzdopen returns nullptr on error.
michael@0 52 if (NS_WARN_IF(!mGZFile))
michael@0 53 return NS_ERROR_FAILURE;
michael@0 54
michael@0 55 mInitialized = true;
michael@0 56
michael@0 57 return NS_OK;
michael@0 58 }
michael@0 59
michael@0 60 NS_IMETHODIMP
michael@0 61 nsGZFileWriter::Write(const nsACString& aStr)
michael@0 62 {
michael@0 63 if (NS_WARN_IF(!mInitialized) ||
michael@0 64 NS_WARN_IF(mFinished))
michael@0 65 return NS_ERROR_FAILURE;
michael@0 66
michael@0 67 // gzwrite uses a return value of 0 to indicate failure. Otherwise, it
michael@0 68 // returns the number of uncompressed bytes written. To ensure we can
michael@0 69 // distinguish between success and failure, don't call gzwrite when we have 0
michael@0 70 // bytes to write.
michael@0 71 if (aStr.IsEmpty()) {
michael@0 72 return NS_OK;
michael@0 73 }
michael@0 74
michael@0 75 // gzwrite never does a short write -- that is, the return value should
michael@0 76 // always be either 0 or aStr.Length(), and we shouldn't have to call it
michael@0 77 // multiple times in order to get it to read the whole buffer.
michael@0 78 int rv = gzwrite(mGZFile, aStr.BeginReading(), aStr.Length());
michael@0 79 if (NS_WARN_IF(rv != static_cast<int>(aStr.Length())))
michael@0 80 return NS_ERROR_FAILURE;
michael@0 81
michael@0 82 return NS_OK;
michael@0 83 }
michael@0 84
michael@0 85 NS_IMETHODIMP
michael@0 86 nsGZFileWriter::Finish()
michael@0 87 {
michael@0 88 if (NS_WARN_IF(!mInitialized) ||
michael@0 89 NS_WARN_IF(mFinished))
michael@0 90 return NS_ERROR_FAILURE;
michael@0 91
michael@0 92 mFinished = true;
michael@0 93 gzclose(mGZFile);
michael@0 94
michael@0 95 // Ignore errors from gzclose; it's not like there's anything we can do about
michael@0 96 // it, at this point!
michael@0 97 return NS_OK;
michael@0 98 }

mercurial