29#include "dbus-protocol.h"
30#include "dbus-errors.h"
32#include "dbus-internals.h"
33#include "dbus-sysdeps.h"
34#include "dbus-sysdeps-unix.h"
65 const char *filename_c;
67 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
72 fd = open (filename_c, O_RDONLY | O_BINARY);
76 "Failed to open \"%s\": %s",
78 _dbus_strerror (errno));
82 _dbus_verbose (
"file fd %d opened\n", fd);
84 if (fstat (fd, &sb) < 0)
87 "Failed to stat \"%s\": %s",
89 _dbus_strerror (errno));
91 _dbus_verbose (
"fstat() failed: %s",
92 _dbus_strerror (errno));
99 if (sb.st_size > _DBUS_ONE_MEGABYTE)
102 "File size %lu of \"%s\" is too large.",
103 (
unsigned long) sb.st_size, filename_c);
110 if (sb.st_size > 0 && S_ISREG (sb.st_mode))
114 while (total < (
int) sb.st_size)
121 "Error reading \"%s\": %s",
123 _dbus_strerror (errno));
125 _dbus_verbose (
"read() failed: %s",
126 _dbus_strerror (errno));
139 else if (sb.st_size != 0)
141 _dbus_verbose (
"Can only open regular files at the moment.\n");
143 "\"%s\" is not a regular file",
173 const char *filename_c;
175 const char *tmp_filename_c;
180 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
206#define N_TMP_FILENAME_RANDOM_BYTES 8
217 fd = open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
218 world_readable ? 0644 : 0600);
222 "Could not create %s: %s", tmp_filename_c,
223 _dbus_strerror (errno));
232 if (fchmod (fd, 0644) < 0)
235 "Could not chmod %s: %s", tmp_filename_c,
236 _dbus_strerror (errno));
241 _dbus_verbose (
"tmp file fd %d opened\n", fd);
248 while (total < bytes_to_write)
253 bytes_to_write - total);
255 if (bytes_written <= 0)
258 "Could not write to %s: %s", tmp_filename_c,
259 _dbus_strerror (errno));
264 total += bytes_written;
270 "Could not synchronize file %s: %s",
271 tmp_filename_c, _dbus_strerror (errno));
279 "Could not close file %s: %s",
280 tmp_filename_c, _dbus_strerror (errno));
287 if (rename (tmp_filename_c, filename_c) < 0)
290 "Could not rename %s to %s: %s",
291 tmp_filename_c, filename_c,
292 _dbus_strerror (errno));
309 if (need_unlink && unlink (tmp_filename_c) < 0)
310 _dbus_verbose (
"Failed to unlink temp file %s: %s\n",
311 tmp_filename_c, _dbus_strerror (errno));
315 _DBUS_ASSERT_ERROR_XOR_BOOL (error, retval);
329 const char *filename_c;
331 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
334 if (chmod (filename_c, 0644) == -1)
338 "Could not change permissions of file %s: %s\n",
340 _dbus_strerror (errno));
357 const char *filename_c;
359 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
363 fd = open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
369 "Could not create file %s: %s\n",
371 _dbus_strerror (errno));
375 _dbus_verbose (
"exclusive file fd %d opened\n", fd);
381 "Could not close file %s: %s\n",
383 _dbus_strerror (errno));
402 const char *filename_c;
404 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
408 if (unlink (filename_c) < 0)
411 "Failed to delete file %s: %s\n",
412 filename_c, _dbus_strerror (errno));
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_delete_file(const DBusString *filename, DBusError *error)
Deletes the given file.
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.
const char * _dbus_error_from_errno(int error_number)
Converts a UNIX errno, or Windows errno or WinSock error value into a DBusError name.
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".
#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...
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...
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
const char * _dbus_string_get_const_data(const DBusString *str)
Gets the raw character buffer from a const string.
dbus_bool_t _dbus_close(int fd, DBusError *error)
Closes a file descriptor.
int _dbus_write(int fd, const DBusString *buffer, int start, int len)
Thin wrapper around the write() system call that writes a part of a DBusString and handles EINTR for ...
int _dbus_read(int fd, DBusString *buffer, int count)
Thin wrapper around the read() system call that appends the data it reads to the DBusString buffer.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Object representing an exception.