mirror of
https://gitlab.freedesktop.org/dbus/dbus.git
synced 2025-12-29 06:40:10 +01:00
2003-01-26 Havoc Pennington <hp@pobox.com>
* dbus/dbus-sysdeps.c (_dbus_file_get_contents): new function * dbus/dbus-errors.c (dbus_result_to_string): add file errors * dbus/dbus-message-builder.c: new file, will contain code to load up messages from files. Not implemented yet.
This commit is contained in:
parent
8ca39d9925
commit
9a0b07f225
9 changed files with 239 additions and 2 deletions
10
ChangeLog
10
ChangeLog
|
|
@ -1,3 +1,13 @@
|
|||
2003-01-26 Havoc Pennington <hp@pobox.com>
|
||||
|
||||
* dbus/dbus-sysdeps.c (_dbus_file_get_contents): new function
|
||||
|
||||
* dbus/dbus-errors.c (dbus_result_to_string): add
|
||||
file errors
|
||||
|
||||
* dbus/dbus-message-builder.c: new file, will contain code to load
|
||||
up messages from files. Not implemented yet.
|
||||
|
||||
2003-01-26 Havoc Pennington <hp@pobox.com>
|
||||
|
||||
* dbus/dbus-message.c (dbus_message_set_sender): support deleting
|
||||
|
|
|
|||
|
|
@ -67,6 +67,8 @@ libdbus_convenience_la_SOURCES= \
|
|||
dbus-marshal.h \
|
||||
dbus-mempool.c \
|
||||
dbus-mempool.h \
|
||||
dbus-message-builder.c \
|
||||
dbus-message-builder.h \
|
||||
dbus-string.c \
|
||||
dbus-string.h \
|
||||
dbus-sysdeps.c \
|
||||
|
|
|
|||
|
|
@ -103,6 +103,8 @@ dbus_result_to_string (DBusResultCode code)
|
|||
return "Invalid fields.";
|
||||
case DBUS_RESULT_NO_REPLY:
|
||||
return "Did not get a reply message.";
|
||||
case DBUS_RESULT_FILE_NOT_FOUND:
|
||||
return "File doesn't exist.";
|
||||
|
||||
/* no default, it would break our compiler warnings */
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,6 +52,7 @@ typedef enum
|
|||
DBUS_RESULT_DISCONNECTED, /**< No more connection. */
|
||||
DBUS_RESULT_INVALID_FIELDS, /**< One or more invalid fields encountered. */
|
||||
DBUS_RESULT_NO_REPLY, /**< Did not get a reply message. */
|
||||
DBUS_RESULT_FILE_NOT_FOUND /**< File doesn't exist */
|
||||
} DBusResultCode;
|
||||
|
||||
void dbus_set_result (DBusResultCode *code_address,
|
||||
|
|
|
|||
|
|
@ -196,6 +196,9 @@ _dbus_strerror (int error_number)
|
|||
/**
|
||||
* Converts a UNIX errno into a DBusResultCode.
|
||||
*
|
||||
* @todo should cover more errnos, specifically those
|
||||
* from open().
|
||||
*
|
||||
* @param error_number the errno.
|
||||
* @returns the result code.
|
||||
*/
|
||||
|
|
@ -274,7 +277,15 @@ _dbus_result_from_errno (int error_number)
|
|||
#ifdef EADDRINUSE
|
||||
case EADDRINUSE:
|
||||
return DBUS_RESULT_ADDRESS_IN_USE;
|
||||
#endif
|
||||
#endif
|
||||
#ifdef EEXIST
|
||||
case EEXIST:
|
||||
return DBUS_RESULT_FILE_NOT_FOUND;
|
||||
#endif
|
||||
#ifdef ENOENT
|
||||
case ENOENT:
|
||||
return DBUS_RESULT_FILE_NOT_FOUND;
|
||||
#endif
|
||||
}
|
||||
|
||||
return DBUS_RESULT_FAILED;
|
||||
|
|
|
|||
73
dbus/dbus-message-builder.c
Normal file
73
dbus/dbus-message-builder.c
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
/* -*- mode: C; c-file-style: "gnu" -*- */
|
||||
/* dbus-message-builder.c Build messages from text files for testing (internal to D-BUS implementation)
|
||||
*
|
||||
* Copyright (C) 2003 Red Hat, Inc.
|
||||
*
|
||||
* Licensed under the Academic Free License version 1.2
|
||||
*
|
||||
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
#ifdef DBUS_BUILD_TESTS
|
||||
#include "dbus-message-builder.h"
|
||||
|
||||
/**
|
||||
* @defgroup DBusMessageBuilder code for loading test message data
|
||||
* @ingroup DBusInternals
|
||||
* @brief code for loading up test data for unit tests
|
||||
*
|
||||
* The code in here is used for unit testing, it loads
|
||||
* up message data from a description in a file.
|
||||
*
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Reads the given filename, which should be in "message description
|
||||
* language" (look at some examples), and builds up the message data
|
||||
* from it. The message data may be invalid, or valid.
|
||||
*
|
||||
* The file format is:
|
||||
* @code
|
||||
* ALIGN <N> aligns to the given value
|
||||
* UNALIGN skips alignment for the next marshal
|
||||
* BYTE <N> inserts the given integer in [0,255]
|
||||
* SAVE_LENGTH <name> records the current length under the given name
|
||||
* LENGTH <name> inserts the saved length of the same name
|
||||
* @endcode
|
||||
*
|
||||
* Following commands insert aligned data unless
|
||||
* preceded by "UNALIGN":
|
||||
* @code
|
||||
* INT32 <N> marshals an INT32
|
||||
* UINT32 <N> marshals a UINT32
|
||||
* DOUBLE <N> marshals a double
|
||||
* STRING "Foo" marshals a string
|
||||
* @endcode
|
||||
*
|
||||
* @param dest the string to append the message data to
|
||||
* @param filename the filename to load
|
||||
* @returns #TRUE on success
|
||||
*/
|
||||
dbus_bool_t
|
||||
_dbus_message_data_load (DBusString *dest,
|
||||
const DBusString *filename)
|
||||
{
|
||||
/* FIXME implement */
|
||||
|
||||
}
|
||||
|
||||
/** @} */
|
||||
#endif /* DBUS_BUILD_TESTS */
|
||||
39
dbus/dbus-message-builder.h
Normal file
39
dbus/dbus-message-builder.h
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/* -*- mode: C; c-file-style: "gnu" -*- */
|
||||
/* dbus-message-builder.h Build messages from text files for testing (internal to D-BUS implementation)
|
||||
*
|
||||
* Copyright (C) 2003 Red Hat, Inc.
|
||||
*
|
||||
* Licensed under the Academic Free License version 1.2
|
||||
*
|
||||
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef DBUS_MESSAGE_BUILDER_H
|
||||
#define DBUS_MESSAGE_BUILDER_H
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <dbus/dbus-memory.h>
|
||||
#include <dbus/dbus-types.h>
|
||||
|
||||
DBUS_BEGIN_DECLS;
|
||||
|
||||
dbus_bool_t _dbus_message_data_load (DBusString *dest,
|
||||
const DBusString *filename);
|
||||
|
||||
DBUS_END_DECLS;
|
||||
|
||||
#endif /* DBUS_MESSAGE_BUILDER_H */
|
||||
|
|
@ -23,12 +23,12 @@
|
|||
|
||||
#include "dbus-internals.h"
|
||||
#include "dbus-sysdeps.h"
|
||||
#include <sys/types.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/socket.h>
|
||||
|
|
@ -36,6 +36,7 @@
|
|||
#include <pwd.h>
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/stat.h>
|
||||
#ifdef HAVE_WRITEV
|
||||
#include <sys/uio.h>
|
||||
#endif
|
||||
|
|
@ -43,6 +44,10 @@
|
|||
#include <sys/poll.h>
|
||||
#endif
|
||||
|
||||
#ifndef O_BINARY
|
||||
#define O_BINARY 0
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @addtogroup DBusInternalsUtils
|
||||
* @{
|
||||
|
|
@ -1004,4 +1009,95 @@ _dbus_get_current_time (long *tv_sec,
|
|||
*tv_usec = t.tv_usec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the contents of the given file to the string,
|
||||
* returning result code. At the moment, won't open a file
|
||||
* more than a megabyte in size.
|
||||
*
|
||||
* @param str the string to append to
|
||||
* @param filename filename to load
|
||||
* @returns result
|
||||
*/
|
||||
DBusResultCode
|
||||
_dbus_file_get_contents (DBusString *str,
|
||||
const DBusString *filename)
|
||||
{
|
||||
int fd;
|
||||
struct stat sb;
|
||||
int orig_len;
|
||||
int total;
|
||||
const char *filename_c;
|
||||
|
||||
_dbus_string_get_const_data (filename, &filename_c);
|
||||
|
||||
/* O_BINARY useful on Cygwin */
|
||||
fd = open (filename_c, O_RDONLY | O_BINARY);
|
||||
if (fd < 0)
|
||||
return _dbus_result_from_errno (errno);
|
||||
|
||||
if (fstat (fd, &sb) < 0)
|
||||
{
|
||||
DBusResultCode result;
|
||||
|
||||
result = _dbus_result_from_errno (errno); /* prior to close() */
|
||||
|
||||
_dbus_verbose ("fstat() failed: %s",
|
||||
_dbus_strerror (errno));
|
||||
|
||||
close (fd);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
if (sb.st_size > _DBUS_ONE_MEGABYTE)
|
||||
{
|
||||
_dbus_verbose ("File size %lu is too large.\n",
|
||||
(unsigned long) sb.st_size);
|
||||
close (fd);
|
||||
return DBUS_RESULT_FAILED;
|
||||
}
|
||||
|
||||
total = 0;
|
||||
orig_len = _dbus_string_get_length (str);
|
||||
if (sb.st_size > 0 && S_ISREG (sb.st_mode))
|
||||
{
|
||||
int bytes_read;
|
||||
|
||||
while (total < (int) sb.st_size)
|
||||
{
|
||||
bytes_read = _dbus_read (fd, str,
|
||||
sb.st_size - total);
|
||||
if (bytes_read <= 0)
|
||||
{
|
||||
DBusResultCode result;
|
||||
|
||||
result = _dbus_result_from_errno (errno); /* prior to close() */
|
||||
|
||||
_dbus_verbose ("read() failed: %s",
|
||||
_dbus_strerror (errno));
|
||||
|
||||
close (fd);
|
||||
_dbus_string_set_length (str, orig_len);
|
||||
return result;
|
||||
}
|
||||
else
|
||||
total += bytes_read;
|
||||
}
|
||||
|
||||
close (fd);
|
||||
return DBUS_RESULT_SUCCESS;
|
||||
}
|
||||
else if (sb.st_size != 0)
|
||||
{
|
||||
_dbus_verbose ("Can only open regular files at the moment.\n");
|
||||
close (fd);
|
||||
return DBUS_RESULT_FAILED;
|
||||
}
|
||||
else
|
||||
{
|
||||
close (fd);
|
||||
return DBUS_RESULT_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
/** @} end of sysdeps */
|
||||
|
|
|
|||
|
|
@ -121,6 +121,9 @@ void _dbus_sleep_milliseconds (int milliseconds);
|
|||
void _dbus_get_current_time (long *tv_sec,
|
||||
long *tv_usec);
|
||||
|
||||
DBusResultCode _dbus_file_get_contents (DBusString *str,
|
||||
const DBusString *filename);
|
||||
|
||||
DBUS_END_DECLS;
|
||||
|
||||
#endif /* DBUS_SYSDEPS_H */
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue