/src/mozilla-central/xpcom/base/nsGZFileWriter.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | NS_IMPL_ISUPPORTS(nsGZFileWriter, nsIGZFileWriter) |
20 | | |
21 | | nsGZFileWriter::nsGZFileWriter(Operation aMode) |
22 | | : mMode(aMode) |
23 | | , mInitialized(false) |
24 | | , mFinished(false) |
25 | | , mGZFile(nullptr) |
26 | 0 | { |
27 | 0 | } |
28 | | |
29 | | nsGZFileWriter::~nsGZFileWriter() |
30 | 0 | { |
31 | 0 | if (mInitialized && !mFinished) { |
32 | 0 | Finish(); |
33 | 0 | } |
34 | 0 | } |
35 | | |
36 | | NS_IMETHODIMP |
37 | | nsGZFileWriter::Init(nsIFile* aFile) |
38 | 0 | { |
39 | 0 | if (NS_WARN_IF(mInitialized) || |
40 | 0 | NS_WARN_IF(mFinished)) { |
41 | 0 | return NS_ERROR_FAILURE; |
42 | 0 | } |
43 | 0 | |
44 | 0 | // Get a FILE out of our nsIFile. Convert that into a file descriptor which |
45 | 0 | // gzip can own. Then close our FILE, leaving only gzip's fd open. |
46 | 0 | |
47 | 0 | FILE* file; |
48 | 0 | nsresult rv = aFile->OpenANSIFileDesc(mMode == Create ? "wb" : "ab", &file); |
49 | 0 | if (NS_WARN_IF(NS_FAILED(rv))) { |
50 | 0 | return rv; |
51 | 0 | } |
52 | 0 | return InitANSIFileDesc(file); |
53 | 0 | } |
54 | | |
55 | | NS_IMETHODIMP |
56 | | nsGZFileWriter::InitANSIFileDesc(FILE* aFile) |
57 | 0 | { |
58 | 0 | mGZFile = gzdopen(dup(fileno(aFile)), mMode == Create ? "wb" : "ab"); |
59 | 0 | fclose(aFile); |
60 | 0 |
|
61 | 0 | // gzdopen returns nullptr on error. |
62 | 0 | if (NS_WARN_IF(!mGZFile)) { |
63 | 0 | return NS_ERROR_FAILURE; |
64 | 0 | } |
65 | 0 | |
66 | 0 | mInitialized = true; |
67 | 0 |
|
68 | 0 | return NS_OK; |
69 | 0 | } |
70 | | |
71 | | NS_IMETHODIMP |
72 | | nsGZFileWriter::Write(const nsACString& aStr) |
73 | 0 | { |
74 | 0 | if (NS_WARN_IF(!mInitialized) || |
75 | 0 | NS_WARN_IF(mFinished)) { |
76 | 0 | return NS_ERROR_FAILURE; |
77 | 0 | } |
78 | 0 | |
79 | 0 | // gzwrite uses a return value of 0 to indicate failure. Otherwise, it |
80 | 0 | // returns the number of uncompressed bytes written. To ensure we can |
81 | 0 | // distinguish between success and failure, don't call gzwrite when we have 0 |
82 | 0 | // bytes to write. |
83 | 0 | if (aStr.IsEmpty()) { |
84 | 0 | return NS_OK; |
85 | 0 | } |
86 | 0 | |
87 | 0 | // gzwrite never does a short write -- that is, the return value should |
88 | 0 | // always be either 0 or aStr.Length(), and we shouldn't have to call it |
89 | 0 | // multiple times in order to get it to read the whole buffer. |
90 | 0 | int rv = gzwrite(mGZFile, aStr.BeginReading(), aStr.Length()); |
91 | 0 | if (NS_WARN_IF(rv != static_cast<int>(aStr.Length()))) { |
92 | 0 | return NS_ERROR_FAILURE; |
93 | 0 | } |
94 | 0 | |
95 | 0 | return NS_OK; |
96 | 0 | } |
97 | | |
98 | | NS_IMETHODIMP |
99 | | nsGZFileWriter::Finish() |
100 | 0 | { |
101 | 0 | if (NS_WARN_IF(!mInitialized) || |
102 | 0 | NS_WARN_IF(mFinished)) { |
103 | 0 | return NS_ERROR_FAILURE; |
104 | 0 | } |
105 | 0 | |
106 | 0 | mFinished = true; |
107 | 0 | gzclose(mGZFile); |
108 | 0 |
|
109 | 0 | // Ignore errors from gzclose; it's not like there's anything we can do about |
110 | 0 | // it, at this point! |
111 | 0 | return NS_OK; |
112 | 0 | } |