a/command_util.sh: Convert template members also in the short description.
[paraslash.git] / fsck.c
diff --git a/fsck.c b/fsck.c
index 7bca01637beddba149f7476bd994f1e2940618a0..48fae27d490bf2021f85682616e2503341f5fe83 100644 (file)
--- a/fsck.c
+++ b/fsck.c
@@ -1,4 +1,17 @@
+/*
+ * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
+ *
+ * Licensed under the GPL v2. For licencing details see COPYING.
+ */
+
+/** \file fsck.c The program used to check an osl table. */
+
+
+#include <sys/types.h>
+#include <dirent.h>
+
 #include "para.h"
 #include "para.h"
+#include "fd.h"
 #include "error.h"
 #include "osl_core.h"
 #include "fsck.cmdline.h"
 #include "error.h"
 #include "osl_core.h"
 #include "fsck.cmdline.h"
@@ -163,7 +176,7 @@ static int map_index(const struct osl_table_description *desc, struct osl_object
        char *filename = index_filename(desc);
        int ret;
 
        char *filename = index_filename(desc);
        int ret;
 
-       ret = mmap_full_file(filename, O_RDWR, map);
+       ret = mmap_full_file(filename, O_RDWR, &map->data, &map->size, NULL);
        PARA_INFO_LOG("mapping index %s: ret: %d, size: %zu\n", filename, ret, map->size);
        free(filename);
        return ret;
        PARA_INFO_LOG("mapping index %s: ret: %d, size: %zu\n", filename, ret, map->size);
        free(filename);
        return ret;
@@ -558,6 +571,7 @@ static int check_disk_storage_columns(struct osl_table *t)
 out_close_hash_tree:
        osl_close_table(hash_tree_table, 0);
        free(hashes);
 out_close_hash_tree:
        osl_close_table(hash_tree_table, 0);
        free(hashes);
+       hashes = NULL;
 out:
        clear_rbtrees(t); /* TODO why are we doing that here? Seems odd */
        return ret;
 out:
        clear_rbtrees(t); /* TODO why are we doing that here? Seems odd */
        return ret;
@@ -737,14 +751,14 @@ static int dump_row(struct osl_table *t, unsigned row_num, const char *row_dir)
        if (!t->num_disk_storage_columns)
                return 1;
        dsnc = t->disk_storage_name_column;
        if (!t->num_disk_storage_columns)
                return 1;
        dsnc = t->disk_storage_name_column;
-       ret = get_mapped_object(t, dsnc, i, &obj);
+       ret = get_mapped_object(t, dsnc, row_num, &obj);
        if (ret < 0)
                return ret;
        hash_object(&obj, hash);
        ds_name = disk_storage_name_of_hash(t, hash);
        FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
                filename = disk_storage_path(t, i, ds_name);
        if (ret < 0)
                return ret;
        hash_object(&obj, hash);
        ds_name = disk_storage_name_of_hash(t, hash);
        FOR_EACH_DISK_STORAGE_COLUMN(i, t, cd) {
                filename = disk_storage_path(t, i, ds_name);
-               ret = mmap_full_file(filename, O_RDONLY, &obj);
+               ret = mmap_full_file(filename, O_RDONLY, &obj.data, &obj.size, NULL);
                free(filename);
                if (ret < 0)
                        goto out;
                free(filename);
                if (ret < 0)
                        goto out;
@@ -775,12 +789,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);
                        PARA_NOTICE_LOG("dumping rows %u - %u\n", i, i + 999);
                        ret = para_mkdir(current_dir, 0777);
                        current_dir = make_message("%s/rows_%u-%u", dump_dir, i, i + 999);
                        PARA_NOTICE_LOG("dumping rows %u - %u\n", i, i + 999);
                        ret = para_mkdir(current_dir, 0777);
-                       if (ret < 0)
+                       if (ret < 0 && !is_errno(-ret, EEXIST))
                                goto out;
                }
                row_dir = make_message("%s/row_%03u", current_dir, i);
                ret = para_mkdir(row_dir, 0777);
                                goto out;
                }
                row_dir = make_message("%s/row_%03u", current_dir, i);
                ret = para_mkdir(row_dir, 0777);
-               if (ret < 0) {
+               if (ret < 0 && !is_errno(-ret, EEXIST)) {
                        free(row_dir);
                        goto out;
                }
                        free(row_dir);
                        goto out;
                }
@@ -799,13 +813,18 @@ static int dump_table(char *dump_dir, struct osl_table_description *desc)
        struct osl_table *t = NULL;
        int fd, ret = fsck_init(desc, &t);
        char *desc_file;
        struct osl_table *t = NULL;
        int fd, ret = fsck_init(desc, &t);
        char *desc_file;
+       char *table_dump_dir = NULL;
 
        if (ret < 0)
                goto out;
        ret = para_mkdir(dump_dir, 0777);
 
        if (ret < 0)
                goto out;
        ret = para_mkdir(dump_dir, 0777);
-       if (ret < 0)
+       if (ret < 0 && !is_errno(-ret, EEXIST))
                goto out;
                goto out;
-       desc_file = make_message("%s/table_description.c", dump_dir);
+       table_dump_dir = make_message("%s/%s", dump_dir, desc->name);
+       ret = para_mkdir(table_dump_dir, 0777);
+       if (ret < 0 && !is_errno(-ret, EEXIST))
+               goto out;
+       desc_file = make_message("%s/table_description.c", table_dump_dir);
        ret = para_open(desc_file, O_WRONLY | O_CREAT | O_EXCL, 0644);
        free(desc_file);
        if (ret < 0)
        ret = para_open(desc_file, O_WRONLY | O_CREAT | O_EXCL, 0644);
        free(desc_file);
        if (ret < 0)
@@ -815,8 +834,9 @@ static int dump_table(char *dump_dir, struct osl_table_description *desc)
        close(fd);
        if (ret < 0)
                goto out;
        close(fd);
        if (ret < 0)
                goto out;
-       ret = dump_rows(dump_dir, t);
+       ret = dump_rows(table_dump_dir, t);
 out:
 out:
+       free(table_dump_dir);
        fsck_cleanup(t);
        return ret;
 }
        fsck_cleanup(t);
        return ret;
 }
@@ -856,10 +876,60 @@ out:
        return ret;
 }
 
        return ret;
 }
 
-int main(__a_unused int argc, char **argv)
+static int check_table(char *base_dir, char *table_name)
+{
+       struct osl_table_description desc = {
+               .column_descriptions = NULL,
+               .dir = base_dir,
+               .name = table_name
+       };
+       int ret;
+
+       if (!conf.no_fsck_given) {
+               ret = fsck(&desc);
+               if (ret < 0)
+                       return ret;
+       }
+       if (!conf.dump_dir_given || !*conf.dump_dir_arg)
+               return 1;
+       return dump_table(conf.dump_dir_arg, &desc);
+}
+
+static int check_all_tables(char *base_dir)
+{
+       DIR *dir;
+       struct dirent *entry;
+       int cwd_fd, ret2, ret = para_opendir(base_dir, &dir, &cwd_fd);
+
+       if (ret < 0)
+               return ret;
+       while ((entry = readdir(dir))) {
+               mode_t m;
+               struct stat s;
+               if (!strcmp(entry->d_name, "."))
+                       continue;
+               if (!strcmp(entry->d_name, ".."))
+                       continue;
+               if (lstat(entry->d_name, &s) == -1)
+                       continue;
+               m = s.st_mode;
+               if (!S_ISDIR(m))
+                       continue;
+               ret = check_table(base_dir, entry->d_name);
+               if (ret < 0)
+                       break;
+       }
+       closedir(dir);
+       ret2 = para_fchdir(cwd_fd);
+       if (ret2 < 0 && ret >= 0)
+               ret = ret2;
+       close(cwd_fd);
+       return ret;
+}
+
+int main(int argc, char **argv)
 {
        int i, ret;
 {
        int i, ret;
-       struct osl_table_description desc = {.column_descriptions = NULL};
        char *base_dir = NULL;
 
        ret = fsck_cmdline_parser(argc, argv, &conf);
        char *base_dir = NULL;
 
        ret = fsck_cmdline_parser(argc, argv, &conf);
@@ -868,34 +938,25 @@ int main(__a_unused int argc, char **argv)
                goto out;
        }
        HANDLE_VERSION_FLAG("fsck", conf);
                goto out;
        }
        HANDLE_VERSION_FLAG("fsck", conf);
-       if (!conf.inputs_num) {
-               /* FIXME: Find all subdirs */
-               ret = -E_FSCK_SYNTAX;
-               goto out;
-       }
-       desc.dir = conf.base_dir_arg;
-       if (!conf.base_dir_given) {
+       if (conf.base_dir_given)
+               base_dir = conf.base_dir_arg;
+       else {
                char *home = para_homedir();
                base_dir = make_message("%s/.paraslash/afs_database", home);
                free(home);
                char *home = para_homedir();
                base_dir = make_message("%s/.paraslash/afs_database", home);
                free(home);
-               desc.dir = base_dir;
+       }
+       if (!conf.inputs_num) {
+               ret = check_all_tables(base_dir);
+               goto out;
        }
        for (i = 0; i < conf.inputs_num; i++) {
        }
        for (i = 0; i < conf.inputs_num; i++) {
-               desc.name = conf.inputs[i];
-               if (!conf.no_fsck_given) {
-                       ret = fsck(&desc);
-                       if (ret < 0)
-                               break;
-               }
-               if (conf.dump_dir_given && *conf.dump_dir_arg) {
-                       ret = dump_table(conf.dump_dir_arg, &desc);
-                       if (ret < 0)
-                               break;
-               }
+               ret = check_table(base_dir, conf.inputs[i]);
+               if (ret < 0)
+                       break;
        }
        }
-       free(base_dir);
-       ret = 1;
 out:
 out:
+       if (!conf.base_dir_given)
+               free(base_dir);
        if (ret < 0)
                PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
        return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
        if (ret < 0)
                PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
        return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;