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