Re: [PATCH] Seek back to current filepos when mmap()ing with NO_MMAP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 11/15/06, Johannes Schindelin <Johannes.Schindelin@xxxxxx> wrote:

"git-index-pack --fix-thin" relies on mmap() not changing the current
file position (otherwise the pack will be corrupted when writing the
final SHA1). Meet that expectation.


Thanks! I was wondering for some considerable time why the
hell t5500-fetch-pack fails.

BTW, I extended error handling in that mmap.  Dunno why.

Signed-off-by: Alex Riesen <raa.lkml@xxxxxxxxx>
commit 40ed0644ca8d250f716f49a5c3e027aa2c1d3167
Author: Alex Riesen <raa.lkml@xxxxxxxxx>
Date:   Wed Nov 15 19:55:06 2006 +0100

    Seek back to current filepos when mmap()ing with NO_MMAP, eh improved
    
    "git-index-pack --fix-thin" relies on mmap() not changing the current
    file position (otherwise the pack will be corrupted when writing the
    final SHA1). Meet that expectation.
    
    Signed-off-by: Johannes Schindelin <Johannes.Schindelin@xxxxxx>
    Signed-off-by: Alex Riesen <raa.lkml@xxxxxxxxx>

diff --git a/compat/mmap.c b/compat/mmap.c
index 55cb120..c6d5cfe 100644
--- a/compat/mmap.c
+++ b/compat/mmap.c
@@ -6,7 +6,8 @@
 
 void *gitfakemmap(void *start, size_t length, int prot , int flags, int fd, off_t offset)
 {
-	int n = 0;
+	int n = 0, err = 0;
+	off_t current_offset = lseek(fd, 0, SEEK_CUR);
 
 	if (start != NULL || !(flags & MAP_PRIVATE))
 		die("Invalid usage of gitfakemmap.");
@@ -18,8 +19,8 @@ void *gitfakemmap(void *start, size_t le
 
 	start = xmalloc(length);
 	if (start == NULL) {
-		errno = ENOMEM;
-		return MAP_FAILED;
+		err = ENOMEM;
+		goto unseek;
 	}
 
 	while (n < length) {
@@ -31,14 +32,22 @@ void *gitfakemmap(void *start, size_t le
 		}
 
 		if (count < 0) {
-			free(start);
-			errno = EACCES;
-			return MAP_FAILED;
+			err = EACCES;
+			goto unseek;
 		}
 
 		n += count;
 	}
 
+unseek:
+	if (current_offset != lseek(fd, current_offset, SEEK_SET))
+		err = EINVAL;
+
+	if (err) {
+		free(start);
+		start = MAP_FAILED;
+		errno = err;
+	}
 	return start;
 }
 

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]