D-Bus 1.14.10
dbus-file-unix.c
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)
3 *
4 * Copyright (C) 2002, 2003, 2006 Red Hat, Inc.
5 * Copyright (C) 2003 CodeFactory AB
6 *
7 * Licensed under the Academic Free License version 2.1
8 *
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.
13 *
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.
18 *
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
22 *
23 */
24
25#include <config.h>
26
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"
33
34#include <sys/stat.h>
35#include <stdio.h>
36#include <fcntl.h>
37#include <unistd.h>
38#include <errno.h>
39
40#ifndef O_BINARY
41#define O_BINARY 0
42#endif
43
56 const DBusString *filename,
57 DBusError *error)
58{
59 int fd;
60 struct stat sb;
61 int orig_len;
62 int total;
63 const char *filename_c;
64
65 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
66
67 filename_c = _dbus_string_get_const_data (filename);
68
69 /* O_BINARY useful on Cygwin */
70 fd = open (filename_c, O_RDONLY | O_BINARY);
71 if (fd < 0)
72 {
74 "Failed to open \"%s\": %s",
75 filename_c,
76 _dbus_strerror (errno));
77 return FALSE;
78 }
79
80 _dbus_verbose ("file fd %d opened\n", fd);
81
82 if (fstat (fd, &sb) < 0)
83 {
85 "Failed to stat \"%s\": %s",
86 filename_c,
87 _dbus_strerror (errno));
88
89 _dbus_verbose ("fstat() failed: %s",
90 _dbus_strerror (errno));
91
92 _dbus_close (fd, NULL);
93
94 return FALSE;
95 }
96
97 if (sb.st_size > _DBUS_ONE_MEGABYTE)
98 {
100 "File size %lu of \"%s\" is too large.",
101 (unsigned long) sb.st_size, filename_c);
102 _dbus_close (fd, NULL);
103 return FALSE;
104 }
105
106 total = 0;
107 orig_len = _dbus_string_get_length (str);
108 if (sb.st_size > 0 && S_ISREG (sb.st_mode))
109 {
110 int bytes_read;
111
112 while (total < (int) sb.st_size)
113 {
114 bytes_read = _dbus_read (fd, str,
115 sb.st_size - total);
116 if (bytes_read <= 0)
117 {
119 "Error reading \"%s\": %s",
120 filename_c,
121 _dbus_strerror (errno));
122
123 _dbus_verbose ("read() failed: %s",
124 _dbus_strerror (errno));
125
126 _dbus_close (fd, NULL);
127 _dbus_string_set_length (str, orig_len);
128 return FALSE;
129 }
130 else
131 total += bytes_read;
132 }
133
134 _dbus_close (fd, NULL);
135 return TRUE;
136 }
137 else if (sb.st_size != 0)
138 {
139 _dbus_verbose ("Can only open regular files at the moment.\n");
141 "\"%s\" is not a regular file",
142 filename_c);
143 _dbus_close (fd, NULL);
144 return FALSE;
145 }
146 else
147 {
148 _dbus_close (fd, NULL);
149 return TRUE;
150 }
151}
152
165 const DBusString *filename,
166 dbus_bool_t world_readable,
167 DBusError *error)
168{
169 int fd;
170 int bytes_to_write;
171 const char *filename_c;
172 DBusString tmp_filename;
173 const char *tmp_filename_c;
174 int total;
175 dbus_bool_t need_unlink;
176 dbus_bool_t retval;
177
178 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
179
180 fd = -1;
181 retval = FALSE;
182 need_unlink = FALSE;
183
184 if (!_dbus_string_init (&tmp_filename))
185 {
187 return FALSE;
188 }
189
190 if (!_dbus_string_copy (filename, 0, &tmp_filename, 0))
191 {
193 _dbus_string_free (&tmp_filename);
194 return FALSE;
195 }
196
197 if (!_dbus_string_append (&tmp_filename, "."))
198 {
200 _dbus_string_free (&tmp_filename);
201 return FALSE;
202 }
203
204#define N_TMP_FILENAME_RANDOM_BYTES 8
205 if (!_dbus_generate_random_ascii (&tmp_filename, N_TMP_FILENAME_RANDOM_BYTES,
206 error))
207 {
208 _dbus_string_free (&tmp_filename);
209 return FALSE;
210 }
211
212 filename_c = _dbus_string_get_const_data (filename);
213 tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
214
215 fd = open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
216 world_readable ? 0644 : 0600);
217 if (fd < 0)
218 {
220 "Could not create %s: %s", tmp_filename_c,
221 _dbus_strerror (errno));
222 goto out;
223 }
224 if (world_readable)
225 {
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
229 */
230 if (fchmod (fd, 0644) < 0)
231 {
233 "Could not chmod %s: %s", tmp_filename_c,
234 _dbus_strerror (errno));
235 goto out;
236 }
237 }
238
239 _dbus_verbose ("tmp file fd %d opened\n", fd);
240
241 need_unlink = TRUE;
242
243 total = 0;
244 bytes_to_write = _dbus_string_get_length (str);
245
246 while (total < bytes_to_write)
247 {
248 int bytes_written;
249
250 bytes_written = _dbus_write (fd, str, total,
251 bytes_to_write - total);
252
253 if (bytes_written <= 0)
254 {
256 "Could not write to %s: %s", tmp_filename_c,
257 _dbus_strerror (errno));
258
259 goto out;
260 }
261
262 total += bytes_written;
263 }
264
265 if (fsync(fd))
266 {
268 "Could not synchronize file %s: %s",
269 tmp_filename_c, _dbus_strerror (errno));
270
271 goto out;
272 }
273
274 if (!_dbus_close (fd, NULL))
275 {
277 "Could not close file %s: %s",
278 tmp_filename_c, _dbus_strerror (errno));
279
280 goto out;
281 }
282
283 fd = -1;
284
285 if (rename (tmp_filename_c, filename_c) < 0)
286 {
288 "Could not rename %s to %s: %s",
289 tmp_filename_c, filename_c,
290 _dbus_strerror (errno));
291
292 goto out;
293 }
294
295 need_unlink = FALSE;
296
297 retval = TRUE;
298
299 out:
300 /* close first, then unlink, to prevent ".nfs34234235" garbage
301 * files
302 */
303
304 if (fd >= 0)
305 _dbus_close (fd, NULL);
306
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));
310
311 _dbus_string_free (&tmp_filename);
312
313 _DBUS_ASSERT_ERROR_XOR_BOOL (error, retval);
314 return retval;
315}
316
325 DBusError *error)
326{
327 const char *filename_c;
328
329 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
330
331 filename_c = _dbus_string_get_const_data (filename);
332 if (chmod (filename_c, 0644) == -1)
333 {
334 dbus_set_error (error,
336 "Could not change permissions of file %s: %s\n",
337 filename_c,
338 _dbus_strerror (errno));
339 return FALSE;
340 }
341 return TRUE;
342}
343
352 DBusError *error)
353{
354 int fd;
355 const char *filename_c;
356
357 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
358
359 filename_c = _dbus_string_get_const_data (filename);
360
361 fd = open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
362 0600);
363 if (fd < 0)
364 {
365 dbus_set_error (error,
367 "Could not create file %s: %s\n",
368 filename_c,
369 _dbus_strerror (errno));
370 return FALSE;
371 }
372
373 _dbus_verbose ("exclusive file fd %d opened\n", fd);
374
375 if (!_dbus_close (fd, NULL))
376 {
377 dbus_set_error (error,
379 "Could not close file %s: %s\n",
380 filename_c,
381 _dbus_strerror (errno));
382 return FALSE;
383 }
384
385 return TRUE;
386}
387
398 DBusError *error)
399{
400 const char *filename_c;
401
402 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
403
404 filename_c = _dbus_string_get_const_data (filename);
405
406 if (unlink (filename_c) < 0)
407 {
409 "Failed to delete file %s: %s\n",
410 filename_c, _dbus_strerror (errno));
411 return FALSE;
412 }
413 else
414 return TRUE;
415}
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:354
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.
Definition: dbus-sysdeps.c:599
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...
Definition: dbus-sysdeps.c:559
#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.
Definition: dbus-string.c:833
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:966
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:182
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...
Definition: dbus-string.c:1343
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
Definition: dbus-string.c:278
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.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
Object representing an exception.
Definition: dbus-errors.h:49