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"
36#ifdef HAVE_LINUX_MAGIC_H
37#include <linux/magic.h>
69#if defined(HAVE_FSTATFS) && defined(PROC_SUPER_MAGIC)
75 const char *filename_c;
76 dbus_bool_t is_procfs =
FALSE;
78 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
80 filename_c = _dbus_string_get_const_data (filename);
83 fd = open (filename_c, O_RDONLY | O_BINARY);
87 "Failed to open \"%s\": %s",
89 _dbus_strerror (errno));
93 _dbus_verbose (
"file fd %d opened\n", fd);
95 if (fstat (fd, &sb) < 0)
98 "Failed to stat \"%s\": %s",
100 _dbus_strerror (errno));
102 _dbus_verbose (
"fstat() failed: %s",
103 _dbus_strerror (errno));
110 if (sb.st_size > _DBUS_ONE_MEGABYTE)
113 "File size %lu of \"%s\" is too large.",
114 (
unsigned long) sb.st_size, filename_c);
122#if defined(HAVE_FSTATFS) && defined(PROC_SUPER_MAGIC)
125 if (fstatfs(fd, &sfs) < 0)
128 "Failed to stat \"%s\": %s",
130 _dbus_strerror (errno));
132 _dbus_verbose (
"fstatvfs() failed: %s",
133 _dbus_strerror (errno));
139 if (sfs.f_type == PROC_SUPER_MAGIC)
145 file_size = _DBUS_ONE_MEGABYTE;
147 file_size = sb.st_size;
150 orig_len = _dbus_string_get_length (str);
151 if (file_size > 0 && S_ISREG (sb.st_mode))
162 "Error reading \"%s\": %s",
164 _dbus_strerror (errno));
166 _dbus_verbose (
"read() failed: %s",
167 _dbus_strerror (errno));
176 while (total < file_size && !is_procfs);
181 else if (file_size != 0)
183 _dbus_verbose (
"Can only open regular files at the moment.\n");
185 "\"%s\" is not a regular file",
210 dbus_bool_t world_readable,
215 const char *filename_c;
217 const char *tmp_filename_c;
219 dbus_bool_t need_unlink;
222 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
248#define N_TMP_FILENAME_RANDOM_BYTES 8
256 filename_c = _dbus_string_get_const_data (filename);
257 tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
259 fd = open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
260 world_readable ? 0644 : 0600);
264 "Could not create %s: %s", tmp_filename_c,
265 _dbus_strerror (errno));
274 if (fchmod (fd, 0644) < 0)
277 "Could not chmod %s: %s", tmp_filename_c,
278 _dbus_strerror (errno));
283 _dbus_verbose (
"tmp file fd %d opened\n", fd);
288 bytes_to_write = _dbus_string_get_length (str);
290 while (total < bytes_to_write)
295 bytes_to_write - total);
297 if (bytes_written <= 0)
300 "Could not write to %s: %s", tmp_filename_c,
301 _dbus_strerror (errno));
306 total += bytes_written;
312 "Could not synchronize file %s: %s",
313 tmp_filename_c, _dbus_strerror (errno));
321 "Could not close file %s: %s",
322 tmp_filename_c, _dbus_strerror (errno));
329 if (rename (tmp_filename_c, filename_c) < 0)
332 "Could not rename %s to %s: %s",
333 tmp_filename_c, filename_c,
334 _dbus_strerror (errno));
351 if (need_unlink && unlink (tmp_filename_c) < 0)
352 _dbus_verbose (
"Failed to unlink temp file %s: %s\n",
353 tmp_filename_c, _dbus_strerror (errno));
357 _DBUS_ASSERT_ERROR_XOR_BOOL (error, retval);
371 const char *filename_c;
373 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
375 filename_c = _dbus_string_get_const_data (filename);
376 if (chmod (filename_c, 0644) == -1)
380 "Could not change permissions of file %s: %s\n",
382 _dbus_strerror (errno));
399 const char *filename_c;
401 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
403 filename_c = _dbus_string_get_const_data (filename);
405 fd = open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
411 "Could not create file %s: %s\n",
413 _dbus_strerror (errno));
417 _dbus_verbose (
"exclusive file fd %d opened\n", fd);
423 "Could not close file %s: %s\n",
425 _dbus_strerror (errno));
444 const char *filename_c;
446 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
448 filename_c = _dbus_string_get_const_data (filename);
450 if (unlink (filename_c) < 0)
453 "Failed to delete file %s: %s\n",
454 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_IN...
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.
Object representing an exception.