}
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;
}
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;
}
return ret;
}
attribute_table = NULL;
- if (ret == -E_NOENT)
+ if (ret >= 0 || is_errno(-ret, ENOENT))
return 1;
return ret;
}
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. */
#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"), \
#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)
* 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).
*/
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);
}
/**
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);
}
/**
* \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);
}
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;
}
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);
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);
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;
}
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;
}
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);
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;
}