[patch] munmap-before-rename, cygwin need

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Yakov Lerner
Date: Sunday, May 7, 2006 - 12:58 pm

I found that mmap() works on cygwin, but needs a patch.
On Cygwin, rename() fails if target file has active mmap().
The patch below adds  munmap() before rename().

If you excuse me for posting not in git-generated format
(I did not yet completely make git working on my cygwin).
I'm adding the copy as attachment, I'm afraid the mailer
may interfere with spaces ?

Yakov Lerner

Makefile     |    5 +++++
cache.h      |    1 +
index.c      |    3 +++
read-cache.c |   14 ++++++++++++++

--- Makefile.000 2006-05-07 22:32:04.000000000 +0300
+++ Makefile 2006-05-07 22:30:38.000000000 +0300
@@ -46,6 +46,9 @@
#
# Define NO_MMAP if you want to avoid mmap.
#
+# Define MUMNAP_BEFORE_RENAME if munmap() on target file is required
+# before rename().
+#
# Define WITH_OWN_SUBPROCESS_PY if you want to use with python 2.3.
#
# Define NO_IPV6 if you lack IPv6 support and getaddrinfo().
@@ -270,6 +273,8 @@
# NO_MMAP =3D YesPlease
NO_IPV6 =3D YesPlease
X =3D .exe
+ MUMNAP_BEFORE_RENAME =3D YesPlease
+ NO_SOCKADDR_STORAGE =3D YesPlease
endif
ifeq ($(uname_S),FreeBSD)
NEEDS_LIBICONV =3D YesPlease
--- cache.h.000 2006-05-02 11:35:50.000000000 +0300
+++ cache.h 2006-05-02 11:33:34.000000000 +0300
@@ -140,6 +140,7 @@

/* Initialize and use the cache information */
extern int read_cache(void);
+extern void unmap_cache(void);
extern int write_cache(int newfd, struct cache_entry **cache, int entries);
extern int cache_name_pos(const char *name, int namelen);
#define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */
--- read-cache.c.000 2006-05-07 22:33:42.000000000 +0300
+++ read-cache.c 2006-05-02 11:32:56.000000000 +0300
@@ -513,6 +513,9 @@
return 0;
}

+static void *mapaddr =3D MAP_FAILED;
+static unsigned long mapsize;
+
int read_cache(void)
{
int fd, i;
@@ -541,6 +544,8 @@
errno =3D EINVAL;
if (size >=3D sizeof(struct cache_header) + 20)
map =3D mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
+ mapaddr =3D map;
+ mapsize =3D size;
}
close(fd);
if (map =3D=3D MAP_FAILED)
@@ -565,10 +570,19 @@

unmap:
munmap(map, size);
+ mapaddr =3D MAP_FAILED;
errno =3D EINVAL;
die("index file corrupt");
}

+void unmap_cache(void)
+{
+ if ( mapaddr !=3D MAP_FAILED ) {
+ munmap(mapaddr, mapsize);
+ mapaddr =3D MAP_FAILED;
+ }
+}
+
#define WRITE_BUFFER_SIZE 8192
static unsigned char write_buffer[WRITE_BUFFER_SIZE];
static unsigned long write_buffer_len;
--- index.c.000 2006-05-07 22:36:04.000000000 +0300
+++ index.c 2006-05-07 22:36:40.000000000 +0300
@@ -43,6 +43,9 @@
strcpy(indexfile, cf->lockfile);
i =3D strlen(indexfile) - 5; /* .lock */
indexfile[i] =3D 0;
+#ifdef MUMNAP_BEFORE_RENAME
+ unmap_cache();
+#endif
i =3D rename(cf->lockfile, indexfile);
cf->lockfile[0] =3D 0;
return i;
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[patch] munmap-before-rename, cygwin need, Yakov Lerner, (Sun May 7, 12:58 pm)
Re: [patch] munmap-before-rename, cygwin need, Junio C Hamano, (Sun May 7, 2:14 pm)
Re: [patch] munmap-before-rename, cygwin need, Yakov Lerner, (Mon May 8, 3:58 am)
Re: [patch] munmap-before-rename, cygwin need, Yakov Lerner, (Mon May 8, 7:47 am)
Re: [patch] munmap-before-rename, cygwin need, Junio C Hamano, (Mon May 8, 1:51 pm)