| 235 | } |
| 236 | |
| 237 | bool FileSystem::Delete(Exception& ex, const char* path, size_t size, Mode mode) { |
| 238 | Status status; |
| 239 | if (!Stat(path, size, status)) |
| 240 | return true; // already deleted |
| 241 | |
| 242 | if (status.st_mode&S_IFDIR) { |
| 243 | if (!size) |
| 244 | path = "."; |
| 245 | if (mode==HEAVY) { |
| 246 | FileSystem::ForEach forEach([&ex](const string& path, UInt16 level) { |
| 247 | Delete(ex, path, HEAVY); |
| 248 | }); |
| 249 | Exception ignore; |
| 250 | ListFiles(ignore, path, forEach); |
| 251 | if (ignore) |
| 252 | return true;; // if exception it's a not existent folder |
| 253 | if (ex) // impossible to remove a sub file/folder, so the parent folder can't be removed too (keep the exact exception) |
| 254 | return false; |
| 255 | } |
| 256 | } |
| 257 | #if defined(_WIN32) |
| 258 | wchar_t wFile[_MAX_PATH]; |
| 259 | MultiByteToWideChar(CP_UTF8, 0, path, -1, wFile, _MAX_PATH); |
| 260 | if (status.st_mode&S_IFDIR) { |
| 261 | if (RemoveDirectoryW(wFile) || GetLastError() == ERROR_FILE_NOT_FOUND) |
| 262 | return true; |
| 263 | ex.set(Exception::FILE, "Impossible to remove folder ", path); |
| 264 | } else { |
| 265 | if (DeleteFileW(wFile) || GetLastError()==ERROR_FILE_NOT_FOUND) |
| 266 | return true; |
| 267 | ex.set(Exception::FILE, "Impossible to remove file ", path); |
| 268 | } |
| 269 | #else |
| 270 | if(status.st_mode&S_IFDIR) { |
| 271 | if(rmdir(path)==0 || errno==ENOENT) |
| 272 | return true; |
| 273 | ex.set(Exception::FILE, "Impossible to remove folder ", path); |
| 274 | } else { |
| 275 | if(unlink(path)==0 || errno==ENOENT) |
| 276 | return true; |
| 277 | ex.set(Exception::FILE, "Impossible to remove file ", path); |
| 278 | } |
| 279 | #endif |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | UInt32 FileSystem::ListFiles(Exception& ex, const char* path, const ForEach& forEach, Mode mode) { |
| 284 | string directory(*path ? path : "."); |