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);
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 defined HAVE_PDPLINUX && defined DBUS_ENABLE_VERBOSE_MODE 83 _dbus_verbose (
"Read file: '%s'\n", filename_c);
86 if (fstat (fd, &sb) < 0)
89 "Failed to stat \"%s\": %s",
91 _dbus_strerror (errno));
93 _dbus_verbose (
"fstat() failed: %s",
94 _dbus_strerror (errno));
101 if (sb.st_size > _DBUS_ONE_MEGABYTE)
104 "File size %lu of \"%s\" is too large.",
105 (
unsigned long) sb.st_size, filename_c);
111 orig_len = _dbus_string_get_length (str);
112 if (sb.st_size > 0 && S_ISREG (sb.st_mode))
116 while (total < (
int) sb.st_size)
123 "Error reading \"%s\": %s",
125 _dbus_strerror (errno));
127 _dbus_verbose (
"read() failed: %s",
128 _dbus_strerror (errno));
141 else if (sb.st_size != 0)
143 _dbus_verbose (
"Can only open regular files at the moment.\n");
145 "\"%s\" is not a regular file",
175 const char *filename_c;
177 const char *tmp_filename_c;
182 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
208 #define N_TMP_FILENAME_RANDOM_BYTES 8 216 filename_c = _dbus_string_get_const_data (filename);
217 tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
219 fd = open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
220 world_readable ? 0644 : 0600);
224 "Could not create %s: %s", tmp_filename_c,
225 _dbus_strerror (errno));
234 if (fchmod (fd, 0644) < 0)
237 "Could not chmod %s: %s", tmp_filename_c,
238 _dbus_strerror (errno));
243 _dbus_verbose (
"tmp file fd %d opened\n", fd);
248 bytes_to_write = _dbus_string_get_length (str);
250 while (total < bytes_to_write)
255 bytes_to_write - total);
257 if (bytes_written <= 0)
260 "Could not write to %s: %s", tmp_filename_c,
261 _dbus_strerror (errno));
266 total += bytes_written;
272 "Could not synchronize file %s: %s",
273 tmp_filename_c, _dbus_strerror (errno));
281 "Could not close file %s: %s",
282 tmp_filename_c, _dbus_strerror (errno));
289 if (rename (tmp_filename_c, filename_c) < 0)
292 "Could not rename %s to %s: %s",
293 tmp_filename_c, filename_c,
294 _dbus_strerror (errno));
311 if (need_unlink && unlink (tmp_filename_c) < 0)
312 _dbus_verbose (
"Failed to unlink temp file %s: %s\n",
313 tmp_filename_c, _dbus_strerror (errno));
318 _DBUS_ASSERT_ERROR_IS_SET (error);
333 const char *filename_c;
335 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
337 filename_c = _dbus_string_get_const_data (filename);
338 if (chmod (filename_c, 0644) == -1)
342 "Could not change permissions of file %s: %s\n",
344 _dbus_strerror (errno));
361 const char *filename_c;
363 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
365 filename_c = _dbus_string_get_const_data (filename);
367 fd = open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
373 "Could not create file %s: %s\n",
375 _dbus_strerror (errno));
379 _dbus_verbose (
"exclusive file fd %d opened\n", fd);
385 "Could not close file %s: %s\n",
387 _dbus_strerror (errno));
406 const char *filename_c;
408 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
410 filename_c = _dbus_string_get_const_data (filename);
412 if (unlink (filename_c) < 0)
415 "Failed to delete file %s: %s\n",
416 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...
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().
#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.