Merge branch 'bw/mingw-avoid-inheriting-fd-to-lockfile'
authorJunio C Hamano <gitster@pobox.com>
Thu, 25 Aug 2016 20:55:07 +0000 (13:55 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 25 Aug 2016 20:55:07 +0000 (13:55 -0700)
The tempfile (hence its user lockfile) API lets the caller to open
a file descriptor to a temporary file, write into it and then
finalize it by first closing the filehandle and then either
removing or renaming the temporary file. When the process spawns a
subprocess after obtaining the file descriptor, and if the
subprocess has not exited when the attempt to remove or rename is
made, the last step fails on Windows, because the subprocess has
the file descriptor still open. Open tempfile with O_CLOEXEC flag
to avoid this (on Windows, this is mapped to O_NOINHERIT).

* bw/mingw-avoid-inheriting-fd-to-lockfile:
mingw: ensure temporary file handles are not inherited by child processes
t6026-merge-attr: child processes must not inherit index.lock handles

1  2 
compat/mingw.h
git-compat-util.h
diff --combined compat/mingw.h
index 2cadb816eebf305e84181379f48aec061ea158f7,6090e83947f82312463e1fabba6b93a4d6b52f16..034fff9479d03d2a2e3c7017a4fe4131461f0ec6
@@@ -67,6 -67,10 +67,10 @@@ typedef int pid_t
  #define F_SETFD 2
  #define FD_CLOEXEC 0x1
  
+ #if !defined O_CLOEXEC && defined O_NOINHERIT
+ #define O_CLOEXEC     O_NOINHERIT
+ #endif
  #ifndef EAFNOSUPPORT
  #define EAFNOSUPPORT WSAEAFNOSUPPORT
  #endif
@@@ -417,6 -421,9 +421,6 @@@ int mingw_offset_1st_component(const ch
  #include <inttypes.h>
  #endif
  
 -void mingw_open_html(const char *path);
 -#define open_html mingw_open_html
 -
  /**
   * Converts UTF-8 encoded string to UTF-16LE.
   *
@@@ -535,7 -542,7 +539,7 @@@ extern CRITICAL_SECTION pinfo_cs
  void mingw_startup(void);
  #define main(c,v) dummy_decl_mingw_main(void); \
  static int mingw_main(c,v); \
 -int main(int argc, char **argv) \
 +int main(int argc, const char **argv) \
  { \
        mingw_startup(); \
        return mingw_main(__argc, (void *)__argv); \
diff --combined git-compat-util.h
index f52e00b58057c7bfe040e516835a42d117be1de2,f1f98b50d7e3453c66b492e7763f3640198f803f..db89ba774891a9608531e52252de19d552cbf535
@@@ -473,23 -473,6 +473,23 @@@ static inline int skip_prefix(const cha
        return 0;
  }
  
 +/*
 + * Like skip_prefix, but promises never to read past "len" bytes of the input
 + * buffer, and returns the remaining number of bytes in "out" via "outlen".
 + */
 +static inline int skip_prefix_mem(const char *buf, size_t len,
 +                                const char *prefix,
 +                                const char **out, size_t *outlen)
 +{
 +      size_t prefix_len = strlen(prefix);
 +      if (prefix_len <= len && !memcmp(buf, prefix, prefix_len)) {
 +              *out = buf + prefix_len;
 +              *outlen = len - prefix_len;
 +              return 1;
 +      }
 +      return 0;
 +}
 +
  /*
   * If buf ends with suffix, return 1 and subtract the length of the suffix
   * from *len. Otherwise, return 0 and leave *len untouched.
@@@ -667,6 -650,10 +667,10 @@@ void *gitmemmem(const void *haystack, s
  #define getpagesize() sysconf(_SC_PAGESIZE)
  #endif
  
+ #ifndef O_CLOEXEC
+ #define O_CLOEXEC 0
+ #endif
  #ifdef FREAD_READS_DIRECTORIES
  #ifdef fopen
  #undef fopen
@@@ -815,7 -802,7 +819,7 @@@ extern FILE *fopen_for_writing(const ch
   * you can do:
   *
   *   struct foo *f;
 - *   FLEX_ALLOC_STR(f, name, src);
 + *   FLEXPTR_ALLOC_STR(f, name, src);
   *
   * and "name" will point to a block of memory after the struct, which will be
   * freed along with the struct (but the pointer can be repointed anywhere).
@@@ -1062,5 -1049,3 +1066,5 @@@ struct tm *git_gmtime_r(const time_t *
  #endif
  
  #endif
 +
 +extern int cmd_main(int, const char **);