28#include "dbus-protocol.h"
29#include "dbus-string.h"
30#include "dbus-internals.h"
31#include "dbus-sysdeps-win.h"
49_dbus_file_read (HANDLE hnd,
59 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
63 start = _dbus_string_get_length (buffer);
73 result = ReadFile (hnd, data, count, &bytes_read,
NULL);
76 char *emsg = _dbus_win_error_string (GetLastError ());
78 "Failed to read from %p: %s", hnd, emsg);
79 _dbus_win_free_error_string (emsg);
118 const char *filename_c;
120 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
122 filename_c = _dbus_string_get_const_data (filename);
124 hnd = CreateFileA (filename_c, GENERIC_READ,
125 FILE_SHARE_READ | FILE_SHARE_WRITE,
126 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL);
127 if (hnd == INVALID_HANDLE_VALUE)
129 char *emsg = _dbus_win_error_string (GetLastError ());
131 "Failed to open \"%s\": %s", filename_c, emsg);
132 _dbus_win_free_error_string (emsg);
136 _dbus_verbose (
"file %s hnd %p opened\n", filename_c, hnd);
138 fsize = GetFileSize (hnd, &fsize_hi);
139 if (fsize == 0xFFFFFFFF && GetLastError() != NO_ERROR)
141 char *emsg = _dbus_win_error_string (GetLastError ());
143 "Failed to get file size for \"%s\": %s",
145 _dbus_win_free_error_string (emsg);
147 _dbus_verbose (
"GetFileSize() failed: %s", emsg);
154 if (fsize_hi != 0 || fsize > _DBUS_ONE_MEGABYTE)
157 "File size %lu/%lu of \"%s\" is too large.",
158 (
unsigned long) fsize_hi,
159 (
unsigned long) fsize, filename_c);
165 orig_len = _dbus_string_get_length (str);
170 while (total < fsize)
172 bytes_read = _dbus_file_read (hnd, str, fsize - total, error);
178 "Premature EOF reading \"%s\"",
182 _DBUS_ASSERT_ERROR_IS_SET (error);
216 dbus_bool_t world_readable,
221 const char *filename_c;
223 const char *tmp_filename_c;
226 dbus_bool_t need_unlink;
229 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
231 hnd = INVALID_HANDLE_VALUE;
255#define N_TMP_FILENAME_RANDOM_BYTES 8
263 filename_c = _dbus_string_get_const_data (filename);
264 tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
267 hnd = CreateFileA (tmp_filename_c, GENERIC_WRITE,
268 FILE_SHARE_READ | FILE_SHARE_WRITE,
269 NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,
270 INVALID_HANDLE_VALUE);
271 if (hnd == INVALID_HANDLE_VALUE)
273 char *emsg = _dbus_win_error_string (GetLastError ());
275 "Could not create \"%s\": %s", filename_c, emsg);
276 _dbus_win_free_error_string (emsg);
285 _dbus_verbose (
"tmp file %s hnd %p opened\n", tmp_filename_c, hnd);
290 bytes_to_write = _dbus_string_get_length (str);
291 str_c = _dbus_string_get_const_data (str);
293 while (total < bytes_to_write)
298 res = WriteFile (hnd, str_c + total, bytes_to_write - total,
299 &bytes_written,
NULL);
301 if (res == 0 || bytes_written <= 0)
303 char *emsg = _dbus_win_error_string (GetLastError ());
305 "Could not write to %s: %s", tmp_filename_c, emsg);
306 _dbus_win_free_error_string (emsg);
310 total += bytes_written;
313 if (CloseHandle (hnd) == 0)
315 char *emsg = _dbus_win_error_string (GetLastError ());
317 "Could not close file %s: %s", tmp_filename_c, emsg);
318 _dbus_win_free_error_string (emsg);
322 hnd = INVALID_HANDLE_VALUE;
325 if (!MoveFileExA (tmp_filename_c, filename_c, MOVEFILE_REPLACE_EXISTING))
327 char *emsg = _dbus_win_error_string (GetLastError ());
329 "Could not rename %s to %s: %s",
330 tmp_filename_c, filename_c, emsg);
331 _dbus_win_free_error_string (emsg);
343 if (hnd != INVALID_HANDLE_VALUE)
346 if (need_unlink && DeleteFileA (tmp_filename_c) == 0)
348 char *emsg = _dbus_win_error_string (GetLastError ());
349 _dbus_verbose (
"Failed to unlink temp file %s: %s", tmp_filename_c,
351 _dbus_win_free_error_string (emsg);
357 _DBUS_ASSERT_ERROR_IS_SET (error);
374 const char *filename_c;
376 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
378 filename_c = _dbus_string_get_const_data (filename);
380 hnd = CreateFileA (filename_c, GENERIC_WRITE,
381 FILE_SHARE_READ | FILE_SHARE_WRITE,
382 NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,
383 INVALID_HANDLE_VALUE);
384 if (hnd == INVALID_HANDLE_VALUE)
386 char *emsg = _dbus_win_error_string (GetLastError ());
388 "Could not create file %s: %s",
390 _dbus_win_free_error_string (emsg);
394 _dbus_verbose (
"exclusive file %s hnd %p opened\n", filename_c, hnd);
396 if (CloseHandle (hnd) == 0)
398 char *emsg = _dbus_win_error_string (GetLastError ());
400 "Could not close file %s: %s",
402 _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_IN...
dbus_bool_t _dbus_string_lengthen(DBusString *str, int additional_length)
Makes a string longer by the given number of bytes.
Object representing an exception.