osl_update_object(): Honor OSL_DONT_FREE.
[osl.git] / fsck.c
diff --git a/fsck.c b/fsck.c
index 574509fcebdda24c52a8a1e45d0f4c94f6c6eef3..ce8f1dc704c6c335f356233284a85093b11ddcad 100644 (file)
--- a/fsck.c
+++ b/fsck.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2008 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2007-2009 Andre Noll <maan@tuebingen.mpg.de>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
 #include "log.h"
 #include "osl.h"
-#include "error.h"
-#include "fd.h"
+#include "util.h"
 #include "osl_core.h"
 #include "fsck.cmdline.h"
 
-/** version text used by various commands if -V switch was given */
-#define VERSION_TEXT(prefix) "osl_" prefix " " VERSION " " "\n" \
-       "Copyright (C) 2008 Andre Noll\n" \
+#define VERSION_TEXT(prefix) "osl" prefix " " VERSION " " "\n" \
+       "Copyright (C) 2008-2009 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"
+       "Report bugs to <maan@tuebingen.mpg.de>.\n"
 
 /** print out \p VERSION_TEXT and exit if version flag was given */
 #define HANDLE_VERSION_FLAG(_prefix, _args_info_struct) \
@@ -39,8 +37,12 @@ static struct fsck_args_info conf;
 #define FSCK_ERRORS \
        FSCK_ERROR(RANGE_VIOLATION, "range violation detected, very bad"), \
        FSCK_ERROR(NOT_A_REGULAR_FILE, "not a regular file"), \
-       FSCK_ERROR(SYNTAX, "fsck syntax error"),
+       FSCK_ERROR(SYNTAX, "fsck syntax error"), \
+       FSCK_ERROR(ACCESS, "permission denied"), \
+       FSCK_ERROR(CHDIR, "could not change directory"), \
+       FSCK_ERROR(OPENDIR, "could not open directory"),
 
+#define FSCK_ERROR_BIT 29
 #define FSCK_ERROR(num, txt) E_FSCK_ ## num
 enum {
        FSCK_DUMMY = (1 << FSCK_ERROR_BIT) - 1,
@@ -48,7 +50,7 @@ enum {
 };
 #undef FSCK_ERROR
 #define FSCK_ERROR(num, txt) txt
-const char const *fsck_errors[] = {
+static const char *fsck_errors[] = {
        FSCK_ERRORS
 };
 
@@ -62,7 +64,7 @@ static const char *fsck_strerror(int num)
 extern int loglevel;
 
 /* taken from git */
-signed char hexval_table[256] = {
+static signed char hexval_table[256] = {
         -1, -1, -1, -1, -1, -1, -1, -1,                /* 00-07 */
         -1, -1, -1, -1, -1, -1, -1, -1,                /* 08-0f */
         -1, -1, -1, -1, -1, -1, -1, -1,                /* 10-17 */
@@ -97,7 +99,7 @@ signed char hexval_table[256] = {
         -1, -1, -1, -1, -1, -1, -1, -1,                /* f8-ff */
 };
 
-int asc_to_hash(const char *asc_hash, int len, HASH_TYPE *hash)
+static int asc_to_hash(const char *asc_hash, int len, HASH_TYPE *hash)
 {
        int i = 0;
        const unsigned char *asc = (const unsigned char *) asc_hash;
@@ -118,8 +120,8 @@ static int _write_all(int fd, const char *buf, size_t len)
        return write_all(fd, buf, &len);
 }
 
-/**
- * Paraslash's version of malloc().
+/*
+ * Wrapper for malloc().
  *
  * \param size The desired new size.
  *
@@ -144,7 +146,7 @@ __must_check __malloc static void *fsck_malloc(size_t size)
 }
 
 /**
- * Paraslash's version of calloc().
+ * Allocate memory and fill with zeros.
  *
  * \param size The desired new size.
  *
@@ -164,7 +166,7 @@ __must_check __malloc static void *fsck_calloc(size_t size)
 }
 
 /**
- * Paraslash's version of strdup().
+ * Save version of strdup().
  *
  * \param s The string to be duplicated.
  *
@@ -208,6 +210,86 @@ static int uint32_compare(const struct osl_object *obj1, const struct osl_object
        return 0;
 }
 
+/**
+ * A wrapper for fchdir().
+ *
+ * \param fd An open file descriptor.
+ *
+ * \return Standard.
+ */
+static inline int __fchdir(int fd)
+{
+       if (fchdir(fd) >= 0)
+               return 1;
+       return errno == EACCES? -E_FSCK_ACCESS : -E_FSCK_CHDIR;
+}
+
+/**
+ * Wrapper for chdir(2).
+ *
+ * \param path The specified directory.
+ *
+ * \return Standard.
+ */
+_static_inline_ int __chdir(const char *path)
+{
+       if (chdir(path) >= 0)
+               return 1;
+       return errno == EACCES? -E_FSCK_ACCESS : -E_FSCK_CHDIR;
+}
+
+/**
+ * Save the cwd and open a given directory.
+ *
+ * \param dirname Path to the directory to open.
+ * \param dir Result pointer.
+ * \param cwd File descriptor of the current working directory.
+ *
+ * \return Standard.
+ *
+ * Opening the current directory (".") and calling fchdir() to return is
+ * usually faster and more reliable than saving cwd in some buffer and calling
+ * chdir() afterwards.
+ *
+ * If \a cwd is not \p NULL "." is opened and the resulting file descriptor is
+ * stored in \a cwd. If the function returns success, and \a cwd is not \p
+ * NULL, the caller must close this file descriptor (probably after calling
+ * fchdir(*cwd)).
+ *
+ * On errors, the function undos everything, so the caller needs neither close
+ * any files, nor change back to the original working directory.
+ *
+ * \sa getcwd(3).
+ *
+ */
+static int fsck_opendir(const char *dirname, DIR **dir, int *cwd)
+{
+       int ret;
+
+       if (cwd) {
+               ret = osl_open(".", O_RDONLY, 0);
+               if (ret < 0)
+                       return ret;
+               *cwd = ret;
+       }
+       ret = __chdir(dirname);
+       if (ret < 0)
+               goto close_cwd;
+       *dir = opendir(".");
+       if (*dir)
+               return 1;
+       ret = errno == EACCES? -E_FSCK_ACCESS : -E_FSCK_OPENDIR;
+/* Ignore return value of fchdir() and close(). We're busted anyway. */
+       if (cwd)
+               fchdir(*cwd);
+close_cwd:
+       if (cwd)
+               close(*cwd);
+       return ret;
+}
+
+
+
 /**
  * Traverse the given directory recursively.
  *
@@ -227,10 +309,10 @@ static int for_each_file_in_dir(const char *dirname,
 {
        DIR *dir;
        struct dirent *entry;
-       int cwd_fd, ret2, ret = para_opendir(dirname, &dir, &cwd_fd);
+       int cwd_fd, ret2, ret = fsck_opendir(dirname, &dir, &cwd_fd);
 
        if (ret < 0)
-               return ret == -ERRNO_TO_ERROR(EACCES)? 1 : ret;
+               return ret == -E_FSCK_ACCESS? 1 : ret;
        /* scan cwd recursively */
        while ((entry = readdir(dir))) {
                mode_t m;
@@ -263,7 +345,7 @@ static int for_each_file_in_dir(const char *dirname,
        ret = 1;
 out:
        closedir(dir);
-       ret2 = para_fchdir(cwd_fd);
+       ret2 = __fchdir(cwd_fd);
        if (ret2 < 0 && ret >= 0)
                ret = ret2;
        close(cwd_fd);
@@ -305,6 +387,13 @@ static int check_range(struct osl_table *t, uint32_t row_num, uint32_t col_num)
        return 1;
 }
 
+static int fsck_mark_row_invalid(struct osl_table *t, int i)
+{
+       if (conf.dry_run_given)
+               return 0;
+       return mark_row_invalid(t, i);
+}
+
 /*
  * check all cells of the given table for boundary violations
  */
@@ -316,25 +405,18 @@ static int check_index_ranges(struct osl_table *t)
        //DEBUG_LOG("%d rows. %d columns\n", t->num_rows, t->desc->num_columns);
        t->num_invalid_rows = 0;
        for (i = 0; i < t->num_rows; i++) {
+               const struct osl_column_description *cd;
+
                if (row_is_invalid(t, i)) {
                        t->num_invalid_rows++;
                        continue;
                }
-               for (j = 0; j < t->desc->num_columns; j++) { /* FXIME */
-                       const struct osl_column_description *cd =
-                               get_column_description(t->desc, j);
-                       if (cd->storage_type != OSL_MAPPED_STORAGE)
-                               continue;
+               FOR_EACH_MAPPED_COLUMN(j, t, cd) {
                        ret = check_range(t, i, j);
                        if (ret < 0) {
-                               if (ret != -E_OSL_INVALID_OBJECT &&
-                                               ret != -E_FSCK_RANGE_VIOLATION)
+                               if (ret != -E_FSCK_RANGE_VIOLATION)
                                        goto err;
-                               if (ret == -E_OSL_INVALID_OBJECT) {
-                                       CRIT_LOG("row %d, col %d maps to an "
-                                               "invalid object\n", i, j);
-                               }
-                               ret = mark_row_invalid(t, i);
+                               ret = fsck_mark_row_invalid(t, i);
                                if (ret < 0)
                                        goto err;
                                t->num_invalid_rows++;
@@ -389,8 +471,11 @@ static int prune_invalid_rows_from_index(struct osl_table *t)
                INFO_LOG("all rows are valid, good\n");
                return 1;
        }
-       NOTICE_LOG("deleting %u invalid row(s) (%d bytes) from index\n",
+       NOTICE_LOG("index contains %u invalid row(s) (%d bytes)\n",
                t->num_invalid_rows, t->row_index_size * t->num_invalid_rows);
+       if (conf.dry_run_given)
+               return 0;
+       NOTICE_LOG("removing invalid rows from index\n");
        bottom = t->num_rows - 1;
        while (top < bottom) {
                if (!row_is_invalid(t, top)) {
@@ -434,17 +519,14 @@ static int check_for_invalid_objects(struct osl_table *t, uint32_t **lost_bytes)
        /* first count used bytes */
        FOR_EACH_MAPPED_COLUMN(i, t, cd) {
                loss[i] = t->columns[i].data_map.size;
+               DEBUG_LOG("column %i data map: %zu bytes\n", i,
+                       t->columns[i].data_map.size);
                for (j = 0; j < t->num_rows; j++) {
                        struct osl_object obj;
                        ret = get_mapped_object(t, i, j, &obj);
-                       if (ret >= 0) {
-                               loss[i] -= obj.size + 1; /* add one for header byte */
-                               continue;
-                       }
-                       if (ret != -E_OSL_INVALID_OBJECT)
+                       if (ret < 0)
                                goto err;
-                       CRIT_LOG("row %d, col %d points to an invalid "
-                               "mapped object, bad\n", j, i);
+                       loss[i] -= obj.size;
                }
        }
        ret = 0;
@@ -480,10 +562,10 @@ static int prune_mapped_column(struct osl_table *t, uint32_t col_num, int fd)
                ret = get_mapped_object(t, col_num, i, &obj);
                if (ret < 0)
                        return ret;
-               ret = _write_all(fd, (char *)(obj.data) - 1, obj.size + 1);
+               ret = _write_all(fd, (char *)(obj.data), obj.size);
                if (ret < 0)
                        return ret;
-               written += obj.size + 1;
+               written += obj.size;
                ret = get_row_index(t, i, &index_entry);
                if (ret < 0)
                        return ret;
@@ -504,7 +586,8 @@ static int prune_objects(struct osl_table *t, uint32_t *lost_bytes)
 
        NOTICE_LOG("removing unreferenced objects from data files\n");
        /* first make a copy of the index */
-       ret = osl_open(old_idx_filename, O_WRONLY | O_CREAT | O_EXCL, 0644);
+       DEBUG_LOG("opening %s\n", old_idx_filename);
+       ret = osl_open(old_idx_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
        if (ret < 0)
                goto out_free;
        fd = ret;
@@ -598,11 +681,14 @@ static int check_disk_storage_column(struct osl_table *t, int row_num,
                struct osl_row *row;
                (*num_missing_objects)++;
                ERROR_LOG("row %d: object %s is missing\n", row_num, path);
+               ret = 0;
+               if (conf.dry_run_given)
+                       goto out;
                NOTICE_LOG("trying to delete row %d\n", row_num);
                ret = osl_get_row(t, dsnc, &obj, &row);
                if (ret < 0) {
                        CRIT_LOG("unable to get row %d\n", row_num);
-                       mark_row_invalid(t, row_num);
+                       fsck_mark_row_invalid(t, row_num);
                        CRIT_LOG("Please re-run fsck\n");
                        goto out;
                }
@@ -671,10 +757,10 @@ static int dummy_compare(const struct osl_object *obj1, const struct osl_object
 
 static unsigned files_pruned;
 
-int prune_disk_storage_file(const char *path, void *private_data)
+static int prune_disk_storage_file(const char *path, void *private_data)
 {
        HASH_TYPE hash[HASH_SIZE];
-       unsigned flags = *(unsigned *)private_data;
+       uint8_t flags = *(uint8_t *)private_data;
        struct osl_object obj = {.data = hash, .size = HASH_SIZE};
        struct osl_row *row;
        int ret = -1;
@@ -717,6 +803,8 @@ int prune_disk_storage_file(const char *path, void *private_data)
 invalid:
        ERROR_LOG("could not read hash value of %s\n", path);
 remove:
+       if (conf.dry_run_given)
+               return 0;
        NOTICE_LOG("removing %s\n", path);
        unlink(path);
        files_pruned++;
@@ -731,8 +819,9 @@ static int prune_disk_storage_files(struct osl_table *t)
        INFO_LOG("looking for unreferenced disk storage files\n");
        FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
                char *dirname = column_filename(t, i);
+               uint8_t flags = t->desc->flags;
                ret = for_each_file_in_dir(dirname, prune_disk_storage_file,
-                       (unsigned *)&t->desc->flags);
+                       &flags);
                free(dirname);
        }
        if (files_pruned)
@@ -810,8 +899,11 @@ static int fsck_init(struct osl_table_description *desc, struct osl_table **t)
                ret = map_table(*t, (MAP_TBL_FL_IGNORE_DIRTY));
        else
                ret = map_table(*t, 0);
-       if (ret >= 0)
+       if (ret >= 0) {
                (*t)->num_rows = table_num_rows(*t);
+               DEBUG_LOG("index header size: %d\n", (*t)->index_header_size);
+               DEBUG_LOG("row index size: %d\n", (*t)->row_index_size);
+       }
 out:
        return ret;
 }
@@ -837,7 +929,7 @@ static void fsck_cleanup(struct osl_table *t)
 
 #define ST_CASE(st) case st: return #st
 
-const char *get_asc_storage_type(enum osl_storage_type st)
+static const char *get_asc_storage_type(enum osl_storage_type st)
 {
        switch (st) {
                ST_CASE(OSL_MAPPED_STORAGE);
@@ -852,7 +944,7 @@ const char *get_asc_storage_type(enum osl_storage_type st)
        else str = fsck_strdup(#flag); }} while (0)
 
 
-char *get_asc_storage_flags(enum osl_storage_type sf)
+static char *get_asc_storage_flags(enum osl_storage_type sf)
 {
        char *asc_sf = NULL;
 
@@ -988,12 +1080,12 @@ static int dump_rows(char *dump_dir, struct osl_table *t)
                        current_dir = make_message("%s/rows_%u-%u", dump_dir, i, i + 999);
                        NOTICE_LOG("dumping rows %u - %u\n", i, i + 999);
                        ret = osl_mkdir(current_dir, 0777);
-                       if (ret < 0 && !is_errno(-ret, EEXIST))
+                       if (ret < 0 && ret != -E_OSL_DIR_EXISTS)
                                goto out;
                }
                row_dir = make_message("%s/row_%03u", current_dir, i);
                ret = osl_mkdir(row_dir, 0777);
-               if (ret < 0 && !is_errno(-ret, EEXIST)) {
+               if (ret < 0 && ret != -E_OSL_DIR_EXISTS) {
                        free(row_dir);
                        goto out;
                }
@@ -1017,11 +1109,11 @@ static int dump_table(char *dump_dir, struct osl_table_description *desc)
        if (ret < 0)
                goto out;
        ret = osl_mkdir(dump_dir, 0777);
-       if (ret < 0 && !is_errno(-ret, EEXIST))
+       if (ret < 0 && ret != -E_OSL_DIR_EXISTS)
                goto out;
        table_dump_dir = make_message("%s/%s", dump_dir, desc->name);
        ret = osl_mkdir(table_dump_dir, 0777);
-       if (ret < 0 && !is_errno(-ret, EEXIST))
+       if (ret < 0 && ret != -E_OSL_DIR_EXISTS)
                goto out;
        desc_file = make_message("%s/table_description.c", table_dump_dir);
        ret = osl_open(desc_file, O_WRONLY | O_CREAT | O_EXCL, 0644);
@@ -1061,7 +1153,7 @@ static int fsck(struct osl_table_description *desc)
        ret = check_for_invalid_objects(t, &lost_bytes);
        if (ret < 0)
                goto out_unmap;
-       if (ret > 0) { /* at least one mapped data file needs pruning */
+       if (ret > 0 && !conf.dry_run_given) { /* at least one mapped data file needs pruning */
                ret = prune_objects(t, lost_bytes);
                if (ret < 0)
                        goto out_unmap;
@@ -1095,7 +1187,8 @@ static int check_table(char *db_dir, char *table_name)
        ret = dump_table(conf.dump_dir_arg, &desc);
 out:
        if (ret < 0)
-               ERROR_LOG("failed to check table %s\n", table_name);
+               ERROR_LOG("failed to check table %s: %s\n", table_name,
+                       fsck_strerror(-ret));
        else
                NOTICE_LOG("successfully checked table %s\n", table_name);
        return ret;
@@ -1105,7 +1198,7 @@ static int check_all_tables(char *db_dir)
 {
        DIR *dir;
        struct dirent *entry;
-       int cwd_fd, ret2, ret = para_opendir(db_dir, &dir, &cwd_fd);
+       int cwd_fd, ret2, ret = fsck_opendir(db_dir, &dir, &cwd_fd);
 
        if (ret < 0)
                return ret;
@@ -1126,7 +1219,7 @@ static int check_all_tables(char *db_dir)
                        break;
        }
        closedir(dir);
-       ret2 = para_fchdir(cwd_fd);
+       ret2 = __fchdir(cwd_fd);
        if (ret2 < 0 && ret >= 0)
                ret = ret2;
        close(cwd_fd);