mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-18 19:31:02 +00:00
Add fs arg to mg_file* and mg_http_upload API
This commit is contained in:
1 parent
09df542db8
commit
56a7438114
20 files changed
+447
-367
No files matched your search
@@ -1,4 +1,5 @@
|
||||
#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));
|
||||
@@ -19,3 +20,56 @@ void mg_fs_close(struct mg_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;
|
||||
}
|
||||
Reference in new issue
Block a user