#include "dbus-sysdeps-win.h"
#include "dbus-pipe.h"
-#include <errno.h>
-#include <fcntl.h>
-#include <io.h>
-#include <sys/stat.h>
-
#include <windows.h>
+
+
+/**
+ * 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
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;
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;
}
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.
const DBusString *filename,
DBusError *error)
{
- int fd;
+ HANDLE hnd;
int bytes_to_write;
const char *filename_c;
DBusString tmp_filename;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
- fd = -1;
+ hnd = INVALID_HANDLE_VALUE;
retval = FALSE;
need_unlink = FALSE;
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;
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;
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);
_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;
- }
-}
DBusError *error);
dbus_bool_t _dbus_delete_file (const DBusString *filename,
DBusError *error);
-int _dbus_file_read (int fd,
- DBusString *buffer,
- int count);
/** @} */
#include <stdio.h>
-#ifdef HAVE_ERRNO_H
-# include <errno.h>
-#endif
-
static dbus_bool_t
do_check_nonce (int fd, const DBusString *nonce, 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;
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;
}
}
if (!_dbus_create_directory (&noncefile->dir, error))
{
+ _DBUS_ASSERT_ERROR_IS_SET (error);
goto on_error;
}
+ _DBUS_ASSERT_ERROR_IS_CLEAR (error);
}
else
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);
#include "dbus-internals.h"
#include "dbus-pipe.h"
-#include <io.h>
-#include <errno.h>
+#include <windows.h>
/**
* write data to a 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;
}
{
_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
#undef interface
+#if HAVE_ERRNO_H
#include <errno.h>
+#endif
/* Make use of the fact that the WSAE* error codes don't
* overlap with errno E* codes. Wrapper functions store
#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)
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)
{
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;
_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);
{
DBusBabysitter *sitter;
HANDLE sitter_thread;
- int sitter_thread_id;
-
+ DWORD sitter_thread_id;
+
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
*sitter_p = NULL;
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)
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
+#if HAVE_ERRNO_H
#include <errno.h>
+#endif
#include <winsock2.h> // WSA error codes
/**
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;
}
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;
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;
wfad.ftCreationTime.dwLowDateTime) / 10000000 - DBUS_INT64_CONSTANT (116444736000000000);
return TRUE;
-#endif //DBUS_WINCE
}
-#ifdef HAVE_DIRENT_H
-
-// mingw ships with dirent.h
-#include <dirent.h>
-#define _dbus_opendir opendir
-#define _dbus_readdir readdir
-#define _dbus_closedir closedir
-
-#else
-
-#ifdef HAVE_IO_H
-#include <io.h> // win32 file functions
-#endif
-
-#include <sys/types.h>
-#include <stdlib.h>
-
/* This file is part of the KDE project
Copyright (C) 2000 Werner Almesberger
/* 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 */
}
* 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.
**********************************************************************/
{
DIR *dp;
char *filespec;
- long handle;
+ HANDLE handle;
int index;
filespec = malloc(strlen(dir) + 2 + 1);
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;
}
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)
return 0;
}
-#endif //#ifdef HAVE_DIRENT_H
/**
* Internals of directory iterator
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);
_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] == '.' &&
#include <string.h>
#include <mbstring.h>
+#if HAVE_ERRNO_H
#include <errno.h>
+#endif
#include <sys/stat.h>
#include <sys/types.h>
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';
if (!_dbus_string_lengthen (buffer, count))
{
- errno = ENOMEM;
+ _dbus_win_set_errno (ENOMEM);
return -1;
}
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
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);
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
u_long arg;
fd_set read_set, write_set;
struct timeval tv;
+ int res;
_dbus_win_startup_winsock ();
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;
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)
{
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)
{
dbus_set_error (error, _dbus_error_from_errno (errno),
"Could not setup socket pair: %s",
- _dbus_strerror (errno));
+ _dbus_strerror_from_errno ());
return FALSE;
}
{
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)
{
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");
dbus_set_error (error,
_dbus_error_from_errno (errno),
"Failed to create socket: %s",
- _dbus_strerror (errno));
+ _dbus_strerror_from_errno ());
return -1;
}
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);
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);
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;
}
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;
}
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;
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;
}
{
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)
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
{
char *last_slash;
- if (!GetTempPath (sizeof (buf), buf))
+ if (!GetTempPathA (sizeof (buf), buf))
{
_dbus_warn ("GetTempPath failed\n");
_dbus_abort ();
{
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
HANDLE mutex;
DWORD gotMutex;
- mutex = CreateMutex( NULL, FALSE, mutexname );
+ mutex = CreateMutexA( NULL, FALSE, mutexname );
if( !mutex )
{
return FALSE;
_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 );
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 );
// 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)
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);
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;
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;
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;
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;
}
#include <lm.h>
#include <io.h>
#include <share.h>
-#include <direct.h>
-
-#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);
_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;
_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;
/* Set errno to zero, so that we can distinguish zero results
and underflows */
- errno = 0;
+ _dbus_set_errno_to_zero ();
if (decimal_point_pos)
{
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;
return DBUS_ERROR_FAILED;
}
+/**
+ * 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
*/
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);