# [PATCH 2/8] Add a lockfile function to append to a file

3 messages from 2008-04-17 to 2008-04-17. Participants: Daniel Barkalow, Junio C Hamano.
Thread: https://gitlist.dev/t/13167

## Daniel Barkalow, 2008-04-17 23:32

Subject: [PATCH 2/8] Add a lockfile function to append to a file
Message-ID: <alpine.LNX.1.00.0804171931480.19665@iabervon.org>
URL: https://gitlist.dev/e/alpine.LNX.1.00.0804171931480.19665%40iabervon.org

```
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.

Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
---
 cache.h    |    1 +
 lockfile.c |   17 +++++++++++++++++
 2 files changed, 18 insertions(+), 0 deletions(-)

diff --git a/cache.h b/cache.h
index 2a1e7ec..4a113b0 100644
--- a/cache.h
+++ b/cache.h
@@ -391,6 +391,7 @@ struct lock_file {
 	char filename[PATH_MAX];
 };
 extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
+extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
 extern int commit_lock_file(struct lock_file *);
 
 extern int hold_locked_index(struct lock_file *, int);
diff --git a/lockfile.c b/lockfile.c
index 663f18f..95fd3d1 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -160,6 +160,23 @@ int hold_lock_file_for_update(struct lock_file *lk, const char *path, int die_on
 	return fd;
 }
 
+int hold_lock_file_for_append(struct lock_file *lk, const char *path, int die_on_error)
+{
+	int fd = lock_file(lk, path);
+	struct stat st;
+	if (!stat(path, &st)) {
+		int orig_fd = open(path, O_RDONLY);
+		size_t mmap_size = xsize_t(st.st_size);
+		void *mmap = xmmap(NULL, mmap_size, PROT_READ, MAP_PRIVATE,
+				   orig_fd, 0);
+		write_or_die(fd, mmap, mmap_size);
+		munmap(mmap, mmap_size);
+	}
+	if (fd < 0 && die_on_error)
+		die("unable to create '%s.lock': %s", path, strerror(errno));
+	return fd;
+}
+
 int close_lock_file(struct lock_file *lk)
 {
 	int fd = lk->fd;
-- 
1.5.4.3.610.gea6cd

```

## Junio C Hamano, 2008-04-17 23:41

Subject: Re: [PATCH 2/8] Add a lockfile function to append to a file
Message-ID: <7v3apkm4lz.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v3apkm4lz.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <alpine.LNX.1.00.0804171931480.19665@iabervon.org>

```
Daniel Barkalow <barkalow@iabervon.org> writes:

> 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.
>
> Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
> ---
>  cache.h    |    1 +
>  lockfile.c |   17 +++++++++++++++++
>  2 files changed, 18 insertions(+), 0 deletions(-)
>
> diff --git a/cache.h b/cache.h
> index 2a1e7ec..4a113b0 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -391,6 +391,7 @@ struct lock_file {
>  	char filename[PATH_MAX];
>  };
>  extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
> +extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
>  extern int commit_lock_file(struct lock_file *);
>  
>  extern int hold_locked_index(struct lock_file *, int);
> diff --git a/lockfile.c b/lockfile.c
> index 663f18f..95fd3d1 100644
> --- a/lockfile.c
> +++ b/lockfile.c
> @@ -160,6 +160,23 @@ int hold_lock_file_for_update(struct lock_file *lk, const char *path, int die_on
>  	return fd;
>  }
>  
> +int hold_lock_file_for_append(struct lock_file *lk, const char *path, int die_on_error)
> +{
> +	int fd = lock_file(lk, path);
> +	struct stat st;
> +	if (!stat(path, &st)) {
> +		int orig_fd = open(path, O_RDONLY);
> +		size_t mmap_size = xsize_t(st.st_size);
> +		void *mmap = xmmap(NULL, mmap_size, PROT_READ, MAP_PRIVATE,
> +				   orig_fd, 0);
> +		write_or_die(fd, mmap, mmap_size);
> +		munmap(mmap, mmap_size);
> +	}
> +	if (fd < 0 && die_on_error)
> +		die("unable to create '%s.lock': %s", path, strerror(errno));

Copying and checking are the other way around, aren't they?  Grab the
lock, error out if you cannot, then read and copy.

> +	return fd;
> +}
> +
>  int close_lock_file(struct lock_file *lk)
>  {
>  	int fd = lk->fd;
> -- 
> 1.5.4.3.610.gea6cd

```

## Junio C Hamano, 2008-04-17 23:51

Subject: Re: [PATCH 2/8] Add a lockfile function to append to a file
Message-ID: <7vy77ckpkk.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vy77ckpkk.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <alpine.LNX.1.00.0804171931480.19665@iabervon.org>

```
Daniel Barkalow <barkalow@iabervon.org> writes:

> +int hold_lock_file_for_append(struct lock_file *lk, const char *path, int die_on_error)
> +{
> +	int fd = lock_file(lk, path);
> +	struct stat st;
> +	if (!stat(path, &st)) {
> +		int orig_fd = open(path, O_RDONLY);
> +		size_t mmap_size = xsize_t(st.st_size);
> +		void *mmap = xmmap(NULL, mmap_size, PROT_READ, MAP_PRIVATE,
> +				   orig_fd, 0);
> +		write_or_die(fd, mmap, mmap_size);
> +		munmap(mmap, mmap_size);
> +	}
> +	if (fd < 0 && die_on_error)
> +		die("unable to create '%s.lock': %s", path, strerror(errno));
> +	return fd;
> +}

Another glitch.  What should we do when stat(path) fails but the file
cannot be read?

I think the sequence actually should be:

	fd = lock_file();
        if (fd < 0)
        	error out;
	orig_fd = open(path, O_RDONLY);
        if (orig_fd < 0) {
		if (errno != ENOENT)
			die("unable to open %s", path);
		copy;
	}
	return fd;

```
