| 1 | #include <base/fs.h> |
| 2 | #include <base/log.h> |
| 3 | #include <base/system.h> |
| 4 | #include <base/types.h> |
| 5 | #include <base/windows.h> |
| 6 | |
| 7 | #include <cerrno> |
| 8 | #include <cstring> |
| 9 | |
| 10 | #if defined(CONF_FAMILY_UNIX) |
| 11 | #include <sys/stat.h> |
| 12 | #include <unistd.h> |
| 13 | |
| 14 | #elif defined(CONF_FAMILY_WINDOWS) |
| 15 | #include <io.h> |
| 16 | #include <windows.h> |
| 17 | |
| 18 | #include <string> |
| 19 | #else |
| 20 | #error NOT IMPLEMENTED |
| 21 | #endif |
| 22 | |
| 23 | int fs_makedir(const char *path) |
| 24 | { |
| 25 | #if defined(CONF_FAMILY_WINDOWS) |
| 26 | const std::wstring wide_path = windows_utf8_to_wide(path); |
| 27 | if(CreateDirectoryW(wide_path.c_str(), nullptr) != 0) |
| 28 | { |
| 29 | return 0; |
| 30 | } |
| 31 | const DWORD error = GetLastError(); |
| 32 | if(error == ERROR_ALREADY_EXISTS) |
| 33 | { |
| 34 | return 0; |
| 35 | } |
| 36 | log_error("filesystem" , "Failed to create folder '%s' (%ld '%s')" , path, error, windows_format_system_message(error).c_str()); |
| 37 | return -1; |
| 38 | #else |
| 39 | #if defined(CONF_PLATFORM_HAIKU) |
| 40 | if(fs_is_dir(path)) |
| 41 | { |
| 42 | return 0; |
| 43 | } |
| 44 | #endif |
| 45 | if(mkdir(path: path, mode: 0755) == 0 || errno == EEXIST) |
| 46 | { |
| 47 | return 0; |
| 48 | } |
| 49 | log_error("filesystem" , "Failed to create folder '%s' (%d '%s')" , path, errno, strerror(errno)); |
| 50 | return -1; |
| 51 | #endif |
| 52 | } |
| 53 | |
| 54 | int fs_removedir(const char *path) |
| 55 | { |
| 56 | #if defined(CONF_FAMILY_WINDOWS) |
| 57 | const std::wstring wide_path = windows_utf8_to_wide(path); |
| 58 | if(RemoveDirectoryW(wide_path.c_str()) != 0) |
| 59 | { |
| 60 | return 0; |
| 61 | } |
| 62 | const DWORD error = GetLastError(); |
| 63 | if(error == ERROR_FILE_NOT_FOUND) |
| 64 | { |
| 65 | return 0; |
| 66 | } |
| 67 | log_error("filesystem" , "Failed to remove folder '%s' (%ld '%s')" , path, error, windows_format_system_message(error).c_str()); |
| 68 | return -1; |
| 69 | #else |
| 70 | if(rmdir(path: path) == 0 || errno == ENOENT) |
| 71 | { |
| 72 | return 0; |
| 73 | } |
| 74 | log_error("filesystem" , "Failed to remove folder '%s' (%d '%s')" , path, errno, strerror(errno)); |
| 75 | return -1; |
| 76 | #endif |
| 77 | } |
| 78 | |