#include "fs.h" #include "util.h" struct mg_fd *mg_fs_open(struct mg_fs *fs, const char *path, int flags) { struct mg_fd *fd = (struct mg_fd *) calloc(1, sizeof(*fd)); if (fd != NULL) { fd->fd = fs->open(path, flags); fd->fs = fs; if (fd->fd == NULL) { free(fd); fd = NULL; } } return fd; } void mg_fs_close(struct mg_fd *fd) { if (fd != NULL) { fd->fs->close(fd->fd); free(fd); } } char *mg_file_read(struct mg_fs *fs, const char *path, size_t *sizep) { struct mg_fd *fd; char *data = NULL; size_t size = 0; fs->stat(path, &size, NULL); if ((fd = mg_fs_open(fs, path, MG_FS_READ)) != NULL) { data = (char *) calloc(1, size + 1); if (data != NULL) { if (fs->read(fd->fd, data, size) != size) { free(data); data = NULL; } else { data[size] = '\0'; if (sizep != NULL) *sizep = size; } } mg_fs_close(fd); } return data; } bool mg_file_write(struct mg_fs *fs, const char *path, const void *buf, size_t len) { bool result = false; struct mg_fd *fd; char tmp[MG_PATH_MAX]; snprintf(tmp, sizeof(tmp), "%s..%d", path, rand()); if ((fd = mg_fs_open(fs, tmp, MG_FS_WRITE)) != NULL) { result = fs->write(fd->fd, buf, len) == len; mg_fs_close(fd); if (result) { fs->remove(path); fs->rename(tmp, path); } else { fs->remove(tmp); } } return result; } bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...) { char tmp[256], *buf = tmp; bool result; int len; va_list ap; va_start(ap, fmt); len = mg_vasprintf(&buf, sizeof(tmp), fmt, ap); va_end(ap); result = mg_file_write(fs, path, buf, len > 0 ? (size_t) len : 0); if (buf != tmp) free(buf); return result; }