From 4a964f25668a26caae35122eb824e35175d0de1c Mon Sep 17 00:00:00 2001 From: Tor Lillqvist Date: Thu, 1 Dec 2005 04:03:58 +0000 Subject: [PATCH] In the Win32 implementation, strip trailing slash(es) for non-root 2005-12-01 Tor Lillqvist * glib/gstdio.c (g_stat): In the Win32 implementation, strip trailing slash(es) for non-root folders. stat() fails if non-root folders are specified with trailing slashes. It's too much hassle to demand that callers strip such slashes themselves, especially as it is easy to get it wrong and strip the slash of a root folder. (g_rename): On NT-based Windows, use MoveFileEx() with MOVEFILE_REPLACE_EXISTING to better match Unix behaviour. --- ChangeLog | 11 +++++++++++ ChangeLog.pre-2-10 | 11 +++++++++++ ChangeLog.pre-2-12 | 11 +++++++++++ glib/gstdio.c | 37 +++++++++++++++++++++++++++++++++++-- 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index ddd46ce1..fcbeee22 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2005-12-01 Tor Lillqvist + + * glib/gstdio.c (g_stat): In the Win32 implementation, strip + trailing slash(es) for non-root folders. stat() fails if non-root + folders are specified with trailing slashes. It's too much hassle + to demand that callers strip such slashes themselves, especially + as it is easy to get it wrong and strip the slash of a root + folder. + (g_rename): On NT-based Windows, use MoveFileEx() with + MOVEFILE_REPLACE_EXISTING to better match Unix behaviour. + 2005-11-28 Matthias Clasen Fix G_STMT_START / G_STMT_END on Solaris. (#321972, diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index ddd46ce1..fcbeee22 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,14 @@ +2005-12-01 Tor Lillqvist + + * glib/gstdio.c (g_stat): In the Win32 implementation, strip + trailing slash(es) for non-root folders. stat() fails if non-root + folders are specified with trailing slashes. It's too much hassle + to demand that callers strip such slashes themselves, especially + as it is easy to get it wrong and strip the slash of a root + folder. + (g_rename): On NT-based Windows, use MoveFileEx() with + MOVEFILE_REPLACE_EXISTING to better match Unix behaviour. + 2005-11-28 Matthias Clasen Fix G_STMT_START / G_STMT_END on Solaris. (#321972, diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index ddd46ce1..fcbeee22 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,14 @@ +2005-12-01 Tor Lillqvist + + * glib/gstdio.c (g_stat): In the Win32 implementation, strip + trailing slash(es) for non-root folders. stat() fails if non-root + folders are specified with trailing slashes. It's too much hassle + to demand that callers strip such slashes themselves, especially + as it is easy to get it wrong and strip the slash of a root + folder. + (g_rename): On NT-based Windows, use MoveFileEx() with + MOVEFILE_REPLACE_EXISTING to better match Unix behaviour. + 2005-11-28 Matthias Clasen Fix G_STMT_START / G_STMT_END on Solaris. (#321972, diff --git a/glib/gstdio.c b/glib/gstdio.c index 53cd135d..c6cd7ba8 100644 --- a/glib/gstdio.c +++ b/glib/gstdio.c @@ -33,6 +33,7 @@ #endif #ifdef G_OS_WIN32 +#include #include #include #include @@ -372,8 +373,26 @@ g_rename (const gchar *oldfilename, return -1; } - retval = _wrename (woldfilename, wnewfilename); - save_errno = errno; + if (MoveFileExW (woldfilename, wnewfilename, MOVEFILE_REPLACE_EXISTING)) + retval = 0; + else + { + retval = -1; + switch (GetLastError ()) + { +#define CASE(a,b) case ERROR_##a: save_errno = b; break + CASE (FILE_NOT_FOUND, ENOENT); + CASE (PATH_NOT_FOUND, ENOENT); + CASE (ACCESS_DENIED, EACCES); + CASE (NOT_SAME_DEVICE, EXDEV); + CASE (LOCK_VIOLATION, EACCES); + CASE (SHARING_VIOLATION, EACCES); + CASE (FILE_EXISTS, EEXIST); + CASE (ALREADY_EXISTS, EEXIST); +#undef CASE + default: save_errno = EIO; + } + } g_free (woldfilename); g_free (wnewfilename); @@ -570,6 +589,7 @@ g_stat (const gchar *filename, wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL); int retval; int save_errno; + int len; if (wfilename == NULL) { @@ -577,6 +597,12 @@ g_stat (const gchar *filename, return -1; } + len = wcslen (wfilename); + while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1])) + len--; + if (len > g_path_skip_root (filename) - filename) + wfilename[len] = '\0'; + retval = _wstat (wfilename, (struct _stat *) buf); save_errno = errno; @@ -590,6 +616,7 @@ g_stat (const gchar *filename, gchar *cp_filename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL); int retval; int save_errno; + int len; if (cp_filename == NULL) { @@ -597,6 +624,12 @@ g_stat (const gchar *filename, return -1; } + len = strlen (cp_filename); + while (len > 0 && G_IS_DIR_SEPARATOR (cp_filename[len-1])) + len--; + if (len > g_path_skip_root (filename) - filename) + cp_filename[len] = '\0'; + retval = stat (cp_filename, buf); save_errno = errno; -- 2.34.1