#define AL_LOG_SECTION "file" #include #include #include "../error.h" #include "file.h" /* #if defined NAUNET_ON_WINDOWS && defined AL_WE_32BIT AL_ASSERT_TYPE_SIZE(off_t, 4); #else AL_ASSERT_TYPE_SIZE(off_t, 8); #endif */ static inline bool open_stdio_file(FILE **file, str *path, const char *mode) { char *c_str = al_str_to_c_str(path); #if defined NAUNET_ON_WINDOWS && !defined NAUNET_WIN32_COMPAT_MODE // Still sets errno, according to the Microsoft docs. errno_t ret = fopen_s(file, c_str, mode); #else s32 ret = (*file = fopen(c_str, mode)) ? 0 : errno; #endif al_free(c_str); return ret == 0; } static bool query_filesize_stdio(struct nn_file *file) { off_t pos = nn_file_seek(file, 0, SEEK_END); if (pos == -1) return false; file->size = pos; rewind(file->file); return true; } bool nn_file_open(struct nn_file *file, str *path, s32 flags) { const char *mode = (flags & NNWT_FILE_CREATE) ? "wb+" : "rb+"; if (!open_stdio_file(&file->file, path, mode)) { log_error("fopen(%.*s) failed (%d: %s).", al_str_x(path), errno, nn_strerror(errno)); return false; } if (!query_filesize_stdio(file)) { nn_file_close(file); return false; } al_str_clone(&file->path, path); return true; } bool nn_file_exists(str *path) { FILE *file; bool opened = open_stdio_file(&file, path, "r"); if (opened) fclose(file); return opened; } bool nn_file_create(str *path) { FILE *file; bool opened = open_stdio_file(&file, path, "w+"); if (opened) fclose(file); return opened; } off_t nn_file_seek(struct nn_file *file, off_t offset, s32 whence) { if (fseek(file->file, offset, whence) != 0) { log_error("fseek(%zd, %d) failed (%s).", offset, whence, nn_strerror(errno)); return -1; } return ftell(file->file); } bool nn_file_truncate(struct nn_file *file, off_t size) { file->size = size; return true; } bool nn_file_write(struct nn_file *file, void *buf, size_t size) { size_t ret = fwrite(buf, size, 1, file->file); if (ret == 0) { log_error("fwrite(%zu) failed (%s).", size, nn_strerror(errno)); return false; } return true; } bool nn_file_read(struct nn_file *file, void *buf, size_t size) { size_t ret = fread(buf, size, 1, file->file); if (ret == 0) { log_error("fread(%zu) failed (%s).", size, nn_strerror(errno)); return false; } return true; } void *nn_file_mmap(struct nn_file *file) { (void)file; al_assert_and_return(NULL); } void nn_file_close(struct nn_file *file) { fclose(file->file); al_str_free(&file->path); } bool nn_dir_open(struct nn_dir *dir, str *path) { (void)dir; (void)path; return false; } void nn_dir_close(struct nn_dir *dir) { (void)dir; } bool nn_dir_exists(str *path) { (void)path; return false; } bool nn_dir_create(str *path) { (void)path; return false; } bool nn_dir_read(struct nn_dir *dir, struct nn_dir_entry *entry) { (void)dir; (void)entry; return false; } void nn_dir_entry_get_name(struct nn_dir_entry *entry, str *name) { (void)entry; (void)name; }; const char *nn_dir_entry_get_os_name(struct nn_dir_entry *entry) { (void)entry; return NULL; }; void nn_dir_entry_get_path(struct nn_dir_entry *entry, struct nn_dir *dir, str *path) { (void)entry; (void)dir; (void)path; }