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
26 #include "dbus-file.h"
29 * Appends the contents of the given file to the string,
30 * returning error code. At the moment, won't open a file
31 * more than a megabyte in size.
33 * @param str the string to append to
34 * @param filename filename to load
35 * @param error place to set an error
36 * @returns #FALSE if error was set
39 _dbus_file_get_contents (DBusString *str,
40 const DBusString *filename,
47 const char *filename_c;
49 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
51 filename_c = _dbus_string_get_const_data (filename);
53 /* O_BINARY useful on Cygwin */
54 fd = open (filename_c, O_RDONLY | O_BINARY);
57 dbus_set_error (error, _dbus_error_from_errno (errno),
58 "Failed to open \"%s\": %s",
60 _dbus_strerror (errno));
64 _dbus_verbose ("file fd %d opened\n", fd);
66 if (fstat (fd, &sb) < 0)
68 dbus_set_error (error, _dbus_error_from_errno (errno),
69 "Failed to stat \"%s\": %s",
71 _dbus_strerror (errno));
73 _dbus_verbose ("fstat() failed: %s",
74 _dbus_strerror (errno));
76 _dbus_close (fd, NULL);
81 if (sb.st_size > _DBUS_ONE_MEGABYTE)
83 dbus_set_error (error, DBUS_ERROR_FAILED,
84 "File size %lu of \"%s\" is too large.",
85 (unsigned long) sb.st_size, filename_c);
86 _dbus_close (fd, NULL);
91 orig_len = _dbus_string_get_length (str);
92 if (sb.st_size > 0 && S_ISREG (sb.st_mode))
96 while (total < (int) sb.st_size)
98 bytes_read = _dbus_read (fd, str,
102 dbus_set_error (error, _dbus_error_from_errno (errno),
103 "Error reading \"%s\": %s",
105 _dbus_strerror (errno));
107 _dbus_verbose ("read() failed: %s",
108 _dbus_strerror (errno));
110 _dbus_close (fd, NULL);
111 _dbus_string_set_length (str, orig_len);
118 _dbus_close (fd, NULL);
121 else if (sb.st_size != 0)
123 _dbus_verbose ("Can only open regular files at the moment.\n");
124 dbus_set_error (error, DBUS_ERROR_FAILED,
125 "\"%s\" is not a regular file",
127 _dbus_close (fd, NULL);
132 _dbus_close (fd, NULL);
138 * Writes a string out to a file. If the file exists,
139 * it will be atomically overwritten by the new data.
141 * @param str the string to write out
142 * @param filename the file to save string to
143 * @param error error to be filled in on failure
144 * @returns #FALSE on failure
147 _dbus_string_save_to_file (const DBusString *str,
148 const DBusString *filename,
153 const char *filename_c;
154 DBusString tmp_filename;
155 const char *tmp_filename_c;
157 dbus_bool_t need_unlink;
160 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
166 if (!_dbus_string_init (&tmp_filename))
168 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
172 if (!_dbus_string_copy (filename, 0, &tmp_filename, 0))
174 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
175 _dbus_string_free (&tmp_filename);
179 if (!_dbus_string_append (&tmp_filename, "."))
181 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
182 _dbus_string_free (&tmp_filename);
186 #define N_TMP_FILENAME_RANDOM_BYTES 8
187 if (!_dbus_generate_random_ascii (&tmp_filename, N_TMP_FILENAME_RANDOM_BYTES))
189 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
190 _dbus_string_free (&tmp_filename);
194 filename_c = _dbus_string_get_const_data (filename);
195 tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
197 fd = open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
201 dbus_set_error (error, _dbus_error_from_errno (errno),
202 "Could not create %s: %s", tmp_filename_c,
203 _dbus_strerror (errno));
207 _dbus_verbose ("tmp file fd %d opened\n", fd);
212 bytes_to_write = _dbus_string_get_length (str);
214 while (total < bytes_to_write)
218 bytes_written = _dbus_write (fd, str, total,
219 bytes_to_write - total);
221 if (bytes_written <= 0)
223 dbus_set_error (error, _dbus_error_from_errno (errno),
224 "Could not write to %s: %s", tmp_filename_c,
225 _dbus_strerror (errno));
230 total += bytes_written;
235 dbus_set_error (error, _dbus_error_from_errno (errno),
236 "Could not synchronize file %s: %s",
237 tmp_filename_c, _dbus_strerror (errno));
242 if (!_dbus_close (fd, NULL))
244 dbus_set_error (error, _dbus_error_from_errno (errno),
245 "Could not close file %s: %s",
246 tmp_filename_c, _dbus_strerror (errno));
253 if (rename (tmp_filename_c, filename_c) < 0)
255 dbus_set_error (error, _dbus_error_from_errno (errno),
256 "Could not rename %s to %s: %s",
257 tmp_filename_c, filename_c,
258 _dbus_strerror (errno));
268 /* close first, then unlink, to prevent ".nfs34234235" garbage
273 _dbus_close (fd, NULL);
275 if (need_unlink && unlink (tmp_filename_c) < 0)
276 _dbus_verbose ("Failed to unlink temp file %s: %s\n",
277 tmp_filename_c, _dbus_strerror (errno));
279 _dbus_string_free (&tmp_filename);
282 _DBUS_ASSERT_ERROR_IS_SET (error);
287 /** Makes the file readable by every user in the system.
289 * @param filename the filename
290 * @param error error location
291 * @returns #TRUE if the file's permissions could be changed.
294 _dbus_make_file_world_readable(const DBusString *filename,
297 const char *filename_c;
299 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
301 filename_c = _dbus_string_get_const_data (filename);
302 if (chmod (filename_c, 0644) == -1)
304 dbus_set_error (error,
306 "Could not change permissions of file %s: %s\n",
308 _dbus_strerror (errno));
314 /** Creates the given file, failing if the file already exists.
316 * @param filename the filename
317 * @param error error location
318 * @returns #TRUE if we created the file and it didn't exist
321 _dbus_create_file_exclusively (const DBusString *filename,
325 const char *filename_c;
327 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
329 filename_c = _dbus_string_get_const_data (filename);
331 fd = open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
335 dbus_set_error (error,
337 "Could not create file %s: %s\n",
339 _dbus_strerror (errno));
343 _dbus_verbose ("exclusive file fd %d opened\n", fd);
345 if (!_dbus_close (fd, NULL))
347 dbus_set_error (error,
349 "Could not close file %s: %s\n",
351 _dbus_strerror (errno));
359 * Deletes the given file.
361 * @param filename the filename
362 * @param error error location
364 * @returns #TRUE if unlink() succeeded
367 _dbus_delete_file (const DBusString *filename,
370 const char *filename_c;
372 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
374 filename_c = _dbus_string_get_const_data (filename);
376 if (unlink (filename_c) < 0)
378 dbus_set_error (error, DBUS_ERROR_FAILED,
379 "Failed to delete file %s: %s\n",
380 filename_c, _dbus_strerror (errno));