1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-file-unix.c unix related file implementation (internal to D-Bus implementation)
4 * Copyright (C) 2002, 2003, 2006 Red Hat, Inc.
5 * Copyright (C) 2003 CodeFactory AB
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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"
45 * Appends the contents of the given file to the string,
46 * returning error code. At the moment, won't open a file
47 * more than a megabyte in size.
49 * @param str the string to append to
50 * @param filename filename to load
51 * @param error place to set an error
52 * @returns #FALSE if error was set
55 _dbus_file_get_contents (DBusString *str,
56 const DBusString *filename,
63 const char *filename_c;
65 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
67 filename_c = _dbus_string_get_const_data (filename);
69 /* O_BINARY useful on Cygwin */
70 fd = open (filename_c, O_RDONLY | O_BINARY);
73 dbus_set_error (error, _dbus_error_from_errno (errno),
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)
84 dbus_set_error (error, _dbus_error_from_errno (errno),
85 "Failed to stat \"%s\": %s",
87 _dbus_strerror (errno));
89 _dbus_verbose ("fstat() failed: %s",
90 _dbus_strerror (errno));
92 _dbus_close (fd, NULL);
97 if (sb.st_size > _DBUS_ONE_MEGABYTE)
99 dbus_set_error (error, DBUS_ERROR_FAILED,
100 "File size %lu of \"%s\" is too large.",
101 (unsigned long) sb.st_size, filename_c);
102 _dbus_close (fd, NULL);
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)
114 bytes_read = _dbus_read (fd, str,
118 dbus_set_error (error, _dbus_error_from_errno (errno),
119 "Error reading \"%s\": %s",
121 _dbus_strerror (errno));
123 _dbus_verbose ("read() failed: %s",
124 _dbus_strerror (errno));
126 _dbus_close (fd, NULL);
127 _dbus_string_set_length (str, orig_len);
134 _dbus_close (fd, NULL);
137 else if (sb.st_size != 0)
139 _dbus_verbose ("Can only open regular files at the moment.\n");
140 dbus_set_error (error, DBUS_ERROR_FAILED,
141 "\"%s\" is not a regular file",
143 _dbus_close (fd, NULL);
148 _dbus_close (fd, NULL);
154 * Writes a string out to a file. If the file exists,
155 * it will be atomically overwritten by the new data.
157 * @param str the string to write out
158 * @param filename the file to save string to
159 * @param world_readable If set, ensure the file is world readable
160 * @param error error to be filled in on failure
161 * @returns #FALSE on failure
164 _dbus_string_save_to_file (const DBusString *str,
165 const DBusString *filename,
166 dbus_bool_t world_readable,
171 const char *filename_c;
172 DBusString tmp_filename;
173 const char *tmp_filename_c;
175 dbus_bool_t need_unlink;
178 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
184 if (!_dbus_string_init (&tmp_filename))
186 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
190 if (!_dbus_string_copy (filename, 0, &tmp_filename, 0))
192 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
193 _dbus_string_free (&tmp_filename);
197 if (!_dbus_string_append (&tmp_filename, "."))
199 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
200 _dbus_string_free (&tmp_filename);
204 #define N_TMP_FILENAME_RANDOM_BYTES 8
205 if (!_dbus_generate_random_ascii (&tmp_filename, N_TMP_FILENAME_RANDOM_BYTES))
207 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
208 _dbus_string_free (&tmp_filename);
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);
219 dbus_set_error (error, _dbus_error_from_errno (errno),
220 "Could not create %s: %s", tmp_filename_c,
221 _dbus_strerror (errno));
226 /* Ensure the file is world readable even in the presence of
227 * possibly restrictive umasks;
228 * see http://lists.freedesktop.org/archives/dbus/2010-September/013367.html
230 if (fchmod (fd, 0644) < 0)
232 dbus_set_error (error, _dbus_error_from_errno (errno),
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)
250 bytes_written = _dbus_write (fd, str, total,
251 bytes_to_write - total);
253 if (bytes_written <= 0)
255 dbus_set_error (error, _dbus_error_from_errno (errno),
256 "Could not write to %s: %s", tmp_filename_c,
257 _dbus_strerror (errno));
262 total += bytes_written;
267 dbus_set_error (error, _dbus_error_from_errno (errno),
268 "Could not synchronize file %s: %s",
269 tmp_filename_c, _dbus_strerror (errno));
274 if (!_dbus_close (fd, NULL))
276 dbus_set_error (error, _dbus_error_from_errno (errno),
277 "Could not close file %s: %s",
278 tmp_filename_c, _dbus_strerror (errno));
285 if (rename (tmp_filename_c, filename_c) < 0)
287 dbus_set_error (error, _dbus_error_from_errno (errno),
288 "Could not rename %s to %s: %s",
289 tmp_filename_c, filename_c,
290 _dbus_strerror (errno));
300 /* close first, then unlink, to prevent ".nfs34234235" garbage
305 _dbus_close (fd, NULL);
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));
311 _dbus_string_free (&tmp_filename);
314 _DBUS_ASSERT_ERROR_IS_SET (error);
319 /** Makes the file readable by every user in the system.
321 * @param filename the filename
322 * @param error error location
323 * @returns #TRUE if the file's permissions could be changed.
326 _dbus_make_file_world_readable(const DBusString *filename,
329 const char *filename_c;
331 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
333 filename_c = _dbus_string_get_const_data (filename);
334 if (chmod (filename_c, 0644) == -1)
336 dbus_set_error (error,
338 "Could not change permissions of file %s: %s\n",
340 _dbus_strerror (errno));
346 /** Creates the given file, failing if the file already exists.
348 * @param filename the filename
349 * @param error error location
350 * @returns #TRUE if we created the file and it didn't exist
353 _dbus_create_file_exclusively (const DBusString *filename,
357 const char *filename_c;
359 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
361 filename_c = _dbus_string_get_const_data (filename);
363 fd = open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
367 dbus_set_error (error,
369 "Could not create file %s: %s\n",
371 _dbus_strerror (errno));
375 _dbus_verbose ("exclusive file fd %d opened\n", fd);
377 if (!_dbus_close (fd, NULL))
379 dbus_set_error (error,
381 "Could not close file %s: %s\n",
383 _dbus_strerror (errno));
391 * Deletes the given file.
393 * @param filename the filename
394 * @param error error location
396 * @returns #TRUE if unlink() succeeded
399 _dbus_delete_file (const DBusString *filename,
402 const char *filename_c;
404 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
406 filename_c = _dbus_string_get_const_data (filename);
408 if (unlink (filename_c) < 0)
410 dbus_set_error (error, DBUS_ERROR_FAILED,
411 "Failed to delete file %s: %s\n",
412 filename_c, _dbus_strerror (errno));