From d53b5ad0b8396f7956cf0c269944c0deaeddcff8 Mon Sep 17 00:00:00 2001 From: Marcus Brinkmann Date: Tue, 16 Mar 2010 03:27:42 +0100 Subject: [PATCH 01/10] Prepare for WinCE port: Convert windows code to native API, avoid errno. --- dbus/dbus-file-win.c | 326 +++++++++++++++++++++-------------------- dbus/dbus-file.h | 3 - dbus/dbus-nonce.c | 37 +++--- dbus/dbus-pipe-win.c | 20 ++-- dbus/dbus-sockets-win.h | 4 +- dbus/dbus-spawn-win.c | 82 +++++++++-- dbus/dbus-sysdeps-util-win.c | 231 ++++++++++++++++------------- dbus/dbus-sysdeps-win.c | 177 +++++++++++------------ dbus/dbus-sysdeps-win.h | 12 +-- dbus/dbus-sysdeps.c | 19 ++- dbus/dbus-sysdeps.h | 1 + 11 files changed, 497 insertions(+), 415 deletions(-) diff --git a/dbus/dbus-file-win.c b/dbus/dbus-file-win.c index 2dfba00..d1a81e2 100644 --- a/dbus/dbus-file-win.c +++ b/dbus/dbus-file-win.c @@ -29,12 +29,72 @@ #include "dbus-sysdeps-win.h" #include "dbus-pipe.h" -#include -#include -#include -#include - #include + + +/** + * Thin wrapper around the read() system call that appends + * the data it reads to the DBusString buffer. It appends + * up to the given count. + * + * @param hnd the HANDLE to read from + * @param buffer the buffer to append data to + * @param count the amount of data to read + * @param error place to set an error + * @returns the number of bytes read or -1 + */ +static int +_dbus_file_read (HANDLE hnd, + DBusString *buffer, + int count, + DBusError *error) +{ + BOOL result; + DWORD bytes_read; + int start; + char *data; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); + + _dbus_assert (count >= 0); + + start = _dbus_string_get_length (buffer); + + if (!_dbus_string_lengthen (buffer, count)) + { + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + return -1; + } + + data = _dbus_string_get_data_len (buffer, start, count); + + again: + + result = ReadFile (hnd, data, count, &bytes_read, NULL); + if (result == 0) + { + char *emsg = _dbus_win_error_string (GetLastError ()); + dbus_set_error (error, _dbus_win_error_from_last_error (), + "Failed to read from 0x%x: %s", hnd, emsg); + _dbus_win_free_error_string (emsg); + return -1; + } + + if (bytes_read) + { + /* put length back (doesn't actually realloc) */ + _dbus_string_set_length (buffer, start + bytes_read); + +#if 0 + if (bytes_read > 0) + _dbus_verbose_bytes_of_string (buffer, start, bytes_read); +#endif + } + + return bytes_read; +} + + /** * Appends the contents of the given file to the string, * returning error code. At the moment, won't open a file @@ -50,8 +110,9 @@ _dbus_file_get_contents (DBusString *str, const DBusString *filename, DBusError *error) { - int fd; - struct _stati64 sb; + HANDLE hnd; + DWORD fsize; + DWORD fsize_hi; int orig_len; int total; const char *filename_c; @@ -60,62 +121,67 @@ _dbus_file_get_contents (DBusString *str, filename_c = _dbus_string_get_const_data (filename); - fd = _open (filename_c, O_RDONLY | O_BINARY); - if (fd < 0) + hnd = CreateFileA (filename_c, GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + if (hnd == INVALID_HANDLE_VALUE) { - dbus_set_error (error, _dbus_error_from_errno (errno), - "Failed to open \"%s\": %s", - filename_c, - strerror (errno)); + char *emsg = _dbus_win_error_string (GetLastError ()); + dbus_set_error (error, _dbus_win_error_from_last_error (), + "Failed to open \"%s\": %s", filename_c, emsg); + _dbus_win_free_error_string (emsg); return FALSE; } - _dbus_verbose ("file %s fd %d opened\n", filename_c, fd); - - if (_fstati64 (fd, &sb) < 0) - { - dbus_set_error (error, _dbus_error_from_errno (errno), - "Failed to stat \"%s\": %s", - filename_c, - strerror (errno)); + _dbus_verbose ("file %s fd 0x%x opened\n", filename_c, hnd); + + fsize = GetFileSize (hnd, &fsize_hi); + if (fsize == 0xFFFFFFFF && GetLastError() != NO_ERROR) + { + char *emsg = _dbus_win_error_string (GetLastError ()); + dbus_set_error (error, _dbus_win_error_from_last_error (), + "Failed to get file size for \"%s\": %s", + filename_c, emsg); + _dbus_win_free_error_string (emsg); - _dbus_verbose ("fstat() failed: %s", - strerror (errno)); + _dbus_verbose ("GetFileSize() failed: %s", emsg); - _close (fd); + CloseHandle (hnd); return FALSE; } - if (sb.st_size > _DBUS_ONE_MEGABYTE) + if (fsize_hi != 0 || fsize > _DBUS_ONE_MEGABYTE) { dbus_set_error (error, DBUS_ERROR_FAILED, - "File size %lu of \"%s\" is too large.", - (unsigned long) sb.st_size, filename_c); - _close (fd); + "File size %lu/%lu of \"%s\" is too large.", + (unsigned long) fsize_hi, + (unsigned long) fsize, filename_c); + CloseHandle (hnd); return FALSE; } total = 0; orig_len = _dbus_string_get_length (str); - if (sb.st_size > 0 && S_ISREG (sb.st_mode)) + if (fsize > 0) { int bytes_read; - while (total < (int) sb.st_size) + while (total < fsize) { - bytes_read = _dbus_file_read (fd, str, sb.st_size - total); + bytes_read = _dbus_file_read (hnd, str, fsize - total, error); if (bytes_read <= 0) { - dbus_set_error (error, _dbus_error_from_errno (errno), - "Error reading \"%s\": %s", - filename_c, - strerror (errno)); - - _dbus_verbose ("read() failed: %s", - strerror (errno)); - - _close (fd); + if (bytes_read == 0) + { + dbus_set_error (error, DBUS_ERROR_FAILED, + "Premature EOF reading \"%s\"", + filename_c); + } + else + _DBUS_ASSERT_ERROR_IS_SET (error); + + CloseHandle (hnd); _dbus_string_set_length (str, orig_len); return FALSE; } @@ -123,25 +189,17 @@ _dbus_file_get_contents (DBusString *str, total += bytes_read; } - _close (fd); + CloseHandle (hnd); return TRUE; } - else if (sb.st_size != 0) - { - _dbus_verbose ("Can only open regular files at the moment.\n"); - dbus_set_error (error, DBUS_ERROR_FAILED, - "\"%s\" is not a regular file", - filename_c); - _close (fd); - return FALSE; - } else { - _close (fd); + CloseHandle (hnd); return TRUE; } } + /** * Writes a string out to a file. If the file exists, * it will be atomically overwritten by the new data. @@ -156,7 +214,7 @@ _dbus_string_save_to_file (const DBusString *str, const DBusString *filename, DBusError *error) { - int fd; + HANDLE hnd; int bytes_to_write; const char *filename_c; DBusString tmp_filename; @@ -168,7 +226,7 @@ _dbus_string_save_to_file (const DBusString *str, _DBUS_ASSERT_ERROR_IS_CLEAR (error); - fd = -1; + hnd = INVALID_HANDLE_VALUE; retval = FALSE; need_unlink = FALSE; @@ -203,17 +261,20 @@ _dbus_string_save_to_file (const DBusString *str, filename_c = _dbus_string_get_const_data (filename); tmp_filename_c = _dbus_string_get_const_data (&tmp_filename); - fd = _open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT, - 0600); - if (fd < 0) + hnd = CreateFileA (tmp_filename_c, GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, + INVALID_HANDLE_VALUE); + if (hnd == INVALID_HANDLE_VALUE) { - dbus_set_error (error, _dbus_error_from_errno (errno), - "Could not create %s: %s", tmp_filename_c, - strerror (errno)); + char *emsg = _dbus_win_error_string (GetLastError ()); + dbus_set_error (error, _dbus_win_error_from_last_error (), + "Could not create \"%s\": %s", filename_c, emsg); + _dbus_win_free_error_string (emsg); goto out; } - _dbus_verbose ("tmp file %s fd %d opened\n", tmp_filename_c, fd); + _dbus_verbose ("tmp file %s fd 0x%x opened\n", tmp_filename_c, hnd); need_unlink = TRUE; @@ -223,40 +284,42 @@ _dbus_string_save_to_file (const DBusString *str, while (total < bytes_to_write) { - int bytes_written; + DWORD bytes_written; + BOOL res; - bytes_written = _write (fd, str_c + total, bytes_to_write - total); + res = WriteFile (hnd, str_c + total, bytes_to_write - total, + &bytes_written, NULL); - if (bytes_written <= 0) + if (res == 0 || bytes_written <= 0) { - dbus_set_error (error, _dbus_error_from_errno (errno), - "Could not write to %s: %s", tmp_filename_c, - strerror (errno)); + char *emsg = _dbus_win_error_string (GetLastError ()); + dbus_set_error (error, _dbus_win_error_from_last_error (), + "Could not write to %s: %s", tmp_filename_c, emsg); + _dbus_win_free_error_string (emsg); goto out; } total += bytes_written; } - if (_close (fd) < 0) + if (CloseHandle (hnd) == 0) { - dbus_set_error (error, _dbus_error_from_errno (errno), - "Could not close file %s: %s", - tmp_filename_c, strerror (errno)); - + char *emsg = _dbus_win_error_string (GetLastError ()); + dbus_set_error (error, _dbus_win_error_from_last_error (), + "Could not close file %s: %s", tmp_filename_c, emsg); + _dbus_win_free_error_string (emsg); goto out; } - fd = -1; + hnd = INVALID_HANDLE_VALUE; /* Unlike rename(), MoveFileEx() can replace existing files */ if (!MoveFileExA (tmp_filename_c, filename_c, MOVEFILE_REPLACE_EXISTING)) { char *emsg = _dbus_win_error_string (GetLastError ()); - dbus_set_error (error, DBUS_ERROR_FAILED, - "Could not rename %s to %s: %s", - tmp_filename_c, filename_c, - emsg); + dbus_set_error (error, _dbus_win_error_from_last_error (), + "Could not rename %s to %s: %s", + tmp_filename_c, filename_c, emsg); _dbus_win_free_error_string (emsg); goto out; @@ -269,12 +332,16 @@ _dbus_string_save_to_file (const DBusString *str, out: /* close first, then unlink */ - if (fd >= 0) - _close (fd); + if (hnd != INVALID_HANDLE_VALUE) + CloseHandle (hnd); - if (need_unlink && _unlink (tmp_filename_c) < 0) - _dbus_verbose ("failed to unlink temp file %s: %s\n", - tmp_filename_c, strerror (errno)); + if (need_unlink && DeleteFileA (tmp_filename_c) == 0) + { + char *emsg = _dbus_win_error_string (GetLastError ()); + _dbus_verbose ("Failed to unlink temp file %s: %s", tmp_filename_c, + emsg); + _dbus_win_free_error_string (emsg); + } _dbus_string_free (&tmp_filename); @@ -295,99 +362,40 @@ dbus_bool_t _dbus_create_file_exclusively (const DBusString *filename, DBusError *error) { - int fd; + HANDLE hnd; const char *filename_c; _DBUS_ASSERT_ERROR_IS_CLEAR (error); filename_c = _dbus_string_get_const_data (filename); - fd = _open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT, - 0600); - if (fd < 0) + hnd = CreateFileA (filename_c, GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, + INVALID_HANDLE_VALUE); + if (hnd == INVALID_HANDLE_VALUE) { - dbus_set_error (error, - DBUS_ERROR_FAILED, - "Could not create file %s: %s\n", - filename_c, - strerror (errno)); + char *emsg = _dbus_win_error_string (GetLastError ()); + dbus_set_error (error, _dbus_win_error_from_last_error (), + "Could not create file %s: %s", + filename_c, emsg); + _dbus_win_free_error_string (emsg); return FALSE; } - _dbus_verbose ("exclusive file %s fd %d opened\n", filename_c, fd); + _dbus_verbose ("exclusive file %s fd 0x%x opened\n", filename_c, hnd); - if (_close (fd) < 0) + if (CloseHandle (hnd) == 0) { - dbus_set_error (error, - DBUS_ERROR_FAILED, - "Could not close file %s: %s\n", - filename_c, - strerror (errno)); + char *emsg = _dbus_win_error_string (GetLastError ()); + dbus_set_error (error, _dbus_win_error_from_last_error (), + "Could not close file %s: %s", + filename_c, emsg); + _dbus_win_free_error_string (emsg); + return FALSE; } return TRUE; } -/** - * Thin wrapper around the read() system call that appends - * the data it reads to the DBusString buffer. It appends - * up to the given count, and returns the same value - * and same errno as read(). The only exception is that - * _dbus_file_read() handles EINTR for you. Also, - * _dbus_file_read() can return ENOMEM. - * - * @param fd the file descriptor to read from - * @param buffer the buffer to append data to - * @param count the amount of data to read - * @returns the number of bytes read or -1 - */ -int -_dbus_file_read (int fd, - DBusString *buffer, - int count) -{ - int bytes_read; - int start; - char *data; - - _dbus_assert (count >= 0); - - start = _dbus_string_get_length (buffer); - - if (!_dbus_string_lengthen (buffer, count)) - { - errno = ENOMEM; - return -1; - } - - data = _dbus_string_get_data_len (buffer, start, count); - - again: - - bytes_read = _read (fd, data, count); - - if (bytes_read < 0) - { - if (errno == EINTR) - goto again; - else - { - /* put length back (note that this doesn't actually realloc anything) */ - _dbus_string_set_length (buffer, start); - return -1; - } - } - else - { - /* put length back (doesn't actually realloc) */ - _dbus_string_set_length (buffer, start + bytes_read); - -#if 0 - if (bytes_read > 0) - _dbus_verbose_bytes_of_string (buffer, start, bytes_read); -#endif - - return bytes_read; - } -} diff --git a/dbus/dbus-file.h b/dbus/dbus-file.h index b83fc6d..7943198 100644 --- a/dbus/dbus-file.h +++ b/dbus/dbus-file.h @@ -54,9 +54,6 @@ dbus_bool_t _dbus_create_file_exclusively (const DBusString *filename, DBusError *error); dbus_bool_t _dbus_delete_file (const DBusString *filename, DBusError *error); -int _dbus_file_read (int fd, - DBusString *buffer, - int count); /** @} */ diff --git a/dbus/dbus-nonce.c b/dbus/dbus-nonce.c index 3e5070f..5143939 100644 --- a/dbus/dbus-nonce.c +++ b/dbus/dbus-nonce.c @@ -30,10 +30,6 @@ #include -#ifdef HAVE_ERRNO_H -# include -#endif - static dbus_bool_t do_check_nonce (int fd, const DBusString *nonce, DBusError *error) { @@ -191,7 +187,7 @@ generate_and_write_nonce (const DBusString *filename, DBusError *error) * indicate whether the server accepted the nonce. */ dbus_bool_t -_dbus_send_nonce(int fd, const DBusString *noncefile, DBusError *error) +_dbus_send_nonce (int fd, const DBusString *noncefile, DBusError *error) { dbus_bool_t read_result; int send_result; @@ -203,36 +199,33 @@ _dbus_send_nonce(int fd, const DBusString *noncefile, DBusError *error) if (_dbus_string_get_length (noncefile) == 0) return FALSE; - if ( !_dbus_string_init (&nonce) ) + if (!_dbus_string_init (&nonce)) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); return FALSE; - } - - read_result = _dbus_read_nonce (noncefile, &nonce, NULL); + } + read_result = _dbus_read_nonce (noncefile, &nonce, error); if (!read_result) { - dbus_set_error (error, - _dbus_error_from_errno (errno), - "Could not read nonce from file %s (%s)", - _dbus_string_get_const_data (noncefile), _dbus_strerror(errno)); + _DBUS_ASSERT_ERROR_IS_SET (error); _dbus_string_free (&nonce); return FALSE; } + _DBUS_ASSERT_ERROR_IS_CLEAR (error); send_result = _dbus_write_socket (fd, &nonce, 0, _dbus_string_get_length (&nonce)); _dbus_string_free (&nonce); if (send_result == -1) - { - dbus_set_error (error, - _dbus_error_from_errno (errno), - "Failed to send nonce (fd=%d): %s", - fd, _dbus_strerror(errno)); - return FALSE; - } + { + dbus_set_error (error, + _dbus_error_from_system_errno (), + "Failed to send nonce (fd=%d): %s", + fd, _dbus_strerror_from_errno ()); + return FALSE; + } return TRUE; } @@ -284,8 +277,10 @@ do_noncefile_create (DBusNonceFile *noncefile, } if (!_dbus_create_directory (&noncefile->dir, error)) { + _DBUS_ASSERT_ERROR_IS_SET (error); goto on_error; } + _DBUS_ASSERT_ERROR_IS_CLEAR (error); } else @@ -303,10 +298,12 @@ do_noncefile_create (DBusNonceFile *noncefile, if (!generate_and_write_nonce (&noncefile->path, error)) { + _DBUS_ASSERT_ERROR_IS_SET (error); if (use_subdir) _dbus_delete_directory (&noncefile->dir, NULL); //we ignore possible errors deleting the dir and return the write error instead goto on_error; } + _DBUS_ASSERT_ERROR_IS_CLEAR (error); _dbus_string_free (&randomStr); diff --git a/dbus/dbus-pipe-win.c b/dbus/dbus-pipe-win.c index a07e572..f734518 100644 --- a/dbus/dbus-pipe-win.c +++ b/dbus/dbus-pipe-win.c @@ -28,8 +28,7 @@ #include "dbus-internals.h" #include "dbus-pipe.h" -#include -#include +#include /** * write data to a pipe. @@ -48,15 +47,17 @@ _dbus_pipe_write (DBusPipe *pipe, int len, DBusError *error) { - int written; + DWORD written; + BOOL res; + const char *buffer_c = _dbus_string_get_const_data (buffer); - written = _write (pipe->fd_or_handle, buffer_c + start, len); - if (written < 0) + res = WriteFile ((HANDLE) pipe->fd_or_handle, buffer_c + start, len, &written, NULL); + if (res == 0 || written < 0) { dbus_set_error (error, DBUS_ERROR_FAILED, "Writing to pipe: %s\n", - strerror (errno)); + _dbus_strerror_from_errno ()); } return written; } @@ -74,10 +75,11 @@ _dbus_pipe_close (DBusPipe *pipe, { _DBUS_ASSERT_ERROR_IS_CLEAR (error); - if (_close (pipe->fd_or_handle) < 0) + if (CloseHandle ((HANDLE) pipe->fd_or_handle) == 0) { - dbus_set_error (error, _dbus_error_from_errno (errno), - "Could not close pipe %d: %s", pipe->fd_or_handle, strerror (errno)); + dbus_set_error (error, _dbus_error_from_system_errno (), + "Could not close pipe %d: %s", pipe->fd_or_handle, + _dbus_strerror_from_errno ()); return -1; } else diff --git a/dbus/dbus-sockets-win.h b/dbus/dbus-sockets-win.h index 0af5e7b..5dead05 100644 --- a/dbus/dbus-sockets-win.h +++ b/dbus/dbus-sockets-win.h @@ -37,7 +37,9 @@ #undef interface +#if HAVE_ERRNO_H #include +#endif /* Make use of the fact that the WSAE* error codes don't * overlap with errno E* codes. Wrapper functions store @@ -51,7 +53,7 @@ #define DBUS_SOCKET_IS_INVALID(s) ((SOCKET)(s) == INVALID_SOCKET) #define DBUS_SOCKET_API_RETURNS_ERROR(n) ((n) == SOCKET_ERROR) -#define DBUS_SOCKET_SET_ERRNO() errno = WSAGetLastError() +#define DBUS_SOCKET_SET_ERRNO() (_dbus_win_set_errno (WSAGetLastError())) #define DBUS_CLOSE_SOCKET(s) closesocket(s) diff --git a/dbus/dbus-spawn-win.c b/dbus/dbus-spawn-win.c index 6b9fdf5..1365b2c 100644 --- a/dbus/dbus-spawn-win.c +++ b/dbus/dbus-spawn-win.c @@ -315,9 +315,11 @@ _dbus_babysitter_set_child_exit_error (DBusBabysitter *sitter, PING(); if (sitter->have_spawn_errno) { + char *emsg = _dbus_win_error_string (sitter->spawn_errno); dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED, "Failed to execute program %s: %s", - sitter->executable, _dbus_strerror (sitter->spawn_errno)); + sitter->executable, emsg); + _dbus_win_free_error_string (emsg); } else if (sitter->have_child_status) { @@ -467,7 +469,64 @@ protect_argv (char **argv, return argc; } -static unsigned __stdcall + +/* From GPGME, relicensed by g10 Code GmbH. */ +static char * +build_commandline (char **argv) +{ + int i; + int n = 0; + char *buf; + char *p; + const char *ptr; + + for (i = 0; argv[i]; i++) + n += strlen (argv[i]) + 1; + n++; + + buf = p = malloc (n); + if (!buf) + return NULL; + for (i = 0; argv[i]; i++) + { + strcpy (p, argv[i]); + p += strlen (argv[i]); + *(p++) = ' '; + } + if (i) + p--; + *p = '\0'; + + return buf; +} + + +static HANDLE +spawn_program (const char* name, char** argv, char** envp) +{ + PROCESS_INFORMATION pi = { NULL, 0, 0, 0 }; + STARTUPINFOA si; + char *arg_string; + BOOL result; + + arg_string = build_commandline (argv); + if (!arg_string) + return INVALID_HANDLE_VALUE; + + memset (&si, 0, sizeof (si)); + si.cb = sizeof (si); + result = CreateProcessA (name, arg_string, NULL, NULL, FALSE, 0, + envp, NULL, &si, &pi); + free (arg_string); + if (!result) + return INVALID_HANDLE_VALUE; + + CloseHandle (pi.hThread); + return pi.hProcess; +} + + +static DWORD __stdcall babysitter (void *parameter) { DBusBabysitter *sitter = (DBusBabysitter *) parameter; @@ -484,22 +543,17 @@ babysitter (void *parameter) _dbus_verbose ("babysitter: spawning %s\n", sitter->executable); PING(); - if (sitter->envp != NULL) - sitter->child_handle = (HANDLE) spawnve (P_NOWAIT, sitter->executable, - (const char * const *) sitter->argv, - (const char * const *) sitter->envp); - else - sitter->child_handle = (HANDLE) spawnv (P_NOWAIT, sitter->executable, - (const char * const *) sitter->argv); + sitter->child_handle = spawn_program (sitter->executable, + sitter->argv, sitter->envp); PING(); if (sitter->child_handle == (HANDLE) -1) { sitter->child_handle = NULL; sitter->have_spawn_errno = TRUE; - sitter->spawn_errno = errno; + sitter->spawn_errno = GetLastError(); } - + PING(); SetEvent (sitter->start_sync_event); @@ -543,8 +597,8 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, { DBusBabysitter *sitter; HANDLE sitter_thread; - int sitter_thread_id; - + DWORD sitter_thread_id; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); *sitter_p = NULL; @@ -599,7 +653,7 @@ _dbus_spawn_async_with_babysitter (DBusBabysitter **sitter_p, sitter->envp = envp; PING(); - sitter_thread = (HANDLE) _beginthreadex (NULL, 0, babysitter, + sitter_thread = (HANDLE) CreateThread (NULL, 0, babysitter, sitter, 0, &sitter_thread_id); if (sitter_thread == 0) diff --git a/dbus/dbus-sysdeps-util-win.c b/dbus/dbus-sysdeps-util-win.c index 7b2472f..5b2c29c 100644 --- a/dbus/dbus-sysdeps-util-win.c +++ b/dbus/dbus-sysdeps-util-win.c @@ -43,7 +43,9 @@ #include #include #include +#if HAVE_ERRNO_H #include +#endif #include // WSA error codes /** @@ -78,44 +80,70 @@ _dbus_write_pid_file (const DBusString *filename, DBusError *error) { const char *cfilename; - int fd; - FILE *f; + HANDLE hnd; + char pidstr[20]; + int total; + int bytes_to_write; + + _DBUS_ASSERT_ERROR_IS_CLEAR (error); cfilename = _dbus_string_get_const_data (filename); - - fd = _open (cfilename, O_WRONLY|O_CREAT|O_EXCL|O_BINARY, 0644); - - if (fd < 0) + + hnd = CreateFileA (cfilename, GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, + INVALID_HANDLE_VALUE); + if (hnd == INVALID_HANDLE_VALUE) { - dbus_set_error (error, _dbus_error_from_errno (errno), - "Failed to open \"%s\": %s", cfilename, - strerror (errno)); + char *emsg = _dbus_win_error_string (GetLastError ()); + dbus_set_error (error, _dbus_win_error_from_last_error (), + "Could not create PID file %s: %s", + cfilename, emsg); + _dbus_win_free_error_string (emsg); return FALSE; } - if ((f = fdopen (fd, "w")) == NULL) + if (snprintf (pidstr, sizeof (pidstr), "%lu\n", pid) < 0) { - dbus_set_error (error, _dbus_error_from_errno (errno), - "Failed to fdopen fd %d: %s", fd, strerror (errno)); - _close (fd); + dbus_set_error (error, _dbus_error_from_system_errno (), + "Failed to format PID for \"%s\": %s", cfilename, + _dbus_strerror_from_errno ()); + CloseHandle (hnd); return FALSE; } - if (fprintf (f, "%lu\n", pid) < 0) + total = 0; + bytes_to_write = strlen (pidstr);; + + while (total < bytes_to_write) { - dbus_set_error (error, _dbus_error_from_errno (errno), - "Failed to write to \"%s\": %s", cfilename, - strerror (errno)); + DWORD bytes_written; + BOOL res; - fclose (f); - return FALSE; + res = WriteFile (hnd, pidstr + total, bytes_to_write - total, + &bytes_written, NULL); + + if (res == 0 || bytes_written <= 0) + { + char *emsg = _dbus_win_error_string (GetLastError ()); + dbus_set_error (error, _dbus_win_error_from_last_error (), + "Could not write to %s: %s", cfilename, emsg); + _dbus_win_free_error_string (emsg); + CloseHandle (hnd); + return FALSE; + } + + total += bytes_written; } - if (fclose (f) == EOF) + if (CloseHandle (hnd) == 0) { - dbus_set_error (error, _dbus_error_from_errno (errno), - "Failed to close \"%s\": %s", cfilename, - strerror (errno)); + char *emsg = _dbus_win_error_string (GetLastError ()); + dbus_set_error (error, _dbus_win_error_from_last_error (), + "Could not close file %s: %s", + cfilename, emsg); + _dbus_win_free_error_string (emsg); + return FALSE; } @@ -293,22 +321,16 @@ _dbus_stat(const DBusString *filename, DBusStat *statbuf, DBusError *error) { -#ifdef DBUS_WINCE - return TRUE; - //TODO -#else const char *filename_c; WIN32_FILE_ATTRIBUTE_DATA wfad; char *lastdot; DWORD rc; - PSID owner_sid, group_sid; - PSECURITY_DESCRIPTOR sd; _DBUS_ASSERT_ERROR_IS_CLEAR (error); filename_c = _dbus_string_get_const_data (filename); - if (!GetFileAttributesEx (filename_c, GetFileExInfoStandard, &wfad)) + if (!GetFileAttributesExA (filename_c, GetFileExInfoStandard, &wfad)) { _dbus_win_set_error_from_win_error (error, GetLastError ()); return FALSE; @@ -332,28 +354,36 @@ _dbus_stat(const DBusString *filename, statbuf->nlink = 1; - sd = NULL; - rc = GetNamedSecurityInfo ((char *) filename_c, SE_FILE_OBJECT, - OWNER_SECURITY_INFORMATION | - GROUP_SECURITY_INFORMATION, - &owner_sid, &group_sid, - NULL, NULL, - &sd); - if (rc != ERROR_SUCCESS) - { - _dbus_win_set_error_from_win_error (error, rc); - if (sd != NULL) - LocalFree (sd); - return FALSE; - } - #ifdef ENABLE_UID_TO_SID - /* FIXME */ - statbuf->uid = _dbus_win_sid_to_uid_t (owner_sid); - statbuf->gid = _dbus_win_sid_to_uid_t (group_sid); -#endif + { + PSID owner_sid, group_sid; + PSECURITY_DESCRIPTOR sd; + + sd = NULL; + rc = GetNamedSecurityInfo ((char *) filename_c, SE_FILE_OBJECT, + OWNER_SECURITY_INFORMATION | + GROUP_SECURITY_INFORMATION, + &owner_sid, &group_sid, + NULL, NULL, + &sd); + if (rc != ERROR_SUCCESS) + { + _dbus_win_set_error_from_win_error (error, rc); + if (sd != NULL) + LocalFree (sd); + return FALSE; + } + + /* FIXME */ + statbuf->uid = _dbus_win_sid_to_uid_t (owner_sid); + statbuf->gid = _dbus_win_sid_to_uid_t (group_sid); - LocalFree (sd); + LocalFree (sd); + } +#else + statbuf->uid = DBUS_UID_UNSET; + statbuf->gid = DBUS_GID_UNSET; +#endif statbuf->size = ((dbus_int64_t) wfad.nFileSizeHigh << 32) + wfad.nFileSizeLow; @@ -370,27 +400,9 @@ _dbus_stat(const DBusString *filename, wfad.ftCreationTime.dwLowDateTime) / 10000000 - DBUS_INT64_CONSTANT (116444736000000000); return TRUE; -#endif //DBUS_WINCE } -#ifdef HAVE_DIRENT_H - -// mingw ships with dirent.h -#include -#define _dbus_opendir opendir -#define _dbus_readdir readdir -#define _dbus_closedir closedir - -#else - -#ifdef HAVE_IO_H -#include // win32 file functions -#endif - -#include -#include - /* This file is part of the KDE project Copyright (C) 2000 Werner Almesberger @@ -430,10 +442,10 @@ struct dirent /* typedef DIR - not the same as Unix */ typedef struct { - long handle; /* _findfirst/_findnext handle */ - short offset; /* offset into directory */ + HANDLE handle; /* FindFirst/FindNext handle */ + short offset; /* offset into directory */ short finished; /* 1 if there are not more files */ - struct _finddata_t fileinfo; /* from _findfirst/_findnext */ + WIN32_FIND_DATAA fileinfo; /* from FindFirst/FindNext */ char *dir; /* the dir we are reading */ struct dirent dent; /* the dirent to return */ } @@ -449,6 +461,8 @@ DIR; * The dirent struct is compatible with Unix, except that d_ino is * always 1 and d_off is made up as we go along. * +* Error codes are not available with errno but GetLastError. +* * The DIR typedef is not compatible with Unix. **********************************************************************/ @@ -456,7 +470,7 @@ static DIR * _dbus_opendir(const char *dir) { DIR *dp; char *filespec; - long handle; + HANDLE handle; int index; filespec = malloc(strlen(dir) + 2 + 1); @@ -471,9 +485,10 @@ static DIR * _dbus_opendir(const char *dir) dp->finished = 0; dp->dir = strdup(dir); - if ((handle = _findfirst(filespec, &(dp->fileinfo))) < 0) + handle = FindFirstFileA(filespec, &(dp->fileinfo)); + if (handle == INVALID_HANDLE_VALUE) { - if (errno == ENOENT) + if (GetLastError() == ERROR_NO_MORE_FILES) dp->finished = 1; else return NULL; @@ -486,35 +501,40 @@ static DIR * _dbus_opendir(const char *dir) } static struct dirent * _dbus_readdir(DIR *dp) - { - if (!dp || dp->finished) - return NULL; - - if (dp->offset != 0) - { - if (_findnext(dp->handle, &(dp->fileinfo)) < 0) - { - dp->finished = 1; - errno = 0; - return NULL; - } - } - dp->offset++; +{ + int saved_err = GetLastError(); - strncpy(dp->dent.d_name, dp->fileinfo.name, _MAX_FNAME); - dp->dent.d_ino = 1; - dp->dent.d_reclen = strlen(dp->dent.d_name); - dp->dent.d_off = dp->offset; + if (!dp || dp->finished) + return NULL; - return &(dp->dent); - } + if (dp->offset != 0) + { + if (FindNextFileA(dp->handle, &(dp->fileinfo)) == 0) + { + if (GetLastError() == ERROR_NO_MORE_FILES) + { + SetLastError(saved_err); + dp->finished = 1; + } + return NULL; + } + } + dp->offset++; + + strncpy(dp->dent.d_name, dp->fileinfo.cFileName, _MAX_FNAME); + dp->dent.d_ino = 1; + dp->dent.d_reclen = strlen(dp->dent.d_name); + dp->dent.d_off = dp->offset; + + return &(dp->dent); +} static int _dbus_closedir(DIR *dp) { if (!dp) return 0; - _findclose(dp->handle); + FindClose(dp->handle); if (dp->dir) free(dp->dir); if (dp) @@ -523,7 +543,6 @@ static int _dbus_closedir(DIR *dp) return 0; } -#endif //#ifdef HAVE_DIRENT_H /** * Internals of directory iterator @@ -556,10 +575,11 @@ _dbus_directory_open (const DBusString *filename, d = _dbus_opendir (filename_c); if (d == NULL) { - dbus_set_error (error, _dbus_error_from_errno (errno), + char *emsg = _dbus_win_error_string (GetLastError ()); + dbus_set_error (error, _dbus_win_error_from_last_error (), "Failed to read directory \"%s\": %s", - filename_c, - _dbus_strerror (errno)); + filename_c, emsg); + _dbus_win_free_error_string (emsg); return NULL; } iter = dbus_new0 (DBusDirIter, 1); @@ -599,14 +619,17 @@ _dbus_directory_get_next_file (DBusDirIter *iter, _DBUS_ASSERT_ERROR_IS_CLEAR (error); again: - errno = 0; + SetLastError (0); ent = _dbus_readdir (iter->d); if (ent == NULL) { - if (errno != 0) - dbus_set_error (error, - _dbus_error_from_errno (errno), - "%s", _dbus_strerror (errno)); + if (GetLastError() != 0) + { + char *emsg = _dbus_win_error_string (GetLastError ()); + dbus_set_error (error, _dbus_win_error_from_last_error (), + "Failed to get next in directory: %s", emsg); + _dbus_win_free_error_string (emsg); + } return FALSE; } else if (ent->d_name[0] == '.' && diff --git a/dbus/dbus-sysdeps-win.c b/dbus/dbus-sysdeps-win.c index 4b32e09..f00cfd2 100644 --- a/dbus/dbus-sysdeps-win.c +++ b/dbus/dbus-sysdeps-win.c @@ -65,7 +65,9 @@ extern BOOL WINAPI ConvertSidToStringSidA (PSID Sid, LPSTR *StringSid); #include #include +#if HAVE_ERRNO_H #include +#endif #include #include @@ -85,16 +87,56 @@ extern BOOL WINAPI ConvertSidToStringSidA (PSID Sid, LPSTR *StringSid); typedef int socklen_t; + +void +_dbus_win_set_errno (int err) +{ + errno = err; +} + + +/* Convert GetLastError() to a dbus error. */ +const char* +_dbus_win_error_from_last_error (void) +{ + switch (GetLastError()) + { + case 0: + return DBUS_ERROR_FAILED; + + case ERROR_NO_MORE_FILES: + case ERROR_TOO_MANY_OPEN_FILES: + return DBUS_ERROR_LIMITS_EXCEEDED; /* kernel out of memory */ + + case ERROR_ACCESS_DENIED: + case ERROR_CANNOT_MAKE: + return DBUS_ERROR_ACCESS_DENIED; + + case ERROR_NOT_ENOUGH_MEMORY: + return DBUS_ERROR_NO_MEMORY; + + case ERROR_FILE_EXISTS: + return DBUS_ERROR_FILE_EXISTS; + + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + return DBUS_ERROR_FILE_NOT_FOUND; + } + + return DBUS_ERROR_FAILED; +} + + char* _dbus_win_error_string (int error_number) { char *msg; - FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_IGNORE_INSERTS | - FORMAT_MESSAGE_FROM_SYSTEM, - NULL, error_number, 0, - (LPSTR) &msg, 0, NULL); + FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_IGNORE_INSERTS | + FORMAT_MESSAGE_FROM_SYSTEM, + NULL, error_number, 0, + (LPSTR) &msg, 0, NULL); if (msg[strlen (msg) - 1] == '\n') msg[strlen (msg) - 1] = '\0'; @@ -145,7 +187,7 @@ _dbus_read_socket (int fd, if (!_dbus_string_lengthen (buffer, count)) { - errno = ENOMEM; + _dbus_win_set_errno (ENOMEM); return -1; } @@ -219,7 +261,7 @@ _dbus_write_socket (int fd, if (bytes_written == SOCKET_ERROR) { DBUS_SOCKET_SET_ERRNO(); - _dbus_verbose ("send: failed: %s\n", _dbus_strerror (errno)); + _dbus_verbose ("send: failed: %s\n", _dbus_strerror_from_errno ()); bytes_written = -1; } else @@ -260,7 +302,7 @@ _dbus_close_socket (int fd, dbus_set_error (error, _dbus_error_from_errno (errno), "Could not close socket: socket=%d, , %s", - fd, _dbus_strerror (errno)); + fd, _dbus_strerror_from_errno ()); return FALSE; } _dbus_verbose ("_dbus_close_socket: socket=%d, \n", fd); @@ -385,7 +427,7 @@ _dbus_write_socket_two (int fd, if (rc < 0) { DBUS_SOCKET_SET_ERRNO (); - _dbus_verbose ("WSASend: failed: %s\n", _dbus_strerror (errno)); + _dbus_verbose ("WSASend: failed: %s\n", _dbus_strerror_from_errno ()); bytes_written = -1; } else @@ -763,6 +805,7 @@ _dbus_full_duplex_pipe (int *fd1, u_long arg; fd_set read_set, write_set; struct timeval tv; + int res; _dbus_win_startup_winsock (); @@ -773,13 +816,6 @@ _dbus_full_duplex_pipe (int *fd1, goto out0; } - arg = 1; - if (ioctlsocket (temp, FIONBIO, &arg) == SOCKET_ERROR) - { - DBUS_SOCKET_SET_ERRNO (); - goto out0; - } - _DBUS_ZERO (saddr); saddr.sin_family = AF_INET; saddr.sin_port = 0; @@ -811,34 +847,12 @@ _dbus_full_duplex_pipe (int *fd1, goto out0; } - arg = 1; - if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR) + if (connect (socket1, (struct sockaddr *)&saddr, len) == SOCKET_ERROR) { DBUS_SOCKET_SET_ERRNO (); goto out1; } - if (connect (socket1, (struct sockaddr *)&saddr, len) != SOCKET_ERROR || - WSAGetLastError () != WSAEWOULDBLOCK) - { - DBUS_SOCKET_SET_ERRNO (); - goto out1; - } - - FD_ZERO (&read_set); - FD_SET (temp, &read_set); - - tv.tv_sec = 0; - tv.tv_usec = 0; - - if (select (0, &read_set, NULL, NULL, NULL) == SOCKET_ERROR) - { - DBUS_SOCKET_SET_ERRNO (); - goto out1; - } - - _dbus_assert (FD_ISSET (temp, &read_set)); - socket2 = accept (temp, (struct sockaddr *) &saddr, &len); if (socket2 == INVALID_SOCKET) { @@ -846,38 +860,15 @@ _dbus_full_duplex_pipe (int *fd1, goto out1; } - FD_ZERO (&write_set); - FD_SET (socket1, &write_set); - - tv.tv_sec = 0; - tv.tv_usec = 0; - - if (select (0, NULL, &write_set, NULL, NULL) == SOCKET_ERROR) - { - DBUS_SOCKET_SET_ERRNO (); - goto out2; - } - - _dbus_assert (FD_ISSET (socket1, &write_set)); - - if (blocking) + if (!blocking) { - arg = 0; + arg = 1; if (ioctlsocket (socket1, FIONBIO, &arg) == SOCKET_ERROR) { DBUS_SOCKET_SET_ERRNO (); goto out2; } - arg = 0; - if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR) - { - DBUS_SOCKET_SET_ERRNO (); - goto out2; - } - } - else - { arg = 1; if (ioctlsocket (socket2, FIONBIO, &arg) == SOCKET_ERROR) { @@ -905,7 +896,7 @@ out0: dbus_set_error (error, _dbus_error_from_errno (errno), "Could not setup socket pair: %s", - _dbus_strerror (errno)); + _dbus_strerror_from_errno ()); return FALSE; } @@ -999,7 +990,7 @@ _dbus_poll (DBusPollFD *fds, { DBUS_SOCKET_SET_ERRNO (); if (errno != WSAEWOULDBLOCK) - _dbus_verbose ("WSAWaitForMultipleEvents: failed: %s\n", strerror (errno)); + _dbus_verbose ("WSAWaitForMultipleEvents: failed: %s\n", _dbus_strerror_from_errno ()); ret = -1; } else if (ready == WSA_WAIT_TIMEOUT) @@ -1137,7 +1128,7 @@ _dbus_poll (DBusPollFD *fds, { DBUS_SOCKET_SET_ERRNO (); if (errno != WSAEWOULDBLOCK) - _dbus_verbose ("select: failed: %s\n", _dbus_strerror (errno)); + _dbus_verbose ("select: failed: %s\n", _dbus_strerror_from_errno ()); } else if (ready == 0) _dbus_verbose ("select: = 0\n"); @@ -1273,7 +1264,7 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to create socket: %s", - _dbus_strerror (errno)); + _dbus_strerror_from_errno ()); return -1; } @@ -1322,7 +1313,7 @@ _dbus_connect_tcp_socket_with_nonce (const char *host, dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to open socket: %s", - _dbus_strerror (errno)); + _dbus_strerror_from_errno ()); return -1; } _DBUS_ASSERT_ERROR_IS_CLEAR(error); @@ -1465,7 +1456,7 @@ _dbus_listen_tcp_socket (const char *host, dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to open socket: %s", - _dbus_strerror (errno)); + _dbus_strerror_from_errno ()); goto failed; } _DBUS_ASSERT_ERROR_IS_CLEAR(error); @@ -1475,7 +1466,7 @@ _dbus_listen_tcp_socket (const char *host, closesocket (fd); dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to bind socket \"%s:%s\": %s", - host ? host : "*", port, _dbus_strerror (errno)); + host ? host : "*", port, _dbus_strerror_from_errno ()); goto failed; } @@ -1484,7 +1475,7 @@ _dbus_listen_tcp_socket (const char *host, closesocket (fd); dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to listen on socket \"%s:%s\": %s", - host ? host : "*", port, _dbus_strerror (errno)); + host ? host : "*", port, _dbus_strerror_from_errno ()); goto failed; } @@ -1494,7 +1485,7 @@ _dbus_listen_tcp_socket (const char *host, closesocket (fd); dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to allocate file handle array: %s", - _dbus_strerror (errno)); + _dbus_strerror_from_errno ()); goto failed; } listen_fd = newlisten_fd; @@ -1549,10 +1540,10 @@ _dbus_listen_tcp_socket (const char *host, if (!nlisten_fd) { - errno = WSAEADDRINUSE; + _dbus_win_set_errno (WSAEADDRINUSE); dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to bind socket \"%s:%s\": %s", - host ? host : "*", port, _dbus_strerror (errno)); + host ? host : "*", port, _dbus_strerror_from_errno ()); return -1; } @@ -1651,7 +1642,7 @@ again: { dbus_set_error (error, _dbus_error_from_errno (errno), "Failed to write credentials byte: %s", - _dbus_strerror (errno)); + _dbus_strerror_from_errno ()); return FALSE; } else if (bytes_written == 0) @@ -1942,14 +1933,14 @@ _dbus_create_directory (const DBusString *filename, filename_c = _dbus_string_get_const_data (filename); - if (!CreateDirectory (filename_c, NULL)) + if (!CreateDirectoryA (filename_c, NULL)) { if (GetLastError () == ERROR_ALREADY_EXISTS) return TRUE; dbus_set_error (error, DBUS_ERROR_FAILED, "Failed to create directory %s: %s\n", - filename_c, strerror (errno)); + filename_c, _dbus_strerror_from_errno ()); return FALSE; } else @@ -2010,7 +2001,7 @@ _dbus_get_tmpdir(void) { char *last_slash; - if (!GetTempPath (sizeof (buf), buf)) + if (!GetTempPathA (sizeof (buf), buf)) { _dbus_warn ("GetTempPath failed\n"); _dbus_abort (); @@ -2055,7 +2046,7 @@ _dbus_delete_file (const DBusString *filename, { dbus_set_error (error, DBUS_ERROR_FAILED, "Failed to delete file %s: %s\n", - filename_c, strerror (errno)); + filename_c, _dbus_strerror_from_errno ()); return FALSE; } else @@ -2437,7 +2428,7 @@ HANDLE _dbus_global_lock (const char *mutexname) HANDLE mutex; DWORD gotMutex; - mutex = CreateMutex( NULL, FALSE, mutexname ); + mutex = CreateMutexA( NULL, FALSE, mutexname ); if( !mutex ) { return FALSE; @@ -2491,7 +2482,7 @@ _dbus_daemon_publish_session_bus_address (const char* address) _dbus_assert (address); // before _dbus_global_lock to keep correct lock/release order - hDBusDaemonMutex = CreateMutex( NULL, FALSE, cDBusDaemonMutex ); + hDBusDaemonMutex = CreateMutexA( NULL, FALSE, cDBusDaemonMutex ); ret = WaitForSingleObject( hDBusDaemonMutex, 1000 ); if ( ret != WAIT_OBJECT_0 ) { _dbus_warn("Could not lock mutex %s (return code %d). daemon already running? Bus address not published.\n", cDBusDaemonMutex, ret ); @@ -2502,7 +2493,7 @@ _dbus_daemon_publish_session_bus_address (const char* address) lock = _dbus_global_lock( cUniqueDBusInitMutex ); // create shm - hDBusSharedMem = CreateFileMapping( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, + hDBusSharedMem = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, strlen( address ) + 1, cDBusDaemonAddressInfo ); _dbus_assert( hDBusSharedMem ); @@ -2549,7 +2540,7 @@ _dbus_get_autolaunch_shm (DBusString *address) // read shm for(i=0;i<20;++i) { // we know that dbus-daemon is available, so we wait until shm is available - sharedMem = OpenFileMapping( FILE_MAP_READ, FALSE, cDBusDaemonAddressInfo ); + sharedMem = OpenFileMappingA( FILE_MAP_READ, FALSE, cDBusDaemonAddressInfo ); if( sharedMem == 0 ) Sleep( 100 ); if ( sharedMem != 0) @@ -2587,7 +2578,7 @@ _dbus_daemon_already_runs (DBusString *address) lock = _dbus_global_lock( cUniqueDBusInitMutex ); // do checks - daemon = CreateMutex( NULL, FALSE, cDBusDaemonMutex ); + daemon = CreateMutexA( NULL, FALSE, cDBusDaemonMutex ); if(WaitForSingleObject( daemon, 10 ) != WAIT_TIMEOUT) { ReleaseMutex (daemon); @@ -2840,7 +2831,7 @@ _dbus_get_install_root(char *prefix, int len) DWORD pathLength; char *lastSlash; SetLastError( 0 ); - pathLength = GetModuleFileName(_dbus_win_get_dll_hmodule(), prefix, len); + pathLength = GetModuleFileNameA(_dbus_win_get_dll_hmodule(), prefix, len); if ( pathLength == 0 || GetLastError() != 0 ) { *prefix = '\0'; return FALSE; @@ -3064,7 +3055,7 @@ _dbus_append_keyring_directory_for_credentials (DBusString *directory, dbus_bool_t _dbus_file_exists (const char *file) { - DWORD attributes = GetFileAttributes (file); + DWORD attributes = GetFileAttributesA (file); if (attributes != INVALID_FILE_ATTRIBUTES && GetLastError() != ERROR_PATH_NOT_FOUND) return TRUE; @@ -3227,7 +3218,7 @@ _dbus_win_set_error_from_win_error (DBusError *error, FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, NULL, code, MAKELANGID (LANG_ENGLISH, SUBLANG_ENGLISH_US), - (LPTSTR) &msg, 0, NULL); + (LPSTR) &msg, 0, NULL); if (msg) { char *msg_copy; @@ -3271,11 +3262,13 @@ _dbus_delete_directory (const DBusString *filename, filename_c = _dbus_string_get_const_data (filename); - if (_rmdir (filename_c) != 0) + if (RemoveDirectoryA (filename_c) == 0) { - dbus_set_error (error, DBUS_ERROR_FAILED, - "Failed to remove directory %s: %s\n", - filename_c, strerror (errno)); + char *emsg = _dbus_win_error_string (GetLastError ()); + dbus_set_error (error, _dbus_win_error_from_last_error (), + "Failed to remove directory %s: %s", + filename_c, emsg); + _dbus_win_free_error_string (emsg); return FALSE; } diff --git a/dbus/dbus-sysdeps-win.h b/dbus/dbus-sysdeps-win.h index 15ce831..a2e2ad7 100644 --- a/dbus/dbus-sysdeps-win.h +++ b/dbus/dbus-sysdeps-win.h @@ -40,20 +40,14 @@ extern void *_dbus_win_get_dll_hmodule (void); #include #include #include -#include - -#define mkdir(path, mode) _mkdir (path) - -#ifndef DBUS_WINCE -#ifndef S_ISREG -#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) -#endif -#endif #define DBUS_CONSOLE_DIR "/var/run/console/" +void _dbus_win_set_errno (int err); +const char* _dbus_win_error_from_last_error (void); + void _dbus_win_startup_winsock (void); void _dbus_win_warn_win_error (const char *message, int code); diff --git a/dbus/dbus-sysdeps.c b/dbus/dbus-sysdeps.c index de9f904..afa5664 100644 --- a/dbus/dbus-sysdeps.c +++ b/dbus/dbus-sysdeps.c @@ -518,7 +518,7 @@ _dbus_string_parse_int (const DBusString *str, _dbus_string_get_length (str) - start); end = NULL; - errno = 0; + _dbus_set_errno_to_zero (); v = strtol (p, &end, 0); if (end == NULL || end == p || errno != 0) return FALSE; @@ -557,7 +557,7 @@ _dbus_string_parse_uint (const DBusString *str, _dbus_string_get_length (str) - start); end = NULL; - errno = 0; + _dbus_set_errno_to_zero (); v = strtoul (p, &end, 0); if (end == NULL || end == p || errno != 0) return FALSE; @@ -699,7 +699,7 @@ ascii_strtod (const char *nptr, /* Set errno to zero, so that we can distinguish zero results and underflows */ - errno = 0; + _dbus_set_errno_to_zero (); if (decimal_point_pos) { @@ -773,7 +773,7 @@ _dbus_string_parse_double (const DBusString *str, return FALSE; end = NULL; - errno = 0; + _dbus_set_errno_to_zero (); v = ascii_strtod (p, &end); if (end == NULL || end == p || errno != 0) return FALSE; @@ -994,6 +994,17 @@ _dbus_error_from_errno (int error_number) } /** + * Converts the current system errno value into a #DBusError name. + * + * @returns an error name + */ +const char* +_dbus_error_from_system_errno (void) +{ + return _dbus_error_from_errno (errno); +} + +/** * Assign 0 to the global errno variable */ void diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index a52709c..c8c249a 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -343,6 +343,7 @@ dbus_bool_t _dbus_generate_random_ascii (DBusString *str, int n_bytes); const char* _dbus_error_from_errno (int error_number); +const char* _dbus_error_from_system_errno (void); void _dbus_set_errno_to_zero (void); dbus_bool_t _dbus_get_is_errno_nonzero (void); -- 1.7.0