diff --git a/src/BufferedFile.cc b/src/BufferedFile.cc index 44ea076e..1342c125 100644 --- a/src/BufferedFile.cc +++ b/src/BufferedFile.cc @@ -50,12 +50,11 @@ BufferedFile::BufferedFile(const char* filename, const char* mode) #else // !__MINGW32__ fp_(a2fopen(filename, mode)), #endif // !__MINGW32__ - open_(fp_), supportsColor_(fp_ ? isatty(fileno(fp_)) : false) {} BufferedFile::BufferedFile(FILE* fp) - : fp_(fp), open_(fp_), supportsColor_(fp_ ? isatty(fileno(fp_)) : false) + : fp_(fp), supportsColor_(fp_ ? isatty(fileno(fp_)) : false) {} BufferedFile::~BufferedFile() @@ -80,11 +79,12 @@ char* BufferedFile::onGets(char* s, int size) int BufferedFile::onClose() { - if (open_) { - open_ = false; - return fclose(fp_); + int rv = 0; + if (fp_) { + rv = fclose(fp_); + fp_ = 0; } - return 0; + return rv; } int BufferedFile::onVprintf(const char* format, va_list va) @@ -114,7 +114,7 @@ bool BufferedFile::isEOF() const bool BufferedFile::isOpen() const { - return open_; + return fp_; } } // namespace aria2 diff --git a/src/BufferedFile.h b/src/BufferedFile.h index f3612fc4..1f242616 100644 --- a/src/BufferedFile.h +++ b/src/BufferedFile.h @@ -69,7 +69,6 @@ private: BufferedFile& operator=(const BufferedFile&); FILE* fp_; - bool open_; bool supportsColor_; }; diff --git a/src/GZipFile.cc b/src/GZipFile.cc index f7685cc9..05350df2 100644 --- a/src/GZipFile.cc +++ b/src/GZipFile.cc @@ -44,7 +44,7 @@ namespace aria2 { GZipFile::GZipFile(const char* filename, const char* mode) - : fp_(0), open_(false), + : fp_(0), buflen_(1024), buf_(reinterpret_cast(malloc(buflen_))) { FILE* fp = @@ -54,22 +54,21 @@ GZipFile::GZipFile(const char* filename, const char* mode) a2fopen(filename, mode); #endif // !__MINGW32__ - open_ = fp; - if (open_) { + if (fp) { int fd = dup(fileno(fp)); - if ((open_ = (fd >= 0))) { - open_ = (fp_ = gzdopen(fd, mode)); - if (!open_) { - ::close(fd); - } - } - if (open_) { + if (fd != -1) { + fp_ = gzdopen(fd, mode); + if (fp_) { + // fp_ retains fd and gzclose() will close fd as well. #if HAVE_GZBUFFER - gzbuffer(fp_, 1<<17); + gzbuffer(fp_, 1<<17); #endif #if HAVE_GZSETPARAMS - gzsetparams(fp_, 2, Z_DEFAULT_STRATEGY); + gzsetparams(fp_, 2, Z_DEFAULT_STRATEGY); #endif + } else { + ::close(fd); + } } fclose(fp); } @@ -83,11 +82,12 @@ GZipFile::~GZipFile() int GZipFile::onClose() { - if (open_) { - open_ = false; - return gzclose(fp_); + int rv = 0; + if (fp_) { + rv = gzclose(fp_); + fp_ = 0; } - return 0; + return rv; } bool GZipFile::onSupportsColor() @@ -109,7 +109,7 @@ bool GZipFile::isEOF() const bool GZipFile::isOpen() const { - return open_; + return fp_; } size_t GZipFile::onRead(void* ptr, size_t count) diff --git a/src/GZipFile.h b/src/GZipFile.h index f02c8df6..49dc716a 100644 --- a/src/GZipFile.h +++ b/src/GZipFile.h @@ -63,8 +63,6 @@ private: GZipFile& operator=(const GZipFile&); gzFile fp_; - bool open_; - size_t buflen_; char* buf_; };