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