From c458b9d45e7c6ddda6285fca3797fddff815bd0a Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Tue, 9 Oct 2007 21:11:05 +0200 Subject: [PATCH 1/1] Remove some system errors from errno.h No need to have system errors duplicated in error.h. Just return -ERRNO_TO_PARA_ERROR(errno) in these cases. --- afs.c | 2 +- aft.c | 4 +++- attribute.c | 2 +- blob.c | 4 +++- error.h | 14 +++++--------- fd.c | 39 +++++---------------------------------- fsck.c | 8 ++++---- osl.c | 23 ++++++++--------------- 8 files changed, 30 insertions(+), 66 deletions(-) diff --git a/afs.c b/afs.c index e13e257b..17557e5e 100644 --- a/afs.c +++ b/afs.c @@ -537,7 +537,7 @@ static int make_database_dir(void) } PARA_INFO_LOG("afs_database dir %s\n", database_dir); ret = para_mkdir(database_dir, 0777); - if (ret >= 0 || ret == -E_EXIST) + if (ret >= 0 || is_errno(-ret, EEXIST)) return 1; free(database_dir); database_dir = NULL; diff --git a/aft.c b/aft.c index c6ca289f..5112affa 100644 --- a/aft.c +++ b/aft.c @@ -2213,5 +2213,7 @@ int aft_init(struct table_info *ti, const char *db) } PARA_INFO_LOG("failed to open audio file table\n"); audio_file_table = NULL; - return ret == -E_NOENT? 1 : ret; + if (ret >= 0 || is_errno(-ret, ENOENT)) + return 1; + return ret; } diff --git a/attribute.c b/attribute.c index d34f7d63..e0a7392f 100644 --- a/attribute.c +++ b/attribute.c @@ -538,7 +538,7 @@ int attribute_init(struct table_info *ti, const char *db) return ret; } attribute_table = NULL; - if (ret == -E_NOENT) + if (ret >= 0 || is_errno(-ret, ENOENT)) return 1; return ret; } diff --git a/blob.c b/blob.c index 150a15e4..9282b5f5 100644 --- a/blob.c +++ b/blob.c @@ -471,7 +471,9 @@ static int blob_init(struct osl_table **table, if (ret >= 0) return ret; *table = NULL; - return ret == -E_NOENT? 1 : ret; + if (ret >= 0 || is_errno(-ret, -ENOENT)) + return 1; + return ret; } /** Define the \p init function for this blob type. */ diff --git a/error.h b/error.h index 62540f4f..537e7393 100644 --- a/error.h +++ b/error.h @@ -408,18 +408,10 @@ extern const char **para_errlist[]; #define FD_ERRORS \ - PARA_ERROR(NOTDIR, "error: not a directory"), \ PARA_ERROR(FGETS, "fgets error"), \ - PARA_ERROR(EXIST, "file or directory already exists"), \ - PARA_ERROR(ISDIR, "error: is a directory"), \ - PARA_ERROR(NOENT, "no such file or directory"), \ - PARA_ERROR(OPEN_PERM, "open error (permission denied)"), \ - PARA_ERROR(MKDIR_PERM, "mkdir error (permission denied)"), \ - PARA_ERROR(MKDIR, "failed to create directory"), \ PARA_ERROR(CHDIR, "failed to change directory"), \ PARA_ERROR(FCHDIR, "fchdir failed"), \ PARA_ERROR(OPENDIR, "can not open directory"), \ - PARA_ERROR(NOSPC, "no space left on device"), \ PARA_ERROR(OPEN, "failed to open file"), \ PARA_ERROR(CHDIR_PERM, "insufficient permissions to chdir"), \ @@ -520,7 +512,11 @@ extern const char **para_errlist[]; #define ERRNO_TO_PARA_ERROR(num) ((num) | (1 << SYSTEM_ERROR_BIT)) -#define IS_SYSTEM_ERRNO(val, _errno) (ERRNO_TO_PARA_ERROR(_errno) == (val)) +static inline int is_errno(int val, int _errno) +{ + assert(val > 0 && _errno > 0); + return ERRNO_TO_PARA_ERROR(_errno) == val; +} /** * paraslash's version of strerror(3) diff --git a/fd.c b/fd.c index 6bd21b5b..ed616645 100644 --- a/fd.c +++ b/fd.c @@ -170,7 +170,7 @@ void *para_mmap(size_t length, int prot, int flags, int fd, off_t offset) * The mode parameter must be specified when O_CREAT is in the flags, and is ignored * otherwise. * - * \return Positive on success, negative on errors. + * \return The file descriptor on success, negative on errors. * * \sa open(2). */ @@ -180,22 +180,7 @@ int para_open(const char *path, int flags, mode_t mode) if (ret >= 0) return ret; - switch (errno) { - case EEXIST: - ret = -E_EXIST; - break; - case EISDIR: - ret = -E_ISDIR; - break; - case ENOENT: - ret = -E_NOENT; - break; - case EPERM: - ret = -E_OPEN_PERM; - break; - }; - PARA_ERROR_LOG("failed to open %s: %s\n", path, strerror(errno)); - return -E_OPEN; + return -ERRNO_TO_PARA_ERROR(errno); } /** @@ -211,13 +196,7 @@ int para_chdir(const char *path) if (ret >= 0) return 1; - switch (errno) { - case ENOENT: - return -E_NOENT; - case EACCES: - return -E_CHDIR_PERM; - }; - return -E_CHDIR; + return -ERRNO_TO_PARA_ERROR(errno); } /** @@ -292,19 +271,11 @@ int para_fchdir(int fd) * \param path Name of the directory to create. * \param mode The permissions to use. * - * \return positive on success, negative on errors. + * \return Standard. */ int para_mkdir(const char *path, mode_t mode) { if (!mkdir(path, mode)) return 1; - if (errno == EEXIST) - return -E_EXIST; - if (errno == ENOSPC) - return -E_NOSPC; - if (errno == ENOTDIR) - return -E_NOTDIR; - if (errno == EPERM) - return -E_MKDIR_PERM; - return -E_MKDIR; + return -ERRNO_TO_PARA_ERROR(errno); } diff --git a/fsck.c b/fsck.c index 4d719820..19f7be67 100644 --- a/fsck.c +++ b/fsck.c @@ -789,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); - if (ret < 0 && ret != -E_EXIST) + 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); - if (ret < 0 && ret != -E_EXIST) { + if (ret < 0 && !is_errno(-ret, EEXIST)) { free(row_dir); goto out; } @@ -818,11 +818,11 @@ static int dump_table(char *dump_dir, struct osl_table_description *desc) if (ret < 0) goto out; ret = para_mkdir(dump_dir, 0777); - if (ret < 0 && ret != -E_EXIST) + if (ret < 0 && !is_errno(-ret, EEXIST)) goto out; table_dump_dir = make_message("%s/%s", dump_dir, desc->name); ret = para_mkdir(table_dump_dir, 0777); - if (ret < 0 && ret != -E_EXIST) + 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); diff --git a/osl.c b/osl.c index 0b14447b..19d7961e 100644 --- a/osl.c +++ b/osl.c @@ -679,7 +679,7 @@ int osl_create_table(const struct osl_table_description *desc) continue; if (!table_dir) { ret = para_mkdir(desc->dir, 0777); - if (ret < 0 && ret != -E_EXIST) + if (ret < 0 && !is_errno(-ret, EEXIST)) goto out; table_dir = make_message("%s/%s", desc->dir, desc->name); @@ -1226,13 +1226,10 @@ int osl_open_table(const struct osl_table_description *table_desc, ret = stat(dirname, &statbuf); free(dirname); if (ret < 0) { - if (errno == ENOENT) - ret = -E_NOENT; - else - ret = -E_STAT; + ret = -ERRNO_TO_PARA_ERROR(errno); goto err; } - ret = -E_NOTDIR; + ret = -ERRNO_TO_PARA_ERROR(ENOTDIR); if (!S_ISDIR(statbuf.st_mode)) goto err; } @@ -1265,7 +1262,7 @@ static int create_disk_storage_object_dir(const struct osl_table *t, dirname = disk_storage_dirname(t, col_num, ds_name); ret = para_mkdir(dirname, 0777); free(dirname); - if (ret < 0 && ret != -E_EXIST) + if (ret < 0 && !is_errno(-ret, EEXIST)) return ret; return 1; } @@ -1358,15 +1355,11 @@ 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 = unlink(filename); + int ret = unlink(filename), err = errno; - PARA_DEBUG_LOG("deleted %s\n", filename); free(filename); - if (ret < 0) { - if (errno == ENOENT) - return -E_NOENT; - return -E_UNLINK; - } + if (ret < 0) + return -ERRNO_TO_PARA_ERROR(err); if (!(t->desc->flags & OSL_LARGE_TABLE)) return 1; dirname = disk_storage_dirname(t, col_num, ds_name); @@ -1877,7 +1870,7 @@ 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 && ret != -E_NOENT) { + if (ret < 0 && !is_errno(-ret, ENOENT)) { free(ds_name); return ret; } -- 2.30.2