26#include "dbus-protocol.h"
27#include "dbus-string.h"
28#include "dbus-internals.h"
29#include "dbus-sysdeps-win.h"
47_dbus_file_read (HANDLE hnd,
57 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
61 start = _dbus_string_get_length (buffer);
71 result = ReadFile (hnd, data, count, &bytes_read,
NULL);
74 char *emsg = _dbus_win_error_string (GetLastError ());
76 "Failed to read from %p: %s", hnd, emsg);
77 _dbus_win_free_error_string (emsg);
116 const char *filename_c;
118 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
120 filename_c = _dbus_string_get_const_data (filename);
122 hnd = CreateFileA (filename_c, GENERIC_READ,
123 FILE_SHARE_READ | FILE_SHARE_WRITE,
124 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL);
125 if (hnd == INVALID_HANDLE_VALUE)
127 char *emsg = _dbus_win_error_string (GetLastError ());
129 "Failed to open \"%s\": %s", filename_c, emsg);
130 _dbus_win_free_error_string (emsg);
134 _dbus_verbose (
"file %s hnd %p opened\n", filename_c, hnd);
136 fsize = GetFileSize (hnd, &fsize_hi);
137 if (fsize == 0xFFFFFFFF && GetLastError() != NO_ERROR)
139 char *emsg = _dbus_win_error_string (GetLastError ());
141 "Failed to get file size for \"%s\": %s",
143 _dbus_win_free_error_string (emsg);
145 _dbus_verbose (
"GetFileSize() failed: %s", emsg);
152 if (fsize_hi != 0 || fsize > _DBUS_ONE_MEGABYTE)
155 "File size %lu/%lu of \"%s\" is too large.",
156 (
unsigned long) fsize_hi,
157 (
unsigned long) fsize, filename_c);
163 orig_len = _dbus_string_get_length (str);
168 while (total < fsize)
170 bytes_read = _dbus_file_read (hnd, str, fsize - total, error);
176 "Premature EOF reading \"%s\"",
180 _DBUS_ASSERT_ERROR_IS_SET (error);
219 const char *filename_c;
221 const char *tmp_filename_c;
227 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
229 hnd = INVALID_HANDLE_VALUE;
253#define N_TMP_FILENAME_RANDOM_BYTES 8
261 filename_c = _dbus_string_get_const_data (filename);
262 tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
265 hnd = CreateFileA (tmp_filename_c, GENERIC_WRITE,
266 FILE_SHARE_READ | FILE_SHARE_WRITE,
267 NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,
268 INVALID_HANDLE_VALUE);
269 if (hnd == INVALID_HANDLE_VALUE)
271 char *emsg = _dbus_win_error_string (GetLastError ());
273 "Could not create \"%s\": %s", filename_c, emsg);
274 _dbus_win_free_error_string (emsg);
283 _dbus_verbose (
"tmp file %s hnd %p opened\n", tmp_filename_c, hnd);
288 bytes_to_write = _dbus_string_get_length (str);
289 str_c = _dbus_string_get_const_data (str);
291 while (total < bytes_to_write)
296 res = WriteFile (hnd, str_c + total, bytes_to_write - total,
297 &bytes_written,
NULL);
299 if (res == 0 || bytes_written <= 0)
301 char *emsg = _dbus_win_error_string (GetLastError ());
303 "Could not write to %s: %s", tmp_filename_c, emsg);
304 _dbus_win_free_error_string (emsg);
308 total += bytes_written;
311 if (CloseHandle (hnd) == 0)
313 char *emsg = _dbus_win_error_string (GetLastError ());
315 "Could not close file %s: %s", tmp_filename_c, emsg);
316 _dbus_win_free_error_string (emsg);
320 hnd = INVALID_HANDLE_VALUE;
323 if (!MoveFileExA (tmp_filename_c, filename_c, MOVEFILE_REPLACE_EXISTING))
325 char *emsg = _dbus_win_error_string (GetLastError ());
327 "Could not rename %s to %s: %s",
328 tmp_filename_c, filename_c, emsg);
329 _dbus_win_free_error_string (emsg);
341 if (hnd != INVALID_HANDLE_VALUE)
344 if (need_unlink && DeleteFileA (tmp_filename_c) == 0)
346 char *emsg = _dbus_win_error_string (GetLastError ());
347 _dbus_verbose (
"Failed to unlink temp file %s: %s", tmp_filename_c,
349 _dbus_win_free_error_string (emsg);
355 _DBUS_ASSERT_ERROR_IS_SET (error);
372 const char *filename_c;
374 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
376 filename_c = _dbus_string_get_const_data (filename);
378 hnd = CreateFileA (filename_c, GENERIC_WRITE,
379 FILE_SHARE_READ | FILE_SHARE_WRITE,
380 NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,
381 INVALID_HANDLE_VALUE);
382 if (hnd == INVALID_HANDLE_VALUE)
384 char *emsg = _dbus_win_error_string (GetLastError ());
386 "Could not create file %s: %s",
388 _dbus_win_free_error_string (emsg);
392 _dbus_verbose (
"exclusive file %s hnd %p opened\n", filename_c, hnd);
394 if (CloseHandle (hnd) == 0)
396 char *emsg = _dbus_win_error_string (GetLastError ());
398 "Could not close file %s: %s",
400 _dbus_win_free_error_string (emsg);
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
dbus_bool_t _dbus_create_file_exclusively(const DBusString *filename, DBusError *error)
Creates the given file, failing if the file already exists.
dbus_bool_t _dbus_string_save_to_file(const DBusString *str, const DBusString *filename, dbus_bool_t world_readable, DBusError *error)
Writes a string out to a file.
dbus_bool_t _dbus_make_file_world_readable(const DBusString *filename, DBusError *error)
Makes the file readable by every user in the system.
dbus_bool_t _dbus_file_get_contents(DBusString *str, const DBusString *filename, DBusError *error)
Appends the contents of the given file to the string, returning error code.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
dbus_bool_t _dbus_generate_random_ascii(DBusString *str, int n_bytes, DBusError *error)
Generates the given number of random bytes, where the bytes are chosen from the alphanumeric ASCII su...
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
DBUS_PRIVATE_EXPORT void _dbus_verbose_bytes_of_string(const DBusString *str, int start, int len)
Dump the given part of the string to verbose log.
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
dbus_bool_t _dbus_string_copy(const DBusString *source, int start, DBusString *dest, int insert_at)
Like _dbus_string_move(), but does not delete the section of the source string that's copied to the d...
char * _dbus_string_get_data_len(DBusString *str, int start, int len)
Gets a sub-portion of the raw character buffer from the string.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
dbus_bool_t _dbus_string_lengthen(DBusString *str, int additional_length)
Makes a string longer by the given number of bytes.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Object representing an exception.