mirror of
https://github.com/git/git
synced 2024-10-06 00:29:28 +00:00
compat/mingw.c: Teach mingw_rename() to replace read-only files
On POSIX, rename() can replace files that are not writable. On Windows, however, read-only files cannot be replaced without additional efforts: We have to make the destination writable first. Since the situations where the destination is read-only are rare, we do not make the destination writable on every invocation, but only if the first try to rename a file failed with an "access denied" error. Signed-off-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
3eb91bfc0d
commit
632f701787
|
@ -865,6 +865,8 @@ int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz)
|
||||||
#undef rename
|
#undef rename
|
||||||
int mingw_rename(const char *pold, const char *pnew)
|
int mingw_rename(const char *pold, const char *pnew)
|
||||||
{
|
{
|
||||||
|
DWORD attrs;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Try native rename() first to get errno right.
|
* Try native rename() first to get errno right.
|
||||||
* It is based on MoveFile(), which cannot overwrite existing files.
|
* It is based on MoveFile(), which cannot overwrite existing files.
|
||||||
|
@ -876,12 +878,19 @@ int mingw_rename(const char *pold, const char *pnew)
|
||||||
if (MoveFileEx(pold, pnew, MOVEFILE_REPLACE_EXISTING))
|
if (MoveFileEx(pold, pnew, MOVEFILE_REPLACE_EXISTING))
|
||||||
return 0;
|
return 0;
|
||||||
/* TODO: translate more errors */
|
/* TODO: translate more errors */
|
||||||
if (GetLastError() == ERROR_ACCESS_DENIED) {
|
if (GetLastError() == ERROR_ACCESS_DENIED &&
|
||||||
DWORD attrs = GetFileAttributes(pnew);
|
(attrs = GetFileAttributes(pnew)) != INVALID_FILE_ATTRIBUTES) {
|
||||||
if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) {
|
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
|
||||||
errno = EISDIR;
|
errno = EISDIR;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
if ((attrs & FILE_ATTRIBUTE_READONLY) &&
|
||||||
|
SetFileAttributes(pnew, attrs & ~FILE_ATTRIBUTE_READONLY)) {
|
||||||
|
if (MoveFileEx(pold, pnew, MOVEFILE_REPLACE_EXISTING))
|
||||||
|
return 0;
|
||||||
|
/* revert file attributes on failure */
|
||||||
|
SetFileAttributes(pnew, attrs);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
errno = EACCES;
|
errno = EACCES;
|
||||||
return -1;
|
return -1;
|
||||||
|
|
Loading…
Reference in a new issue