dbus-file-win.c
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-file-win.c windows related file implementation (internal to D-Bus implementation)
*
* Copyright (C) 2002, 2003, 2006 Red Hat, Inc.
* Copyright (C) 2003 CodeFactory AB
*
* SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <config.h>
#include "dbus-protocol.h"
#include "dbus-string.h"
#include "dbus-internals.h"
#include "dbus-sysdeps-win.h"
#include "dbus-pipe.h"
#include <windows.h>
static int
_dbus_file_read (HANDLE hnd,
DBusString *buffer,
int count,
DBusError *error)
{
BOOL result;
DWORD bytes_read;
int start;
char *data;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
_dbus_assert (count >= 0);
start = _dbus_string_get_length (buffer);
if (!_dbus_string_lengthen (buffer, count))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
return -1;
}
data = _dbus_string_get_data_len (buffer, start, count);
result = ReadFile (hnd, data, count, &bytes_read, NULL);
if (result == 0)
{
char *emsg = _dbus_win_error_string (GetLastError ());
dbus_set_error (error, _dbus_win_error_from_last_error (),
"Failed to read from %p: %s", hnd, emsg);
_dbus_win_free_error_string (emsg);
return -1;
}
if (bytes_read)
{
/* put length back (doesn't actually realloc) */
_dbus_string_set_length (buffer, start + bytes_read);
#if 0
if (bytes_read > 0)
_dbus_verbose_bytes_of_string (buffer, start, bytes_read);
#endif
}
return bytes_read;
}
dbus_bool_t
_dbus_file_get_contents (DBusString *str,
const DBusString *filename,
DBusError *error)
{
HANDLE hnd;
DWORD fsize;
DWORD fsize_hi;
int orig_len;
unsigned int total;
const char *filename_c;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
filename_c = _dbus_string_get_const_data (filename);
hnd = CreateFileA (filename_c, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hnd == INVALID_HANDLE_VALUE)
{
char *emsg = _dbus_win_error_string (GetLastError ());
dbus_set_error (error, _dbus_win_error_from_last_error (),
"Failed to open \"%s\": %s", filename_c, emsg);
_dbus_win_free_error_string (emsg);
return FALSE;
}
_dbus_verbose ("file %s hnd %p opened\n", filename_c, hnd);
fsize = GetFileSize (hnd, &fsize_hi);
if (fsize == 0xFFFFFFFF && GetLastError() != NO_ERROR)
{
char *emsg = _dbus_win_error_string (GetLastError ());
dbus_set_error (error, _dbus_win_error_from_last_error (),
"Failed to get file size for \"%s\": %s",
filename_c, emsg);
_dbus_win_free_error_string (emsg);
_dbus_verbose ("GetFileSize() failed: %s", emsg);
CloseHandle (hnd);
return FALSE;
}
if (fsize_hi != 0 || fsize > _DBUS_ONE_MEGABYTE)
{
dbus_set_error (error, DBUS_ERROR_FAILED,
"File size %lu/%lu of \"%s\" is too large.",
(unsigned long) fsize_hi,
(unsigned long) fsize, filename_c);
CloseHandle (hnd);
return FALSE;
}
total = 0;
orig_len = _dbus_string_get_length (str);
if (fsize > 0)
{
int bytes_read;
while (total < fsize)
{
bytes_read = _dbus_file_read (hnd, str, fsize - total, error);
if (bytes_read <= 0)
{
if (bytes_read == 0)
{
dbus_set_error (error, DBUS_ERROR_FAILED,
"Premature EOF reading \"%s\"",
filename_c);
}
else
_DBUS_ASSERT_ERROR_IS_SET (error);
CloseHandle (hnd);
_dbus_string_set_length (str, orig_len);
return FALSE;
}
else
total += bytes_read;
}
CloseHandle (hnd);
return TRUE;
}
else
{
CloseHandle (hnd);
return TRUE;
}
}
dbus_bool_t
_dbus_string_save_to_file (const DBusString *str,
const DBusString *filename,
dbus_bool_t world_readable,
DBusError *error)
{
HANDLE hnd;
int bytes_to_write;
const char *filename_c;
DBusString tmp_filename;
const char *tmp_filename_c;
int total;
const char *str_c;
dbus_bool_t need_unlink;
dbus_bool_t retval;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
hnd = INVALID_HANDLE_VALUE;
retval = FALSE;
need_unlink = FALSE;
if (!_dbus_string_init (&tmp_filename))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
return FALSE;
}
if (!_dbus_string_copy (filename, 0, &tmp_filename, 0))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
_dbus_string_free (&tmp_filename);
return FALSE;
}
if (!_dbus_string_append (&tmp_filename, "."))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
_dbus_string_free (&tmp_filename);
return FALSE;
}
#define N_TMP_FILENAME_RANDOM_BYTES 8
if (!_dbus_generate_random_ascii (&tmp_filename, N_TMP_FILENAME_RANDOM_BYTES,
error))
{
_dbus_string_free (&tmp_filename);
return FALSE;
}
filename_c = _dbus_string_get_const_data (filename);
tmp_filename_c = _dbus_string_get_const_data (&tmp_filename);
/* TODO - support world-readable in an atomic fashion */
hnd = CreateFileA (tmp_filename_c, GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,
INVALID_HANDLE_VALUE);
if (hnd == INVALID_HANDLE_VALUE)
{
char *emsg = _dbus_win_error_string (GetLastError ());
dbus_set_error (error, _dbus_win_error_from_last_error (),
"Could not create \"%s\": %s", filename_c, emsg);
_dbus_win_free_error_string (emsg);
goto out;
}
if (world_readable)
{
if (! _dbus_make_file_world_readable (&tmp_filename, error))
goto out;
}
_dbus_verbose ("tmp file %s hnd %p opened\n", tmp_filename_c, hnd);
need_unlink = TRUE;
total = 0;
bytes_to_write = _dbus_string_get_length (str);
str_c = _dbus_string_get_const_data (str);
while (total < bytes_to_write)
{
DWORD bytes_written;
BOOL res;
res = WriteFile (hnd, str_c + total, bytes_to_write - total,
&bytes_written, NULL);
if (res == 0 || bytes_written <= 0)
{
char *emsg = _dbus_win_error_string (GetLastError ());
dbus_set_error (error, _dbus_win_error_from_last_error (),
"Could not write to %s: %s", tmp_filename_c, emsg);
_dbus_win_free_error_string (emsg);
goto out;
}
total += bytes_written;
}
if (CloseHandle (hnd) == 0)
{
char *emsg = _dbus_win_error_string (GetLastError ());
dbus_set_error (error, _dbus_win_error_from_last_error (),
"Could not close file %s: %s", tmp_filename_c, emsg);
_dbus_win_free_error_string (emsg);
goto out;
}
hnd = INVALID_HANDLE_VALUE;
/* Unlike rename(), MoveFileEx() can replace existing files */
if (!MoveFileExA (tmp_filename_c, filename_c, MOVEFILE_REPLACE_EXISTING))
{
char *emsg = _dbus_win_error_string (GetLastError ());
dbus_set_error (error, _dbus_win_error_from_last_error (),
"Could not rename %s to %s: %s",
tmp_filename_c, filename_c, emsg);
_dbus_win_free_error_string (emsg);
goto out;
}
need_unlink = FALSE;
retval = TRUE;
out:
/* close first, then unlink */
if (hnd != INVALID_HANDLE_VALUE)
CloseHandle (hnd);
if (need_unlink && DeleteFileA (tmp_filename_c) == 0)
{
char *emsg = _dbus_win_error_string (GetLastError ());
_dbus_verbose ("Failed to unlink temp file %s: %s", tmp_filename_c,
emsg);
_dbus_win_free_error_string (emsg);
}
_dbus_string_free (&tmp_filename);
if (!retval)
_DBUS_ASSERT_ERROR_IS_SET (error);
return retval;
}
dbus_bool_t
_dbus_create_file_exclusively (const DBusString *filename,
DBusError *error)
{
HANDLE hnd;
const char *filename_c;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
filename_c = _dbus_string_get_const_data (filename);
hnd = CreateFileA (filename_c, GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL,
INVALID_HANDLE_VALUE);
if (hnd == INVALID_HANDLE_VALUE)
{
char *emsg = _dbus_win_error_string (GetLastError ());
dbus_set_error (error, _dbus_win_error_from_last_error (),
"Could not create file %s: %s",
filename_c, emsg);
_dbus_win_free_error_string (emsg);
return FALSE;
}
_dbus_verbose ("exclusive file %s hnd %p opened\n", filename_c, hnd);
if (CloseHandle (hnd) == 0)
{
char *emsg = _dbus_win_error_string (GetLastError ());
dbus_set_error (error, _dbus_win_error_from_last_error (),
"Could not close file %s: %s",
filename_c, emsg);
_dbus_win_free_error_string (emsg);
return FALSE;
}
return TRUE;
}
dbus_set_error
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_create_file_exclusively
dbus_bool_t _dbus_create_file_exclusively(const DBusString *filename, DBusError *error)
Creates the given file, failing if the file already exists.
Definition dbus-file-win.c:370
_dbus_string_save_to_file
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.
Definition dbus-file-win.c:214
_dbus_make_file_world_readable
dbus_bool_t _dbus_make_file_world_readable(const DBusString *filename, DBusError *error)
Makes the file readable by every user in the system.
Definition dbus-file-unix.c:368
_dbus_file_get_contents
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.
Definition dbus-file-win.c:109
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition dbus-internals.h:153
_dbus_generate_random_ascii
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:525
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:51
TRUE
#define TRUE
Expands to "1".
Definition dbus-macros.h:41
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:44
_dbus_verbose_bytes_of_string
DBUS_PRIVATE_EXPORT void _dbus_verbose_bytes_of_string(const DBusString *str, int start, int len)
Dump the given part of the string to verbose log.
Definition dbus-marshal-basic.c:1428
DBUS_ERROR_FAILED
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
Definition dbus-protocol.h:361
DBUS_ERROR_NO_MEMORY
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
Definition dbus-protocol.h:363
_dbus_string_set_length
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition dbus-string.c:847
_dbus_string_append
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_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition dbus-string.c:182
_dbus_string_copy
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
_dbus_string_get_data_len
char * _dbus_string_get_data_len(DBusString *str, int start, int len)
Gets a sub-portion of the raw character buffer from the string.
Definition dbus-string.c:535
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as _DBUS_STRING_IN...
Definition dbus-string.c:278
_dbus_string_lengthen
dbus_bool_t _dbus_string_lengthen(DBusString *str, int additional_length)
Makes a string longer by the given number of bytes.
Definition dbus-string.c:805
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition dbus-types.h:37
DBusError
Object representing an exception.
Definition dbus-errors.h:51
DBusString
Definition dbus-string.h:47