- goto out;
- }
-
- fd = -1;
-
- if (rename (tmp_filename_c, filename_c) < 0)
- {
- dbus_set_error (error, _dbus_error_from_errno (errno),
- "Could not rename %s to %s: %s",
- tmp_filename_c, filename_c,
- _dbus_strerror (errno));
-
- goto out;
- }
-
- need_unlink = FALSE;
-
- retval = TRUE;
-
- out:
- /* close first, then unlink, to prevent ".nfs34234235" garbage
- * files
- */
-
- if (fd >= 0)
- _dbus_close (fd, NULL);
-
- if (need_unlink && unlink (tmp_filename_c) < 0)
- _dbus_verbose ("Failed to unlink temp file %s: %s\n",
- tmp_filename_c, _dbus_strerror (errno));
-
- _dbus_string_free (&tmp_filename);
-
- if (!retval)
- _DBUS_ASSERT_ERROR_IS_SET (error);
-
- return retval;
-}
-
-/** Makes the file readable by every user in the system.
- *
- * @param filename the filename
- * @param error error location
- * @returns #TRUE if the file's permissions could be changed.
- */
-dbus_bool_t
-_dbus_make_file_world_readable(const DBusString *filename,
- DBusError *error)
-{
- const char *filename_c;
-
- _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-
- filename_c = _dbus_string_get_const_data (filename);
- if (chmod (filename_c, 0644) == -1)
- {
- dbus_set_error (error,
- DBUS_ERROR_FAILED,
- "Could not change permissions of file %s: %s\n",
- filename_c,
- _dbus_strerror (errno));
- return FALSE;
- }
- return TRUE;
-}
-
-/** Creates the given file, failing if the file already exists.
- *
- * @param filename the filename
- * @param error error location
- * @returns #TRUE if we created the file and it didn't exist
- */
-dbus_bool_t
-_dbus_create_file_exclusively (const DBusString *filename,
- DBusError *error)
-{
- int fd;
- const char *filename_c;
-
- _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-
- filename_c = _dbus_string_get_const_data (filename);
-
- fd = open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
- 0600);
- if (fd < 0)
- {
- dbus_set_error (error,
- DBUS_ERROR_FAILED,
- "Could not create file %s: %s\n",
- filename_c,
- _dbus_strerror (errno));
- return FALSE;
- }
-
- _dbus_verbose ("exclusive file fd %d opened\n", fd);
-
- if (!_dbus_close (fd, NULL))
- {
- dbus_set_error (error,
- DBUS_ERROR_FAILED,
- "Could not close file %s: %s\n",
- filename_c,
- _dbus_strerror (errno));
- return FALSE;
- }
-
- return TRUE;
-}
-
-/**
- * Deletes the given file.
- *
- * @param filename the filename
- * @param error error location
- *
- * @returns #TRUE if unlink() succeeded
- */
-dbus_bool_t
-_dbus_delete_file (const DBusString *filename,
- DBusError *error)
-{
- const char *filename_c;
-
- _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-
- filename_c = _dbus_string_get_const_data (filename);
-
- if (unlink (filename_c) < 0)
- {
- dbus_set_error (error, DBUS_ERROR_FAILED,
- "Failed to delete file %s: %s\n",
- filename_c, _dbus_strerror (errno));
- return FALSE;
- }
- else
- return TRUE;
-}
-
-/**
- * Creates a directory; succeeds if the directory
- * is created or already existed.
- *
- * @param filename directory filename
- * @param error initialized error object
- * @returns #TRUE on success
- */
-dbus_bool_t
-_dbus_create_directory (const DBusString *filename,
- DBusError *error)
-{
- const char *filename_c;
-
- _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-
- filename_c = _dbus_string_get_const_data (filename);
-
- if (mkdir (filename_c, 0700) < 0)
- {
- if (errno == EEXIST)
- return TRUE;
-
- dbus_set_error (error, DBUS_ERROR_FAILED,
- "Failed to create directory %s: %s\n",
- filename_c, _dbus_strerror (errno));
- return FALSE;
- }
- else
- return TRUE;
-}
-
-/**
- * Appends the given filename to the given directory.
- *
- * @todo it might be cute to collapse multiple '/' such as "foo//"
- * concat "//bar"
- *
- * @param dir the directory name
- * @param next_component the filename
- * @returns #TRUE on success
- */
-dbus_bool_t
-_dbus_concat_dir_and_file (DBusString *dir,
- const DBusString *next_component)
-{
- dbus_bool_t dir_ends_in_slash;
- dbus_bool_t file_starts_with_slash;
-
- if (_dbus_string_get_length (dir) == 0 ||
- _dbus_string_get_length (next_component) == 0)
- return TRUE;
-
- dir_ends_in_slash = '/' == _dbus_string_get_byte (dir,
- _dbus_string_get_length (dir) - 1);
-
- file_starts_with_slash = '/' == _dbus_string_get_byte (next_component, 0);
-
- if (dir_ends_in_slash && file_starts_with_slash)
- {
- _dbus_string_shorten (dir, 1);
- }
- else if (!(dir_ends_in_slash || file_starts_with_slash))
- {
- if (!_dbus_string_append_byte (dir, '/'))
- return FALSE;
- }
-
- return _dbus_string_copy (next_component, 0, dir,
- _dbus_string_get_length (dir));
-}
-
-/** nanoseconds in a second */
-#define NANOSECONDS_PER_SECOND 1000000000
-/** microseconds in a second */
-#define MICROSECONDS_PER_SECOND 1000000
-/** milliseconds in a second */
-#define MILLISECONDS_PER_SECOND 1000
-/** nanoseconds in a millisecond */
-#define NANOSECONDS_PER_MILLISECOND 1000000
-/** microseconds in a millisecond */
-#define MICROSECONDS_PER_MILLISECOND 1000
-
-/**
- * Sleeps the given number of milliseconds.
- * @param milliseconds number of milliseconds
- */
-void
-_dbus_sleep_milliseconds (int milliseconds)
-{
-#ifdef HAVE_NANOSLEEP
- struct timespec req;
- struct timespec rem;
-
- req.tv_sec = milliseconds / MILLISECONDS_PER_SECOND;
- req.tv_nsec = (milliseconds % MILLISECONDS_PER_SECOND) * NANOSECONDS_PER_MILLISECOND;
- rem.tv_sec = 0;
- rem.tv_nsec = 0;