2 // This file is part of the aMule Project.
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 )
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
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.
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
35 #include <zlib.h> // Do_not_auto_remove
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
)
55 CDirIterator::~CDirIterator()
60 CPath
CDirIterator::GetFirstFile(FileType type
, const wxString
& mask
)
67 if (!GetFirst(&fileName
, mask
, type
)) {
71 return CPath(fileName
);
75 CPath
CDirIterator::GetNextFile()
78 if (!GetNext(&fileName
)) {
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()) {
100 int read
= archive
.Read(head
, std::min
<off_t
>(10, archive
.Length()));
102 if (read
== wxInvalidOffset
) {
104 } else if ((head
[0] == 'P') && (head
[1] == 'K')) {
105 // Zip-archives have a header of "PK".
107 } else if (head
[0] == '\x1F' && head
[1] == '\x8B') {
108 // Gzip-archives have a header of 0x1F8B
110 } else if (head
[0] == '\xE0' || head
[0] == '\x0E') {
111 // MET files have either of these headers
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
])) {
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
) {
151 wxString name
= entry
->GetName();
152 // read 'zip' to access the entry's data
153 if (name
== filename
) {
156 zip
.Read(buffer
, sizeof(buffer
));
157 target
.Write(buffer
, zip
.LastRead());
165 filename
= archive
.FindNext();
168 if (target
.Length()) {
178 * Unpacks a GZip file and replaces the archive.
180 bool UnpackGZipFile(const wxString
& file
)
183 wxTempFile
target(file
);
188 // AddDebugLogLineM( false, logFileIO, wxT("Reading gzip stream") );
190 gzFile inputFile
= gzopen(filename2char(file
), "rb");
191 if (inputFile
!= NULL
) {
194 while (int bytesRead
= gzread(inputFile
, buffer
, sizeof(buffer
))) {
196 // AddDebugLogLineM( false, logFileIO, wxString::Format(wxT("Read %u bytes"), bytesRead) );
197 target
.Write(buffer
, bytesRead
);
198 } else if (bytesRead
< 0) {
201 const char* gzerrstr
= gzerror(inputFile
, &gzerrnum
);
202 if (gzerrnum
== Z_ERRNO
) {
203 errString
= wxSysErrorMsg();
205 errString
= wxString::FromAscii(gzerrstr
);
208 // AddDebugLogLineM( false, logFileIO, wxT("Error reading gzip stream (") + errString + wxT(")") );
214 // AddDebugLogLineM( false, logFileIO, wxT("End reading gzip stream") );
217 // AddDebugLogLineM( false, logFileIO, wxT("Error opening gzip file (") + wxString(wxSysErrorMsg()) + wxT(")") );
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());
237 // AddDebugLogLineM( false, logFileIO, wxT("End reading gzip stream") );
239 write
= inputStream
.IsOk() || inputStream
.Eof();
245 // AddDebugLogLineM( false, logFileIO, wxT("Commited gzip stream") );
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
);
260 if (UnpackZipFile(file
, files
)) {
261 // Unpack nested archives if needed.
262 return UnpackResult(true, UnpackArchive(path
, files
).second
);
264 return UnpackResult(false, EFT_Error
);
268 if (UnpackGZipFile(file
)) {
269 // Unpack nested archives if needed.
270 return UnpackResult(true, UnpackArchive(path
, files
).second
);
272 return UnpackResult(false, EFT_Error
);
276 return UnpackResult(false, type
);
279 // File_checked_for_headers