summaryrefslogtreecommitdiff
path: root/zip/ZipArchive/DirEnumerator.cpp
blob: 53185ee9ba8f4715ee2102681ca4d9f153b3c424 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
////////////////////////////////////////////////////////////////////////////////
// This source file is part of the ZipArchive Library Open Source distribution 
// and is Copyrighted 2000 - 2022 by Artpol Software - Tadeusz Dracz
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// For the licensing details refer to the License.txt file.
//
// Web Site: https://www.artpol-software.com
////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#if defined _MSC_VER && _MSC_VER < 1300
	// STL warnings
	#pragma warning (push, 3) 
#endif

#include "DirEnumerator.h"
#include "FileFilter.h"
#include "ZipAutoBuffer.h"

#include <queue>

#if defined __GNUC__ && !defined __MINGW32__
	#include <sys/stat.h>
	#include <dirent.h>	
#else
	#include <io.h>
	#ifdef __BORLANDC__
		#ifndef _tfindfirsti64
			#define _tfindfirsti64 __tfindfirsti64
		#endif
		#ifndef _tfindnexti64
			#define _tfindnexti64 __tfindnexti64
		#endif
		#ifndef _tfinddatai64_t
			#define _tfinddatai64_t __tfinddatai64_t
		#endif		
	#endif	
#endif

namespace ZipArchiveLib
{

#if defined __GNUC__ && !defined __MINGW32__
	#define ZIP_ENUMERATOR_FOR_GNUC
#endif

#ifdef ZIP_ENUMERATOR_FOR_GNUC
	class CFindHandleReleaser
	{
		DIR* m_handle;
	public: 
		CFindHandleReleaser(DIR* handle)
			:m_handle(handle)
		{
		}				
		~CFindHandleReleaser()
		{
			closedir(m_handle);
		}
	};
#else
	class CFindHandleReleaser
	{
#if _MSC_VER > 1200
		intptr_t m_handle;
#else
		long m_handle;
#endif
	public: 
#if _MSC_VER > 1200
		CFindHandleReleaser(intptr_t handle)
#else
		CFindHandleReleaser(long handle)
#endif		
			:m_handle(handle)
		{
		}				
		~CFindHandleReleaser()
		{
			_findclose(m_handle);
		}
	};
#endif


bool CDirEnumerator::Start(CFileFilter& filter)
{	
	OnEnumerationBegin();
	std::queue<CZipString> dirs;
	dirs.push(CZipString(m_lpszDirectory));
	bool ret = true;
	do
	{
		m_szCurrentDirectory = dirs.front();
		dirs.pop();
		CZipPathComponent::AppendSeparator(m_szCurrentDirectory);
		EnterDirectory();

#ifdef ZIP_ENUMERATOR_FOR_GNUC
		_ZIP_WIDE_TO_MULTIBYTE(m_szCurrentDirectory, currentDirectory)
                              
		DIR* dp = opendir(currentDirectory);
		if (dp)
		{
			CFindHandleReleaser handleReleaser(dp);
			while (true)
			{
				struct dirent* entry = readdir(dp);
				if (!entry)
					break;	
				LPCTSTR name;
        #ifdef _UNICODE
                CZipString nameWide;                                
                ZipPlatform::MultiByteToWide(entry->d_name, -1, nameWide, 0);
                name = nameWide;
        #else
                name = entry->d_name;
        #endif                                
				CZipString path(m_szCurrentDirectory + name);
				_ZIP_WIDE_TO_MULTIBYTE(path, pathMultiByte)
                                
	#if !defined __APPLE__ && !defined __CYGWIN__
				struct stat64 sStats;
				if (stat64(pathMultiByte, &sStats) == -1)
	#else
				struct stat sStats;
				if (stat(pathMultiByte, &sStats) == -1)
	#endif
					continue;
				
				CFileInfo info;
				info.m_uAttributes = sStats.st_mode;				
				
#else
		CZipString szFullFileName = m_szCurrentDirectory + _T("*");
		
		_tfinddatai64_t ffInfo;
#if _MSC_VER > 1200
		intptr_t hFile;
#else
		long hFile;
#endif
		if( (hFile = _tfindfirsti64( (LPTSTR)(LPCTSTR)szFullFileName, &ffInfo )) != -1L )
		{
			CFindHandleReleaser handleReleaser(hFile);
			do
			{				
				LPCTSTR name = ffInfo.name;
				CFileInfo info;
				info.m_uAttributes = ffInfo.attrib;				
#endif				
				bool isDir;
				if (ZipPlatform::IsDirectory(info.m_uAttributes))
				{
					if (!m_bRecursive || IsDots(name))
						continue;					
					isDir = true;
				}
				else
					isDir = false;									

#ifdef ZIP_ENUMERATOR_FOR_GNUC
				info.m_uSize  = (ZIP_FILE_USIZE)sStats.st_size;
				info.m_tCreationTime = sStats.st_ctime;
				info.m_tModificationTime = sStats.st_mtime;
				info.m_tLastAccessTime = sStats.st_atime;				
#else
				info.m_uSize = (ZIP_FILE_USIZE)ffInfo.size;
				info.m_tCreationTime = ffInfo.time_create;
				info.m_tModificationTime = ffInfo.time_write;
				info.m_tLastAccessTime = ffInfo.time_access;	
				CZipString path(m_szCurrentDirectory + ffInfo.name);
#endif				
				
				if (isDir)
				{
					bool bAllow;
					if (filter.HandlesFile(info))
						bAllow = filter.Evaluate(path, name, info) && Process(path, info);
					else
						// examine directory, if the filter cannot decide
						bAllow = true;

					if (bAllow)
						dirs.push(path);
				}
				else 
				{
					bool bAllow;
					if (filter.HandlesFile(info))
						bAllow = filter.Evaluate(path, name, info);
					else
						// skip file, if the filter cannot decide
						bAllow = false;

					if (bAllow && !Process(path, info))
					{
						ret = false;
						break;
					}
				}

#ifdef ZIP_ENUMERATOR_FOR_GNUC
			}			
		}		
#else
			}
			while (_tfindnexti64(hFile, &ffInfo) == 0L);			
		}		
#endif		
		ExitDirectory();
	}
	while(!dirs.empty() && ret);
	OnEnumerationEnd(ret);
	return ret;
}

bool CDirEnumerator::IsDots(LPCTSTR lpszName)
{
	CZipString name(lpszName);
	return name.Compare(_T(".")) == 0 || name.Compare(_T("..")) == 0;
}

} // namespace

#if defined _MSC_VER && _MSC_VER < 1300
	// STL warnings 
	#pragma warning (pop) 
#endif