11struct DirectoryHandle {
12 HANDLE hFind = INVALID_HANDLE_VALUE;
13 WIN32_FIND_DATAA findFileData;
14 bool firstEntry =
true;
21#define _POSIX_C_SOURCE 200809L
22#define _XOPEN_SOURCE 700
23#define _CRT_SECURE_NO_WARNINGS
39#define stat_struct struct __stat64
40#define stat_func _stat64
46#define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
49#define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
52#define PATH_SEPARATOR '\\'
59#define stat_struct struct stat
61#define PATH_SEPARATOR '/'
72 return _access(path, 0) == 0;
74 return access(path, F_OK) == 0;
82 return S_ISREG(s.st_mode);
89 return S_ISDIR(s.st_mode);
94 DWORD attr = GetFileAttributesA(path);
95 if (attr == INVALID_FILE_ATTRIBUTES)
97 return (attr & FILE_ATTRIBUTE_REPARSE_POINT) != 0;
100 if (lstat(path, &s) != 0)
102 return S_ISLNK(s.st_mode);
110 return (int64_t)s.st_mtime;
117 return (uint64_t)s.st_size;
124 return (int64_t)s.st_ctime;
131 return (int64_t)s.st_atime;
138 return (
int)s.st_uid;
143 DWORD len = GetTempPathA(0,
nullptr);
147 char *buf = (
char *)malloc(len + 1);
151 if (GetTempPathA(len + 1, buf) == 0) {
157 size_t actual_len = strlen(buf);
158 if (actual_len > 0 && buf[actual_len - 1] ==
'\\') {
159 buf[actual_len - 1] =
'\0';
163 const char *env_temp = getenv(
"TMPDIR");
165 env_temp = getenv(
"TMP");
167 env_temp = getenv(
"TEMP");
169 env_temp = getenv(
"TEMPDIR");
173 return strdup(env_temp);
179 const char *drive = getenv(
"HOMEDRIVE");
180 const char *path = getenv(
"HOMEPATH");
181 const char *userprofile = getenv(
"USERPROFILE");
184 return strdup(userprofile);
185 }
else if (drive && path) {
186 size_t len = strlen(drive) + strlen(path) + 1;
187 char *buf = (
char *)malloc(len);
189 sprintf(buf,
"%s%s", drive, path);
195 const char *home = getenv(
"HOME");
201 struct passwd *pwd = getpwuid(getuid());
203 return strdup(pwd->pw_dir);
213 char *buf = (
char *)malloc(size);
218 while (getcwd(buf, (
int)size) ==
nullptr) {
219 if (errno == ERANGE) {
221 char *new_buf = (
char *)realloc(buf, size);
234 char *final_buf = strdup(buf);
244 return _fullpath(
nullptr, path, 0);
247 return realpath(path,
nullptr);
261 return _mkdir(path) == 0;
263 return mkdir(path, mode) == 0;
271 return _rmdir(path) == 0;
273 return rmdir(path) == 0;
279 if (remove(path) == 0) {
282 return RemoveDirectoryA(path) != 0;
284 return remove(path) == 0;
289 return rename(old_path, new_path) == 0;
294 char *concatenated = (
char *)malloc(strlen(name) + 3);
297 strcpy(concatenated, name);
298 strcat(concatenated,
"/*");
300 DirectoryHandle *dir = (DirectoryHandle *)malloc(
sizeof(DirectoryHandle));
305 dir->hFind = FindFirstFileA(concatenated, &(dir->findFileData));
307 if (dir->hFind == INVALID_HANDLE_VALUE) {
312 dir->firstEntry =
true;
315 return opendir(name);
324 DirectoryHandle *handle = (DirectoryHandle *)dir;
326 if (handle->firstEntry) {
327 handle->firstEntry =
false;
328 return handle->findFileData.cFileName;
331 if (FindNextFileA(handle->hFind, &(handle->findFileData)) != 0) {
332 return handle->findFileData.cFileName;
336 struct dirent *entry = readdir((DIR *)dir);
340 return entry->d_name;
349 DirectoryHandle *handle = (DirectoryHandle *)dir;
350 if (handle->hFind != INVALID_HANDLE_VALUE) {
351 FindClose(handle->hFind);
355 closedir((DIR *)dir);
366 return CreateSymbolicLinkA(dest_path, src_path, flags) != 0;
368 int rc = symlink(src_path, dest_path);
char * runtime_fs_home_dir()
bool runtime_fs_rmdir(const char *path)
int64_t runtime_fs_get_atime(const char *path)
static int get_stat(const char *path, stat_struct *buf)
void runtime_fs_finalize(void *res)
bool runtime_fs_symlink(const char *src_path, const char *dest_path)
bool runtime_fs_isdir(const char *path)
int runtime_fs_get_uid(const char *path)
bool runtime_fs_rename(const char *old_path, const char *new_path)
int64_t runtime_fs_get_mtime(const char *path)
void runtime_fs_closedir(void *dir)
bool runtime_fs_issymlink(const char *path)
int64_t runtime_fs_get_ctime(const char *path)
char * runtime_fs_temp_dir()
bool runtime_fs_exists(const char *path)
char * runtime_fs_realpath(const char *path)
void * runtime_fs_opendir(const char *name)
bool runtime_fs_mkdir(const char *path, int mode)
bool runtime_fs_isfile(const char *path)
char * runtime_fs_readdir(void *dir)
bool runtime_fs_remove(const char *path)
uint64_t runtime_fs_get_size(const char *path)