Merge branch 'js/mingw-o-append' into maint

Further fix for O_APPEND emulation on Windows

* js/mingw-o-append:
  mingw: fix mingw_open_append to work with named pipes
  t0051: test GIT_TRACE to a windows named pipe
This commit is contained in:
Junio C Hamano 2018-11-21 22:57:44 +09:00
commit e1372c37de
6 changed files with 129 additions and 3 deletions

View File

@ -738,6 +738,7 @@ TEST_BUILTINS_OBJS += test-submodule-config.o
TEST_BUILTINS_OBJS += test-subprocess.o
TEST_BUILTINS_OBJS += test-urlmatch-normalization.o
TEST_BUILTINS_OBJS += test-wildmatch.o
TEST_BUILTINS_OBJS += test-windows-named-pipe.o
TEST_BUILTINS_OBJS += test-write-cache.o
TEST_PROGRAMS_NEED_X += test-dump-fsmonitor

View File

@ -341,6 +341,19 @@ int mingw_mkdir(const char *path, int mode)
return ret;
}
/*
* Calling CreateFile() using FILE_APPEND_DATA and without FILE_WRITE_DATA
* is documented in [1] as opening a writable file handle in append mode.
* (It is believed that) this is atomic since it is maintained by the
* kernel unlike the O_APPEND flag which is racily maintained by the CRT.
*
* [1] https://docs.microsoft.com/en-us/windows/desktop/fileio/file-access-rights-constants
*
* This trick does not appear to work for named pipes. Instead it creates
* a named pipe client handle that cannot be written to. Callers should
* just use the regular _wopen() for them. (And since client handle gets
* bound to a unique server handle, it isn't really an issue.)
*/
static int mingw_open_append(wchar_t const *wfilename, int oflags, ...)
{
HANDLE handle;
@ -360,10 +373,12 @@ static int mingw_open_append(wchar_t const *wfilename, int oflags, ...)
NULL, create, FILE_ATTRIBUTE_NORMAL, NULL);
if (handle == INVALID_HANDLE_VALUE)
return errno = err_win_to_posix(GetLastError()), -1;
/*
* No O_APPEND here, because the CRT uses it only to reset the
* file pointer to EOF on write(); but that is not necessary
* for a file created with FILE_APPEND_DATA.
* file pointer to EOF before each write(); but that is not
* necessary (and may lead to races) for a file created with
* FILE_APPEND_DATA.
*/
fd = _open_osfhandle((intptr_t)handle, O_BINARY);
if (fd < 0)
@ -371,6 +386,21 @@ static int mingw_open_append(wchar_t const *wfilename, int oflags, ...)
return fd;
}
/*
* Does the pathname map to the local named pipe filesystem?
* That is, does it have a "//./pipe/" prefix?
*/
static int is_local_named_pipe_path(const char *filename)
{
return (is_dir_sep(filename[0]) &&
is_dir_sep(filename[1]) &&
filename[2] == '.' &&
is_dir_sep(filename[3]) &&
!strncasecmp(filename+4, "pipe", 4) &&
is_dir_sep(filename[8]) &&
filename[9]);
}
int mingw_open (const char *filename, int oflags, ...)
{
typedef int (*open_fn_t)(wchar_t const *wfilename, int oflags, ...);
@ -387,7 +417,7 @@ int mingw_open (const char *filename, int oflags, ...)
if (filename && !strcmp(filename, "/dev/null"))
filename = "nul";
if (oflags & O_APPEND)
if ((oflags & O_APPEND) && !is_local_named_pipe_path(filename))
open_fn = mingw_open_append;
else
open_fn = _wopen;

View File

@ -43,6 +43,9 @@ static struct test_cmd cmds[] = {
{ "subprocess", cmd__subprocess },
{ "urlmatch-normalization", cmd__urlmatch_normalization },
{ "wildmatch", cmd__wildmatch },
#ifdef GIT_WINDOWS_NATIVE
{ "windows-named-pipe", cmd__windows_named_pipe },
#endif
{ "write-cache", cmd__write_cache },
};

View File

@ -39,6 +39,9 @@ int cmd__submodule_config(int argc, const char **argv);
int cmd__subprocess(int argc, const char **argv);
int cmd__urlmatch_normalization(int argc, const char **argv);
int cmd__wildmatch(int argc, const char **argv);
#ifdef GIT_WINDOWS_NATIVE
int cmd__windows_named_pipe(int argc, const char **argv);
#endif
int cmd__write_cache(int argc, const char **argv);
#endif

View File

@ -0,0 +1,72 @@
#include "test-tool.h"
#include "git-compat-util.h"
#include "strbuf.h"
#ifdef GIT_WINDOWS_NATIVE
static const char *usage_string = "<pipe-filename>";
#define TEST_BUFSIZE (4096)
int cmd__windows_named_pipe(int argc, const char **argv)
{
const char *filename;
struct strbuf pathname = STRBUF_INIT;
int err;
HANDLE h;
BOOL connected;
char buf[TEST_BUFSIZE + 1];
if (argc < 2)
goto print_usage;
filename = argv[1];
if (strchr(filename, '/') || strchr(filename, '\\'))
goto print_usage;
strbuf_addf(&pathname, "//./pipe/%s", filename);
/*
* Create a single instance of the server side of the named pipe.
* This will allow exactly one client instance to connect to it.
*/
h = CreateNamedPipeA(
pathname.buf,
PIPE_ACCESS_INBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
TEST_BUFSIZE, TEST_BUFSIZE, 0, NULL);
if (h == INVALID_HANDLE_VALUE) {
err = err_win_to_posix(GetLastError());
fprintf(stderr, "CreateNamedPipe failed: %s\n",
strerror(err));
return err;
}
connected = ConnectNamedPipe(h, NULL)
? TRUE
: (GetLastError() == ERROR_PIPE_CONNECTED);
if (!connected) {
err = err_win_to_posix(GetLastError());
fprintf(stderr, "ConnectNamedPipe failed: %s\n",
strerror(err));
CloseHandle(h);
return err;
}
while (1) {
DWORD nbr;
BOOL success = ReadFile(h, buf, TEST_BUFSIZE, &nbr, NULL);
if (!success || nbr == 0)
break;
buf[nbr] = 0;
write(1, buf, nbr);
}
DisconnectNamedPipe(h);
CloseHandle(h);
return 0;
print_usage:
fprintf(stderr, "usage: %s %s\n", argv[0], usage_string);
return 1;
}
#endif

17
t/t0051-windows-named-pipe.sh Executable file
View File

@ -0,0 +1,17 @@
#!/bin/sh
test_description='Windows named pipes'
. ./test-lib.sh
test_expect_success MINGW 'o_append write to named pipe' '
GIT_TRACE="$(pwd)/expect" git status >/dev/null 2>&1 &&
{ test-tool windows-named-pipe t0051 >actual 2>&1 & } &&
pid=$! &&
sleep 1 &&
GIT_TRACE=//./pipe/t0051 git status >/dev/null 2>warning &&
wait $pid &&
test_cmp expect actual
'
test_done