27#include "dbus-protocol.h"
28#include "dbus-errors.h"
30#include "dbus-internals.h"
31#include "dbus-sysdeps.h"
32#include "dbus-sysdeps-unix.h"
63 const char *filename_c;
65 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
67 filename_c = _dbus_string_get_const_data (filename);
70 fd = open (filename_c, O_RDONLY | O_BINARY);
74 "Failed to open \"%s\": %s",
76 _dbus_strerror (errno));
80 _dbus_verbose (
"file fd %d opened\n", fd);
82 if (fstat (fd, &sb) < 0)
85 "Failed to stat \"%s\": %s",
87 _dbus_strerror (errno));
89 _dbus_verbose (
"fstat() failed: %s",
90 _dbus_strerror (errno));
97 if (sb.st_size > _DBUS_ONE_MEGABYTE)
100 "File size %lu of \"%s\" is too large.",
101 (
unsigned long) sb.st_size, filename_c);
107 orig_len = _dbus_string_get_length (str);
108 if (sb.st_size > 0 && S_ISREG (sb.st_mode))
112 while (total < (
int) sb.st_size)
119 "Error reading \"%s\": %s",
121 _dbus_strerror (errno));
123 _dbus_verbose (
"read() failed: %s",
124 _dbus_strerror (errno));
137 else if (sb.st_size != 0)
139 _dbus_verbose (
"Can only open regular files at the moment.\n");
141 "\"%s\" is not a regular file",
171 const char *filename_c;
173 const char *tmp_filename_c;
178 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
204#define N_TMP_FILENAME_RANDOM_BYTES 8
212 filename_c = _dbus_string_get_const_data (filename);
213 tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
215 fd = open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
216 world_readable ? 0644 : 0600);
220 "Could not create %s: %s", tmp_filename_c,
221 _dbus_strerror (errno));
230 if (fchmod (fd, 0644) < 0)
233 "Could not chmod %s: %s", tmp_filename_c,
234 _dbus_strerror (errno));
239 _dbus_verbose (
"tmp file fd %d opened\n", fd);
244 bytes_to_write = _dbus_string_get_length (str);
246 while (total < bytes_to_write)
251 bytes_to_write - total);
253 if (bytes_written <= 0)
256 "Could not write to %s: %s", tmp_filename_c,
257 _dbus_strerror (errno));
262 total += bytes_written;
268 "Could not synchronize file %s: %s",
269 tmp_filename_c, _dbus_strerror (errno));
277 "Could not close file %s: %s",
278 tmp_filename_c, _dbus_strerror (errno));
285 if (rename (tmp_filename_c, filename_c) < 0)
288 "Could not rename %s to %s: %s",
289 tmp_filename_c, filename_c,
290 _dbus_strerror (errno));
307 if (need_unlink && unlink (tmp_filename_c) < 0)
308 _dbus_verbose (
"Failed to unlink temp file %s: %s\n",
309 tmp_filename_c, _dbus_strerror (errno));
313 _DBUS_ASSERT_ERROR_XOR_BOOL (error, retval);
327 const char *filename_c;
329 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
331 filename_c = _dbus_string_get_const_data (filename);
332 if (chmod (filename_c, 0644) == -1)
336 "Could not change permissions of file %s: %s\n",
338 _dbus_strerror (errno));
355 const char *filename_c;
357 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
359 filename_c = _dbus_string_get_const_data (filename);
361 fd = open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
367 "Could not create file %s: %s\n",
369 _dbus_strerror (errno));
373 _dbus_verbose (
"exclusive file fd %d opened\n", fd);
379 "Could not close file %s: %s\n",
381 _dbus_strerror (errno));
400 const char *filename_c;
402 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
404 filename_c = _dbus_string_get_const_data (filename);
406 if (unlink (filename_c) < 0)
409 "Failed to delete file %s: %s\n",
410 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...
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.