27 #include "dbus-protocol.h"
28 #include "dbus-errors.h"
29 #include "dbus-file.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);
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);
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 && S_ISREG (sb.st_mode))
141 while (bytes_read != 0)
143 if (total > _DBUS_ONE_MEGABYTE)
146 "File size of \"%s\" is larger than %lu.",
147 filename_c, (
unsigned long) _DBUS_ONE_MEGABYTE);
155 "Error reading \"%s\": %s",
157 _dbus_strerror (errno));
159 _dbus_verbose (
"read() failed: %s",
160 _dbus_strerror (errno));
173 else if (sb.st_size != 0)
175 _dbus_verbose (
"Can only open regular files at the moment.\n");
177 "\"%s\" is not a regular file",
207 const char *filename_c;
209 const char *tmp_filename_c;
214 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
240 #define N_TMP_FILENAME_RANDOM_BYTES 8
251 fd = open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
252 world_readable ? 0644 : 0600);
256 "Could not create %s: %s", tmp_filename_c,
257 _dbus_strerror (errno));
266 if (fchmod (fd, 0644) < 0)
269 "Could not chmod %s: %s", tmp_filename_c,
270 _dbus_strerror (errno));
275 _dbus_verbose (
"tmp file fd %d opened\n", fd);
282 while (total < bytes_to_write)
287 bytes_to_write - total);
289 if (bytes_written <= 0)
292 "Could not write to %s: %s", tmp_filename_c,
293 _dbus_strerror (errno));
298 total += bytes_written;
304 "Could not synchronize file %s: %s",
305 tmp_filename_c, _dbus_strerror (errno));
313 "Could not close file %s: %s",
314 tmp_filename_c, _dbus_strerror (errno));
321 if (rename (tmp_filename_c, filename_c) < 0)
324 "Could not rename %s to %s: %s",
325 tmp_filename_c, filename_c,
326 _dbus_strerror (errno));
343 if (need_unlink && unlink (tmp_filename_c) < 0)
344 _dbus_verbose (
"Failed to unlink temp file %s: %s\n",
345 tmp_filename_c, _dbus_strerror (errno));
349 _DBUS_ASSERT_ERROR_XOR_BOOL (error, retval);
363 const char *filename_c;
365 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
368 if (chmod (filename_c, 0644) == -1)
372 "Could not change permissions of file %s: %s\n",
374 _dbus_strerror (errno));
391 const char *filename_c;
393 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
397 fd = open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
403 "Could not create file %s: %s\n",
405 _dbus_strerror (errno));
409 _dbus_verbose (
"exclusive file fd %d opened\n", fd);
415 "Could not close file %s: %s\n",
417 _dbus_strerror (errno));
436 const char *filename_c;
438 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
442 if (unlink (filename_c) < 0)
445 "Failed to delete file %s: %s\n",
446 filename_c, _dbus_strerror (errno));
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
#define NULL
A null pointer, defined appropriately for C or C++.
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.
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 ...
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.
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...
dbus_bool_t _dbus_delete_file(const DBusString *filename, DBusError *error)
Deletes the given file.
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_uint32_t dbus_bool_t
A boolean, valid values are TRUE and 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...
dbus_bool_t _dbus_make_file_world_readable(const DBusString *filename, DBusError *error)
Makes the file readable by every user in the system.
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...
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
Object representing an exception.
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
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...
#define TRUE
Expands to "1".
#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_close(int fd, DBusError *error)
Closes a file descriptor.
#define FALSE
Expands to "0".
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
const char * _dbus_string_get_const_data(const DBusString *str)
Gets the raw character buffer from a const string.