|
1 /* |
|
2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 #ifndef FILEUTILS_H |
|
27 #define FILEUTILS_H |
|
28 |
|
29 |
|
30 #include <fstream> |
|
31 #include "SysInfo.h" |
|
32 |
|
33 |
|
34 namespace FileUtils { |
|
35 |
|
36 // Returns 'true' if the given character is a path separator. |
|
37 bool isDirSeparator(const tstring::value_type c); |
|
38 |
|
39 // checks if the file or directory exists |
|
40 bool isFileExists(const tstring &filePath); |
|
41 |
|
42 // checks is the specified file is a directory |
|
43 // returns false if the path does not exist |
|
44 bool isDirectory(const tstring &filePath); |
|
45 |
|
46 //checks if the specified directory is not empty |
|
47 //returns true if the path is an existing directory and it contains at least one file other than "." or "..". |
|
48 bool isDirectoryNotEmpty(const tstring &dirPath); |
|
49 |
|
50 // returns directory part of the path. |
|
51 // returns empty string if the path contains only filename. |
|
52 // if the path ends with slash/backslash, returns removeTrailingSlashes(path). |
|
53 tstring dirname(const tstring &path); |
|
54 |
|
55 // returns basename part of the path |
|
56 // if the path ends with slash/backslash, returns empty string. |
|
57 tstring basename(const tstring &path); |
|
58 |
|
59 /** |
|
60 * Translates forward slashes to back slashes and returns lower case version |
|
61 * of the given string. |
|
62 */ |
|
63 tstring normalizePath(tstring v); |
|
64 |
|
65 // Returns suffix of the path. If the given path has a suffix the first |
|
66 // character of the return value is '.'. |
|
67 // Otherwise return value if empty string. |
|
68 tstring suffix(const tstring &path); |
|
69 |
|
70 // combines two strings into a path |
|
71 tstring combinePath(const tstring& parent, const tstring& child); |
|
72 |
|
73 // removes trailing slashes and backslashes in the path if any |
|
74 tstring removeTrailingSlash(const tstring& path); |
|
75 |
|
76 // Creates a file with unique name in the specified base directory, throws an exception if operation fails |
|
77 // path is constructed as <prefix><random number><suffix>. |
|
78 // The function fails and throws exception if 'path' doesn't exist. |
|
79 tstring createTempFile(const tstring &prefix = _T(""), const tstring &suffix = _T(".tmp"), const tstring &path=SysInfo::getTempDir()); |
|
80 |
|
81 // Creates a directory with unique name in the specified base directory, throws an exception if operation fails |
|
82 // path is constructed as <prefix><random number><suffix> |
|
83 // The function fails and throws exception if 'path' doesn't exist. |
|
84 tstring createTempDirectory(const tstring &prefix = _T(""), const tstring &suffix = _T(".tmp"), const tstring &basedir=SysInfo::getTempDir()); |
|
85 |
|
86 // If the file referenced with "prototype" parameter DOES NOT exist, the return |
|
87 // value is the given path. No new files created. |
|
88 // Otherwise the function creates another file in the same directory as |
|
89 // the given file with the same suffix and with the basename from the |
|
90 // basename of the given file with some random chars appended to ensure |
|
91 // created file is unique. |
|
92 tstring createUniqueFile(const tstring &prototype); |
|
93 |
|
94 // Creates directory and subdirectories if don't exist. |
|
95 // Currently supports only "standard" path like "c:\bla-bla" |
|
96 // If 'createdDirs' parameter is not NULL, the given array is appended with |
|
97 // all subdirectories created by this function call. |
|
98 void createDirectory(const tstring &path, tstring_array* createdDirs=0); |
|
99 |
|
100 // copies file from fromPath to toPath. Creates output directory if doesn't exist. |
|
101 void copyFile(const tstring& fromPath, const tstring& toPath, bool failIfExists); |
|
102 |
|
103 // moves file from fromPath to toPath. Creates output directory if doesn't exist. |
|
104 void moveFile(const tstring& fromPath, const tstring& toPath, bool failIfExists); |
|
105 |
|
106 // Throws exception if fails to delete specified 'path'. |
|
107 // Exits normally if 'path' doesn't exist or it has been deleted. |
|
108 // Attempts to strip R/O attribute if delete fails and retry delete. |
|
109 void deleteFile(const tstring &path); |
|
110 // Returns 'false' if fails to delete specified 'path'. |
|
111 // Returns 'true' if 'path' doesn't exist or it has been deleted. |
|
112 // Attempts to strip R/O attribute if delete fails and retry delete. |
|
113 bool deleteFile(const tstring &path, const std::nothrow_t &) throw(); |
|
114 |
|
115 // Like deleteFile(), but applies to directories. |
|
116 void deleteDirectory(const tstring &path); |
|
117 bool deleteDirectory(const tstring &path, const std::nothrow_t &) throw(); |
|
118 |
|
119 // Deletes all files (not subdirectories) from the specified directory. |
|
120 // Exits normally if all files in 'dirPath' have been deleted or if |
|
121 // 'dirPath' doesn't exist. |
|
122 // Throws exception if 'dirPath' references existing file system object |
|
123 // which is not a directory or when the first failure of file delete |
|
124 // occurs. |
|
125 void deleteFilesInDirectory(const tstring &dirPath); |
|
126 // Deletes all files (not subdirectories) from the specified directory. |
|
127 // Returns 'true' normally if all files in 'dirPath' have been deleted or |
|
128 // if 'dirPath' doesn't exist. |
|
129 // Returns 'false' if 'dirPath' references existing file system object |
|
130 // which is not a directory or if failed to delete one ore more files in |
|
131 // 'dirPath' directory. |
|
132 // Doesn't abort iteration over files if the given directory after the |
|
133 // first failure to delete a file. |
|
134 bool deleteFilesInDirectory(const tstring &dirPath, const std::nothrow_t &) throw(); |
|
135 // Like deleteFilesInDirectory, but deletes subdirectories as well |
|
136 void deleteDirectoryRecursive(const tstring &dirPath); |
|
137 bool deleteDirectoryRecursive(const tstring &dirPath, const std::nothrow_t &) throw(); |
|
138 |
|
139 class DirectoryCallback { |
|
140 public: |
|
141 virtual ~DirectoryCallback() {}; |
|
142 |
|
143 virtual bool onFile(const tstring& path) { |
|
144 return true; |
|
145 } |
|
146 virtual bool onDirectory(const tstring& path) { |
|
147 return true; |
|
148 } |
|
149 }; |
|
150 |
|
151 // Calls the given callback for every file and subdirectory of |
|
152 // the given directory. |
|
153 void iterateDirectory(const tstring &dirPath, DirectoryCallback& callback); |
|
154 |
|
155 /** |
|
156 * Replace file suffix, example replaceSuffix("file/path.txt", ".csv") |
|
157 * @param path file path to replace suffix |
|
158 * @param suffix new suffix for path |
|
159 * @return return file path with new suffix |
|
160 */ |
|
161 tstring replaceSuffix(const tstring& path, const tstring& suffix=tstring()); |
|
162 |
|
163 class DirectoryIterator: DirectoryCallback { |
|
164 public: |
|
165 DirectoryIterator(const tstring& root=tstring()): root(root) { |
|
166 recurse().withFiles().withFolders(); |
|
167 } |
|
168 |
|
169 DirectoryIterator& recurse(bool v=true) { |
|
170 theRecurse = v; |
|
171 return *this; |
|
172 } |
|
173 |
|
174 DirectoryIterator& withFiles(bool v=true) { |
|
175 theWithFiles = v; |
|
176 return *this; |
|
177 } |
|
178 |
|
179 DirectoryIterator& withFolders(bool v=true) { |
|
180 theWithFolders = v; |
|
181 return *this; |
|
182 } |
|
183 |
|
184 tstring_array findItems() { |
|
185 tstring_array reply; |
|
186 findItems(reply); |
|
187 return reply; |
|
188 } |
|
189 |
|
190 DirectoryIterator& findItems(tstring_array& v); |
|
191 |
|
192 private: |
|
193 virtual bool onFile(const tstring& path); |
|
194 virtual bool onDirectory(const tstring& path); |
|
195 |
|
196 private: |
|
197 bool theRecurse; |
|
198 bool theWithFiles; |
|
199 bool theWithFolders; |
|
200 tstring root; |
|
201 tstring_array items; |
|
202 }; |
|
203 |
|
204 // Returns array of all the files/sub-folders from the given directory, empty array if basedir is not a directory. The returned |
|
205 // array is ordered from top down (i.e. dirs are listed first followed by subfolders and files). |
|
206 // Order of subfolders and files is undefined but usually they are sorted by names. |
|
207 inline tstring_array listAllContents(const tstring& basedir) { |
|
208 return DirectoryIterator(basedir).findItems(); |
|
209 } |
|
210 |
|
211 // Helper to construct path from multiple components. |
|
212 // |
|
213 // Sample usage: |
|
214 // Construct "c:\Program Files\Java" string from three components |
|
215 // |
|
216 // tstring path = FileUtils::mkpath() << _T("c:") |
|
217 // << _T("Program Files") |
|
218 // << _T("Java"); |
|
219 // |
|
220 class mkpath { |
|
221 public: |
|
222 operator const tstring& () const { |
|
223 return path; |
|
224 } |
|
225 |
|
226 mkpath& operator << (const tstring& p) { |
|
227 path = combinePath(path, p); |
|
228 return *this; |
|
229 } |
|
230 |
|
231 // mimic std::string |
|
232 const tstring::value_type* c_str() const { |
|
233 return path.c_str(); |
|
234 } |
|
235 private: |
|
236 tstring path; |
|
237 }; |
|
238 |
|
239 struct Directory { |
|
240 Directory() { |
|
241 } |
|
242 |
|
243 Directory(const tstring &parent, const tstring &subdir) : parent(parent), subdir(subdir) { |
|
244 } |
|
245 |
|
246 operator tstring () const { |
|
247 return getPath(); |
|
248 } |
|
249 |
|
250 tstring getPath() const { |
|
251 return combinePath(parent, subdir); |
|
252 } |
|
253 |
|
254 bool empty() const { |
|
255 return (parent.empty() && subdir.empty()); |
|
256 } |
|
257 |
|
258 tstring parent; |
|
259 tstring subdir; |
|
260 }; |
|
261 |
|
262 // Deletes list of files and directories in batch mode. |
|
263 // Registered files and directories are deleted when destructor is called. |
|
264 // Order or delete operations is following: |
|
265 // - delete items registered with appendFile() calls; |
|
266 // - delete items registered with appendAllFilesInDirectory() calls; |
|
267 // - delete items registered with appendRecursiveDirectory() calls; |
|
268 // - delete items registered with appendEmptyDirectory() calls. |
|
269 class Deleter { |
|
270 public: |
|
271 Deleter() { |
|
272 } |
|
273 |
|
274 ~Deleter() { |
|
275 execute(); |
|
276 } |
|
277 |
|
278 typedef std::pair<tstring, int> Path; |
|
279 typedef std::vector<Path> Paths; |
|
280 |
|
281 /** |
|
282 * Appends all records from the given deleter Deleter into this Deleter |
|
283 * instance. On success array with records in the passed in Deleter |
|
284 * instance is emptied. |
|
285 */ |
|
286 Deleter& appendFrom(Deleter& other) { |
|
287 Paths tmp(paths); |
|
288 tmp.insert(tmp.end(), other.paths.begin(), other.paths.end()); |
|
289 Paths empty; |
|
290 other.paths.swap(empty); |
|
291 paths.swap(tmp); |
|
292 return *this; |
|
293 } |
|
294 |
|
295 // Schedule file for deletion. |
|
296 Deleter& appendFile(const tstring& path); |
|
297 |
|
298 // Schedule files for deletion. |
|
299 template <class It> |
|
300 Deleter& appendFiles(It b, It e) { |
|
301 for (It it = b; it != e; ++it) { |
|
302 appendFile(*it); |
|
303 } |
|
304 return *this; |
|
305 } |
|
306 |
|
307 // Schedule files for deletion in the given directory. |
|
308 template <class It> |
|
309 Deleter& appendFiles(const tstring& dirname, It b, It e) { |
|
310 for (It it = b; it != e; ++it) { |
|
311 appendFile(FileUtils::mkpath() << dirname << *it); |
|
312 } |
|
313 return *this; |
|
314 } |
|
315 |
|
316 // Schedule empty directory for deletion with empty roots (up to Directory.parent). |
|
317 Deleter& appendEmptyDirectory(const Directory& dir); |
|
318 |
|
319 // Schedule empty directory for deletion without roots. |
|
320 // This is a particular case of appendEmptyDirectory(const Directory& dir) |
|
321 // with Directory(dirname(path), basename(path)). |
|
322 Deleter& appendEmptyDirectory(const tstring& path); |
|
323 |
|
324 // Schedule all file from the given directory for deletion. |
|
325 Deleter& appendAllFilesInDirectory(const tstring& path); |
|
326 |
|
327 // Schedule directory for recursive deletion. |
|
328 Deleter& appendRecursiveDirectory(const tstring& path); |
|
329 |
|
330 void cancel() { |
|
331 paths.clear(); |
|
332 } |
|
333 |
|
334 // Deletes scheduled files and directories. After this function |
|
335 // is called internal list of scheduled items is emptied. |
|
336 void execute(); |
|
337 |
|
338 private: |
|
339 Paths paths; |
|
340 }; |
|
341 |
|
342 |
|
343 /** |
|
344 * Helper to write chunks of data into binary file. |
|
345 * Creates temporary file in the same folder with destination file. |
|
346 * All subsequent requests to save data chunks are redirected to temporary |
|
347 * file. finalize() method closes temporary file stream and renames |
|
348 * temporary file. |
|
349 * If finalize() method is not called, temporary file is deleted in |
|
350 * ~FileWriter(), destination file is not touched. |
|
351 */ |
|
352 class FileWriter { |
|
353 public: |
|
354 explicit FileWriter(const tstring& path); |
|
355 |
|
356 FileWriter& write(const void* buf, size_t bytes); |
|
357 |
|
358 template <class Ctnr> |
|
359 FileWriter& write(const Ctnr& buf) { |
|
360 return write(buf.data(), |
|
361 buf.size() * sizeof(typename Ctnr::value_type)); |
|
362 } |
|
363 |
|
364 void finalize(); |
|
365 |
|
366 private: |
|
367 // Not accessible by design! |
|
368 FileWriter& write(const std::wstring& str); |
|
369 |
|
370 private: |
|
371 tstring tmpFile; |
|
372 Deleter cleaner; |
|
373 std::ofstream tmp; |
|
374 tstring dstPath; |
|
375 }; |
|
376 } // FileUtils |
|
377 |
|
378 #endif // FILEUTILS_H |