Transform the database_dir/database_root arg into an absolute path.
[adu.git] / adu.h
diff --git a/adu.h b/adu.h
index ee992acc8aff5b667f81a59d0b694552c8ab3f92..42b9471ee251e0ccf28ffd185628bfdd3d315634 100644 (file)
--- a/adu.h
+++ b/adu.h
@@ -1,14 +1,14 @@
 /*
- * Copyright (C) 1997-2008 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
-/** \file para.h global paraslash definitions */
+/** \file adu.h \brief Global definitions. */
 
 #include <sys/stat.h>
 #include <fcntl.h>
-#include <sys/wait.h>
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h> /* time(), localtime() */
 #include <errno.h>
 #include <limits.h>
 #include <stdarg.h>
-#include <ctype.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <sys/socket.h>
-#include <sys/un.h> /* needed by create_pf_socket */
+#include <inttypes.h>
 #include <string.h>
 #include <assert.h>
+#include <osl.h>
 #include "gcc-compat.h"
-
-/** used in various contexts */
-#define MAXLINE 255
-
-/** compute the minimum of \a a and \a b */
-#define MIN(a,b) ((a) < (b) ? (a) : (b))
-/** compute the maximum of \a a and \a b */
-#define MAX(a,b) ((a) > (b) ? (a) : (b))
-/** compute the absolute value of \a a */
-#define ABS(a) ((a) > 0 ? (a) : -(a))
+#include "portable_io.h"
 
 /** debug loglevel, gets really noisy */
 #define DEBUG 1
 #endif
 /** \endcond */
 
-/**
- * define a standard log function that always writes to stderr
- *
- * \param loglevel_barrier If the loglevel of the current message
- * is less than that, the message is going to be ignored.
- *
- */
-#define INIT_STDERR_LOGGING(loglevel_barrier) \
-       __printf_2_3 void __log(int ll, const char* fmt,...) \
-       { \
-               va_list argp; \
-               if (ll < loglevel_barrier) \
-                       return; \
-               va_start(argp, fmt); \
-               vfprintf(stderr, fmt, argp); \
-               va_end(argp); \
-       }
-
-/** version text used by various commands if -V switch was given */
-#define VERSION_TEXT(prefix) "para_" prefix " " PACKAGE_VERSION " (" CODENAME ")" "\n" \
-       "Copyright (C) 2008 Andre Noll\n" \
-       "This is free software with ABSOLUTELY NO WARRANTY." \
-       " See COPYING for details.\n" \
-       "Written by Andre Noll.\n" \
-       "Report bugs to <maan@systemlinux.org>.\n"
-
-/** print out \p VERSION_TEXT and exit if version flag was given */
-#define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
-       if (_args_info_struct.version_given) { \
-               printf("%s", VERSION_TEXT(_prefix)); \
-               exit(EXIT_SUCCESS); \
-       }
-/** sent by para_server for commands that expect a data file */
-#define AWAITING_DATA_MSG "\nAwaiting Data."
-/** sent by para_server if authentication was successful */
-#define PROCEED_MSG "\nProceed.\n"
-/** length of the \p PROCEED_MSG string */
-#define PROCEED_MSG_LEN strlen(PROCEED_MSG)
-/** sent by para_client to indicate the end of the command line */
-#define EOC_MSG "\nEnd of Command."
-/** sent by para_client, followed by the decrypted challenge number */
-#define CHALLENGE_RESPONSE_MSG "challenge_response:"
-
-/* exec */
-int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
-
-/* time */
-int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff);
-long unsigned tv2ms(const struct timeval*);
-void d2tv(double, struct timeval*);
-void tv_add(const struct timeval*, const struct timeval *, struct timeval *);
-void tv_scale(const unsigned long, const struct timeval *, struct timeval *);
-void tv_divide(const unsigned long divisor, const struct timeval *tv,
-       struct timeval *result);
-int tv_convex_combination(const long a, const struct timeval *tv1,
-               const long b, const struct timeval *tv2,
-               struct timeval *result);
-void ms2tv(const long unsigned n, struct timeval *tv);
-void compute_chunk_time(long unsigned chunk_num,
-               struct timeval *chunk_tv, struct timeval *stream_start,
-               struct timeval *result);
-
-__printf_2_3 void __log(int, const char*, ...);
-
 /**
  * Write a log message to a dynamically allocated string.
  *
@@ -172,7 +96,7 @@ __printf_2_3 void __log(int, const char*, ...);
 { \
        int n; \
        size_t size = 100; \
-       p = para_malloc(size); \
+       p = adu_malloc(size); \
        while (1) { \
                va_list ap; \
                /* Try to print in the allocated space. */ \
@@ -187,39 +111,97 @@ __printf_2_3 void __log(int, const char*, ...);
                        size = n + 1; /* precisely what is needed */ \
                else /* glibc 2.0 */ \
                        size *= 2; /* twice the old size */ \
-               p = para_realloc(p, size); \
+               p = adu_realloc(p, size); \
        } \
 }
 
+/** Evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y. */
+#define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
+
+/** The columns of the directory table. */
+enum dir_table_columns {
+       /** The name of the directory. */
+       DT_NAME,
+       /** The dir count number. */
+       DT_NUM,
+       /** The number of the parent directory. */
+       DT_PARENT_NUM,
+       /** The number of bytes of all regular files. */
+       DT_BYTES,
+       /** The number of all regular files. */
+       DT_FILES,
+       /** Number of columns in this table. */
+       NUM_DT_COLUMNS
+};
+
+extern struct osl_table *dir_table;
+
+/** The adu command line options. */
+extern struct gengetopt_args_info conf;
+
+/** Computed database dir. */
+extern char *database_dir;
+
 /**
- *  Return a random non-negative integer in an interval.
+ * The select command line options.
  *
- * \param max Determines maximal possible return value.
+ * Either given at the command line, or via the \a set command
+ * in interactive mode.
+ */
+extern struct select_args_info select_conf;
+
+/**
+ * Compare two osl objects pointing to unsigned integers of 64 bit size.
  *
- * \return An integer between zero and \p max - 1, inclusively.
+ * \param obj1 Pointer to the first integer.
+ * \param obj2 Pointer to the second integer.
+ *
+ * \return The values required for an osl compare function.
+ *
+ * \sa osl_compare_func, osl_hash_compare().
  */
-static inline long int para_random(unsigned max)
+_static_inline_ int uint64_compare(const struct osl_object *obj1,
+               const struct osl_object *obj2)
 {
-       return ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
+       uint64_t d1 = read_u64((const char *)obj1->data);
+       uint64_t d2 = read_u64((const char *)obj2->data);
+
+       if (d1 < d2)
+               return 1;
+       if (d1 > d2)
+               return -1;
+       return 0;
 }
 
-/** Round up x to a multiple of y */
-#define ROUND_UP(x, y) (((x) + ((y) - 1) / (y)) * (y))
-
-/** Get the size of an array */
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
-
 /**
- * Wrapper for isspace.
- * NetBSD needs this.
- */
-/*
- * The values should be cast to an unsigned char first, then to int.
- * Why? Because the isdigit (as do all other is/to functions/macros)
- * expect a number from 0 upto and including 255 as their (int) argument.
- * Because char is signed on most systems, casting it to int immediately
- * gives the functions an argument between -128 and 127 (inclusive),
- * which they will use as an array index, and which will thus fail
- * horribly for characters which have their most significant bit set.
+ * Compare the size of two directories
+ *
+ * \param obj1 Pointer to the first object.
+ * \param obj2 Pointer to the second object.
+ *
+ * This function first compares the size values as usual integers. If they compare as
+ * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
+ * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
  */
-#define para_isspace(c) isspace((int)(unsigned char)(c))
+_static_inline_ int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
+{
+       uint64_t d1 = *(uint64_t *)obj1->data;
+       uint64_t d2 = *(uint64_t *)obj2->data;
+       int ret = NUM_COMPARE(d2, d1);
+
+       if (ret)
+               return ret;
+       //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
+       return NUM_COMPARE(obj2->data, obj1->data);
+}
+
+/* adu.c */
+__printf_2_3 void __log(int, const char*, ...);
+int open_dir_table(int create);
+void check_signals(void);
+/* create.c */
+int com_create(void);
+
+/* interactive.c */
+void print_interactive_help(void);
+int com_interactive(void);