#include "portable_io.h"
DEFINE_ERRLIST;
+int osl_errno;
/** Command line and config file options. */
static struct gengetopt_args_info conf;
dir_objects[DT_BYTES].size = sizeof(*dir_size);
dir_objects[DT_FILES].data = dir_files;
dir_objects[DT_FILES].size = sizeof(*dir_files);
-
- return osl_add_row(dir_table, dir_objects);
+ return osl(osl_add_row(dir_table, dir_objects));
}
static uint32_t num_uids;
INFO_LOG(".............................uid #%u: %u\n",
(unsigned)num_uids, (unsigned)ui->uid);
if (create) {
- ret = osl_create_table(ui->desc);
+ ret = osl(osl_create_table(ui->desc));
if (ret < 0)
goto err;
num_uids++;
}
- ret = osl_open_table(ui->desc, &ui->table);
+ ret = osl(osl_open_table(ui->desc, &ui->table));
if (ret < 0)
goto err;
return 1;
int ret;
dir_table_desc.dir = para_strdup(conf.database_dir_arg);
- ret = osl_create_table(&dir_table_desc);
+ ret = osl(osl_create_table(&dir_table_desc));
if (ret < 0)
return ret;
create_hash_table();
struct osl_row *row;
struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
- int ret = osl_get_row(t, UT_DIR_NUM, &obj, &row);
+ int ret = osl(osl_get_row(t, UT_DIR_NUM, &obj, &row));
- if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
+ if (ret == -E_OSL && osl_errno != E_OSL_RB_KEY_NOT_FOUND)
return ret;
if (ret < 0) { /* this is the first file we add */
struct osl_object objects[NUM_UT_COLUMNS];
objects[UT_FILES].data = &num_files;
objects[UT_FILES].size = sizeof(num_files);
INFO_LOG("######################### ret: %d\n", ret);
- ret = osl_add_row(t, objects);
+ ret = osl(osl_add_row(t, objects));
INFO_LOG("######################### ret: %d\n", ret);
return ret;
} else { /* add size and increment file count */
uint64_t num;
struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
- ret = osl_get_object(t, row, UT_BYTES, &obj1);
+ ret = osl(osl_get_object(t, row, UT_BYTES, &obj1));
if (ret < 0)
return ret;
num = *(uint64_t *)obj1.data + *add;
- ret = osl_update_object(t, row, UT_BYTES, &obj2);
+ ret = osl(osl_update_object(t, row, UT_BYTES, &obj2));
if (ret < 0)
return ret;
- ret = osl_get_object(t, row, UT_FILES, &obj1);
+ ret = osl(osl_get_object(t, row, UT_FILES, &obj1));
if (ret < 0)
return ret;
num = *(uint64_t *)obj1.data + 1;
- return osl_update_object(t, row, UT_FILES, &obj2);
+ return osl(osl_update_object(t, row, UT_FILES, &obj2));
}
}
int ret;
again:
- ret = osl_get_row(dir_table, DT_NUM, &obj, &row);
+ ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
if (ret < 0)
goto out;
- ret = osl_get_object(dir_table, row, DT_NAME, &obj);
+ ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
if (ret < 0)
goto out;
if (result) {
result = tmp;
} else
result = para_strdup((char *)obj.data);
- ret = osl_get_object(dir_table, row, DT_PARENT_NUM, &obj);
+ ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
if (ret < 0)
goto out;
val = *(uint64_t *)obj.data;
char *this_dir, *prefix = NULL;
*name = NULL;
- ret = osl_get_object(dir_table, dir_table_row, DT_NAME, &obj);
+ ret = osl(osl_get_object(dir_table, dir_table_row, DT_NAME, &obj));
if (ret < 0)
return ret;
this_dir = para_strdup((char *)obj.data);
- ret = osl_get_object(dir_table, dir_table_row, DT_PARENT_NUM, &obj);
+ ret = osl(osl_get_object(dir_table, dir_table_row, DT_PARENT_NUM, &obj));
if (ret < 0)
goto out;
if (!*(uint64_t *)obj.data) {
}
if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
uint64_t files;
- ret = osl_get_object(dir_table, row, DT_FILES, &obj);
+ ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
if (ret < 0)
return ret;
files = *(uint64_t *)obj.data;
}
if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
uint64_t bytes;
- ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
+ ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
if (ret < 0)
return ret;
bytes = *(uint64_t *)obj.data;
return -E_LOOP_COMPLETE;
if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
char *dirname;
- ret = osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj);
+ ret = osl(osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj));
if (ret < 0)
return ret;
ret = get_dir_name_by_number((uint64_t *)obj.data, &dirname);
}
if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
uint64_t files;
- ret = osl_get_object(usi->ui->table, row, UT_FILES, &obj);
+ ret = osl(osl_get_object(usi->ui->table, row, UT_FILES, &obj));
if (ret < 0)
return ret;
files = *(uint64_t *)obj.data;
}
if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
uint64_t bytes;
- ret = osl_get_object(usi->ui->table, row, UT_BYTES, &obj);
+ ret = osl(osl_get_object(usi->ui->table, row, UT_BYTES, &obj));
if (ret < 0)
return ret;
bytes = *(uint64_t *)obj.data;
printf("************************************************ uid %u\n",
(unsigned) ui->uid);
printf("----------------- Largest dirs -------------------\n");
- osl_rbtree_loop_reverse(ui->table, UT_BYTES, &usi,
- user_stats_loop_function);
+ osl(osl_rbtree_loop_reverse(ui->table, UT_BYTES, &usi,
+ user_stats_loop_function));
printf("---------- dirs containing most files ------------\n");
usi.count = conf.limit_arg,
usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
- osl_rbtree_loop_reverse(ui->table, UT_FILES, &usi,
- user_stats_loop_function);
+ osl(osl_rbtree_loop_reverse(ui->table, UT_FILES, &usi,
+ user_stats_loop_function));
}
}
};
printf("----------------- Largest dirs -------------------\n");
- ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &gsi,
- global_stats_loop_function);
+ ret = osl(osl_rbtree_loop_reverse(dir_table, DT_BYTES, &gsi,
+ global_stats_loop_function));
if (ret < 0 && ret != -E_LOOP_COMPLETE)
return ret;
gsi.count = conf.limit_arg;
gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
printf("---------- dirs containing most files ------------\n");
- ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &gsi,
- global_stats_loop_function);
+ ret = osl(osl_rbtree_loop_reverse(dir_table, DT_FILES, &gsi,
+ global_stats_loop_function));
if (ret < 0 && ret != -E_LOOP_COMPLETE)
return ret;
{
if (!dir_table_desc.dir) /* we did not create the table */
dir_table_desc.dir = para_strdup(conf.database_dir_arg);
- return osl_open_table(&dir_table_desc, &dir_table);
+ return osl(osl_open_table(&dir_table_desc, &dir_table));
}
static void close_dir_table(void)
if (!dir_table)
return;
- ret = osl_close_table(dir_table, OSL_MARK_CLEAN);
+ ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
if (ret < 0)
- ERROR_LOG("failed to close dir table: %s\n", error_txt(-ret));
+ ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
free((char *)dir_table_desc.dir);
dir_table = NULL;
}
if (!ui || !ui_used(ui) || !ui_admissible(ui))
return;
- ret = osl_close_table(ui->table, OSL_MARK_CLEAN);
+ ret = osl(osl_close_table(ui->table, OSL_MARK_CLEAN));
if (ret < 0)
ERROR_LOG("failed to close user table %u: %s\n",
- (unsigned) ui->uid, error_txt(-ret));
+ (unsigned) ui->uid, adu_strerror(-ret));
free((char *)ui->desc->name);
ui->desc->name = NULL;
free((char *)ui->desc->dir);
out:
free(admissible_uids);
if (ret < 0) {
- ERROR_LOG("%s\n", error_txt(-ret));
+ ERROR_LOG("%s\n", adu_strerror(-ret));
return -EXIT_FAILURE;
}
return EXIT_SUCCESS;
-extern char *__errlist[];
-extern char *__error_txt;
-
-//__printf_2_3 void __log(int ll, const char* fmt,...);
-
/**
- * This bit indicates whether a number is considered a system error number
+ * This bit indicates whether a number is considered a system error code.
* If yes, the system errno is just the result of clearing this bit from
* the given number.
*/
/** Set the system error bit for the given number. */
#define ERRNO_TO_ERROR(num) ((num) | (1 << SYSTEM_ERROR_BIT))
-/** Check whether a given number is a system error number.
- *
- * \param num The value to be checked.
- * \param _errno The system error number.
- *
- * \return True if \a num is the representation of the system
- * error identified by \a _errno.
- */
-static inline int is_errno(int num, int _errno)
-{
- assert(num > 0 && _errno > 0);
- return ERRNO_TO_ERROR(_errno) == num;
-}
-
-/**
- * version of strerror(3).
- *
- * \param num The error number.
- *
- * \return The error text of \a num.
- */
-static inline char *error_txt(int num)
-{
- assert(num > 0);
- if (IS_SYSTEM_ERROR(num))
- return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
- else
- return __errlist[num];
-}
-
#define ALL_ERRORS \
_ERROR(SUCCESS, "success") \
- _ERROR(BAD_DB_DIR, "invalid database directory") \
- _ERROR(NO_COLUMN_DESC, "missing column description") \
- _ERROR(BAD_NAME, "invalid name for a column/table") \
- _ERROR(BAD_STORAGE_TYPE, "invalid storage type") \
- _ERROR(BAD_STORAGE_FLAGS, "invalid storage flags") \
- _ERROR(NO_COLUMN_NAME, "missing column name") \
- _ERROR(NO_COLUMNS, "at least one column required") \
- _ERROR(BAD_COLUMN_NAME, "invalid name for a table column") \
- _ERROR(NO_UNIQUE_RBTREE_COLUMN, "need at least one mapped column with OSL_UNIQE and OSL_RBTREE OSL") \
- _ERROR(NO_RBTREE_COL, "at least one column needs an rbtree") \
- _ERROR(DUPLICATE_COL_NAME, "column name given twice") \
- _ERROR(BAD_STORAGE_SIZE, "invalid storage size") \
- _ERROR(NO_COMPARE_FUNC, "missing compare function") \
- _ERROR(BAD_DATA_SIZE, "wrong data size for fixed-size column") \
- _ERROR(NOT_MAPPED, "file not mapped") \
- _ERROR(ALREADY_MAPPED, "file already mapped") \
- _ERROR(BAD_SIZE, "invalid size specified") \
- _ERROR(TRUNC, "failed to truncate file") \
- _ERROR(BAD_TABLE, "table not open") \
- _ERROR(BAD_TABLE_DESC, "invalid table description") \
- _ERROR(RB_KEY_EXISTS, "key already exists in rbtree") \
- _ERROR(RB_KEY_NOT_FOUND, "key not found in rbtree") \
- _ERROR(BAD_ROW_NUM, "invalid row number") \
- _ERROR(INDEX_CORRUPTION, "index corruption detected") \
- _ERROR(INVALID_OBJECT, "reference to invalid object") \
- _ERROR(STAT, "can not stat file") \
- _ERROR(WRITE, "write error") \
- _ERROR(LSEEK, "lseek error") \
- _ERROR(BUSY, "table is busy") \
- _ERROR(SHORT_TABLE, "table too short") \
- _ERROR(NO_MAGIC, "missing table header magic") \
- _ERROR(VERSION_MISMATCH, "table version not supported") \
- _ERROR(BAD_COLUMN_NUM, "invalid column number") \
- _ERROR(BAD_TABLE_FLAGS, "invalid flags in table description") \
- _ERROR(BAD_ROW, "invalid row") \
+ _ERROR(SYNTAX, "syntax error") \
+ _ERROR(LOOP_COMPLETE, "loop complete") \
+ _ERROR(HASH_TABLE_OVERFLOW, "hash table too small") \
+ _ERROR(BAD_UID, "uid not found in hash table") \
_ERROR(ATOI_OVERFLOW, "value too large") \
_ERROR(STRTOLL, "unknown strtoll error") \
_ERROR(ATOI_NO_DIGITS, "no digits found in string") \
_ERROR(ATOI_JUNK_AT_END, "further characters after number") \
- _ERROR(FGETS, "fgets error") \
_ERROR(EMPTY, "file empty") \
_ERROR(MMAP, "mmap error") \
- _ERROR(SYNTAX, "syntax error") \
- _ERROR(LOOP_COMPLETE, "loop complete") \
- _ERROR(HASH_TABLE_OVERFLOW, "hash table too small") \
- _ERROR(BAD_UID, "uid not found in hash table")
+ _ERROR(OSL, "osl error") \
/**
};
#undef _ERROR
#define _ERROR(err, msg) msg,
-#define DEFINE_ERRLIST char *__errlist[] = {ALL_ERRORS}
+#define DEFINE_ERRLIST char *adu_errlist[] = {ALL_ERRORS}
+
+extern int osl_errno;
+extern char *adu_errlist[];
+
+
+/**
+ * adu's version of strerror(3).
+ *
+ * \param num The error number.
+ *
+ * \return The error text of \a num.
+ */
+static inline const char *adu_strerror(int num)
+{
+ assert(num > 0);
+ if (num == E_OSL) {
+ fprintf(stderr, "osl error %d\n", osl_errno & 0xff);
+ assert(osl_errno > 0);
+ return osl_strerror((osl_errno));
+ }
+ if (IS_SYSTEM_ERROR(num))
+ return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
+ return adu_errlist[num];
+}
+
+/**
+ * Wrapper for osl library calls.
+ *
+ * This should be used for all calls to osl functions that return an osl error
+ * code. It changes the return value to \p -E_OSL appropriately so that it can
+ * be used for printing the correct error message.
+ *
+ * \return \a ret if \a ret >= 0, \p -E_OSL otherwise.
+ */
+static inline int osl(int ret)
+{
+ if (ret >= 0)
+ return ret;
+ osl_errno = -ret;
+ return -E_OSL;
+}