diff options
Diffstat (limited to 'CPP/Windows/FileFind.h')
-rw-r--r-- | CPP/Windows/FileFind.h | 305 |
1 files changed, 305 insertions, 0 deletions
diff --git a/CPP/Windows/FileFind.h b/CPP/Windows/FileFind.h new file mode 100644 index 0000000..8f28ee3 --- /dev/null +++ b/CPP/Windows/FileFind.h | |||
@@ -0,0 +1,305 @@ | |||
1 | // Windows/FileFind.h | ||
2 | |||
3 | #ifndef __WINDOWS_FILE_FIND_H | ||
4 | #define __WINDOWS_FILE_FIND_H | ||
5 | |||
6 | #ifndef _WIN32 | ||
7 | #include <sys/stat.h> | ||
8 | #include <sys/types.h> | ||
9 | #include <dirent.h> | ||
10 | #endif | ||
11 | |||
12 | #include "../Common/MyString.h" | ||
13 | #include "../Common/MyWindows.h" | ||
14 | #include "Defs.h" | ||
15 | |||
16 | namespace NWindows { | ||
17 | namespace NFile { | ||
18 | namespace NFind { | ||
19 | |||
20 | // bool DoesFileExist(CFSTR name, bool followLink); | ||
21 | bool DoesFileExist_Raw(CFSTR name); | ||
22 | bool DoesFileExist_FollowLink(CFSTR name); | ||
23 | bool DoesDirExist(CFSTR name, bool followLink); | ||
24 | |||
25 | inline bool DoesDirExist(CFSTR name) | ||
26 | { return DoesDirExist(name, false); } | ||
27 | inline bool DoesDirExist_FollowLink(CFSTR name) | ||
28 | { return DoesDirExist(name, true); } | ||
29 | |||
30 | // it's always _Raw | ||
31 | bool DoesFileOrDirExist(CFSTR name); | ||
32 | |||
33 | DWORD GetFileAttrib(CFSTR path); | ||
34 | |||
35 | |||
36 | namespace NAttributes | ||
37 | { | ||
38 | inline bool IsReadOnly(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_READONLY) != 0; } | ||
39 | inline bool IsHidden(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_HIDDEN) != 0; } | ||
40 | inline bool IsSystem(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_SYSTEM) != 0; } | ||
41 | inline bool IsDir(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0; } | ||
42 | inline bool IsArchived(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_ARCHIVE) != 0; } | ||
43 | inline bool IsCompressed(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_COMPRESSED) != 0; } | ||
44 | inline bool IsEncrypted(DWORD attrib) { return (attrib & FILE_ATTRIBUTE_ENCRYPTED) != 0; } | ||
45 | } | ||
46 | |||
47 | class CFileInfoBase | ||
48 | { | ||
49 | bool MatchesMask(UINT32 mask) const { return ((Attrib & mask) != 0); } | ||
50 | public: | ||
51 | UInt64 Size; | ||
52 | FILETIME CTime; | ||
53 | FILETIME ATime; | ||
54 | FILETIME MTime; | ||
55 | DWORD Attrib; | ||
56 | bool IsAltStream; | ||
57 | bool IsDevice; | ||
58 | |||
59 | #ifdef _WIN32 | ||
60 | /* | ||
61 | #ifdef UNDER_CE | ||
62 | DWORD ObjectID; | ||
63 | #else | ||
64 | UINT32 ReparseTag; | ||
65 | #endif | ||
66 | */ | ||
67 | #else | ||
68 | dev_t dev; | ||
69 | ino_t ino; | ||
70 | nlink_t nlink; | ||
71 | mode_t mode; | ||
72 | // bool Use_lstat; | ||
73 | #endif | ||
74 | |||
75 | CFileInfoBase() { ClearBase(); } | ||
76 | void ClearBase() throw(); | ||
77 | |||
78 | void SetAsDir() | ||
79 | { | ||
80 | Attrib = FILE_ATTRIBUTE_DIRECTORY; | ||
81 | #ifndef _WIN32 | ||
82 | Attrib |= (FILE_ATTRIBUTE_UNIX_EXTENSION + (S_IFDIR << 16)); | ||
83 | #endif | ||
84 | } | ||
85 | |||
86 | bool IsArchived() const { return MatchesMask(FILE_ATTRIBUTE_ARCHIVE); } | ||
87 | bool IsCompressed() const { return MatchesMask(FILE_ATTRIBUTE_COMPRESSED); } | ||
88 | bool IsDir() const { return MatchesMask(FILE_ATTRIBUTE_DIRECTORY); } | ||
89 | bool IsEncrypted() const { return MatchesMask(FILE_ATTRIBUTE_ENCRYPTED); } | ||
90 | bool IsHidden() const { return MatchesMask(FILE_ATTRIBUTE_HIDDEN); } | ||
91 | bool IsNormal() const { return MatchesMask(FILE_ATTRIBUTE_NORMAL); } | ||
92 | bool IsOffline() const { return MatchesMask(FILE_ATTRIBUTE_OFFLINE); } | ||
93 | bool IsReadOnly() const { return MatchesMask(FILE_ATTRIBUTE_READONLY); } | ||
94 | bool HasReparsePoint() const { return MatchesMask(FILE_ATTRIBUTE_REPARSE_POINT); } | ||
95 | bool IsSparse() const { return MatchesMask(FILE_ATTRIBUTE_SPARSE_FILE); } | ||
96 | bool IsSystem() const { return MatchesMask(FILE_ATTRIBUTE_SYSTEM); } | ||
97 | bool IsTemporary() const { return MatchesMask(FILE_ATTRIBUTE_TEMPORARY); } | ||
98 | |||
99 | #ifndef _WIN32 | ||
100 | bool IsPosixLink() const | ||
101 | { | ||
102 | const UInt32 mod = Attrib >> 16; | ||
103 | return S_ISLNK(mod); | ||
104 | } | ||
105 | #endif | ||
106 | |||
107 | bool IsOsSymLink() const | ||
108 | { | ||
109 | #ifdef _WIN32 | ||
110 | return HasReparsePoint(); | ||
111 | #else | ||
112 | return IsPosixLink(); | ||
113 | #endif | ||
114 | } | ||
115 | }; | ||
116 | |||
117 | struct CFileInfo: public CFileInfoBase | ||
118 | { | ||
119 | FString Name; | ||
120 | #if defined(_WIN32) && !defined(UNDER_CE) | ||
121 | // FString ShortName; | ||
122 | #endif | ||
123 | |||
124 | bool IsDots() const throw(); | ||
125 | bool Find(CFSTR path, bool followLink = false); | ||
126 | bool Find_FollowLink(CFSTR path) { return Find(path, true); } | ||
127 | |||
128 | #ifdef _WIN32 | ||
129 | bool Fill_From_ByHandleFileInfo(CFSTR path); | ||
130 | // bool FollowReparse(CFSTR path, bool isDir); | ||
131 | #else | ||
132 | bool Find_DontFill_Name(CFSTR path, bool followLink = false); | ||
133 | void SetFrom_stat(const struct stat &st); | ||
134 | #endif | ||
135 | }; | ||
136 | |||
137 | |||
138 | #ifdef _WIN32 | ||
139 | |||
140 | class CFindFileBase MY_UNCOPYABLE | ||
141 | { | ||
142 | protected: | ||
143 | HANDLE _handle; | ||
144 | public: | ||
145 | bool IsHandleAllocated() const { return _handle != INVALID_HANDLE_VALUE; } | ||
146 | CFindFileBase(): _handle(INVALID_HANDLE_VALUE) {} | ||
147 | ~CFindFileBase() { Close(); } | ||
148 | bool Close() throw(); | ||
149 | }; | ||
150 | |||
151 | class CFindFile: public CFindFileBase | ||
152 | { | ||
153 | public: | ||
154 | bool FindFirst(CFSTR wildcard, CFileInfo &fileInfo); | ||
155 | bool FindNext(CFileInfo &fileInfo); | ||
156 | }; | ||
157 | |||
158 | #if defined(_WIN32) && !defined(UNDER_CE) | ||
159 | |||
160 | struct CStreamInfo | ||
161 | { | ||
162 | UString Name; | ||
163 | UInt64 Size; | ||
164 | |||
165 | UString GetReducedName() const; // returns ":Name" | ||
166 | // UString GetReducedName2() const; // returns "Name" | ||
167 | bool IsMainStream() const throw(); | ||
168 | }; | ||
169 | |||
170 | class CFindStream: public CFindFileBase | ||
171 | { | ||
172 | public: | ||
173 | bool FindFirst(CFSTR filePath, CStreamInfo &streamInfo); | ||
174 | bool FindNext(CStreamInfo &streamInfo); | ||
175 | }; | ||
176 | |||
177 | class CStreamEnumerator MY_UNCOPYABLE | ||
178 | { | ||
179 | CFindStream _find; | ||
180 | FString _filePath; | ||
181 | |||
182 | bool NextAny(CFileInfo &fileInfo, bool &found); | ||
183 | public: | ||
184 | CStreamEnumerator(const FString &filePath): _filePath(filePath) {} | ||
185 | bool Next(CStreamInfo &streamInfo, bool &found); | ||
186 | }; | ||
187 | |||
188 | #endif // defined(_WIN32) && !defined(UNDER_CE) | ||
189 | |||
190 | |||
191 | class CEnumerator MY_UNCOPYABLE | ||
192 | { | ||
193 | CFindFile _findFile; | ||
194 | FString _wildcard; | ||
195 | |||
196 | bool NextAny(CFileInfo &fileInfo); | ||
197 | public: | ||
198 | void SetDirPrefix(const FString &dirPrefix); | ||
199 | bool Next(CFileInfo &fileInfo); | ||
200 | bool Next(CFileInfo &fileInfo, bool &found); | ||
201 | }; | ||
202 | |||
203 | |||
204 | class CFindChangeNotification MY_UNCOPYABLE | ||
205 | { | ||
206 | HANDLE _handle; | ||
207 | public: | ||
208 | operator HANDLE () { return _handle; } | ||
209 | bool IsHandleAllocated() const { return _handle != INVALID_HANDLE_VALUE && _handle != 0; } | ||
210 | CFindChangeNotification(): _handle(INVALID_HANDLE_VALUE) {} | ||
211 | ~CFindChangeNotification() { Close(); } | ||
212 | bool Close() throw(); | ||
213 | HANDLE FindFirst(CFSTR pathName, bool watchSubtree, DWORD notifyFilter); | ||
214 | bool FindNext() { return BOOLToBool(::FindNextChangeNotification(_handle)); } | ||
215 | }; | ||
216 | |||
217 | #ifndef UNDER_CE | ||
218 | bool MyGetLogicalDriveStrings(CObjectVector<FString> &driveStrings); | ||
219 | #endif | ||
220 | |||
221 | typedef CFileInfo CDirEntry; | ||
222 | |||
223 | |||
224 | #else // WIN32 | ||
225 | |||
226 | |||
227 | struct CDirEntry | ||
228 | { | ||
229 | ino_t iNode; | ||
230 | #if !defined(_AIX) | ||
231 | Byte Type; | ||
232 | #endif | ||
233 | FString Name; | ||
234 | |||
235 | /* | ||
236 | #if !defined(_AIX) | ||
237 | bool IsDir() const | ||
238 | { | ||
239 | // (Type == DT_UNKNOWN) on some systems | ||
240 | return Type == DT_DIR; | ||
241 | } | ||
242 | #endif | ||
243 | */ | ||
244 | |||
245 | bool IsDots() const throw(); | ||
246 | }; | ||
247 | |||
248 | class CEnumerator MY_UNCOPYABLE | ||
249 | { | ||
250 | DIR *_dir; | ||
251 | FString _wildcard; | ||
252 | |||
253 | bool NextAny(CDirEntry &fileInfo, bool &found); | ||
254 | public: | ||
255 | CEnumerator(): _dir(NULL) {} | ||
256 | ~CEnumerator(); | ||
257 | void SetDirPrefix(const FString &dirPrefix); | ||
258 | |||
259 | bool Next(CDirEntry &fileInfo, bool &found); | ||
260 | bool Fill_FileInfo(const CDirEntry &de, CFileInfo &fileInfo, bool followLink) const; | ||
261 | bool DirEntry_IsDir(const CDirEntry &de, bool followLink) const | ||
262 | { | ||
263 | #if !defined(_AIX) | ||
264 | if (de.Type == DT_DIR) | ||
265 | return true; | ||
266 | if (de.Type != DT_UNKNOWN) | ||
267 | return false; | ||
268 | #endif | ||
269 | CFileInfo fileInfo; | ||
270 | if (Fill_FileInfo(de, fileInfo, followLink)) | ||
271 | { | ||
272 | return fileInfo.IsDir(); | ||
273 | } | ||
274 | return false; // change it | ||
275 | } | ||
276 | }; | ||
277 | |||
278 | /* | ||
279 | inline UInt32 Get_WinAttrib_From_PosixMode(UInt32 mode) | ||
280 | { | ||
281 | UInt32 attrib = S_ISDIR(mode) ? | ||
282 | FILE_ATTRIBUTE_DIRECTORY : | ||
283 | FILE_ATTRIBUTE_ARCHIVE; | ||
284 | if ((st.st_mode & 0222) == 0) // check it !!! | ||
285 | attrib |= FILE_ATTRIBUTE_READONLY; | ||
286 | return attrib; | ||
287 | } | ||
288 | */ | ||
289 | |||
290 | UInt32 Get_WinAttribPosix_From_PosixMode(UInt32 mode); | ||
291 | |||
292 | // UInt32 Get_WinAttrib_From_stat(const struct stat &st); | ||
293 | #if defined(_AIX) | ||
294 | #define MY_ST_TIMESPEC st_timespec | ||
295 | #else | ||
296 | #define MY_ST_TIMESPEC timespec | ||
297 | #endif | ||
298 | |||
299 | void timespec_To_FILETIME(const MY_ST_TIMESPEC &ts, FILETIME &ft); | ||
300 | |||
301 | #endif // WIN32 | ||
302 | |||
303 | }}} | ||
304 | |||
305 | #endif | ||