Skip to content

Commit

Permalink
Fix append failure issue under remote directories git-for-windows#2753
Browse files Browse the repository at this point in the history
  • Loading branch information
sunzhuoshi committed Jan 15, 2022
1 parent 9b2e006 commit 5586a7e
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,47 @@ static int is_local_named_pipe_path(const char *filename)
filename[9]);
}

static int is_local_disk_file(const char *filename)
{
wchar_t wpath[MAX_LONG_PATH];
wchar_t wfullpath[MAX_LONG_PATH];
size_t windex;

if (is_local_named_pipe_path(filename))
return 0;

/*
* Do everything in wide chars because the drive letter might be
* a multi-byte sequence. See win32_has_dos_drive_prefix().
*/
if (xutftowcs_long_path(wpath, filename) < 0)
return 1;

/*
* GetDriveTypeW() requires a final slash.
*/
windex = wcslen(wpath) - 1;
while (windex >= 0) {
if (is_dir_sep(wpath[windex]))
break;
windex--;
}
wpath[windex++] = L'\\';
wpath[windex] = 0;

/*
* Normalize the path. If nothing else, this converts forward
* slashes to backslashes. This is essential to get GetDriveTypeW()
* correctly handle some UNC "\\server\share\..." paths.
*/
if (!GetFullPathNameW(wpath, MAX_LONG_PATH, wfullpath, NULL))
return 1;

if (GetDriveTypeW(wfullpath) == DRIVE_REMOTE)
return 0;
return 1;
}

int mingw_open (const char *filename, int oflags, ...)
{
typedef int (*open_fn_t)(wchar_t const *wfilename, int oflags, ...);
Expand All @@ -759,7 +800,7 @@ int mingw_open (const char *filename, int oflags, ...)
return -1;
}

if ((oflags & O_APPEND) && !is_local_named_pipe_path(filename))
if ((oflags & O_APPEND) && is_local_disk_file(filename))
open_fn = mingw_open_append;
else
open_fn = _wopen;
Expand Down

0 comments on commit 5586a7e

Please sign in to comment.