+/*
+ * 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 "fd.h"
#include "error.h"
#include "osl_core.h"
+#include "fsck.cmdline.h"
-#define OSL_DUMP_DIR "/tmp/osldump"
-
-enum errors {
- E_FSCK_SYNTAX = 501,
- E_RANGE_VIOLATION,
- E_NO_DS_FILE,
- E_NOT_A_REGULAR_FILE,
- E_BAD_HASH_PATH,
-};
+static struct fsck_args_info conf;
-INIT_STDERR_LOGGING(1);
+INIT_FSCK_ERRLISTS;
+INIT_STDERR_LOGGING(conf.loglevel_arg);
/* taken from git */
signed char hexval_table[256] = {
// PARA_INFO_LOG("obj: %p..%p\n", obj_start, obj_start + obj.size);
// PARA_INFO_LOG("map: %p..%p\n", map_start, map_start + col->data_map.size);
if (obj_start < map_start || obj_start + obj.size > map_start + col->data_map.size) {
- PARA_CRIT_LOG("row %u, col %u: range violation, very bad\n", row_num, col_num);
+ PARA_CRIT_LOG("range violation in row %u, col %u\n", row_num,
+ col_num);
return -E_RANGE_VIOLATION;
}
PARA_DEBUG_LOG("col %u: ok\n", col_num);
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;
}
-#define FSCK 1
-#define FORCE 1
-#define DUMP 0
-
static void set_dummy_contents(struct osl_table_description *desc)
{
int i;
}
PARA_INFO_LOG("unmapping index\n");
para_munmap(map.data, map.size);
- if (FORCE)
+ if (conf.force_given)
ret = map_table(*t, (MAP_TBL_FL_IGNORE_DIRTY));
else
ret = map_table(*t, 0);
fsck_cleanup(t);
return ret;
}
-int main(__a_unused int argc, __a_unused 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;
- struct osl_table_description desc = {.column_descriptions = NULL};
- ret = -E_FSCK_SYNTAX;
- if (argc < 3)
- goto out;
- desc.dir = argv[1];
- desc.name = argv[2];
- if (FSCK) {
+ if (!conf.no_fsck_given) {
ret = fsck(&desc);
if (ret < 0)
- goto out;
+ 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(__a_unused int argc, char **argv)
+{
+ int i, ret;
+ char *base_dir = NULL;
+
+ ret = fsck_cmdline_parser(argc, argv, &conf);
+ if (ret < 0) {
+ ret = -E_FSCK_SYNTAX;
+ goto out;
+ }
+ HANDLE_VERSION_FLAG("fsck", conf);
+ 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);
+ }
+ if (!conf.inputs_num) {
+ ret = check_all_tables(base_dir);
+ goto out;
+ }
+ for (i = 0; i < conf.inputs_num; i++) {
+ ret = check_table(base_dir, conf.inputs[i]);
+ if (ret < 0)
+ break;
}
- if (DUMP)
- ret = dump_table(OSL_DUMP_DIR, &desc);
out:
+ if (!conf.base_dir_given)
+ free(base_dir);
if (ret < 0)
- PARA_ERROR_LOG("error %d\n", ret);
- return ret < 0? EXIT_FAILURE: EXIT_SUCCESS;
+ PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
+ return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
}