PatchworkOS
Loading...
Searching...
No Matches

Unique location in the filesystem. More...

Data Structures

struct  path_t
 Path structure. More...
 
struct  pathname_t
 Pathname structure. More...
 

Macros

#define PATH_DEFER(path)   __attribute__((cleanup(path_defer_cleanup))) path_t* CONCAT(i, __COUNTER__) = (path)
 Defer path put.
 
#define PATH_VALID_CHAR(ch)    (strchr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-. ()[]{}~!@#$%^&?',;=+", (ch)))
 Check if a char is valid.
 
#define PATH_HANDLE_DOTDOT_MAX_ITER   1000
 Maximum iterations to handle .. in a path.
 
#define PATHNAME_IS_VALID(pathname)   ((pathname) != NULL && (pathname)->isValid)
 Check if a pathname is valid.
 
#define PATHNAME(string)
 Helper to create a pathname.
 
#define PATH_EMPTY
 Helper to create an empty path.
 
#define PATH_CREATE(inMount, inDentry)
 Helper to create a path.
 

Enumerations

enum  path_flags_t {
  PATH_NONE = 0 ,
  PATH_NONBLOCK = 1 << 0 ,
  PATH_APPEND = 1 << 1 ,
  PATH_CREATE = 1 << 2 ,
  PATH_EXCLUSIVE = 1 << 3 ,
  PATH_TRUNCATE = 1 << 4 ,
  PATH_DIRECTORY = 1 << 5 ,
  PATH_RECURSIVE = 1 << 6 ,
  PATH_FLAGS_AMOUNT = 7
}
 Path flags. More...
 
enum  walk_flags_t {
  WALK_NONE = 0 ,
  WALK_NEGATIVE_IS_OK = 1 << 0
}
 Flags for walking a path. More...
 

Functions

void path_flags_init (void)
 Initialize path flags resolution.
 
uint64_t pathname_init (pathname_t *pathname, const char *string)
 Initialize a pathname.
 
void path_set (path_t *path, mount_t *mount, dentry_t *dentry)
 Set a path.
 
void path_copy (path_t *dest, const path_t *src)
 Copy a path.
 
void path_put (path_t *path)
 Put a path.
 
uint64_t path_walk_single_step (path_t *outPath, const path_t *parent, const char *name, walk_flags_t flags, namespace_t *ns)
 Traverse a single component from a parent path.
 
uint64_t path_walk (path_t *outPath, const pathname_t *pathname, const path_t *start, walk_flags_t flags, namespace_t *ns)
 Traverse a pathname from a specified starting path.
 
uint64_t path_walk_parent (path_t *outPath, const pathname_t *pathname, const path_t *start, char *outLastName, walk_flags_t flags, namespace_t *ns)
 Traverse a pathname to its parent and get the last component name.
 
uint64_t path_to_name (const path_t *path, pathname_t *pathname)
 Convert a path to a pathname.
 
static void path_defer_cleanup (path_t **path)
 

Detailed Description

Unique location in the filesystem.

A path is a single unique location in the filesystem hierarchy. It consists of a mount and a dentry. The mount is the filesystem that the path is in and the dentry is the actual location in that filesystem.

Note how just a dentry is not enough to uniquely identify a location in the filesystem, this is because of mountpoints. A dentry can exist in multiple places if its part of a filesystem that has been mounted in multiple places.

Flags

Paths can have flags appended to them which is how we handle general file flags. Each flag starts with : and the same path can contain multiples of different or the same flag, for example /path/to/file:create:create:nonblock.

Included is a list of all available flags:

Macro Definition Documentation

◆ PATH_CREATE

#define PATH_CREATE (   inMount,
  inDentry 
)
Value:
(path_t) \
{ \
.mount = REF(inMount), .dentry = REF(inDentry), \
}
#define REF(ptr)
Increment reference count.
Definition ref.h:65
Path structure.
Definition path.h:110

Helper to create a path.

Parameters
inMountThe mount of the path.
inDentryThe dentry of the path.
Returns
The created path.

Definition at line 195 of file path.h.

◆ PATH_DEFER

#define PATH_DEFER (   path)    __attribute__((cleanup(path_defer_cleanup))) path_t* CONCAT(i, __COUNTER__) = (path)

Defer path put.

This macro will call path_put() on the given path when it goes out of scope.

Parameters
pathThe path to defer.

Definition at line 53 of file path.h.

◆ PATH_EMPTY

#define PATH_EMPTY
Value:
(path_t) \
{ \
.mount = NULL, .dentry = NULL \
}
#define NULL
Pointer error value.
Definition NULL.h:23

Helper to create an empty path.

Its important to always use this as some functions, for example path_copy(), will deref the existing mount and dentry in the path.

Returns
An empty path.

Definition at line 182 of file path.h.

◆ PATH_HANDLE_DOTDOT_MAX_ITER

#define PATH_HANDLE_DOTDOT_MAX_ITER   1000

Maximum iterations to handle .. in a path.

This is to prevent infinite loops in case of a corrupted filesystem.

Definition at line 74 of file path.h.

◆ PATH_VALID_CHAR

#define PATH_VALID_CHAR (   ch)     (strchr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-. ()[]{}~!@#$%^&?',;=+", (ch)))

Check if a char is valid.

A valid char is one of the following ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-. ()[]{}~!#$%^&?’,;=+`.

TODO: Replace with array lookup.

Parameters
chThe char to check.
Returns
true if the char is valid, false otherwise.

Definition at line 66 of file path.h.

◆ PATHNAME

#define PATHNAME (   string)
Value:
({ \
pathname_t* pathname = alloca(sizeof(pathname_t)); \
pathname_init(pathname, string); \
pathname; \
})
#define alloca(size)
Definition alloca.h:11
Pathname structure.
Definition path.h:122

Helper to create a pathname.

This macro will create a pathname on the stack and initialize it with the given string.

This is also the reason we have the isValid flag in the pathname_t structure, to be able to check if this macro failed without having to return an error code, streamlining the code a bit.

Parameters
stringThe string to initialize the pathname with.
Returns
The initialized pathname.

Definition at line 156 of file path.h.

◆ PATHNAME_IS_VALID

#define PATHNAME_IS_VALID (   pathname)    ((pathname) != NULL && (pathname)->isValid)

Check if a pathname is valid.

A valid pathname is not NULL and has its isValid flag set to true.

This flag is set in pathname_init().

Parameters
pathnameThe pathname to check.
Returns
true if the pathname is valid, false otherwise.

Definition at line 143 of file path.h.

Enumeration Type Documentation

◆ path_flags_t

Path flags.

Used to store parsed flags.

Enumerator
PATH_NONE 
PATH_NONBLOCK 
PATH_APPEND 
PATH_CREATE 
PATH_EXCLUSIVE 
PATH_TRUNCATE 
PATH_DIRECTORY 
PATH_RECURSIVE 
PATH_FLAGS_AMOUNT 

Definition at line 82 of file path.h.

◆ walk_flags_t

Flags for walking a path.

Enumerator
WALK_NONE 

No flags.

WALK_NEGATIVE_IS_OK 

If a negative dentry is ok, if not specified then it is considered an error.

Definition at line 99 of file path.h.

Function Documentation

◆ path_copy()

void path_copy ( path_t dest,
const path_t src 
)

Copy a path.

Will deref the existing mount and dentry in the destination path if they are not NULL.

Parameters
destThe destination path.
srcThe source path.

Definition at line 232 of file path.c.

References path_t::dentry, DEREF, path_t::mount, NULL, and REF.

Referenced by file_new(), namespace_traverse_mount(), path_to_name(), path_walk(), path_walk_parent(), path_walk_single_step(), vfs_create(), vfs_ctx_get_cwd(), vfs_ctx_init(), vfs_ctx_set_cwd(), vfs_open_lookup(), and vfs_walk_parent_and_child().

◆ path_defer_cleanup()

static void path_defer_cleanup ( path_t **  path)
inlinestatic

Definition at line 282 of file path.h.

References NULL, and path_put().

◆ path_flags_init()

void path_flags_init ( void  )

Initialize path flags resolution.

Definition at line 66 of file path.c.

References ERR, flagEntries, flagMap, flagShortMap, map_entry_init(), map_init(), map_insert(), map_key_buffer(), map_key_string(), NULL, panic(), and PATH_FLAGS_AMOUNT.

Referenced by vfs_init().

◆ path_put()

void path_put ( path_t path)

Put a path.

Will deref the mount and dentry in the path if they are not NULL.

Parameters
pathThe path to put.

Definition at line 257 of file path.c.

References path_t::dentry, DEREF, path_t::mount, and NULL.

Referenced by file_free(), path_defer_cleanup(), path_walk(), process_dir_init(), process_init(), socket_new(), SYSCALL_DEFINE(), sysfs_mount_new(), vfs_ctx_deinit(), and vfs_ctx_set_cwd().

◆ path_set()

void path_set ( path_t path,
mount_t mount,
dentry_t dentry 
)

Set a path.

Will deref the existing mount and dentry in the path if they are not NULL.

Parameters
pathThe path to set.
mountThe mount to set.
dentryThe dentry to set.

Definition at line 207 of file path.c.

References path_t::dentry, DEREF, path_t::mount, mount, NULL, and REF.

Referenced by namespace_get_root_path(), namespace_traverse_mount(), path_to_name(), path_walk_single_step(), socket_family_get_dir(), and sysfs_mount_new().

◆ path_to_name()

uint64_t path_to_name ( const path_t path,
pathname_t pathname 
)

Convert a path to a pathname.

The resulting pathname will be absolute.

Parameters
pathThe path to convert.
pathnameThe output pathname.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 583 of file path.c.

References path_t::dentry, DENTRY_IS_ROOT, EINVAL, ENAMETOOLONG, ERR, errno, pathname_t::flags, pathname_t::isValid, MAX_NAME, MAX_PATH, memcpy(), memmove(), memset(), path_t::mount, mount_t::mountpoint, dentry_t::name, NULL, dentry_t::parent, mount_t::parent, path_copy(), PATH_DEFER, PATH_EMPTY, PATH_NONE, path_set(), pathname_t::string, and strnlen_s().

Referenced by process_cwd_read().

◆ path_walk()

uint64_t path_walk ( path_t outPath,
const pathname_t pathname,
const path_t start,
walk_flags_t  flags,
namespace_t ns 
)

Traverse a pathname from a specified starting path.

Parameters
outPathThe output path.
pathnameThe patname to traverse to.
startThe path to start at if the pathname is relative.
flagsFlags for the path walk.
nsThe namespace to access mountpoints.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 376 of file path.c.

References assert, atomic_load, path_t::dentry, DENTRY_NEGATIVE, EBADFLAG, EINVAL, ENAMETOOLONG, ENOENT, ERR, errno, MAX_NAME, memcpy(), path_t::mount, dentry_t::mountCount, namespace_get_root_path(), namespace_traverse_mount(), next, NULL, path_copy(), PATH_DEFER, PATH_EMPTY, path_handle_dotdot(), path_put(), PATH_VALID_CHAR, path_walk_single_step(), PATHNAME_IS_VALID, start(), strcmp(), pathname_t::string, and WALK_NEGATIVE_IS_OK.

Referenced by path_walk_parent(), and vfs_walk().

◆ path_walk_parent()

uint64_t path_walk_parent ( path_t outPath,
const pathname_t pathname,
const path_t start,
char *  outLastName,
walk_flags_t  flags,
namespace_t ns 
)

Traverse a pathname to its parent and get the last component name.

Parameters
outPathThe output parent path.
pathnameThe pathname to traverse.
startThe path to start at if the pathname is relative.
outLastNameThe output last component name.
flagsFlags for the path walk.
nsThe namespace to access mountpoints.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 511 of file path.c.

References EINVAL, ENOENT, ERR, errno, MAX_NAME, MAX_PATH, memset(), NULL, path_copy(), path_walk(), pathname_init(), PATHNAME_IS_VALID, start(), strcmp(), pathname_t::string, strncpy(), strnlen_s(), and strrchr().

Referenced by vfs_walk_parent().

◆ path_walk_single_step()

uint64_t path_walk_single_step ( path_t outPath,
const path_t parent,
const char *  name,
walk_flags_t  flags,
namespace_t ns 
)

Traverse a single component from a parent path.

Parameters
outPathThe output path.
parentThe parent path.
nameThe name of the child dentry.
flagsFlags for the path walk.
nsThe namespace to access mountpoints.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 329 of file path.c.

References atomic_load, path_t::dentry, DENTRY_NEGATIVE, DEREF_DEFER, EINVAL, ENOENT, ERR, errno, path_t::mount, dentry_t::mountCount, namespace_traverse_mount(), next, NULL, path_copy(), PATH_DEFER, PATH_EMPTY, path_set(), vfs_get_or_lookup_dentry(), vfs_is_name_valid(), and WALK_NEGATIVE_IS_OK.

Referenced by path_walk(), and vfs_walk_parent_and_child().

◆ pathname_init()

uint64_t pathname_init ( pathname_t pathname,
const char *  string 
)

Initialize a pathname.

If the string is invalid, it will error and set pathname->isValid to false.

Parameters
pathnameThe pathname to initialize.
stringThe string to initialize the pathname with.
Returns
On success, 0. On failure, ERR and errno is set.

Definition at line 100 of file path.c.

References EBADFLAG, EINVAL, ENAMETOOLONG, ERR, errno, path_flag_entry_t::flag, pathname_t::flags, isalnum(), pathname_t::isValid, MAX_NAME, MAX_PATH, memset(), NULL, path_flags_get(), PATH_NONE, PATH_VALID_CHAR, pathname_t::string, and strnlen_s().

Referenced by loader_spawn(), path_walk_parent(), and thread_copy_from_user_pathname().