michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsGZFileWriter.h" michael@0: #include "nsIFile.h" michael@0: #include "nsString.h" michael@0: #include "zlib.h" michael@0: michael@0: #ifdef XP_WIN michael@0: #include michael@0: #define _dup dup michael@0: #else michael@0: #include michael@0: #endif michael@0: michael@0: NS_IMPL_ISUPPORTS(nsGZFileWriter, nsIGZFileWriter) michael@0: michael@0: nsGZFileWriter::nsGZFileWriter() michael@0: : mInitialized(false) michael@0: , mFinished(false) michael@0: {} michael@0: michael@0: nsGZFileWriter::~nsGZFileWriter() michael@0: { michael@0: if (mInitialized && !mFinished) { michael@0: Finish(); michael@0: } michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGZFileWriter::Init(nsIFile* aFile) michael@0: { michael@0: if (NS_WARN_IF(mInitialized) || michael@0: NS_WARN_IF(mFinished)) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: // Get a FILE out of our nsIFile. Convert that into a file descriptor which michael@0: // gzip can own. Then close our FILE, leaving only gzip's fd open. michael@0: michael@0: FILE* file; michael@0: nsresult rv = aFile->OpenANSIFileDesc("wb", &file); michael@0: if (NS_WARN_IF(NS_FAILED(rv))) michael@0: return rv; michael@0: michael@0: mGZFile = gzdopen(dup(fileno(file)), "wb"); michael@0: fclose(file); michael@0: michael@0: // gzdopen returns nullptr on error. michael@0: if (NS_WARN_IF(!mGZFile)) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: mInitialized = true; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGZFileWriter::Write(const nsACString& aStr) michael@0: { michael@0: if (NS_WARN_IF(!mInitialized) || michael@0: NS_WARN_IF(mFinished)) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: // gzwrite uses a return value of 0 to indicate failure. Otherwise, it michael@0: // returns the number of uncompressed bytes written. To ensure we can michael@0: // distinguish between success and failure, don't call gzwrite when we have 0 michael@0: // bytes to write. michael@0: if (aStr.IsEmpty()) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: // gzwrite never does a short write -- that is, the return value should michael@0: // always be either 0 or aStr.Length(), and we shouldn't have to call it michael@0: // multiple times in order to get it to read the whole buffer. michael@0: int rv = gzwrite(mGZFile, aStr.BeginReading(), aStr.Length()); michael@0: if (NS_WARN_IF(rv != static_cast(aStr.Length()))) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGZFileWriter::Finish() michael@0: { michael@0: if (NS_WARN_IF(!mInitialized) || michael@0: NS_WARN_IF(mFinished)) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: mFinished = true; michael@0: gzclose(mGZFile); michael@0: michael@0: // Ignore errors from gzclose; it's not like there's anything we can do about michael@0: // it, at this point! michael@0: return NS_OK; michael@0: }