From b9665cff53c8f3a6228e57d40277c015839bf377 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Thu, 11 Jun 2009 22:35:54 +0200 Subject: [PATCH 01/16] Replace ERRNO_TO_ERROR(ENOMEM) by E_OSL_NOMEM. osl calls should only return osl error codes. Therefore, ERRNO_TO_ERROR() must die. This is a first step to get rid of this macro. --- errlist | 1 + osl.c | 58 ++++++++++++++++++++++++++++----------------------------- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/errlist b/errlist index 9538aff..dfd9f6f 100644 --- a/errlist +++ b/errlist @@ -32,3 +32,4 @@ BAD_ROW "invalid row" EMPTY "file empty" MMAP "mmap error" LOOP "loop terminated" +NOMEM "cannot allocate memory" diff --git a/osl.c b/osl.c index 50f6398..295c88e 100644 --- a/osl.c +++ b/osl.c @@ -181,7 +181,7 @@ static int disk_storage_name_of_row(const struct osl_table *t, *name = disk_storage_name_of_object(t, &obj); if (*name) return 1; - return -ERRNO_TO_ERROR(ENOMEM); + return -E_OSL_NOMEM; } static void column_name_hash(const char *col_name, HASH_TYPE *hash) @@ -250,7 +250,7 @@ int init_table_structure(const struct osl_table_description *desc, { const struct osl_column_description *cd; struct osl_table *t = calloc(1, sizeof(*t)); - int i, ret = -ERRNO_TO_ERROR(ENOMEM), have_disk_storage_name_column = 0; + int i, ret = -E_OSL_NOMEM, have_disk_storage_name_column = 0; if (!t) return ret; @@ -265,7 +265,7 @@ int init_table_structure(const struct osl_table_description *desc, ret = -E_OSL_NO_COLUMNS; if (!desc->num_columns) goto err; - ret = -ERRNO_TO_ERROR(ENOMEM); + ret = -E_OSL_NOMEM; t->columns = calloc(desc->num_columns, sizeof(struct osl_column)); if (!t->columns) goto err; @@ -368,7 +368,7 @@ int read_table_desc(struct osl_object *map, struct osl_table_description *desc) desc->column_descriptions = calloc(desc->num_columns, sizeof(struct osl_column_description)); if (!desc->column_descriptions) - return -ERRNO_TO_ERROR(ENOMEM); + return -E_OSL_NOMEM; offset = IDX_COLUMN_DESCRIPTIONS; FOR_EACH_COLUMN(i, desc, cd) { char *null_byte; @@ -388,7 +388,7 @@ int read_table_desc(struct osl_object *map, struct osl_table_description *desc) ret = -E_OSL_INDEX_CORRUPTION; if (!null_byte) goto err; - ret = -ERRNO_TO_ERROR(ENOMEM); + ret = -E_OSL_NOMEM; cd->name = strdup(buf + offset + IDX_CD_NAME); if (!cd->name) goto err; @@ -431,7 +431,7 @@ static int compare_table_descriptions(struct osl_table *t) struct osl_column_description *cd; unsigned diff = t->desc->num_columns - desc.num_columns; INFO_LOG("extending table by %u volatile columns\n", diff); - ret = -ERRNO_TO_ERROR(ENOMEM); + ret = -E_OSL_NOMEM; desc.column_descriptions = realloc(desc.column_descriptions, t->desc->num_columns * sizeof(struct osl_column_description)); if (!desc.column_descriptions) @@ -486,7 +486,7 @@ static int create_table_index(struct osl_table *t) t->desc->name); buf = calloc(1, size); if (!buf) - return -ERRNO_TO_ERROR(ENOMEM); + return -E_OSL_NOMEM; sprintf(buf + IDX_PARA_MAGIC, "%s", PARA_MAGIC); write_u8(buf + IDX_TABLE_FLAGS, t->desc->flags); write_u8(buf + IDX_DIRTY_FLAG, 0); @@ -513,7 +513,7 @@ static int create_table_index(struct osl_table *t) if (filename) ret = write_file(filename, buf, size); else - ret = -ERRNO_TO_ERROR(ENOMEM); + ret = -E_OSL_NOMEM; free(buf); free(filename); return ret; @@ -538,14 +538,14 @@ __export int osl_create_table(const struct osl_table_description *desc) goto out; table_dir = make_message("%s/%s", desc->dir, desc->name); - ret = -ERRNO_TO_ERROR(ENOMEM); + ret = -E_OSL_NOMEM; if (!table_dir) goto out; ret = osl_mkdir(table_dir, 0777); if (ret < 0) goto out; } - ret = -ERRNO_TO_ERROR(ENOMEM); + ret = -E_OSL_NOMEM; filename = column_filename(t, i); if (!filename) goto out; @@ -649,7 +649,7 @@ static int map_column(struct osl_table *t, unsigned col_num) int ret; if (!filename) - return -ERRNO_TO_ERROR(ENOMEM); + return -E_OSL_NOMEM; ret = osl_stat(filename, &statbuf); if (ret < 0) { free(filename); @@ -690,7 +690,7 @@ int map_table(struct osl_table *t, enum map_table_flags flags) return -E_OSL_ALREADY_MAPPED; filename = index_filename(t->desc); if (!filename) - return -ERRNO_TO_ERROR(ENOMEM); + return -E_OSL_NOMEM; INFO_LOG("mapping table '%s' (index: %s)\n", t->desc->name, filename); ret = mmap_full_file(filename, flags & MAP_TBL_FL_MAP_RDONLY? O_RDONLY : O_RDWR, &t->index_map.data, &t->index_map.size, NULL); @@ -864,7 +864,7 @@ static int add_row_to_rbtrees(struct osl_table *t, uint32_t row_num, const struct osl_column_description *cd; if (!row) - return -ERRNO_TO_ERROR(ENOMEM); + return -E_OSL_NOMEM; row->num = row_num; row->volatile_objects = volatile_objs; FOR_EACH_RBTREE_COLUMN(i, t, cd) { @@ -1066,7 +1066,7 @@ __export int osl_open_table(const struct osl_table_description *table_desc, struct stat statbuf; char *dirname = column_filename(t, i); - ret = -ERRNO_TO_ERROR(ENOMEM); + ret = -E_OSL_NOMEM; if (!dirname) goto err; /* check if directory exists */ @@ -1108,7 +1108,7 @@ static int create_disk_storage_object_dir(const struct osl_table *t, return 1; dirname = disk_storage_dirname(t, col_num, ds_name); if (!dirname) - return -ERRNO_TO_ERROR(ENOMEM); + return -E_OSL_NOMEM; ret = osl_mkdir(dirname, 0777); free(dirname); if (ret < 0 && !is_errno(-ret, EEXIST)) @@ -1127,7 +1127,7 @@ static int write_disk_storage_file(const struct osl_table *t, unsigned col_num, return ret; filename = disk_storage_path(t, col_num, ds_name); if (!filename) - return -ERRNO_TO_ERROR(ENOMEM); + return -E_OSL_NOMEM; ret = write_file(filename, obj->data, obj->size); free(filename); return ret; @@ -1140,7 +1140,7 @@ static int append_map_file(const struct osl_table *t, unsigned col_num, int ret; if (!filename) - return -ERRNO_TO_ERROR(ENOMEM); + return -E_OSL_NOMEM; ret = append_file(filename, obj->data, obj->size, new_size); free(filename); return ret; @@ -1155,7 +1155,7 @@ static int append_row_index(const struct osl_table *t, char *row_index) return 1; filename = index_filename(t->desc); if (!filename) - return -ERRNO_TO_ERROR(ENOMEM); + return -E_OSL_NOMEM; ret = append_file(filename, row_index, t->row_index_size, NULL); free(filename); return ret; @@ -1168,7 +1168,7 @@ static int truncate_mapped_file(const struct osl_table *t, unsigned col_num, char *filename = column_filename(t, col_num); if (!filename) - return -ERRNO_TO_ERROR(ENOMEM); + return -E_OSL_NOMEM; ret = truncate_file(filename, size); free(filename); return ret; @@ -1181,7 +1181,7 @@ static int delete_disk_storage_file(const struct osl_table *t, unsigned col_num, int ret, err; if (!filename) - return -ERRNO_TO_ERROR(ENOMEM); + return -E_OSL_NOMEM; ret = unlink(filename); err = errno; free(filename); @@ -1191,7 +1191,7 @@ static int delete_disk_storage_file(const struct osl_table *t, unsigned col_num, return 1; dirname = disk_storage_dirname(t, col_num, ds_name); if (!dirname) - return -ERRNO_TO_ERROR(ENOMEM); + return -E_OSL_NOMEM; rmdir(dirname); free(dirname); return 1; @@ -1211,18 +1211,18 @@ __export int osl_add_and_get_row(struct osl_table *t, struct osl_object *objects return -E_OSL_BAD_TABLE; rb_parents = malloc(t->num_rbtrees * sizeof(struct rn_node*)); if (!rb_parents) - return -ERRNO_TO_ERROR(ENOMEM); + return -E_OSL_NOMEM; rb_links = malloc(t->num_rbtrees * sizeof(struct rn_node**)); if (!rb_links) { free(rb_parents); - return -ERRNO_TO_ERROR(ENOMEM); + return -E_OSL_NOMEM; } if (t->num_mapped_columns) { new_row_index = malloc(t->row_index_size); if (!new_row_index) { free(rb_links); free(rb_parents); - return -ERRNO_TO_ERROR(ENOMEM); + return -E_OSL_NOMEM; } } /* pass 1: sanity checks */ @@ -1258,7 +1258,7 @@ __export int osl_add_and_get_row(struct osl_table *t, struct osl_object *objects if (t->num_disk_storage_columns) { ds_name = disk_storage_name_of_object(t, &objects[t->disk_storage_name_column]); - ret = -ERRNO_TO_ERROR(ENOMEM); + ret = -E_OSL_NOMEM; if (!ds_name) goto out; } @@ -1301,7 +1301,7 @@ __export int osl_add_and_get_row(struct osl_table *t, struct osl_object *objects } /* pass 3: add entry to rbtrees */ if (t->num_volatile_columns) { - ret = -ERRNO_TO_ERROR(ENOMEM); + ret = -E_OSL_NOMEM; volatile_objs = calloc(t->num_volatile_columns, sizeof(struct osl_object)); if (!volatile_objs) @@ -1505,7 +1505,7 @@ static int rename_disk_storage_objects(struct osl_table *t, return 1; /* object did not change */ old_ds_name = disk_storage_name_of_object(t, old_obj); new_ds_name = disk_storage_name_of_object(t, new_obj); - ret = -ERRNO_TO_ERROR(ENOMEM); + ret = -E_OSL_NOMEM; if (!old_ds_name || ! new_ds_name) goto out; @@ -1517,7 +1517,7 @@ static int rename_disk_storage_objects(struct osl_table *t, old_filename = disk_storage_path(t, i, old_ds_name); new_filename = disk_storage_path(t, i, new_ds_name); if (!old_filename || !new_filename) - ret = -ERRNO_TO_ERROR(ENOMEM); + ret = -E_OSL_NOMEM; else ret = osl_rename(old_filename, new_filename); free(old_filename); @@ -1633,7 +1633,7 @@ __export int osl_open_disk_object(const struct osl_table *t, const struct osl_ro filename = disk_storage_path(t, col_num, ds_name); free(ds_name); if (!filename) - return -ERRNO_TO_ERROR(ENOMEM); + return -E_OSL_NOMEM; DEBUG_LOG("filename: %s\n", filename); ret = mmap_full_file(filename, O_RDONLY, &obj->data, &obj->size, NULL); free(filename); -- 2.39.2 From 06fa6acd87ac23ee3ca422dbc56cf97a3c71aa09 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Thu, 11 Jun 2009 22:49:03 +0200 Subject: [PATCH 02/16] Get rid of all remaining users of ERRNO_TO_ERROR() in osl.c. --- errlist | 3 +++ osl.c | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/errlist b/errlist index dfd9f6f..e4435df 100644 --- a/errlist +++ b/errlist @@ -33,3 +33,6 @@ EMPTY "file empty" MMAP "mmap error" LOOP "loop terminated" NOMEM "cannot allocate memory" +NOTDIR "not a directory" +STAT "stat error" +UNLINK "failed to unlink file" diff --git a/osl.c b/osl.c index 295c88e..3f55ed4 100644 --- a/osl.c +++ b/osl.c @@ -1073,10 +1073,10 @@ __export int osl_open_table(const struct osl_table_description *table_desc, ret = stat(dirname, &statbuf); free(dirname); if (ret < 0) { - ret = -ERRNO_TO_ERROR(errno); + ret = -E_OSL_STAT; goto err; } - ret = -ERRNO_TO_ERROR(ENOTDIR); + ret = -E_OSL_NOTDIR; if (!S_ISDIR(statbuf.st_mode)) goto err; } @@ -1186,7 +1186,7 @@ static int delete_disk_storage_file(const struct osl_table *t, unsigned col_num, err = errno; free(filename); if (ret < 0) - return -ERRNO_TO_ERROR(err); + return -E_OSL_UNLINK; if (!(t->desc->flags & OSL_LARGE_TABLE)) return 1; dirname = disk_storage_dirname(t, col_num, ds_name); -- 2.39.2 From e2950383a28766d521a69ec5660b8271f322cbdb Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Thu, 11 Jun 2009 23:06:04 +0200 Subject: [PATCH 03/16] Kill all users of ERRNO_TO_ERROR() in util.c and util.h. --- errlist | 7 +++++++ util.c | 10 +++++----- util.h | 6 +++--- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/errlist b/errlist index e4435df..728e3f8 100644 --- a/errlist +++ b/errlist @@ -36,3 +36,10 @@ NOMEM "cannot allocate memory" NOTDIR "not a directory" STAT "stat error" UNLINK "failed to unlink file" +WRITE "write error" +OPEN "failed to open file" +TRUNCATE "could not truncate file" +MKDIR "failed to create directory" +RENAME "could not rename file" +MUNMAP "munmap error" +FSTAT "fstat error" diff --git a/util.c b/util.c index 7bf74b0..79b4944 100644 --- a/util.c +++ b/util.c @@ -40,7 +40,7 @@ static ssize_t __write(int fd, const void *buf, size_t size) ret = write(fd, buf, size); if ((ret < 0) && (errno == EAGAIN || errno == EINTR)) continue; - return ret >= 0? ret : -ERRNO_TO_ERROR(errno); + return ret >= 0? ret : -E_OSL_WRITE; } } @@ -89,7 +89,7 @@ int osl_open(const char *path, int flags, mode_t mode) if (ret >= 0) return ret; - return -ERRNO_TO_ERROR(errno); + return -E_OSL_OPEN; } /** @@ -153,7 +153,7 @@ int mmap_full_file(const char *path, int open_mode, void **map, return ret; fd = ret; if (fstat(fd, &file_status) < 0) { - ret = -ERRNO_TO_ERROR(errno); + ret = -E_OSL_FSTAT; goto out; } *size = file_status.st_size; @@ -194,7 +194,7 @@ int osl_munmap(void *start, size_t length) err = errno; ERROR_LOG("munmap (%p/%zu) failed: %s\n", start, length, strerror(err)); - return -ERRNO_TO_ERROR(err); + return -E_OSL_MUNMAP; } /** @@ -267,6 +267,6 @@ int truncate_file(const char *path, off_t size) if (statbuf.st_size < size) return ret; if (truncate(path, statbuf.st_size - size) < 0) - return -ERRNO_TO_ERROR(errno); + return -E_OSL_TRUNCATE; return 1; } diff --git a/util.h b/util.h index 2e3c66f..53221c9 100644 --- a/util.h +++ b/util.h @@ -26,7 +26,7 @@ static inline int osl_mkdir(const char *path, mode_t mode) { if (!mkdir(path, mode)) return 1; - return -ERRNO_TO_ERROR(errno); + return -E_OSL_MKDIR; } /** @@ -42,7 +42,7 @@ static inline int osl_mkdir(const char *path, mode_t mode) _static_inline_ int osl_rename(const char *old_path, const char *new_path) { if (rename(old_path, new_path) < 0) - return -ERRNO_TO_ERROR(errno); + return -E_OSL_RENAME; return 1; } @@ -50,5 +50,5 @@ _static_inline_ int osl_stat(const char *path, struct stat *buf) { if (stat(path, buf) >= 0) return 1; - return -ERRNO_TO_ERROR(errno); + return -E_OSL_STAT; } -- 2.39.2 From 7d53d2f571da94ec9caf85fb456118ebee85fcf9 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Thu, 11 Jun 2009 23:19:45 +0200 Subject: [PATCH 04/16] Kill all remaining users of ERRNO_TO_ERROR(). --- fsck.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/fsck.c b/fsck.c index 68e3107..357fbe7 100644 --- a/fsck.c +++ b/fsck.c @@ -39,7 +39,10 @@ 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(num, txt) E_FSCK_ ## num enum { @@ -217,9 +220,9 @@ static int uint32_compare(const struct osl_object *obj1, const struct osl_object */ static inline int __fchdir(int fd) { - if (fchdir(fd) < 0) - return -ERRNO_TO_ERROR(errno); - return 1; + if (fchdir(fd) >= 0) + return 1; + return errno == EACCES? -E_FSCK_ACCESS : -E_FSCK_CHDIR; } /** @@ -233,7 +236,7 @@ _static_inline_ int __chdir(const char *path) { if (chdir(path) >= 0) return 1; - return -ERRNO_TO_ERROR(errno); + return errno == EACCES? -E_FSCK_ACCESS : -E_FSCK_CHDIR; } /** @@ -276,7 +279,7 @@ static int fsck_opendir(const char *dirname, DIR **dir, int *cwd) *dir = opendir("."); if (*dir) return 1; - ret = -ERRNO_TO_ERROR(errno); + ret = errno == EACCES? -E_FSCK_ACCESS : -E_FSCK_OPENDIR; /* Ignore return value of fchdir() and close(). We're busted anyway. */ if (cwd) fchdir(*cwd); @@ -310,7 +313,7 @@ static int for_each_file_in_dir(const char *dirname, 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; -- 2.39.2 From 1c580157d0fe6dc4dc0c10f97d9f9000a354b0d3 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Thu, 11 Jun 2009 23:47:53 +0200 Subject: [PATCH 05/16] Kill all users of is_errno(). --- errlist | 2 ++ fsck.c | 8 ++++---- osl.c | 14 +++++++------- util.h | 2 +- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/errlist b/errlist index 728e3f8..b80c1ca 100644 --- a/errlist +++ b/errlist @@ -43,3 +43,5 @@ MKDIR "failed to create directory" RENAME "could not rename file" MUNMAP "munmap error" FSTAT "fstat error" +DIR_EXISTS "directory exists" +NOENT "no such file" diff --git a/fsck.c b/fsck.c index 357fbe7..e623b37 100644 --- a/fsck.c +++ b/fsck.c @@ -1062,12 +1062,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; } @@ -1091,11 +1091,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); diff --git a/osl.c b/osl.c index 3f55ed4..eb1d24f 100644 --- a/osl.c +++ b/osl.c @@ -534,7 +534,7 @@ __export int osl_create_table(const struct osl_table_description *desc) continue; if (!table_dir) { ret = osl_mkdir(desc->dir, 0777); - if (ret < 0 && !is_errno(-ret, EEXIST)) + if (ret < 0 && ret != -E_OSL_DIR_EXISTS) goto out; table_dir = make_message("%s/%s", desc->dir, desc->name); @@ -1111,7 +1111,7 @@ static int create_disk_storage_object_dir(const struct osl_table *t, return -E_OSL_NOMEM; ret = osl_mkdir(dirname, 0777); free(dirname); - if (ret < 0 && !is_errno(-ret, EEXIST)) + if (ret < 0 && ret != -E_OSL_DIR_EXISTS) return ret; return 1; } @@ -1178,15 +1178,15 @@ static int delete_disk_storage_file(const struct osl_table *t, unsigned col_num, const char *ds_name) { char *dirname, *filename = disk_storage_path(t, col_num, ds_name); - int ret, err; + int ret = 1; if (!filename) return -E_OSL_NOMEM; - ret = unlink(filename); - err = errno; + if (unlink(filename) < 0) + ret = errno == ENOENT? -E_OSL_NOENT : -E_OSL_UNLINK; free(filename); if (ret < 0) - return -E_OSL_UNLINK; + return ret; if (!(t->desc->flags & OSL_LARGE_TABLE)) return 1; dirname = disk_storage_dirname(t, col_num, ds_name); @@ -1563,7 +1563,7 @@ __export int osl_update_object(struct osl_table *t, const struct osl_row *r, if (ret < 0) return ret; ret = delete_disk_storage_file(t, col_num, ds_name); - if (ret < 0 && !is_errno(-ret, ENOENT)) { + if (ret < 0 && ret != -E_OSL_NOENT) { free(ds_name); return ret; } diff --git a/util.h b/util.h index 53221c9..f4c26ad 100644 --- a/util.h +++ b/util.h @@ -26,7 +26,7 @@ static inline int osl_mkdir(const char *path, mode_t mode) { if (!mkdir(path, mode)) return 1; - return -E_OSL_MKDIR; + return errno == EEXIST? -E_OSL_DIR_EXISTS : -E_OSL_MKDIR; } /** -- 2.39.2 From 22abcc2013d2f68ec55e5e0113c682899662c7d7 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Thu, 11 Jun 2009 23:52:20 +0200 Subject: [PATCH 06/16] Kill error.h. This completes the cleanup of the error handling. --- error.h | 28 ---------------------------- fsck.c | 2 +- osl.c | 3 --- util.c | 1 - 4 files changed, 1 insertion(+), 33 deletions(-) delete mode 100644 error.h diff --git a/error.h b/error.h deleted file mode 100644 index f28c39d..0000000 --- a/error.h +++ /dev/null @@ -1,28 +0,0 @@ -#define FSCK_ERROR_BIT 29 -/** - * This bit indicates whether a number is considered a system error number - * If yes, the system errno is just the result of clearing this bit from - * the given number. - */ -#define SYSTEM_ERROR_BIT 30 - -/** Check whether the system error bit is set. */ -#define IS_SYSTEM_ERROR(num) (!!((num) & (1 << SYSTEM_ERROR_BIT))) - -/** 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; -} diff --git a/fsck.c b/fsck.c index e623b37..bd1b556 100644 --- a/fsck.c +++ b/fsck.c @@ -14,7 +14,6 @@ #include "log.h" #include "osl.h" -#include "error.h" #include "util.h" #include "osl_core.h" #include "fsck.cmdline.h" @@ -44,6 +43,7 @@ static struct fsck_args_info conf; 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, diff --git a/osl.c b/osl.c index eb1d24f..5b9308e 100644 --- a/osl.c +++ b/osl.c @@ -10,7 +10,6 @@ #include "log.h" #include "osl.h" -#include "error.h" #include "util.h" #include "osl_core.h" @@ -38,8 +37,6 @@ static const unsigned int errmsgidx[] = { __export const char *osl_strerror(int num) { - if (IS_SYSTEM_ERROR(num)) - return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1)); return msgstr.str + errmsgidx[num]; } diff --git a/util.c b/util.c index 79b4944..7500c08 100644 --- a/util.c +++ b/util.c @@ -14,7 +14,6 @@ #include "log.h" #include "osl.h" -#include "error.h" #include "util.h" /** -- 2.39.2 From cf207dcadaa03addb1381012f9b822a754a526af Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Fri, 12 Jun 2009 21:54:19 +0200 Subject: [PATCH 07/16] Return -E_OSL_NOENT if stat/open fails with errno == ENOENT. This often indicates a non-fatal error, so callers might want to know. --- osl.c | 6 ++---- util.c | 4 ++-- util.h | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/osl.c b/osl.c index 5b9308e..a9dcc29 100644 --- a/osl.c +++ b/osl.c @@ -1067,12 +1067,10 @@ __export int osl_open_table(const struct osl_table_description *table_desc, if (!dirname) goto err; /* check if directory exists */ - ret = stat(dirname, &statbuf); + ret = osl_stat(dirname, &statbuf); free(dirname); - if (ret < 0) { - ret = -E_OSL_STAT; + if (ret < 0) goto err; - } ret = -E_OSL_NOTDIR; if (!S_ISDIR(statbuf.st_mode)) goto err; diff --git a/util.c b/util.c index 7500c08..64d83c8 100644 --- a/util.c +++ b/util.c @@ -88,7 +88,7 @@ int osl_open(const char *path, int flags, mode_t mode) if (ret >= 0) return ret; - return -E_OSL_OPEN; + return errno == ENOENT? -E_OSL_NOENT : -E_OSL_OPEN; } /** @@ -152,7 +152,7 @@ int mmap_full_file(const char *path, int open_mode, void **map, return ret; fd = ret; if (fstat(fd, &file_status) < 0) { - ret = -E_OSL_FSTAT; + ret = errno == ENOENT? -E_OSL_NOENT : -E_OSL_STAT; goto out; } *size = file_status.st_size; diff --git a/util.h b/util.h index f4c26ad..79b0d0e 100644 --- a/util.h +++ b/util.h @@ -50,5 +50,5 @@ _static_inline_ int osl_stat(const char *path, struct stat *buf) { if (stat(path, buf) >= 0) return 1; - return -E_OSL_STAT; + return errno == ENOENT? -E_OSL_NOENT : -E_OSL_STAT; } -- 2.39.2 From aaedc06d0448b048c7d41074985f9ee81e93d077 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Fri, 12 Jun 2009 21:55:08 +0200 Subject: [PATCH 08/16] Add #ifdef wrapper for osl.h. This allows to include osl.h more than once. --- Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index cbffcb7..0674495 100644 --- a/Makefile +++ b/Makefile @@ -148,7 +148,10 @@ errtab.h: errlist sed -e 's/^\([A-Z_]*\)\s*\(.*\)/OSL_ERROR(E_OSL_\1, \2)/g' $< > $@ osl.h: osl.h.in osl_errors.h Makefile - cat osl.h.in osl_errors.h > $@ + echo '#ifndef _OSL_H' > $@ + echo '#define _OSL_H' >> $@ + cat osl.h.in osl_errors.h >> $@ + echo '#endif /* _OSL_H */' >> $@ clean: rm -f *.o $(realname) osl.h osl_errors.h errtab.h fsck.cmdline.h \ fsck.cmdline.c oslfsck -- 2.39.2 From 2dba3a38dcfe721842404f01e9b2adb59c302604 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Mon, 6 Jul 2009 23:27:01 +0200 Subject: [PATCH 09/16] Remove remaining references to paraslash. Mostly trivial stuff. We can't change the magic string though, so this will have to stay "PARASLASH" for the rest of eternity. --- fsck.c | 6 +++--- gcc-compat.h | 2 +- osl.c | 8 ++++---- osl_core.h | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/fsck.c b/fsck.c index bd1b556..00f14f3 100644 --- a/fsck.c +++ b/fsck.c @@ -121,8 +121,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. * @@ -147,7 +147,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. * diff --git a/gcc-compat.h b/gcc-compat.h index 5d905ed..20e07fc 100644 --- a/gcc-compat.h +++ b/gcc-compat.h @@ -13,7 +13,7 @@ # define __printf(p,q) __attribute__ ((format (printf, p, q))) /* * as direct use of __printf(p,q) confuses doxygen, here are two extra macros - * for those values p,q that are actually used by paraslash. + * for those values p,q that are actually used by libosl. */ #define __printf_1_2 __printf(1,2) #define __printf_2_3 __printf(2,3) diff --git a/osl.c b/osl.c index a9dcc29..5a0dde8 100644 --- a/osl.c +++ b/osl.c @@ -331,8 +331,8 @@ err: * \param map The memory mapping of the index file. * \param desc The values found in the index header are returned here. * - * Read the index header, check for the paraslash magic string and the table version number. - * Read all information stored in the index header into \a desc. + * Read the index header, check for the osl magic string and the table version + * number. Read all information stored in the index header into \a desc. * * \return Standard. * @@ -349,7 +349,7 @@ int read_table_desc(struct osl_object *map, struct osl_table_description *desc) if (map->size < MIN_INDEX_HEADER_SIZE(1)) return -E_OSL_SHORT_TABLE; - if (strncmp(buf + IDX_PARA_MAGIC, PARA_MAGIC, strlen(PARA_MAGIC))) + if (strncmp(buf + IDX_OSL_MAGIC, OSL_MAGIC, strlen(OSL_MAGIC))) return -E_OSL_NO_MAGIC; version = read_u8(buf + IDX_VERSION); if (version < MIN_TABLE_VERSION || version > MAX_TABLE_VERSION) @@ -484,7 +484,7 @@ static int create_table_index(struct osl_table *t) buf = calloc(1, size); if (!buf) return -E_OSL_NOMEM; - sprintf(buf + IDX_PARA_MAGIC, "%s", PARA_MAGIC); + sprintf(buf + IDX_OSL_MAGIC, "%s", OSL_MAGIC); write_u8(buf + IDX_TABLE_FLAGS, t->desc->flags); write_u8(buf + IDX_DIRTY_FLAG, 0); write_u8(buf + IDX_VERSION, CURRENT_TABLE_VERSION); diff --git a/osl_core.h b/osl_core.h index df32b60..3359aa7 100644 --- a/osl_core.h +++ b/osl_core.h @@ -133,7 +133,7 @@ _static_inline_ struct osl_column_description *get_column_description( */ enum index_header_offsets { /** Bytes 0-8: PARASLASH. */ - IDX_PARA_MAGIC = 0, + IDX_OSL_MAGIC = 0, /** Byte 9: Dirty flag (nonzero if table is mapped). */ IDX_DIRTY_FLAG = 9, /** Byte 10: osl table version number. */ @@ -165,7 +165,7 @@ enum index_column_desc_offsets { }; /** Magic string contained in the header of the index file of each osl table. */ -#define PARA_MAGIC "PARASLASH" +#define OSL_MAGIC "PARASLASH" /** * The minimal number of bytes for a column in the index header. -- 2.39.2 From 884d9b3ac360755d8dd8e28e3d049cb6b0e2b1e8 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Fri, 10 Jul 2009 22:05:55 +0200 Subject: [PATCH 10/16] Fix stale comment. --- fsck.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fsck.c b/fsck.c index 00f14f3..246a0b6 100644 --- a/fsck.c +++ b/fsck.c @@ -167,7 +167,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. * -- 2.39.2 From edb632b26c12a94fb7d8a7bfcb787a8df8b77a1c Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sat, 11 Jul 2009 21:39:27 +0200 Subject: [PATCH 11/16] fsck: Overwrite the backup index file if it exists. A bit dangerous, but without this change, osfsck always fails if this file exists. Also, print a bit more information about what is going on. --- fsck.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/fsck.c b/fsck.c index 246a0b6..bc77424 100644 --- a/fsck.c +++ b/fsck.c @@ -577,7 +577,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; @@ -1169,7 +1170,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; -- 2.39.2 From 1297f561259ea010d295aee88c8c6864e726d5d2 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sat, 11 Jul 2009 23:24:02 +0200 Subject: [PATCH 12/16] fsck: Fix a serious bug in prune_mapped_column(). This function still contained a relict from the old database format that was changed in commit 6d7dce7f277fc8606fb8d5ed6360660c13a218af. This caused prune_mapped_column() to write one byte too much to the data file of a mapped column for each removed row. Running the buggy oslfsck on a table with mapped columns where rows have been deleted likely results in data corruption to objects adjacent to the deleted objects. Worst of all, since only the data files but not the index is affected, the corruption will not immediately be noticed, which is probably why it took one year to find this bug. Kill it with pleasure. --- fsck.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fsck.c b/fsck.c index bc77424..f4f1071 100644 --- a/fsck.c +++ b/fsck.c @@ -553,10 +553,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; -- 2.39.2 From d16e7226ea99f7979333b14422b98a55231f566d Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sat, 11 Jul 2009 23:25:29 +0200 Subject: [PATCH 13/16] fsck: Add some more log messages. These were helpful to find the bug which was fixed by the previous patch. --- fsck.c | 7 ++++++- util.c | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/fsck.c b/fsck.c index f4f1071..c5fdf35 100644 --- a/fsck.c +++ b/fsck.c @@ -512,6 +512,8 @@ 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: %u 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); @@ -885,8 +887,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; } diff --git a/util.c b/util.c index 64d83c8..db80854 100644 --- a/util.c +++ b/util.c @@ -61,8 +61,10 @@ int write_all(int fd, const char *buf, size_t *len) *len = 0; while (*len < total) { int ret = __write(fd, buf + *len, total - *len); - if (ret < 0) + if (ret < 0) { + ERROR_LOG("write error: %s\n", strerror(errno)); return ret; + } *len += ret; } return 1; -- 2.39.2 From 789cbb1f0df5931251788af4d16cc8951c1bd843 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 12 Jul 2009 23:40:23 +0200 Subject: [PATCH 14/16] Simplify check_index_ranges(). Use FOR_EACH_MAPPED_COLUMN rather than open-code it. --- fsck.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/fsck.c b/fsck.c index c5fdf35..68f08d9 100644 --- a/fsck.c +++ b/fsck.c @@ -399,15 +399,13 @@ 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_FSCK_RANGE_VIOLATION) -- 2.39.2 From ad58fcdf85adb652fd1454470c12e3dec9f8cc36 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 12 Jul 2009 23:42:55 +0200 Subject: [PATCH 15/16] Replace underscores with dashes in fsck command line options. The auto-generated --detailed-help uses a dash, so use dashes everywhere. --- fsck.ggo | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fsck.ggo b/fsck.ggo index edb608b..2452e3d 100644 --- a/fsck.ggo +++ b/fsck.ggo @@ -31,7 +31,7 @@ details=" given tables to the specified path. " -option "no_fsck" n +option "no-fsck" n #~~~~~~~~~~~~~~~~~ "Disable fsck mode." flag off @@ -48,7 +48,7 @@ details=" Ignore the dirty bit when opening osl tables. " -option "dry_run" - +option "dry-run" - #~~~~~~~~~~~~~~~~~ "Only report problems, don't try to fix them." flag off -- 2.39.2 From c9d2589c6764a308142afd17e92cef630c7fe940 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Mon, 13 Jul 2009 00:11:48 +0200 Subject: [PATCH 16/16] Change copyright year to 2009. --- fsck.c | 4 ++-- hash.h | 2 +- log.h | 2 +- osl.c | 2 +- osl.h.in | 2 +- osl_core.h | 2 +- portable_io.h | 2 +- sha1.c | 2 +- util.c | 2 +- util.h | 2 +- 10 files changed, 11 insertions(+), 11 deletions(-) diff --git a/fsck.c b/fsck.c index 68f08d9..f700e9f 100644 --- a/fsck.c +++ b/fsck.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2008 Andre Noll + * Copyright (C) 2007-2009 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ @@ -20,7 +20,7 @@ /** 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" \ + "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" \ diff --git a/hash.h b/hash.h index 03b45e0..27b0601 100644 --- a/hash.h +++ b/hash.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2008 Andre Noll + * Copyright (C) 2007-2009 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ diff --git a/log.h b/log.h index 8c648f4..68adc32 100644 --- a/log.h +++ b/log.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 1997-2008 Andre Noll + * Copyright (C) 1997-2009 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ diff --git a/osl.c b/osl.c index 5a0dde8..430d120 100644 --- a/osl.c +++ b/osl.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2008 Andre Noll + * Copyright (C) 2007-2009 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ diff --git a/osl.h.in b/osl.h.in index 5255877..cd39a3b 100644 --- a/osl.h.in +++ b/osl.h.in @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2008 Andre Noll + * Copyright (C) 2007-2009 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ diff --git a/osl_core.h b/osl_core.h index 3359aa7..6c87d6c 100644 --- a/osl_core.h +++ b/osl_core.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2008 Andre Noll + * Copyright (C) 2007-2009 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ diff --git a/portable_io.h b/portable_io.h index e296bcb..c2ed8af 100644 --- a/portable_io.h +++ b/portable_io.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2008 Andre Noll + * Copyright (C) 2007-2009 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ diff --git a/sha1.c b/sha1.c index 3cb2ea4..3bd79fb 100644 --- a/sha1.c +++ b/sha1.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007-2008 Andre Noll + * Copyright (C) 2007-2009 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ diff --git a/util.c b/util.c index db80854..49f1914 100644 --- a/util.c +++ b/util.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2008 Andre Noll + * Copyright (C) 2006-2009 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ diff --git a/util.h b/util.h index 79b0d0e..70beae4 100644 --- a/util.h +++ b/util.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2006-2008 Andre Noll + * Copyright (C) 2006-2009 Andre Noll * * Licensed under the GPL v2. For licencing details see COPYING. */ -- 2.39.2