D-Bus  1.13.16
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  {
73  dbus_set_error (error, _dbus_error_from_errno (errno),
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  {
84  dbus_set_error (error, _dbus_error_from_errno (errno),
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  {
118  dbus_set_error (error, _dbus_error_from_errno (errno),
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 && S_ISREG (sb.st_mode))
138  {
139  int bytes_read = -1;
140 
141  while (bytes_read != 0)
142  {
143  if (total > _DBUS_ONE_MEGABYTE)
144  {
146  "File size of \"%s\" is larger than %lu.",
147  filename_c, (unsigned long) _DBUS_ONE_MEGABYTE);
148  _dbus_close (fd, NULL);
149  return FALSE;
150  }
151  bytes_read = _dbus_read (fd, str, 1024);
152  if (bytes_read < 0)
153  {
154  dbus_set_error (error, _dbus_error_from_errno (errno),
155  "Error reading \"%s\": %s",
156  filename_c,
157  _dbus_strerror (errno));
158 
159  _dbus_verbose ("read() failed: %s",
160  _dbus_strerror (errno));
161 
162  _dbus_close (fd, NULL);
163  _dbus_string_set_length (str, orig_len);
164  return FALSE;
165  }
166  else
167  total += bytes_read;
168  }
169 
170  _dbus_close (fd, NULL);
171  return TRUE;
172  }
173  else if (sb.st_size != 0)
174  {
175  _dbus_verbose ("Can only open regular files at the moment.\n");
177  "\"%s\" is not a regular file",
178  filename_c);
179  _dbus_close (fd, NULL);
180  return FALSE;
181  }
182  else
183  {
184  _dbus_close (fd, NULL);
185  return TRUE;
186  }
187 }
188 
201  const DBusString *filename,
202  dbus_bool_t world_readable,
203  DBusError *error)
204 {
205  int fd;
206  int bytes_to_write;
207  const char *filename_c;
208  DBusString tmp_filename;
209  const char *tmp_filename_c;
210  int total;
211  dbus_bool_t need_unlink;
212  dbus_bool_t retval;
213 
214  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
215 
216  fd = -1;
217  retval = FALSE;
218  need_unlink = FALSE;
219 
220  if (!_dbus_string_init (&tmp_filename))
221  {
223  return FALSE;
224  }
225 
226  if (!_dbus_string_copy (filename, 0, &tmp_filename, 0))
227  {
229  _dbus_string_free (&tmp_filename);
230  return FALSE;
231  }
232 
233  if (!_dbus_string_append (&tmp_filename, "."))
234  {
236  _dbus_string_free (&tmp_filename);
237  return FALSE;
238  }
239 
240 #define N_TMP_FILENAME_RANDOM_BYTES 8
241  if (!_dbus_generate_random_ascii (&tmp_filename, N_TMP_FILENAME_RANDOM_BYTES,
242  error))
243  {
244  _dbus_string_free (&tmp_filename);
245  return FALSE;
246  }
247 
248  filename_c = _dbus_string_get_const_data (filename);
249  tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
250 
251  fd = open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
252  world_readable ? 0644 : 0600);
253  if (fd < 0)
254  {
255  dbus_set_error (error, _dbus_error_from_errno (errno),
256  "Could not create %s: %s", tmp_filename_c,
257  _dbus_strerror (errno));
258  goto out;
259  }
260  if (world_readable)
261  {
262  /* Ensure the file is world readable even in the presence of
263  * possibly restrictive umasks;
264  * see http://lists.freedesktop.org/archives/dbus/2010-September/013367.html
265  */
266  if (fchmod (fd, 0644) < 0)
267  {
268  dbus_set_error (error, _dbus_error_from_errno (errno),
269  "Could not chmod %s: %s", tmp_filename_c,
270  _dbus_strerror (errno));
271  goto out;
272  }
273  }
274 
275  _dbus_verbose ("tmp file fd %d opened\n", fd);
276 
277  need_unlink = TRUE;
278 
279  total = 0;
280  bytes_to_write = _dbus_string_get_length (str);
281 
282  while (total < bytes_to_write)
283  {
284  int bytes_written;
285 
286  bytes_written = _dbus_write (fd, str, total,
287  bytes_to_write - total);
288 
289  if (bytes_written <= 0)
290  {
291  dbus_set_error (error, _dbus_error_from_errno (errno),
292  "Could not write to %s: %s", tmp_filename_c,
293  _dbus_strerror (errno));
294 
295  goto out;
296  }
297 
298  total += bytes_written;
299  }
300 
301  if (fsync(fd))
302  {
303  dbus_set_error (error, _dbus_error_from_errno (errno),
304  "Could not synchronize file %s: %s",
305  tmp_filename_c, _dbus_strerror (errno));
306 
307  goto out;
308  }
309 
310  if (!_dbus_close (fd, NULL))
311  {
312  dbus_set_error (error, _dbus_error_from_errno (errno),
313  "Could not close file %s: %s",
314  tmp_filename_c, _dbus_strerror (errno));
315 
316  goto out;
317  }
318 
319  fd = -1;
320 
321  if (rename (tmp_filename_c, filename_c) < 0)
322  {
323  dbus_set_error (error, _dbus_error_from_errno (errno),
324  "Could not rename %s to %s: %s",
325  tmp_filename_c, filename_c,
326  _dbus_strerror (errno));
327 
328  goto out;
329  }
330 
331  need_unlink = FALSE;
332 
333  retval = TRUE;
334 
335  out:
336  /* close first, then unlink, to prevent ".nfs34234235" garbage
337  * files
338  */
339 
340  if (fd >= 0)
341  _dbus_close (fd, NULL);
342 
343  if (need_unlink && unlink (tmp_filename_c) < 0)
344  _dbus_verbose ("Failed to unlink temp file %s: %s\n",
345  tmp_filename_c, _dbus_strerror (errno));
346 
347  _dbus_string_free (&tmp_filename);
348 
349  _DBUS_ASSERT_ERROR_XOR_BOOL (error, retval);
350  return retval;
351 }
352 
361  DBusError *error)
362 {
363  const char *filename_c;
364 
365  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
366 
367  filename_c = _dbus_string_get_const_data (filename);
368  if (chmod (filename_c, 0644) == -1)
369  {
370  dbus_set_error (error,
372  "Could not change permissions of file %s: %s\n",
373  filename_c,
374  _dbus_strerror (errno));
375  return FALSE;
376  }
377  return TRUE;
378 }
379 
388  DBusError *error)
389 {
390  int fd;
391  const char *filename_c;
392 
393  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
394 
395  filename_c = _dbus_string_get_const_data (filename);
396 
397  fd = open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
398  0600);
399  if (fd < 0)
400  {
401  dbus_set_error (error,
403  "Could not create file %s: %s\n",
404  filename_c,
405  _dbus_strerror (errno));
406  return FALSE;
407  }
408 
409  _dbus_verbose ("exclusive file fd %d opened\n", fd);
410 
411  if (!_dbus_close (fd, NULL))
412  {
413  dbus_set_error (error,
415  "Could not close file %s: %s\n",
416  filename_c,
417  _dbus_strerror (errno));
418  return FALSE;
419  }
420 
421  return TRUE;
422 }
423 
433 _dbus_delete_file (const DBusString *filename,
434  DBusError *error)
435 {
436  const char *filename_c;
437 
438  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
439 
440  filename_c = _dbus_string_get_const_data (filename);
441 
442  if (unlink (filename_c) < 0)
443  {
445  "Failed to delete file %s: %s\n",
446  filename_c, _dbus_strerror (errno));
447  return FALSE;
448  }
449  else
450  return TRUE;
451 }
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:959
#define NULL
A null pointer, defined appropriately for C or C++.
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.
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 ...
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.
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&#39;s copied to the d...
Definition: dbus-string.c:1307
dbus_bool_t _dbus_delete_file(const DBusString *filename, DBusError *error)
Deletes the given file.
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_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
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
dbus_bool_t _dbus_make_file_world_readable(const DBusString *filename, DBusError *error)
Makes the file readable by every user in the system.
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...
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
Definition: dbus-string.c:763
Object representing an exception.
Definition: dbus-errors.h:48
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
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:271
#define TRUE
Expands to &quot;1&quot;.
#define DBUS_ERROR_FAILED
A generic error; &quot;something went wrong&quot; - see the error message for more.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
dbus_bool_t _dbus_close(int fd, DBusError *error)
Closes a file descriptor.
#define FALSE
Expands to &quot;0&quot;.
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:826
const char * _dbus_string_get_const_data(const DBusString *str)
Gets the raw character buffer from a const string.
Definition: dbus-string.c:492