Add a lockfile function to append to a file
This takes care of copying the original contents into the replacement file after the lock is held, so that concurrent additions can't miss each other's changes. [jc: munged to drop mmap in favor of copy_file.] Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Daniel Barkalow committed
Apr 17, 2008 at 19:32 UTC
ea3cd5c7c63fadacd66c364ae4b8c6d01e5809b1
2 files changed
+29
cache.h
+1
@@ -391,6 +391,7 @@ struct lock_file {
391
char filename[PATH_MAX];
392
};
393
extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
394
+extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
395
extern int commit_lock_file(struct lock_file *);
396
397
extern int hold_locked_index(struct lock_file *, int);
lockfile.c
+28
@@ -160,6 +160,34 @@ int hold_lock_file_for_update(struct lock_file *lk, const char *path, int die_on
160
return fd;
161
}
162
163
+int hold_lock_file_for_append(struct lock_file *lk, const char *path, int die_on_error)
164
+{
165
+ int fd, orig_fd;
166
+
167
+ fd = lock_file(lk, path);
168
+ if (fd < 0) {
169
+ if (die_on_error)
170
+ die("unable to create '%s.lock': %s", path, strerror(errno));
171
+ return fd;
172
+ }
173
+
174
+ orig_fd = open(path, O_RDONLY);
175
+ if (orig_fd < 0) {
176
+ if (errno != ENOENT) {
177
+ if (die_on_error)
178
+ die("cannot open '%s' for copying", path);
179
+ close(fd);
180
+ return error("cannot open '%s' for copying", path);
181
+ }
182
+ } else if (copy_fd(orig_fd, fd)) {
183
+ if (die_on_error)
184
+ exit(128);
185
+ close(fd);
186
+ return -1;
187
+ }
188
+ return fd;
189
+}
190
+
191
int close_lock_file(struct lock_file *lk)
192
{
193
int fd = lk->fd;