/** * @file FileSystem.cpp * @author apio (cloudapio.eu) * @brief APIs to read and modify the general file system. * * @copyright Copyright (c) 2023, the Luna authors. * */ #include #include #include #include #include #include #include #include #include #include #include namespace os::FileSystem { bool exists(const Path& path, bool follow_symlinks) { struct stat st; if (stat(path, st, follow_symlinks).has_error()) return false; return true; } bool is_directory(const Path& path, bool follow_symlinks) { struct stat st; if (stat(path, st, follow_symlinks).has_error()) return false; return S_ISDIR(st.st_mode); } Result stat(const Path& path, struct stat& st, bool follow_symlinks) { int rc = fstatat(path.dirfd(), path.name().chars(), &st, (int)(path.is_empty_path() | (follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW))); if (rc < 0) return err(errno); return {}; } Result create_directory(StringView path, mode_t mode) { int rc = mkdir(path.chars(), mode); if (rc < 0) return err(errno); return {}; } Result remove(const Path& path) { // FIXME: This will not work on many operating systems that require rmdir() for directories. int rc = unlinkat(path.dirfd(), path.name().chars(), 0); if (rc < 0) return err(errno); return {}; } Result readlink(const Path& path) { struct stat st; TRY(stat(path, st, false)); if (!S_ISLNK(st.st_mode)) return String {}; Buffer buf = TRY(Buffer::create_sized(st.st_size + 1)); memset(buf.data(), 0, buf.size()); ssize_t nread = readlinkat(path.dirfd(), path.name().chars(), (char*)buf.data(), st.st_size); if (nread < 0) return err(errno); return String { (char*)buf.release_data(), (usize)nread }; } Result working_directory() { char* ptr = getcwd(NULL, 0); if (!ptr) return err(errno); return String { ptr }; } Result home_directory() { char* home = getenv("HOME"); if (home) return String::from_cstring(home); struct passwd* pw = getpwuid(getuid()); if (!pw) return err(ENOENT); return String::from_cstring(pw->pw_dir); } Result change_directory(StringView path) { int rc = chdir(path.chars()); if (rc < 0) return err(errno); return {}; } }