Skip to content

Commit

Permalink
Merge branch 'js/mingw-rename-fix' into maint
Browse files Browse the repository at this point in the history
* js/mingw-rename-fix:
  compat/mingw.c: Teach mingw_rename() to replace read-only files
  • Loading branch information
Junio C Hamano committed Dec 3, 2008
2 parents 25e30fa + 632f701 commit e23f682
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down

0 comments on commit e23f682

Please sign in to comment.