Skip to content

Commit

Permalink
Windows: Work around misbehaved rename().
Browse files Browse the repository at this point in the history
Windows's rename() is based on the MoveFile() API, which fails if the
destination exists. Here we work around the problem by using MoveFileEx().
Furthermore, the posixly correct error is returned if the destination is
a directory.

The implementation is still slightly incomplete, however, because of the
missing error code translation: We assume that the failure is due to
permissions.

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
  • Loading branch information
Johannes Sixt committed Jun 23, 2008
1 parent 132a6e9 commit ea9e98c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
25 changes: 25 additions & 0 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@ char *mingw_getcwd(char *pointer, int len)
return ret;
}

#undef rename
int mingw_rename(const char *pold, const char *pnew)
{
/*
* Try native rename() first to get errno right.
* It is based on MoveFile(), which cannot overwrite existing files.
*/
if (!rename(pold, pnew))
return 0;
if (errno != EEXIST)
return -1;
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)) {
errno = EISDIR;
return -1;
}
}
errno = EACCES;
return -1;
}

struct passwd *getpwuid(int uid)
{
static char user_name[100];
Expand Down
3 changes: 3 additions & 0 deletions compat/mingw.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ int mingw_open (const char *filename, int oflags, ...);
char *mingw_getcwd(char *pointer, int len);
#define getcwd mingw_getcwd

int mingw_rename(const char*, const char*);
#define rename mingw_rename

/*
* git specific compatibility
*/
Expand Down

0 comments on commit ea9e98c

Please sign in to comment.