1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-file-win.c windows 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-protocol.h"
27 #include "dbus-string.h"
28 #include "dbus-internals.h"
29 #include "dbus-sysdeps-win.h"
30 #include "dbus-pipe.h"
39 * Appends the contents of the given file to the string,
40 * returning error code. At the moment, won't open a file
41 * more than a megabyte in size.
43 * @param str the string to append to
44 * @param filename filename to load
45 * @param error place to set an error
46 * @returns #FALSE if error was set
49 _dbus_file_get_contents (DBusString *str,
50 const DBusString *filename,
57 const char *filename_c;
59 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
61 filename_c = _dbus_string_get_const_data (filename);
63 fd = _open (filename_c, O_RDONLY | O_BINARY);
66 dbus_set_error (error, _dbus_error_from_errno (errno),
67 "Failed to open \"%s\": %s",
73 _dbus_verbose ("file %s fd %d opened\n", filename_c, fd);
75 if (_fstati64 (fd, &sb) < 0)
77 dbus_set_error (error, _dbus_error_from_errno (errno),
78 "Failed to stat \"%s\": %s",
82 _dbus_verbose ("fstat() failed: %s",
90 if (sb.st_size > _DBUS_ONE_MEGABYTE)
92 dbus_set_error (error, DBUS_ERROR_FAILED,
93 "File size %lu of \"%s\" is too large.",
94 (unsigned long) sb.st_size, filename_c);
100 orig_len = _dbus_string_get_length (str);
101 if (sb.st_size > 0 && S_ISREG (sb.st_mode))
105 while (total < (int) sb.st_size)
107 bytes_read = _dbus_file_read (fd, str, sb.st_size - total);
110 dbus_set_error (error, _dbus_error_from_errno (errno),
111 "Error reading \"%s\": %s",
115 _dbus_verbose ("read() failed: %s",
119 _dbus_string_set_length (str, orig_len);
129 else if (sb.st_size != 0)
131 _dbus_verbose ("Can only open regular files at the moment.\n");
132 dbus_set_error (error, DBUS_ERROR_FAILED,
133 "\"%s\" is not a regular file",
146 * Writes a string out to a file. If the file exists,
147 * it will be atomically overwritten by the new data.
149 * @param str the string to write out
150 * @param filename the file to save string to
151 * @param error error to be filled in on failure
152 * @returns #FALSE on failure
155 _dbus_string_save_to_file (const DBusString *str,
156 const DBusString *filename,
161 const char *filename_c;
162 DBusString tmp_filename;
163 const char *tmp_filename_c;
166 dbus_bool_t need_unlink;
169 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
175 if (!_dbus_string_init (&tmp_filename))
177 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
181 if (!_dbus_string_copy (filename, 0, &tmp_filename, 0))
183 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
184 _dbus_string_free (&tmp_filename);
188 if (!_dbus_string_append (&tmp_filename, "."))
190 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
191 _dbus_string_free (&tmp_filename);
195 #define N_TMP_FILENAME_RANDOM_BYTES 8
196 if (!_dbus_generate_random_ascii (&tmp_filename, N_TMP_FILENAME_RANDOM_BYTES))
198 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
199 _dbus_string_free (&tmp_filename);
203 filename_c = _dbus_string_get_const_data (filename);
204 tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
206 fd = _open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
210 dbus_set_error (error, _dbus_error_from_errno (errno),
211 "Could not create %s: %s", tmp_filename_c,
216 _dbus_verbose ("tmp file %s fd %d opened\n", tmp_filename_c, fd);
221 bytes_to_write = _dbus_string_get_length (str);
222 str_c = _dbus_string_get_const_data (str);
224 while (total < bytes_to_write)
228 bytes_written = _write (fd, str_c + total, bytes_to_write - total);
230 if (bytes_written <= 0)
232 dbus_set_error (error, _dbus_error_from_errno (errno),
233 "Could not write to %s: %s", tmp_filename_c,
238 total += bytes_written;
243 dbus_set_error (error, _dbus_error_from_errno (errno),
244 "Could not close file %s: %s",
245 tmp_filename_c, strerror (errno));
252 /* Unlike rename(), MoveFileEx() can replace existing files */
253 if (!MoveFileExA (tmp_filename_c, filename_c, MOVEFILE_REPLACE_EXISTING))
255 char *emsg = _dbus_win_error_string (GetLastError ());
256 dbus_set_error (error, DBUS_ERROR_FAILED,
257 "Could not rename %s to %s: %s",
258 tmp_filename_c, filename_c,
260 _dbus_win_free_error_string (emsg);
270 /* close first, then unlink */
275 if (need_unlink && _unlink (tmp_filename_c) < 0)
276 _dbus_verbose ("failed to unlink temp file %s: %s\n",
277 tmp_filename_c, strerror (errno));
279 _dbus_string_free (&tmp_filename);
282 _DBUS_ASSERT_ERROR_IS_SET (error);
288 /** Creates the given file, failing if the file already exists.
290 * @param filename the filename
291 * @param error error location
292 * @returns #TRUE if we created the file and it didn't exist
295 _dbus_create_file_exclusively (const DBusString *filename,
299 const char *filename_c;
301 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
303 filename_c = _dbus_string_get_const_data (filename);
305 fd = _open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
309 dbus_set_error (error,
311 "Could not create file %s: %s\n",
317 _dbus_verbose ("exclusive file %s fd %d opened\n", filename_c, fd);
321 dbus_set_error (error,
323 "Could not close file %s: %s\n",
333 * Thin wrapper around the read() system call that appends
334 * the data it reads to the DBusString buffer. It appends
335 * up to the given count, and returns the same value
336 * and same errno as read(). The only exception is that
337 * _dbus_file_read() handles EINTR for you. Also,
338 * _dbus_file_read() can return ENOMEM.
340 * @param fd the file descriptor to read from
341 * @param buffer the buffer to append data to
342 * @param count the amount of data to read
343 * @returns the number of bytes read or -1
346 _dbus_file_read (int fd,
354 _dbus_assert (count >= 0);
356 start = _dbus_string_get_length (buffer);
358 if (!_dbus_string_lengthen (buffer, count))
364 data = _dbus_string_get_data_len (buffer, start, count);
368 bytes_read = _read (fd, data, count);
376 /* put length back (note that this doesn't actually realloc anything) */
377 _dbus_string_set_length (buffer, start);
383 /* put length back (doesn't actually realloc) */
384 _dbus_string_set_length (buffer, start + bytes_read);
388 _dbus_verbose_bytes_of_string (buffer, start, bytes_read);