osdep/io: implement rename() wrapper

This is needed to provide the POSIX behaviour of transparently
overwriting existing paths.
This commit is contained in:
sfan5 2023-11-06 22:24:10 +01:00
parent cad24deea1
commit 8f8b63d622
2 changed files with 16 additions and 0 deletions

View File

@ -484,6 +484,20 @@ int mp_creat(const char *filename, int mode)
return mp_open(filename, _O_CREAT | _O_WRONLY | _O_TRUNC, mode);
}
int mp_rename(const char *oldpath, const char *newpath)
{
wchar_t *woldpath = mp_from_utf8(NULL, oldpath),
*wnewpath = mp_from_utf8(NULL, newpath);
BOOL ok = MoveFileExW(woldpath, wnewpath, MOVEFILE_REPLACE_EXISTING);
talloc_free(woldpath);
talloc_free(wnewpath);
if (!ok) {
set_errno_from_lasterror();
return -1;
}
return 0;
}
FILE *mp_fopen(const char *filename, const char *mode)
{
if (!mode[0]) {

View File

@ -98,6 +98,7 @@ int mp_printf(const char *format, ...);
int mp_fprintf(FILE *stream, const char *format, ...);
int mp_open(const char *filename, int oflag, ...);
int mp_creat(const char *filename, int mode);
int mp_rename(const char *oldpath, const char *newpath);
FILE *mp_fopen(const char *filename, const char *mode);
DIR *mp_opendir(const char *path);
struct dirent *mp_readdir(DIR *dir);
@ -164,6 +165,7 @@ void mp_globfree(mp_glob_t *pglob);
#define fprintf(...) mp_fprintf(__VA_ARGS__)
#define open(...) mp_open(__VA_ARGS__)
#define creat(...) mp_creat(__VA_ARGS__)
#define rename(...) mp_rename(__VA_ARGS__)
#define fopen(...) mp_fopen(__VA_ARGS__)
#define opendir(...) mp_opendir(__VA_ARGS__)
#define readdir(...) mp_readdir(__VA_ARGS__)