mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-28 23:49:44 +08:00
Restore mg_file_write() to use tmp file
This commit is contained in:
parent
6ad7af54dc
commit
e48e353066
12
mongoose.c
12
mongoose.c
@ -800,10 +800,18 @@ char *mg_file_read(struct mg_fs *fs, const char *path, size_t *sizep) {
|
|||||||
bool mg_file_write(struct mg_fs *fs, const char *path, const void *buf,
|
bool mg_file_write(struct mg_fs *fs, const char *path, const void *buf,
|
||||||
size_t len) {
|
size_t len) {
|
||||||
bool result = false;
|
bool result = false;
|
||||||
struct mg_fd *fd = mg_fs_open(fs, path, MG_FS_WRITE);
|
struct mg_fd *fd;
|
||||||
if (fd != NULL) {
|
char tmp[MG_PATH_MAX];
|
||||||
|
mg_snprintf(tmp, sizeof(tmp), "%s..%d", path, rand());
|
||||||
|
if ((fd = mg_fs_open(fs, tmp, MG_FS_WRITE)) != NULL) {
|
||||||
result = fs->wr(fd->fd, buf, len) == len;
|
result = fs->wr(fd->fd, buf, len) == len;
|
||||||
mg_fs_close(fd);
|
mg_fs_close(fd);
|
||||||
|
if (result) {
|
||||||
|
fs->rm(path);
|
||||||
|
fs->mv(tmp, path);
|
||||||
|
} else {
|
||||||
|
fs->rm(tmp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
12
src/fs.c
12
src/fs.c
@ -45,10 +45,18 @@ char *mg_file_read(struct mg_fs *fs, const char *path, size_t *sizep) {
|
|||||||
bool mg_file_write(struct mg_fs *fs, const char *path, const void *buf,
|
bool mg_file_write(struct mg_fs *fs, const char *path, const void *buf,
|
||||||
size_t len) {
|
size_t len) {
|
||||||
bool result = false;
|
bool result = false;
|
||||||
struct mg_fd *fd = mg_fs_open(fs, path, MG_FS_WRITE);
|
struct mg_fd *fd;
|
||||||
if (fd != NULL) {
|
char tmp[MG_PATH_MAX];
|
||||||
|
mg_snprintf(tmp, sizeof(tmp), "%s..%d", path, rand());
|
||||||
|
if ((fd = mg_fs_open(fs, tmp, MG_FS_WRITE)) != NULL) {
|
||||||
result = fs->wr(fd->fd, buf, len) == len;
|
result = fs->wr(fd->fd, buf, len) == len;
|
||||||
mg_fs_close(fd);
|
mg_fs_close(fd);
|
||||||
|
if (result) {
|
||||||
|
fs->rm(path);
|
||||||
|
fs->mv(tmp, path);
|
||||||
|
} else {
|
||||||
|
fs->rm(tmp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user