Upstream tarball 10013
[amule.git] / src / libs / common / FileFunctions.cpp
blobda327150e704096e31539964c07c3541f935a4ef
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (c) 2002-2008 Merkur ( devs@emule-project.net / http://www.emule-project.net )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include <wx/dir.h> // Needed for wxDir
28 #include <wx/fs_zip.h> // Needed for wxZipFSHandler
29 #include <wx/wfstream.h> // wxFileInputStream
30 #include <wx/zipstrm.h> // Needed for wxZipInputStream
31 #include <wx/zstream.h> // Needed for wxZlibInputStream
32 #include <wx/log.h> // Needed for wxSysErrorMsg
34 #ifdef __WXMAC__
35 #include <zlib.h> // Do_not_auto_remove
36 #endif
37 #include <memory> // Needed for std::auto_ptr
39 #include "FileFunctions.h"
40 #include "StringFunctions.h"
43 // This class assumes that the following line has been executed:
45 // wxConvFileName = &aMuleConvBrokenFileNames;
47 // This line is necessary so that wxWidgets handles unix file names correctly.
49 CDirIterator::CDirIterator(const CPath& dir)
50 : wxDir(dir.GetRaw())
55 CDirIterator::~CDirIterator()
60 CPath CDirIterator::GetFirstFile(FileType type, const wxString& mask)
62 if (!IsOpened()) {
63 return CPath();
66 wxString fileName;
67 if (!GetFirst(&fileName, mask, type)) {
68 return CPath();
71 return CPath(fileName);
75 CPath CDirIterator::GetNextFile()
77 wxString fileName;
78 if (!GetNext(&fileName)) {
79 return CPath();
82 return CPath(fileName);
86 bool CDirIterator::HasSubDirs(const wxString& spec)
88 // Checking IsOpened() in case we don't have permissions to read that dir.
89 return IsOpened() && wxDir::HasSubDirs(spec);
93 EFileType GuessFiletype(const wxString& file)
95 wxFile archive(file, wxFile::read);
96 if (!archive.IsOpened()) {
97 return EFT_Error;
99 char head[10] = {0};
100 int read = archive.Read(head, std::min<off_t>(10, archive.Length()));
102 if (read == wxInvalidOffset) {
103 return EFT_Unknown;
104 } else if ((head[0] == 'P') && (head[1] == 'K')) {
105 // Zip-archives have a header of "PK".
106 return EFT_Zip;
107 } else if (head[0] == '\x1F' && head[1] == '\x8B') {
108 // Gzip-archives have a header of 0x1F8B
109 return EFT_GZip;
110 } else if (head[0] == '\xE0' || head[0] == '\x0E') {
111 // MET files have either of these headers
112 return EFT_Met;
115 // Check at most the first ten chars, if all are printable,
116 // then we can probably assume it is ascii text-file.
117 for (int i = 0; i < read; ++i) {
118 if (!isprint(head[i]) && !isspace(head[i])) {
119 return EFT_Unknown;
123 return EFT_Text;
128 * Replaces the zip-archive with "guarding.p2p" or "ipfilter.dat",
129 * if either of those files are found in the archive.
131 bool UnpackZipFile(const wxString& file, const wxChar* files[])
133 wxZipFSHandler archive;
134 wxString filename = archive.FindFirst(
135 wxT("file:") + file + wxT("#zip:/*"), wxFILE);
137 wxTempFile target(file);
139 while (!filename.IsEmpty() && !target.Length()) {
140 // Extract the filename part of the URI
141 filename = filename.AfterLast(wxT(':')).Lower();
143 // We only care about the files specified in the array
144 for (size_t i = 0; files[i] && !target.Length(); ++i) {
145 if (files[i] == filename) {
146 std::auto_ptr<wxZipEntry> entry;
147 wxFFileInputStream fileInputStream(file);
148 wxZipInputStream zip(fileInputStream);
149 while (entry.reset(zip.GetNextEntry()), entry.get() != NULL) {
150 // access meta-data
151 wxString name = entry->GetName();
152 // read 'zip' to access the entry's data
153 if (name == filename) {
154 char buffer[10240];
155 while (!zip.Eof()) {
156 zip.Read(buffer, sizeof(buffer));
157 target.Write(buffer, zip.LastRead());
159 break;
165 filename = archive.FindNext();
168 if (target.Length()) {
169 target.Commit();
170 return true;
173 return false;
178 * Unpacks a GZip file and replaces the archive.
180 bool UnpackGZipFile(const wxString& file)
182 char buffer[10240];
183 wxTempFile target(file);
185 bool write = false;
187 #ifdef __WXMAC__
188 // AddDebugLogLineM( false, logFileIO, wxT("Reading gzip stream") );
190 gzFile inputFile = gzopen(filename2char(file), "rb");
191 if (inputFile != NULL) {
192 write = true;
194 while (int bytesRead = gzread(inputFile, buffer, sizeof(buffer))) {
195 if (bytesRead > 0) {
196 // AddDebugLogLineM( false, logFileIO, wxString::Format(wxT("Read %u bytes"), bytesRead) );
197 target.Write(buffer, bytesRead);
198 } else if (bytesRead < 0) {
199 wxString errString;
200 int gzerrnum;
201 const char* gzerrstr = gzerror(inputFile, &gzerrnum);
202 if (gzerrnum == Z_ERRNO) {
203 errString = wxSysErrorMsg();
204 } else {
205 errString = wxString::FromAscii(gzerrstr);
208 // AddDebugLogLineM( false, logFileIO, wxT("Error reading gzip stream (") + errString + wxT(")") );
209 write = false;
210 break;
214 // AddDebugLogLineM( false, logFileIO, wxT("End reading gzip stream") );
215 gzclose(inputFile);
216 } else {
217 // AddDebugLogLineM( false, logFileIO, wxT("Error opening gzip file (") + wxString(wxSysErrorMsg()) + wxT(")") );
219 #else
221 // AddDebugLogLineM( false, logFileIO, wxT("Reading gzip stream") );
223 wxFileInputStream source(file);
224 wxZlibInputStream inputStream(source);
226 while (!inputStream.Eof()) {
227 inputStream.Read(buffer, sizeof(buffer));
229 // AddDebugLogLineM( false, logFileIO, wxString::Format(wxT("Read %u bytes"),inputStream.LastRead()) );
230 if (inputStream.LastRead()) {
231 target.Write(buffer, inputStream.LastRead());
232 } else {
233 break;
237 // AddDebugLogLineM( false, logFileIO, wxT("End reading gzip stream") );
239 write = inputStream.IsOk() || inputStream.Eof();
241 #endif
243 if (write) {
244 target.Commit();
245 // AddDebugLogLineM( false, logFileIO, wxT("Commited gzip stream") );
248 return write;
252 UnpackResult UnpackArchive(const CPath& path, const wxChar* files[])
254 const wxString file = path.GetRaw();
256 // Attempt to discover the filetype and uncompress
257 EFileType type = GuessFiletype(file);
258 switch (type) {
259 case EFT_Zip:
260 if (UnpackZipFile(file, files)) {
261 // Unpack nested archives if needed.
262 return UnpackResult(true, UnpackArchive(path, files).second);
263 } else {
264 return UnpackResult(false, EFT_Error);
267 case EFT_GZip:
268 if (UnpackGZipFile(file)) {
269 // Unpack nested archives if needed.
270 return UnpackResult(true, UnpackArchive(path, files).second);
271 } else {
272 return UnpackResult(false, EFT_Error);
275 default:
276 return UnpackResult(false, type);
279 // File_checked_for_headers