From 5c22069d19047ab6acb132f769f2359595167af3 Mon Sep 17 00:00:00 2001 From: Vreixo Formoso Date: Sat, 29 Dec 2007 16:30:13 +0100 Subject: [PATCH] Add function to check if we have access to IsoFileSource contents. --- src/fs_local.c | 43 +++++++++++++++++++++++++++++++++++++++++++ src/fsource.h | 26 ++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/fs_local.c b/src/fs_local.c index 9bd580a..c143bf3 100644 --- a/src/fs_local.c +++ b/src/fs_local.c @@ -13,6 +13,9 @@ #include "fsource.h" #include "error.h" +/* for eaccess, define in unistd.h */ +#define __USE_GNU + #include #include #include @@ -141,6 +144,45 @@ int lfs_stat(IsoFileSource *src, struct stat *info) return ISO_SUCCESS; } +static +int lfs_access(IsoFileSource *src) +{ + _LocalFsFileSource *data; + + if (src == NULL) { + return ISO_NULL_POINTER; + } + data = src->data; + + if (eaccess(data->path, R_OK) != 0) { + int err; + + /* error, choose an appropriate return code */ + switch (errno) { + case EACCES: + err = ISO_FILE_ACCESS_DENIED; + break; + case ENOTDIR: + case ENAMETOOLONG: + case ELOOP: + err = ISO_FILE_BAD_PATH; + break; + case ENOENT: + err = ISO_FILE_DOESNT_EXIST; + break; + case EFAULT: + case ENOMEM: + err = ISO_MEM_ERROR; + break; + default: + err = ISO_FILE_ERROR; + break; + } + return err; + } + return ISO_SUCCESS; +} + static int lfs_open(IsoFileSource *src) { @@ -394,6 +436,7 @@ IsoFileSourceIface lfs_class = { lfs_get_name, lfs_lstat, lfs_stat, + lfs_access, lfs_open, lfs_close, lfs_read, diff --git a/src/fsource.h b/src/fsource.h index c596551..e2c1433 100644 --- a/src/fsource.h +++ b/src/fsource.h @@ -119,6 +119,26 @@ typedef struct IsoFileSource_Iface */ int (*stat)(IsoFileSource *src, struct stat *info); + /** + * Check if the process has access to read file contents. Note that this + * is not necessarily related with (l)stat functions. For example, in a + * filesystem implementation to deal with an ISO image, if the user has + * read access to the image it will be able to read all files inside it, + * despite of the particular permission of each file in the RR tree, that + * are what the above functions return. + * + * @return + * 1 if process has read access, < 0 on error + * Error codes: + * ISO_FILE_ACCESS_DENIED + * ISO_FILE_BAD_PATH + * ISO_FILE_DOESNT_EXIST + * ISO_MEM_ERROR + * ISO_FILE_ERROR + * ISO_NULL_POINTER + */ + int (*access)(IsoFileSource *src); + /** * Opens the source. * @return 1 on success, < 0 on error @@ -263,6 +283,12 @@ int iso_file_source_lstat(IsoFileSource *src, struct stat *info) return src->class->lstat(src, info); } +extern inline +int iso_file_source_access(IsoFileSource *src) +{ + return src->class->access(src); +} + extern inline int iso_file_source_stat(IsoFileSource *src, struct stat *info) {