Filebuf: Fixed a TODO in filebuf (real lock in lock_file) Added gitfo_creat_locked and gitfo_creat_locked_force
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
diff --git a/src/filebuf.c b/src/filebuf.c
index 63f2897..97dec83 100644
--- a/src/filebuf.c
+++ b/src/filebuf.c
@@ -41,16 +41,14 @@ static int lock_file(git_filebuf *file, int flags)
/* create path to the file buffer is required */
if (flags & GIT_FILEBUF_FORCE) {
- file->fd = gitfo_creat_force(file->path_lock, 0644);
+ file->fd = gitfo_creat_locked_force(file->path_lock, 0644);
} else {
- file->fd = gitfo_creat(file->path_lock, 0644);
+ file->fd = gitfo_creat_locked(file->path_lock, 0644);
}
if (file->fd < 0)
return git__throw(GIT_EOSERR, "Failed to create lock");
- /* TODO: do a flock() in the descriptor file_lock */
-
if ((flags & GIT_FILEBUF_APPEND) && gitfo_exists(file->path_original) == 0) {
git_file source;
char buffer[2048];
diff --git a/src/fileops.c b/src/fileops.c
index 11634c2..c407515 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -66,6 +66,20 @@ int gitfo_creat_force(const char *path, int mode)
return gitfo_creat(path, mode);
}
+int gitfo_creat_locked(const char *path, int mode)
+{
+ int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, mode);
+ return fd >= 0 ? fd : git__throw(GIT_EOSERR, "Failed to create locked file. Could not open %s", path);
+}
+
+int gitfo_creat_locked_force(const char *path, int mode)
+{
+ if (gitfo_mkdir_2file(path) < GIT_SUCCESS)
+ return git__throw(GIT_EOSERR, "Failed to create locked file %s", path);
+
+ return gitfo_creat_locked(path, mode);
+}
+
int gitfo_read(git_file fd, void *buf, size_t cnt)
{
char *b = buf;
diff --git a/src/fileops.h b/src/fileops.h
index aa225dc..23f1654 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -65,6 +65,8 @@ extern int gitfo_exists(const char *path);
extern int gitfo_open(const char *path, int flags);
extern int gitfo_creat(const char *path, int mode);
extern int gitfo_creat_force(const char *path, int mode);
+extern int gitfo_creat_locked(const char *path, int mode);
+extern int gitfo_creat_locked_force(const char *path, int mode);
extern int gitfo_mktemp(char *path_out, const char *filename);
extern int gitfo_isdir(const char *path);
extern int gitfo_isfile(const char *path);