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
25 #include "dbus-protocol.h"
26 #include "dbus-string.h"
27 #include "dbus-internals.h"
28 #include "dbus-sysdeps-win.h"
29 #include "dbus-pipe.h"
38 * Appends the contents of the given file to the string,
39 * returning error code. At the moment, won't open a file
40 * more than a megabyte in size.
42 * @param str the string to append to
43 * @param filename filename to load
44 * @param error place to set an error
45 * @returns #FALSE if error was set
48 _dbus_file_get_contents (DBusString *str,
49 const DBusString *filename,
56 const char *filename_c;
58 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
60 filename_c = _dbus_string_get_const_data (filename);
62 fd = _open (filename_c, O_RDONLY | O_BINARY);
65 dbus_set_error (error, _dbus_error_from_errno (errno),
66 "Failed to open \"%s\": %s",
72 _dbus_verbose ("file %s fd %d opened\n", filename_c, fd);
74 if (_fstati64 (fd, &sb) < 0)
76 dbus_set_error (error, _dbus_error_from_errno (errno),
77 "Failed to stat \"%s\": %s",
81 _dbus_verbose ("fstat() failed: %s",
89 if (sb.st_size > _DBUS_ONE_MEGABYTE)
91 dbus_set_error (error, DBUS_ERROR_FAILED,
92 "File size %lu of \"%s\" is too large.",
93 (unsigned long) sb.st_size, filename_c);
99 orig_len = _dbus_string_get_length (str);
100 if (sb.st_size > 0 && S_ISREG (sb.st_mode))
104 while (total < (int) sb.st_size)
106 bytes_read = _dbus_file_read (fd, str, sb.st_size - total);
109 dbus_set_error (error, _dbus_error_from_errno (errno),
110 "Error reading \"%s\": %s",
114 _dbus_verbose ("read() failed: %s",
118 _dbus_string_set_length (str, orig_len);
128 else if (sb.st_size != 0)
130 _dbus_verbose ("Can only open regular files at the moment.\n");
131 dbus_set_error (error, DBUS_ERROR_FAILED,
132 "\"%s\" is not a regular file",
145 * Writes a string out to a file. If the file exists,
146 * it will be atomically overwritten by the new data.
148 * @param str the string to write out
149 * @param filename the file to save string to
150 * @param error error to be filled in on failure
151 * @returns #FALSE on failure
154 _dbus_string_save_to_file (const DBusString *str,
155 const DBusString *filename,
160 const char *filename_c;
161 DBusString tmp_filename;
162 const char *tmp_filename_c;
165 dbus_bool_t need_unlink;
168 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
174 if (!_dbus_string_init (&tmp_filename))
176 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
180 if (!_dbus_string_copy (filename, 0, &tmp_filename, 0))
182 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
183 _dbus_string_free (&tmp_filename);
187 if (!_dbus_string_append (&tmp_filename, "."))
189 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
190 _dbus_string_free (&tmp_filename);
194 #define N_TMP_FILENAME_RANDOM_BYTES 8
195 if (!_dbus_generate_random_ascii (&tmp_filename, N_TMP_FILENAME_RANDOM_BYTES))
197 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
198 _dbus_string_free (&tmp_filename);
202 filename_c = _dbus_string_get_const_data (filename);
203 tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
205 fd = _open (tmp_filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
209 dbus_set_error (error, _dbus_error_from_errno (errno),
210 "Could not create %s: %s", tmp_filename_c,
215 _dbus_verbose ("tmp file %s fd %d opened\n", tmp_filename_c, fd);
220 bytes_to_write = _dbus_string_get_length (str);
221 str_c = _dbus_string_get_const_data (str);
223 while (total < bytes_to_write)
227 bytes_written = _write (fd, str_c + total, bytes_to_write - total);
229 if (bytes_written <= 0)
231 dbus_set_error (error, _dbus_error_from_errno (errno),
232 "Could not write to %s: %s", tmp_filename_c,
237 total += bytes_written;
242 dbus_set_error (error, _dbus_error_from_errno (errno),
243 "Could not close file %s: %s",
244 tmp_filename_c, strerror (errno));
251 /* Unlike rename(), MoveFileEx() can replace existing files */
252 if (MoveFileExA (tmp_filename_c, filename_c, MOVEFILE_REPLACE_EXISTING) < 0)
254 char *emsg = _dbus_win_error_string (GetLastError ());
255 dbus_set_error (error, DBUS_ERROR_FAILED,
256 "Could not rename %s to %s: %s",
257 tmp_filename_c, filename_c,
259 _dbus_win_free_error_string (emsg);
269 /* close first, then unlink */
274 if (need_unlink && _unlink (tmp_filename_c) < 0)
275 _dbus_verbose ("failed to unlink temp file %s: %s\n",
276 tmp_filename_c, strerror (errno));
278 _dbus_string_free (&tmp_filename);
281 _DBUS_ASSERT_ERROR_IS_SET (error);
287 /** Creates the given file, failing if the file already exists.
289 * @param filename the filename
290 * @param error error location
291 * @returns #TRUE if we created the file and it didn't exist
294 _dbus_create_file_exclusively (const DBusString *filename,
298 const char *filename_c;
300 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
302 filename_c = _dbus_string_get_const_data (filename);
304 fd = _open (filename_c, O_WRONLY | O_BINARY | O_EXCL | O_CREAT,
308 dbus_set_error (error,
310 "Could not create file %s: %s\n",
316 _dbus_verbose ("exclusive file %s fd %d opened\n", filename_c, fd);
320 dbus_set_error (error,
322 "Could not close file %s: %s\n",
332 * Thin wrapper around the read() system call that appends
333 * the data it reads to the DBusString buffer. It appends
334 * up to the given count, and returns the same value
335 * and same errno as read(). The only exception is that
336 * _dbus_file_read() handles EINTR for you. Also,
337 * _dbus_file_read() can return ENOMEM.
339 * @param fd the file descriptor to read from
340 * @param buffer the buffer to append data to
341 * @param count the amount of data to read
342 * @returns the number of bytes read or -1
345 _dbus_file_read (int fd,
353 _dbus_assert (count >= 0);
355 start = _dbus_string_get_length (buffer);
357 if (!_dbus_string_lengthen (buffer, count))
363 data = _dbus_string_get_data_len (buffer, start, count);
367 bytes_read = _read (fd, data, count);
375 /* put length back (note that this doesn't actually realloc anything) */
376 _dbus_string_set_length (buffer, start);
382 /* put length back (doesn't actually realloc) */
383 _dbus_string_set_length (buffer, start + bytes_read);
387 _dbus_verbose_bytes_of_string (buffer, start, bytes_read);