comparison data/fileio/BZipFileDevice.cpp @ 1387:9b52f1a952b5

merge
author Chris Cannam
date Thu, 23 Feb 2017 17:05:26 +0000
parents f204f2fcb15e
children 48e9f538e6e9
comparison
equal deleted inserted replaced
1386:0d45fff7ccd1 1387:9b52f1a952b5
19 19
20 #include <iostream> 20 #include <iostream>
21 21
22 #include "base/Debug.h" 22 #include "base/Debug.h"
23 23
24 // for dup:
25 #ifdef _MSC_VER
26 #include <io.h>
27 #else
28 #include <unistd.h>
29 #endif
30
24 BZipFileDevice::BZipFileDevice(QString fileName) : 31 BZipFileDevice::BZipFileDevice(QString fileName) :
25 m_fileName(fileName), 32 m_fileName(fileName),
26 m_qfile(fileName), 33 m_qfile(fileName),
27 m_file(0), 34 m_file(0),
28 m_bzFile(0), 35 m_bzFile(0),
69 setErrorString(tr("Read and write modes both specified")); 76 setErrorString(tr("Read and write modes both specified"));
70 m_ok = false; 77 m_ok = false;
71 return false; 78 return false;
72 } 79 }
73 80
81 // This is all going to be a bit silly.
82 //
83 // We open the file with QFile so as not to have to worry about locale
84 // support ourselves (especially on Windows). Then we get a fd from
85 // QFile and "convert" it to a FILE* using fdopen because that is what
86 // the bz2 library needs for reading and writing an already-open file.
87 //
88 // fdopen takes over the fd it is given, and will close it when fclose
89 // is called. (We must call fclose, because it's needed to avoid
90 // leaking the file stream structure.)
91 //
92 // But QFile will also close its fd, either when we call QFile::close
93 // or on destruction -- there doesn't seem to be a way to avoid that
94 // for a file that QFile opened.
95 //
96 // So we have to add an extra dup() in to the fdopen to avoid a double
97 // close.
98 //
99 // Note that bz2 will *not* fclose the FILE* it was passed, so we
100 // don't have a problem with calling both bzWriteClose and fclose.
101
74 if (mode & WriteOnly) { 102 if (mode & WriteOnly) {
75 103
76 if (!m_qfile.open(QIODevice::WriteOnly)) { 104 if (!m_qfile.open(QIODevice::WriteOnly)) {
77 setErrorString(tr("Failed to open file for writing")); 105 setErrorString(tr("Failed to open file for writing"));
78 m_ok = false; 106 m_ok = false;
79 return false; 107 return false;
80 } 108 }
81 109
82 m_file = fdopen(m_qfile.handle(), "wb"); 110 m_file = fdopen(dup(m_qfile.handle()), "wb");
83 if (!m_file) { 111 if (!m_file) {
84 setErrorString(tr("Failed to open file handle for writing")); 112 setErrorString(tr("Failed to open file handle for writing"));
85 m_qfile.close(); 113 m_qfile.close();
86 m_ok = false; 114 m_ok = false;
87 return false; 115 return false;
112 setErrorString(tr("Failed to open file for reading")); 140 setErrorString(tr("Failed to open file for reading"));
113 m_ok = false; 141 m_ok = false;
114 return false; 142 return false;
115 } 143 }
116 144
117 m_file = fdopen(m_qfile.handle(), "rb"); 145 m_file = fdopen(dup(m_qfile.handle()), "rb");
118 if (!m_file) { 146 if (!m_file) {
119 setErrorString(tr("Failed to open file handle for reading")); 147 setErrorString(tr("Failed to open file handle for reading"));
120 m_ok = false; 148 m_ok = false;
121 return false; 149 return false;
122 } 150 }
160 188
161 if (openMode() & WriteOnly) { 189 if (openMode() & WriteOnly) {
162 unsigned int in = 0, out = 0; 190 unsigned int in = 0, out = 0;
163 BZ2_bzWriteClose(&bzError, m_bzFile, 0, &in, &out); 191 BZ2_bzWriteClose(&bzError, m_bzFile, 0, &in, &out);
164 // cerr << "Wrote bzip2 stream (in=" << in << ", out=" << out << ")" << endl; 192 // cerr << "Wrote bzip2 stream (in=" << in << ", out=" << out << ")" << endl;
165 if (bzError != BZ_OK) { 193 if (bzError != BZ_OK) {
166 setErrorString(tr("bzip2 stream write close error")); 194 setErrorString(tr("bzip2 stream write close error"));
167 } 195 }
168 fclose(m_file); 196 fclose(m_file);
169 m_qfile.close(); 197 m_qfile.close();
170 m_bzFile = 0; 198 m_bzFile = 0;
171 m_file = 0; 199 m_file = 0;
172 m_ok = false; 200 m_ok = false;