aboutsummaryrefslogtreecommitdiff
path: root/compat/mingw.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2008-11-27 19:25:06 -0800
committerJunio C Hamano <gitster@pobox.com>2008-11-27 19:25:06 -0800
commit8c1944dd3468c8585ab3bdf149834d3256c797b4 (patch)
treece7b8d52858e5bc3dcc83090d9d50f088654bdbf /compat/mingw.c
parentef4daa74af10da41d9af475579a138645ce6d295 (diff)
parent632f70178784291bd2974e07fdcd2b8e8608f252 (diff)
downloadgit-8c1944dd3468c8585ab3bdf149834d3256c797b4.tar.gz
git-8c1944dd3468c8585ab3bdf149834d3256c797b4.tar.xz
Merge branch 'js/mingw-rename-fix'
* js/mingw-rename-fix: compat/mingw.c: Teach mingw_rename() to replace read-only files
Diffstat (limited to 'compat/mingw.c')
-rw-r--r--compat/mingw.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/compat/mingw.c b/compat/mingw.c
index b534a8a47..3dbe6a77f 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -819,6 +819,8 @@ int mingw_connect(int sockfd, struct sockaddr *sa, size_t sz)
#undef rename
int mingw_rename(const char *pold, const char *pnew)
{
+ DWORD attrs;
+
/*
* Try native rename() first to get errno right.
* It is based on MoveFile(), which cannot overwrite existing files.
@@ -830,12 +832,19 @@ int mingw_rename(const char *pold, const char *pnew)
if (MoveFileEx(pold, pnew, MOVEFILE_REPLACE_EXISTING))
return 0;
/* TODO: translate more errors */
- if (GetLastError() == ERROR_ACCESS_DENIED) {
- DWORD attrs = GetFileAttributes(pnew);
- if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) {
+ if (GetLastError() == ERROR_ACCESS_DENIED &&
+ (attrs = GetFileAttributes(pnew)) != INVALID_FILE_ATTRIBUTES) {
+ if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
errno = EISDIR;
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;
return -1;