summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Junio C Hamano <gitster@pobox.com>2008-12-02 22:38:07 -0800
committerLibravatar Junio C Hamano <gitster@pobox.com>2008-12-02 22:38:07 -0800
commite23f6822df5ee0acfe244c819a419c3e7c8c9e7e (patch)
tree2ca7f382b895c7f15c3d716e6be233d9c881ca80
parentUser's Manual: remove duplicated url at the end of Appendix B (diff)
parentcompat/mingw.c: Teach mingw_rename() to replace read-only files (diff)
downloadtgif-e23f6822df5ee0acfe244c819a419c3e7c8c9e7e.tar.xz
Merge branch 'js/mingw-rename-fix' into maint
* js/mingw-rename-fix: compat/mingw.c: Teach mingw_rename() to replace read-only files
-rw-r--r--compat/mingw.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/compat/mingw.c b/compat/mingw.c
index 772cad510d..45733f9e04 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -865,6 +865,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.
@@ -876,12 +878,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;