nsGZFileWriter.cpp 2.6 KB

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