Skip to content

Commit

Permalink
Merge pull request #55 from fischerling/fix-t_dup
Browse files Browse the repository at this point in the history
fix path resolution when creating files
  • Loading branch information
Galfurian committed Mar 8, 2024
2 parents 7462dfe + 2fe1cb3 commit 6ab2086
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions mentos/inc/fs/namei.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#define REMOVE_TRAILING_SLASH 1 << 0
#define FOLLOW_LINKS 1 << 1
#define CREAT_LAST_COMPONENT 1 << 2

/// @brief Resolve the path by following all symbolic links.
/// @param path The path to resolve.
Expand Down
15 changes: 13 additions & 2 deletions mentos/src/fs/namei.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,19 @@ int resolve_path(const char *path, char *buffer, size_t buflen, int flags)
}

vfs_file_t *file;
file = vfs_open_abspath(abspath, O_RDONLY, 0);
if (!file) { return -errno; }
// No permissions are required for path resolution.
// Correct permissions of all path components must be
// checked by the filesystem implementation.
file = vfs_open_abspath(abspath, 0, 0);
if (!file) {
// This is the last path component and we want to create
// it anyway.
if (!sep_after_cur && (flags & CREAT_LAST_COMPONENT)) {
// Just copy the component into the buffer
goto copy_path_component;
}
return -errno;
}

stat_t statbuf;
int ret = vfs_fstat(file, &statbuf);
Expand Down
9 changes: 7 additions & 2 deletions mentos/src/fs/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,13 @@ vfs_file_t *vfs_open(const char *path, int flags, mode_t mode)
{
// Allocate a variable for the path.
char absolute_path[PATH_MAX];
// If the first character is not the '/' then get the absolute path.
int ret = resolve_path(path, absolute_path, sizeof(absolute_path), FOLLOW_LINKS);
// Resolve all symbolic links in the path before opening the file.
int resolve_flags = FOLLOW_LINKS;
// Allow the last component to be non existing when attempting to create it.
if (bitmask_check(flags, O_CREAT)) {
resolve_flags |= CREAT_LAST_COMPONENT;
}
int ret = resolve_path(path, absolute_path, sizeof(absolute_path), resolve_flags);
if (ret < 0) {
pr_err("vfs_open(%s): Cannot resolve path!\n", path);
errno = -ret;
Expand Down

0 comments on commit 6ab2086

Please sign in to comment.