diff options
Diffstat (limited to 'dbus')
-rw-r--r-- | dbus/Makefile.am | 2 | ||||
-rw-r--r-- | dbus/dbus-errors.c | 2 | ||||
-rw-r--r-- | dbus/dbus-errors.h | 1 | ||||
-rw-r--r-- | dbus/dbus-internals.c | 13 | ||||
-rw-r--r-- | dbus/dbus-message-builder.c | 73 | ||||
-rw-r--r-- | dbus/dbus-message-builder.h | 39 | ||||
-rw-r--r-- | dbus/dbus-sysdeps.c | 98 | ||||
-rw-r--r-- | dbus/dbus-sysdeps.h | 3 |
8 files changed, 229 insertions, 2 deletions
diff --git a/dbus/Makefile.am b/dbus/Makefile.am index 5254010b..96169a5a 100644 --- a/dbus/Makefile.am +++ b/dbus/Makefile.am @@ -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 \ diff --git a/dbus/dbus-errors.c b/dbus/dbus-errors.c index 024ddd5a..c70c1bc4 100644 --- a/dbus/dbus-errors.c +++ b/dbus/dbus-errors.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 */ } diff --git a/dbus/dbus-errors.h b/dbus/dbus-errors.h index b1013a60..29f847de 100644 --- a/dbus/dbus-errors.h +++ b/dbus/dbus-errors.h @@ -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, diff --git a/dbus/dbus-internals.c b/dbus/dbus-internals.c index 8413ac00..eed91d14 100644 --- a/dbus/dbus-internals.c +++ b/dbus/dbus-internals.c @@ -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; diff --git a/dbus/dbus-message-builder.c b/dbus/dbus-message-builder.c new file mode 100644 index 00000000..e91fa391 --- /dev/null +++ b/dbus/dbus-message-builder.c @@ -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 */ diff --git a/dbus/dbus-message-builder.h b/dbus/dbus-message-builder.h new file mode 100644 index 00000000..098b0796 --- /dev/null +++ b/dbus/dbus-message-builder.h @@ -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 */ diff --git a/dbus/dbus-sysdeps.c b/dbus/dbus-sysdeps.c index 45405cc2..d8b202c7 100644 --- a/dbus/dbus-sysdeps.c +++ b/dbus/dbus-sysdeps.c @@ -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 */ diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h index 5b9a0c09..8ee7c8b7 100644 --- a/dbus/dbus-sysdeps.h +++ b/dbus/dbus-sysdeps.h @@ -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 */ |