2003-01-26 Havoc Pennington <hp@pobox.com>
authorHavoc Pennington <hp@redhat.com>
Sun, 26 Jan 2003 16:11:11 +0000 (16:11 +0000)
committerHavoc Pennington <hp@redhat.com>
Sun, 26 Jan 2003 16:11:11 +0000 (16:11 +0000)
* 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.

ChangeLog
dbus/Makefile.am
dbus/dbus-errors.c
dbus/dbus-errors.h
dbus/dbus-internals.c
dbus/dbus-message-builder.c [new file with mode: 0644]
dbus/dbus-message-builder.h [new file with mode: 0644]
dbus/dbus-sysdeps.c
dbus/dbus-sysdeps.h

index 6f8ea33..bf0f9ed 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 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
        the sender by setting to NULL
 
index 5254010..96169a5 100644 (file)
@@ -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                          \
index 024ddd5..c70c1bc 100644 (file)
@@ -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 */
     }
index b1013a6..29f847d 100644 (file)
@@ -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,
index 8413ac0..eed91d1 100644 (file)
@@ -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 (file)
index 0000000..e91fa39
--- /dev/null
@@ -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 (file)
index 0000000..098b079
--- /dev/null
@@ -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 */
index 45405cc..d8b202c 100644 (file)
 
 #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
 #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 */
index 5b9a0c0..8ee7c8b 100644 (file)
@@ -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 */