Skip to content

Commit

Permalink
Fixed access function (tamatebako/tebako#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx committed Aug 1, 2023
1 parent 4739693 commit 488c746
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 16 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ jobs:
source: '.'
extensions: 'h,cpp,c'
clangFormatVersion: 14
# inplace: True
# - uses: EndBug/add-and-commit@v9
# with:
# author_name: A robot on behalf of Maxim Samsonov
# author_email: maxirmx@sw.consulting
# message: 'Committing clang-format changes'
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
inplace: True
- uses: EndBug/add-and-commit@v9
with:
author_name: A robot on behalf of Maxim Samsonov
author_email: maxirmx@sw.consulting
message: 'Committing clang-format changes'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28 changes: 26 additions & 2 deletions src/tebako-dfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,19 @@ int safe_dwarfs_call(Functor&& fn,
return ret;
}

// We are testing against owner permissions
static int dwarfs_access_inner(int amode, struct stat* st)
{
int ret = DWARFS_IO_CONTINUE;
if ((!(st->st_mode & S_IRUSR) && (amode & R_OK)) ||
(!(st->st_mode & S_IWUSR) && (amode & W_OK)) ||
(!(st->st_mode & S_IXUSR) && (amode & X_OK))) {
ret = DWARFS_IO_ERROR;
TEBAKO_SET_LAST_ERROR(EACCES);
}
return ret;
}

int dwarfs_access(const char* path,
int amode,
uid_t uid,
Expand All @@ -280,7 +293,7 @@ int dwarfs_access(const char* path,
struct stat st;
int ret = dwarfs_stat(path, &st, lnk);
if (ret == DWARFS_IO_CONTINUE) {
ret = dwarfs_inode_access(st.st_ino, amode, uid, gid);
ret = dwarfs_access_inner(amode, &st);
}
return ret;
}
Expand Down Expand Up @@ -434,8 +447,19 @@ int dwarfs_inode_access(uint32_t inode,
std::function<int(filesystem_v2*, uint32_t, int, uid_t, gid_t)>{
[](filesystem_v2* fs, uint32_t inode, int amode, uid_t uid,
gid_t gid) -> int {
int ret = DWARFS_IO_ERROR;
auto pi = fs->find(inode);
return pi ? fs->access(*pi, amode, uid, gid) : ENOENT;
if (pi) {
struct stat st;
ret = fs->getattr(*pi, &st);
if (ret == DWARFS_IO_CONTINUE) {
ret = dwarfs_access_inner(amode, &st);
}
}
else {
TEBAKO_SET_LAST_ERROR(ENOENT);
}
return ret;
}},
__func__, inode, amode, uid, gid);
}
Expand Down
6 changes: 2 additions & 4 deletions src/tebako-fd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ int sync_tebako_fdtable::openat(int vfd, const char* path, int flags) noexcept
// on a read - only file system and either O_WRONLY, O_RDWR,
// O_CREAT(if the file does not exist), or O_TRUNC is set in the
// oflag argument.
TEBAKO_SET_LAST_ERROR((flags & O_CREAT) ? EROFS : ENOENT);
if (flags & O_CREAT)
TEBAKO_SET_LAST_ERROR(EROFS);
}
}
catch (bad_alloc&) {
Expand Down Expand Up @@ -366,9 +367,6 @@ int sync_tebako_fdtable::fstatat(int vfd,
if (ret == DWARFS_IO_CONTINUE) {
ret = dwarfs_inode_relative_stat(stfd.st_ino, path, st, follow);
}
else {
TEBAKO_SET_LAST_ERROR(ENOENT);
}
}
}
return ret;
Expand Down
20 changes: 19 additions & 1 deletion tests/tests-file-ctl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,21 @@ TEST_F(FileCtlTests, tebako_access_absolute_path)
{
int ret = tebako_access(TEBAKIZE_PATH("file.txt"), F_OK);
EXPECT_EQ(0, ret);
ret = tebako_access(TEBAKIZE_PATH("file.txt"), R_OK);
EXPECT_EQ(0, ret);
ret = tebako_access(TEBAKIZE_PATH("file.txt"), W_OK);
EXPECT_EQ(0, ret);
ret = tebako_access(TEBAKIZE_PATH("file.txt"), X_OK);
EXPECT_EQ(-1, ret);
EXPECT_EQ(EACCES, errno);
}

TEST_F(FileCtlTests, tebako_access_absolute_path_no_file)
{
int ret = tebako_access(TEBAKIZE_PATH("no-directory/file.txt"), W_OK);
int ret = tebako_access(TEBAKIZE_PATH("no-directory/file.txt"), X_OK);
EXPECT_EQ(ENOENT, errno);
EXPECT_EQ(-1, ret);
ret = tebako_access(TEBAKIZE_PATH("no-file.txt"), F_OK);
EXPECT_EQ(ENOENT, errno);
EXPECT_EQ(-1, ret);
}
Expand All @@ -75,8 +85,16 @@ TEST_F(FileCtlTests, tebako_access_relative_path)
{
int ret = tebako_chdir(TEBAKIZE_PATH("directory-2"));
EXPECT_EQ(0, ret);
ret = tebako_access("file-in-directory-2.txt", F_OK);
EXPECT_EQ(0, ret);
ret = tebako_access("file-in-directory-2.txt", R_OK);
EXPECT_EQ(0, ret);
ret = tebako_access("file-in-directory-2.txt", W_OK);
EXPECT_EQ(-1, ret);
EXPECT_EQ(EACCES, errno);
ret = tebako_access("file-in-directory-2.txt", X_OK);
EXPECT_EQ(-1, ret);
EXPECT_EQ(EACCES, errno);
}

TEST_F(FileCtlTests, tebako_access_relative_path_no_file)
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.4
0.3.5

0 comments on commit 488c746

Please sign in to comment.