Skip to content

Commit 515049a

Browse files
sunzhuoshidscho
authored andcommitted
Add config option windows.appendAtomically
Atomic append on windows is only supported on local disk files, and it may cause errors in other situations, e.g. network file system. If that is the case, this config option should be used to turn atomic append off. Co-Authored-By: Johannes Schindelin <[email protected]> Signed-off-by: 孙卓识 <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 11a5032 commit 515049a

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

Documentation/config.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,4 +558,6 @@ include::config/versionsort.adoc[]
558558

559559
include::config/web.adoc[]
560560

561+
include::config/windows.adoc[]
562+
561563
include::config/worktree.adoc[]

Documentation/config/windows.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
windows.appendAtomically::
2+
By default, append atomic API is used on windows. But it works only with
3+
local disk files, if you're working on a network file system, you should
4+
set it false to turn it off.

compat/mingw.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "gettext.h"
2222
#define SECURITY_WIN32
2323
#include <sspi.h>
24+
#include "../repository.h"
2425

2526
#define HCAST(type, handle) ((type)(intptr_t)handle)
2627

@@ -617,6 +618,7 @@ static int is_local_named_pipe_path(const char *filename)
617618

618619
int mingw_open (const char *filename, int oflags, ...)
619620
{
621+
static int append_atomically = -1;
620622
typedef int (*open_fn_t)(wchar_t const *wfilename, int oflags, ...);
621623
va_list args;
622624
unsigned mode;
@@ -633,7 +635,16 @@ int mingw_open (const char *filename, int oflags, ...)
633635
return -1;
634636
}
635637

636-
if ((oflags & O_APPEND) && !is_local_named_pipe_path(filename))
638+
/*
639+
* Only set append_atomically to default value(1) when repo is initialized
640+
* and fail to get config value
641+
*/
642+
if (append_atomically < 0 && the_repository && the_repository->commondir &&
643+
git_config_get_bool("windows.appendatomically", &append_atomically))
644+
append_atomically = 1;
645+
646+
if (append_atomically && (oflags & O_APPEND) &&
647+
!is_local_named_pipe_path(filename))
637648
open_fn = mingw_open_append;
638649
else if (!(oflags & ~(O_ACCMODE | O_NOINHERIT)))
639650
open_fn = mingw_open_existing;
@@ -784,9 +795,28 @@ ssize_t mingw_write(int fd, const void *buf, size_t len)
784795

785796
/* check if fd is a pipe */
786797
HANDLE h = (HANDLE) _get_osfhandle(fd);
787-
if (GetFileType(h) != FILE_TYPE_PIPE)
798+
if (GetFileType(h) != FILE_TYPE_PIPE) {
799+
if (orig == EINVAL) {
800+
wchar_t path[MAX_PATH];
801+
DWORD ret = GetFinalPathNameByHandleW(h, path,
802+
ARRAY_SIZE(path), 0);
803+
UINT drive_type = ret > 0 && ret < ARRAY_SIZE(path) ?
804+
GetDriveTypeW(path) : DRIVE_UNKNOWN;
805+
806+
/*
807+
* The default atomic append causes such an error on
808+
* network file systems, in such a case, it should be
809+
* turned off via config.
810+
*
811+
* `drive_type` of UNC path: DRIVE_NO_ROOT_DIR
812+
*/
813+
if (DRIVE_NO_ROOT_DIR == drive_type || DRIVE_REMOTE == drive_type)
814+
warning("invalid write operation detected; you may try:\n"
815+
"\n\tgit config windows.appendAtomically false");
816+
}
817+
788818
errno = orig;
789-
else if (orig == EINVAL)
819+
} else if (orig == EINVAL)
790820
errno = EPIPE;
791821
else {
792822
DWORD buf_size;

0 commit comments

Comments
 (0)