login
Header Space

 
 

Re: First cut at git port to Cygwin

Score:
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Git Mailing List <git@...>
Cc: Linus Torvalds <torvalds@...>, Alex Riesen <fork0@...>, Chuck Lever <cel@...>, Git Mailing List <git@...>, Junio C Hamano <junkio@...>, Christopher Faylor <me@...>, H. Peter Anvin <hpa@...>
Date: Saturday, October 8, 2005 - 1:38 pm

On Sat, Oct 08, 2005 at 09:11:03AM -0700, Linus Torvalds wrote:
 >=20
 > On Fri, 7 Oct 2005, Alex Riesen wrote:
 > >=20
 > > Make read_cache copy the index into memory, to improve portability on
 > > other OS's which have mmap too, tend to use it less commonly.
 >=20
 > I really think that you should just get rid of the mmap.
 >=20
 > As it is, you're just slowing the code down on sane architectures. That'=
s=20
 > not good.
 >=20
 > So I'd suggest something like this instead.
 >=20
 > Totally untested, of course.
 >=20
 > 		Linus

Slightly adjusted diff below so it compiles ;)  (Note: only the second
die() un hunk #1 was changed.)

Best,
Elfyn

----
diff --git a/read-cache.c b/read-cache.c
--- a/read-cache.c
+++ b/read-cache.c
@@ -454,13 +454,39 @@ static int verify_hdr(struct cache_heade
 	return 0;
 }
=20
+static void *map_index_file(int fd, size_t size)
+{
+	void *map;
+#ifdef NO_MMAP
+	map =3D malloc(size);
+	if (!map)
+		die("Unable to allocate index file mapping");
+	if (read(fd, map, size) !=3D size)
+		die("Unable to read %z bytes from index", size);
+#else
+	map =3D mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
+	if (map =3D=3D MAP_FAILED)
+		die("index file mmap failed (%s)", strerror(errno));
+#endif
+	return map;
+}
+
+static void unmap_index_file(void *map, size_t size)
+{
+#ifdef NO_MMAP
+	free(map);
+#else
+	munmap(map, size);
+#endif
+}
+
 int read_cache(void)
 {
 	int fd, i;
 	struct stat st;
 	unsigned long size, offset;
-	void *map;
 	struct cache_header *hdr;
+	void *map;
=20
 	errno =3D EBUSY;
 	if (active_cache)
@@ -475,16 +501,15 @@ int read_cache(void)
 	}
=20
 	size =3D 0; // avoid gcc warning
-	map =3D MAP_FAILED;
-	if (!fstat(fd, &st)) {
-		size =3D st.st_size;
-		errno =3D EINVAL;
-		if (size >=3D sizeof(struct cache_header) + 20)
-			map =3D mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
-	}
+	if (fstat(fd, &st))
+		die("unable to fstat index file");
+
+	size =3D st.st_size;
+	errno =3D EINVAL;
+	if (size < sizeof(struct cache_header) + 20)
+		goto corrupt;
+	map =3D map_index_file(fd, size);
 	close(fd);
-	if (map =3D=3D MAP_FAILED)
-		die("index file mmap failed (%s)", strerror(errno));
=20
 	hdr =3D map;
 	if (verify_hdr(hdr, size) < 0)
@@ -503,8 +528,9 @@ int read_cache(void)
 	return active_nr;
=20
 unmap:
-	munmap(map, size);
+	unmap_index_file(map, size);
 	errno =3D EINVAL;
+corrupt:
 	die("index file corrupt");
 }


--=20
Elfyn McBratney
Gentoo Developer/Perl Team Lead
beu/irc.freenode.net                            http://dev.gentoo.org/~beu/
+------------O.o--------------------- http://dev.gentoo.org/~beu/pubkey.asc

PGP Key ID: 0x69DF17AD
PGP Key Fingerprint:
  DBD3 B756 ED58 B1B4 47B9  B3BD 8D41 E597 69DF 17AD
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
First cut at git port to Cygwin, H. Peter Anvin, (Wed Sep 28, 8:53 pm)
Re: First cut at git port to Cygwin, Jonas Fonseca, (Wed Oct 5, 9:16 am)
Re: First cut at git port to Cygwin, Johannes Schindelin, (Wed Oct 5, 9:58 am)
[PATCH] Fix symbolic ref validation, Jonas Fonseca, (Wed Oct 5, 11:52 am)
Re: [PATCH] Fix symbolic ref validation, Junio C Hamano, (Wed Oct 5, 12:54 pm)
Re: First cut at git port to Cygwin, Alex Riesen, (Tue Oct 4, 8:31 am)
Re: First cut at git port to Cygwin, H. Peter Anvin, (Tue Oct 4, 11:03 am)
Re: First cut at git port to Cygwin, Alex Riesen, (Wed Oct 5, 7:24 am)
Re: First cut at git port to Cygwin, Alex Riesen, (Wed Oct 5, 11:46 am)
Re: First cut at git port to Cygwin, Christopher Faylor, (Wed Oct 5, 11:54 am)
Re: First cut at git port to Cygwin, Alex Riesen, (Wed Oct 5, 3:17 pm)
Re: First cut at git port to Cygwin, Christopher Faylor, (Wed Oct 5, 4:29 pm)
Re: First cut at git port to Cygwin, Alex Riesen, (Thu Oct 6, 5:05 am)
Re: First cut at git port to Cygwin, Alex Riesen, (Thu Oct 6, 6:07 am)
Re: First cut at git port to Cygwin, Alex Riesen, (Fri Oct 7, 8:44 am)
Re: First cut at git port to Cygwin, Linus Torvalds, (Fri Oct 7, 11:34 am)
Re: First cut at git port to Cygwin, Alex Riesen, (Fri Oct 7, 4:54 pm)
Re: First cut at git port to Cygwin, Alex Riesen, (Fri Oct 7, 5:22 pm)
Re: First cut at git port to Cygwin, Chuck Lever, (Fri Oct 7, 5:29 pm)
Re: First cut at git port to Cygwin, Alex Riesen, (Fri Oct 7, 5:39 pm)
Re: First cut at git port to Cygwin, Linus Torvalds, (Sat Oct 8, 12:11 pm)
Re: First cut at git port to Cygwin, Johannes Schindelin, (Sat Oct 8, 2:27 pm)
Re: First cut at git port to Cygwin, Alex Riesen, (Sat Oct 8, 2:49 pm)
Re: First cut at git port to Cygwin, Junio C Hamano, (Sat Oct 8, 2:44 pm)
Re: First cut at git port to Cygwin, H. Peter Anvin, (Mon Oct 10, 2:43 pm)
Re: First cut at git port to Cygwin, Johannes Schindelin, (Mon Oct 10, 3:01 pm)
Re: First cut at git port to Cygwin, Daniel Barkalow, (Mon Oct 10, 4:27 pm)
Re: First cut at git port to Cygwin, H. Peter Anvin, (Mon Oct 10, 3:26 pm)
Re: First cut at git port to Cygwin, Junio C Hamano, (Mon Oct 10, 4:34 pm)
Re: First cut at git port to Cygwin, H. Peter Anvin, (Mon Oct 10, 4:52 pm)
Re: First cut at git port to Cygwin, Junio C Hamano, (Mon Oct 10, 4:21 pm)
Re: First cut at git port to Cygwin, Johannes Schindelin, (Mon Oct 10, 3:42 pm)
Re: First cut at git port to Cygwin, Johannes Schindelin, (Sat Oct 8, 3:04 pm)
Re: First cut at git port to Cygwin, Junio C Hamano, (Sat Oct 8, 5:10 pm)
Re: First cut at git port to Cygwin, Johannes Schindelin, (Sat Oct 8, 6:06 pm)
Re: First cut at git port to Cygwin, Elfyn McBratney, (Sat Oct 8, 1:43 pm)
Re: First cut at git port to Cygwin, Elfyn McBratney, (Sat Oct 8, 1:38 pm)
Re: First cut at git port to Cygwin, Davide Libenzi, (Wed Oct 5, 12:09 pm)
Re: First cut at git port to Cygwin, Christopher Faylor, (Wed Oct 5, 12:15 pm)
Re: First cut at git port to Cygwin, Davide Libenzi, (Wed Oct 5, 1:29 pm)
Re: First cut at git port to Cygwin, H. Peter Anvin, (Wed Oct 5, 12:23 pm)
Re: First cut at git port to Cygwin, Christopher Faylor, (Wed Oct 5, 12:28 pm)
Re: First cut at git port to Cygwin, Christopher Faylor, (Tue Oct 4, 11:16 pm)
Re: First cut at git port to Cygwin, H. Peter Anvin, (Wed Oct 5, 1:25 am)
Re: First cut at git port to Cygwin, H. Peter Anvin, (Tue Oct 4, 10:06 am)
Re: First cut at git port to Cygwin, Christopher Faylor, (Tue Oct 4, 11:15 pm)
Re: First cut at git port to Cygwin, Alex Riesen, (Tue Oct 4, 9:06 am)
Re: First cut at git port to Cygwin, Johannes Schindelin, (Thu Sep 29, 4:46 am)
Re: First cut at git port to Cygwin, H. Peter Anvin, (Thu Sep 29, 1:25 pm)
Re: First cut at git port to Cygwin, H. Peter Anvin, (Thu Sep 29, 12:11 pm)
Re: First cut at git port to Cygwin, Martin Langhoff, (Thu Sep 29, 12:46 am)
Re: First cut at git port to Cygwin, H. Peter Anvin, (Thu Sep 29, 2:19 am)
Re: First cut at git port to Cygwin, Junio C Hamano, (Thu Sep 29, 1:13 am)
Re: First cut at git port to Cygwin, Junio C Hamano, (Thu Sep 29, 12:30 am)
Re: First cut at git port to Cygwin, H. Peter Anvin, (Thu Sep 29, 1:07 am)
speck-geostationary