diff options
author | Johannes Sixt <johannes.sixt@telecom.at> | 2007-12-07 22:19:40 +0100 |
---|---|---|
committer | Johannes Sixt <johannes.sixt@telecom.at> | 2008-06-23 13:40:18 +0200 |
commit | ea9e98c3a54d3b693367fbb8c87812b17ad95869 (patch) | |
tree | cc4abaa44db9b3067917c2d20c29929d2f9005c1 /compat/mingw.c | |
parent | 132a6e903fd912d02392db55c407267103766f19 (diff) | |
download | git-ea9e98c3a54d3b693367fbb8c87812b17ad95869.tar.gz git-ea9e98c3a54d3b693367fbb8c87812b17ad95869.tar.xz |
Windows: Work around misbehaved rename().
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>
Diffstat (limited to 'compat/mingw.c')
-rw-r--r-- | compat/mingw.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/compat/mingw.c b/compat/mingw.c index 0e1ddbe4d..c6a5c1b21 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -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]; |