D-Bus 1.15.4
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 * SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
8 *
9 * Licensed under the Academic Free License version 2.1
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 */
26
27#include <config.h>
28
29#include "dbus-protocol.h"
30#include "dbus-errors.h"
31#include "dbus-file.h"
32#include "dbus-internals.h"
33#include "dbus-sysdeps.h"
34#include "dbus-sysdeps-unix.h"
35
36#include <sys/stat.h>
37#include <stdio.h>
38#include <fcntl.h>
39#include <unistd.h>
40#include <errno.h>
41
42#ifndef O_BINARY
43#define O_BINARY 0
44#endif
45
58 const DBusString *filename,
59 DBusError *error)
60{
61 int fd;
62 struct stat sb;
63 int orig_len;
64 int total;
65 const char *filename_c;
66
67 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
68
69 filename_c = _dbus_string_get_const_data (filename);
70
71 /* O_BINARY useful on Cygwin */
72 fd = open (filename_c, O_RDONLY | O_BINARY);
73 if (fd < 0)
74 {
76 "Failed to open \"%s\": %s",
77 filename_c,
78 _dbus_strerror (errno));
79 return FALSE;
80 }
81
82 _dbus_verbose ("file fd %d opened\n", fd);
83
84 if (fstat (fd, &sb) < 0)
85 {
87 "Failed to stat \"%s\": %s",
88 filename_c,
89 _dbus_strerror (errno));
90
91 _dbus_verbose ("fstat() failed: %s",
92 _dbus_strerror (errno));
93
94 _dbus_close (fd, NULL);
95
96 return FALSE;
97 }
98
99 if (sb.st_size > _DBUS_ONE_MEGABYTE)
100 {
102 "File size %lu of \"%s\" is too large.",
103 (unsigned long) sb.st_size, filename_c);
104 _dbus_close (fd, NULL);
105 return FALSE;
106 }
107
108 total = 0;
109 orig_len = _dbus_string_get_length (str);
110 if (sb.st_size > 0 && S_ISREG (sb.st_mode))
111 {
112 int bytes_read;
113
114 while (total < (int) sb.st_size)
115 {
116 bytes_read = _dbus_read (fd, str,
117 sb.st_size - total);
118 if (bytes_read <= 0)
119 {
121 "Error reading \"%s\": %s",
122 filename_c,
123 _dbus_strerror (errno));
124
125 _dbus_verbose ("read() failed: %s",
126 _dbus_strerror (errno));
127
128 _dbus_close (fd, NULL);
129 _dbus_string_set_length (str, orig_len);
130 return FALSE;
131 }
132 else
133 total += bytes_read;
134 }
135
136 _dbus_close (fd, NULL);
137 return TRUE;
138 }
139 else if (sb.st_size != 0)
140 {
141 _dbus_verbose ("Can only open regular files at the moment.\n");
143 "\"%s\" is not a regular file",
144 filename_c);
145 _dbus_close (fd, NULL);
146 return FALSE;
147 }
148 else
149 {
150 _dbus_close (fd, NULL);
151 return TRUE;
152 }
153}
154
167 const DBusString *filename,
168 dbus_bool_t world_readable,
169 DBusError *error)
170{
171 int fd;
172 int bytes_to_write;
173 const char *filename_c;
174 DBusString tmp_filename;
175 const char *tmp_filename_c;
176 int total;
177 dbus_bool_t need_unlink;
178 dbus_bool_t retval;
179
180 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
181
182 fd = -1;
183 retval = FALSE;
184 need_unlink = FALSE;
185
186 if (!_dbus_string_init (&tmp_filename))
187 {
189 return FALSE;
190 }
191
192 if (!_dbus_string_copy (filename, 0, &tmp_filename, 0))
193 {
195 _dbus_string_free (&tmp_filename);
196 return FALSE;
197 }
198
199 if (!_dbus_string_append (&tmp_filename, "."))
200 {
202 _dbus_string_free (&tmp_filename);
203 return FALSE;
204 }
205
206#define N_TMP_FILENAME_RANDOM_BYTES 8
207 if (!_dbus_generate_random_ascii (&tmp_filename, N_TMP_FILENAME_RANDOM_BYTES,
208 error))
209 {
210 _dbus_string_free (&tmp_filename);
211 return FALSE;
212 }
213
214 filename_c = _dbus_string_get_const_data (filename);
215 tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
216
217 fd = open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
218 world_readable ? 0644 : 0600);
219 if (fd < 0)
220 {
222 "Could not create %s: %s", tmp_filename_c,
223 _dbus_strerror (errno));
224 goto out;
225 }
226 if (world_readable)
227 {
228 /* Ensure the file is world readable even in the presence of
229 * possibly restrictive umasks;
230 * see http://lists.freedesktop.org/archives/dbus/2010-September/013367.html
231 */
232 if (fchmod (fd, 0644) < 0)
233 {
235 "Could not chmod %s: %s", tmp_filename_c,
236 _dbus_strerror (errno));
237 goto out;
238 }
239 }
240
241 _dbus_verbose ("tmp file fd %d opened\n", fd);
242
243 need_unlink = TRUE;
244
245 total = 0;
246 bytes_to_write = _dbus_string_get_length (str);
247
248 while (total < bytes_to_write)
249 {
250 int bytes_written;
251
252 bytes_written = _dbus_write (fd, str, total,
253 bytes_to_write - total);
254
255 if (bytes_written <= 0)
256 {
258 "Could not write to %s: %s", tmp_filename_c,
259 _dbus_strerror (errno));
260
261 goto out;
262 }
263
264 total += bytes_written;
265 }
266
267 if (fsync(fd))
268 {
270 "Could not synchronize file %s: %s",
271 tmp_filename_c, _dbus_strerror (errno));
272
273 goto out;
274 }
275
276 if (!_dbus_close (fd, NULL))
277 {
279 "Could not close file %s: %s",
280 tmp_filename_c, _dbus_strerror (errno));
281
282 goto out;
283 }
284
285 fd = -1;
286
287 if (rename (tmp_filename_c, filename_c) < 0)
288 {
290 "Could not rename %s to %s: %s",
291 tmp_filename_c, filename_c,
292 _dbus_strerror (errno));
293
294 goto out;
295 }
296
297 need_unlink = FALSE;
298
299 retval = TRUE;
300
301 out:
302 /* close first, then unlink, to prevent ".nfs34234235" garbage
303 * files
304 */
305
306 if (fd >= 0)
307 _dbus_close (fd, NULL);
308
309 if (need_unlink && unlink (tmp_filename_c) < 0)
310 _dbus_verbose ("Failed to unlink temp file %s: %s\n",
311 tmp_filename_c, _dbus_strerror (errno));
312
313 _dbus_string_free (&tmp_filename);
314
315 _DBUS_ASSERT_ERROR_XOR_BOOL (error, retval);
316 return retval;
317}
318
327 DBusError *error)
328{
329 const char *filename_c;
330
331 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
332
333 filename_c = _dbus_string_get_const_data (filename);
334 if (chmod (filename_c, 0644) == -1)
335 {
336 dbus_set_error (error,
338 "Could not change permissions of file %s: %s\n",
339 filename_c,
340 _dbus_strerror (errno));
341 return FALSE;
342 }
343 return TRUE;
344}
345
354 DBusError *error)
355{
356 int fd;
357 const char *filename_c;
358
359 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
360
361 filename_c = _dbus_string_get_const_data (filename);
362
363 fd = open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
364 0600);
365 if (fd < 0)
366 {
367 dbus_set_error (error,
369 "Could not create file %s: %s\n",
370 filename_c,
371 _dbus_strerror (errno));
372 return FALSE;
373 }
374
375 _dbus_verbose ("exclusive file fd %d opened\n", fd);
376
377 if (!_dbus_close (fd, NULL))
378 {
379 dbus_set_error (error,
381 "Could not close file %s: %s\n",
382 filename_c,
383 _dbus_strerror (errno));
384 return FALSE;
385 }
386
387 return TRUE;
388}
389
400 DBusError *error)
401{
402 const char *filename_c;
403
404 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
405
406 filename_c = _dbus_string_get_const_data (filename);
407
408 if (unlink (filename_c) < 0)
409 {
411 "Failed to delete file %s: %s\n",
412 filename_c, _dbus_strerror (errno));
413 return FALSE;
414 }
415 else
416 return TRUE;
417}
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:356
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:601
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:561
#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:847
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:980
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:1345
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
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
Definition: dbus-string.c:784
const char * _dbus_string_get_const_data(const DBusString *str)
Gets the raw character buffer from a const string.
Definition: dbus-string.c:513
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:37
Object representing an exception.
Definition: dbus-errors.h:51