diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/filebuf.c | 3 | ||||
| -rw-r--r-- | src/fileops.c | 32 | ||||
| -rw-r--r-- | src/fileops.h | 18 | ||||
| -rw-r--r-- | src/indexer.c | 9 |
4 files changed, 60 insertions, 2 deletions
diff --git a/src/filebuf.c b/src/filebuf.c index c4a13ffab..f37cf02b4 100644 --- a/src/filebuf.c +++ b/src/filebuf.c @@ -445,6 +445,9 @@ int git_filebuf_commit(git_filebuf *file) goto on_error; } + if (file->do_fsync && git_futils_fsync_parent(file->path_original) < 0) + goto on_error; + file->did_rename = true; git_filebuf_cleanup(file); diff --git a/src/fileops.c b/src/fileops.c index 13b1fc2a1..881d1bed4 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -265,8 +265,13 @@ int git_futils_writebuffer( return error; } - if ((error = p_close(fd)) < 0) + if ((error = p_close(fd)) < 0) { giterr_set(GITERR_OS, "error while closing '%s'", path); + return error; + } + + if (do_fsync && (flags & O_CREAT)) + error = git_futils_fsync_parent(path); return error; } @@ -1119,3 +1124,28 @@ void git_futils_filestamp_set_from_stat( memset(stamp, 0, sizeof(*stamp)); } } + +int git_futils_fsync_dir(const char *path) +{ + int fd, error = -1; + + if ((fd = p_open(path, O_RDONLY)) < 0) { + giterr_set(GITERR_OS, "failed to open directory '%s' for fsync", path); + return -1; + } + + if ((error = p_fsync(fd)) < 0) + giterr_set(GITERR_OS, "failed to fsync directory '%s'", path); + + p_close(fd); + return error; +} + +int git_futils_fsync_parent(const char *path) +{ + char *parent = git_path_dirname(path); + int error = git_futils_fsync_dir(parent); + + git__free(parent); + return error; +} diff --git a/src/fileops.h b/src/fileops.h index 9a6fc3f6b..46886b0d7 100644 --- a/src/fileops.h +++ b/src/fileops.h @@ -363,4 +363,22 @@ extern void git_futils_filestamp_set( extern void git_futils_filestamp_set_from_stat( git_futils_filestamp *stamp, struct stat *st); +/** + * `fsync` the parent directory of the given path, if `fsync` is + * supported for directories on this platform. + * + * @param path Path of the directory to sync. + * @return 0 on success, -1 on error + */ +extern int git_futils_fsync_dir(const char *path); + +/** + * `fsync` the parent directory of the given path, if `fsync` is + * supported for directories on this platform. + * + * @param path Path of the file whose parent directory should be synced. + * @return 0 on success, -1 on error + */ +extern int git_futils_fsync_parent(const char *path); + #endif /* INCLUDE_fileops_h__ */ diff --git a/src/indexer.c b/src/indexer.c index 30c365565..9510253b5 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -1086,7 +1086,14 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats) goto on_error; /* And don't forget to rename the packfile to its new place. */ - p_rename(idx->pack->pack_name, git_buf_cstr(&filename)); + if (p_rename(idx->pack->pack_name, git_buf_cstr(&filename)) < 0) + goto on_error; + + /* And fsync the parent directory if we're asked to. */ + if (git_object__synchronized_writing && + git_futils_fsync_parent(git_buf_cstr(&filename)) < 0) + goto on_error; + idx->pack_committed = 1; git_buf_free(&filename); |
