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 error error to be filled in on failure
160 * @returns #FALSE on failure
163 _dbus_string_save_to_file (const DBusString *str,
164 const DBusString *filename,
169 const char *filename_c;
170 DBusString tmp_filename;
171 const char *tmp_filename_c;
173 dbus_bool_t need_unlink;
176 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
182 if (!_dbus_string_init (&tmp_filename))
184 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
188 if (!_dbus_string_copy (filename, 0, &tmp_filename, 0))
190 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
191 _dbus_string_free (&tmp_filename);
195 if (!_dbus_string_append (&tmp_filename, "."))
197 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
198 _dbus_string_free (&tmp_filename);
202 #define N_TMP_FILENAME_RANDOM_BYTES 8
203 if (!_dbus_generate_random_ascii (&tmp_filename, N_TMP_FILENAME_RANDOM_BYTES))
205 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
206 _dbus_string_free (&tmp_filename);
210 filename_c = _dbus_string_get_const_data (filename);
211 tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
213 fd = open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
217 dbus_set_error (error, _dbus_error_from_errno (errno),
218 "Could not create %s: %s", tmp_filename_c,
219 _dbus_strerror (errno));
223 _dbus_verbose ("tmp file fd %d opened\n", fd);
228 bytes_to_write = _dbus_string_get_length (str);
230 while (total < bytes_to_write)
234 bytes_written = _dbus_write (fd, str, total,
235 bytes_to_write - total);
237 if (bytes_written <= 0)
239 dbus_set_error (error, _dbus_error_from_errno (errno),
240 "Could not write to %s: %s", tmp_filename_c,
241 _dbus_strerror (errno));
246 total += bytes_written;
251 dbus_set_error (error, _dbus_error_from_errno (errno),
252 "Could not synchronize file %s: %s",
253 tmp_filename_c, _dbus_strerror (errno));
258 if (!_dbus_close (fd, NULL))
260 dbus_set_error (error, _dbus_error_from_errno (errno),
261 "Could not close file %s: %s",
262 tmp_filename_c, _dbus_strerror (errno));
269 if (rename (tmp_filename_c, filename_c) < 0)
271 dbus_set_error (error, _dbus_error_from_errno (errno),
272 "Could not rename %s to %s: %s",
273 tmp_filename_c, filename_c,
274 _dbus_strerror (errno));
284 /* close first, then unlink, to prevent ".nfs34234235" garbage
289 _dbus_close (fd, NULL);
291 if (need_unlink && unlink (tmp_filename_c) < 0)
292 _dbus_verbose ("Failed to unlink temp file %s: %s\n",
293 tmp_filename_c, _dbus_strerror (errno));
295 _dbus_string_free (&tmp_filename);
298 _DBUS_ASSERT_ERROR_IS_SET (error);
303 /** Makes the file readable by every user in the system.
305 * @param filename the filename
306 * @param error error location
307 * @returns #TRUE if the file's permissions could be changed.
310 _dbus_make_file_world_readable(const DBusString *filename,
313 const char *filename_c;
315 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
317 filename_c = _dbus_string_get_const_data (filename);
318 if (chmod (filename_c, 0644) == -1)
320 dbus_set_error (error,
322 "Could not change permissions of file %s: %s\n",
324 _dbus_strerror (errno));
330 /** Creates the given file, failing if the file already exists.
332 * @param filename the filename
333 * @param error error location
334 * @returns #TRUE if we created the file and it didn't exist
337 _dbus_create_file_exclusively (const DBusString *filename,
341 const char *filename_c;
343 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
345 filename_c = _dbus_string_get_const_data (filename);
347 fd = open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
351 dbus_set_error (error,
353 "Could not create file %s: %s\n",
355 _dbus_strerror (errno));
359 _dbus_verbose ("exclusive file fd %d opened\n", fd);
361 if (!_dbus_close (fd, NULL))
363 dbus_set_error (error,
365 "Could not close file %s: %s\n",
367 _dbus_strerror (errno));
375 * Deletes the given file.
377 * @param filename the filename
378 * @param error error location
380 * @returns #TRUE if unlink() succeeded
383 _dbus_delete_file (const DBusString *filename,
386 const char *filename_c;
388 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
390 filename_c = _dbus_string_get_const_data (filename);
392 if (unlink (filename_c) < 0)
394 dbus_set_error (error, DBUS_ERROR_FAILED,
395 "Failed to delete file %s: %s\n",
396 filename_c, _dbus_strerror (errno));