|
solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
|
Cross-platform file path manipulation and directory traversal utilities. More...
#include "file.h"#include "macros.h"#include "platform.h"#include <stdbool.h>#include <stddef.h>#include <stdint.h>#include <stdio.h>#include <dirent.h>#include <pwd.h>#include <sys/mman.h>#include <sys/random.h>#include <sys/stat.h>#include <unistd.h>Go to the source code of this file.
Classes | |
| struct | Directory |
Macros | |
| #define | PATH_SEP '/' |
| #define | PATH_SEP_STR "/" |
Typedefs | |
| typedef enum WalkDirOption | WalkDirOption |
| typedef WalkDirOption(* | WalkDirCallback) (const FileAttributes *attr, const char *path, const char *name, void *data) |
Enumerations | |
| enum | WalkDirOption { DirContinue , DirStop , DirSkip , DirError } |
Functions | |
| WARN_UNUSED_RESULT Directory * | dir_open (const char *path) |
| void | dir_close (Directory *dir) |
| WARN_UNUSED_RESULT char * | dir_next (Directory *dir) |
| int | dir_create (const char *path) |
| int | dir_remove (const char *path, bool recursive) |
| int | dir_rename (const char *oldpath, const char *newpath) |
| int | dir_chdir (const char *path) |
| WARN_UNUSED_RESULT char ** | dir_list (const char *path, size_t *count) |
| void | dir_list_with_callback (const char *path, void(*callback)(const char *name)) |
| bool | is_dir (const char *path) |
| bool | is_file (const char *path) |
| bool | is_symlink (const char *path) |
| bool | filepath_makedirs (const char *path) |
| WARN_UNUSED_RESULT char * | get_tempdir (void) |
| WARN_UNUSED_RESULT char * | make_tempfile (void) |
| WARN_UNUSED_RESULT char * | make_tempdir (void) |
| int | dir_walk (const char *path, WalkDirCallback callback, void *data) |
| int | dir_walk_depth_first (const char *path, WalkDirCallback callback, void *data) |
| ssize_t | dir_size (const char *path) |
| bool | path_exists (const char *path) |
| WARN_UNUSED_RESULT char * | get_cwd (void) |
| void | filepath_basename (const char *path, char *basename, size_t size) |
| void | filepath_dirname (const char *path, char *dirname, size_t size) |
| void | filepath_extension (const char *path, char *ext, size_t size) |
| void | filepath_nameonly (const char *path, char *name, size_t size) |
| WARN_UNUSED_RESULT char * | filepath_absolute (const char *path) |
| int | filepath_remove (const char *path) |
| int | filepath_rename (const char *oldpath, const char *newpath) |
| const char * | user_home_dir (void) |
| WARN_UNUSED_RESULT char * | filepath_expanduser (const char *path) |
| bool | filepath_expanduser_buf (const char *path, char *expanded, size_t len) |
| WARN_UNUSED_RESULT char * | filepath_join (const char *path1, const char *path2) |
| bool | filepath_join_buf (const char *path1, const char *path2, char *abspath, size_t len) |
| void | filepath_split (const char *path, char *dir, char *name, size_t dir_size, size_t name_size) |
Cross-platform file path manipulation and directory traversal utilities.
This module provides a unified API for file system operations across Windows and POSIX platforms. All functions handle path separators appropriately for the target platform.
Thread Safety: Individual functions document their thread-safety guarantees. Most functions are thread-safe unless otherwise noted.
Definition in file filepath.h.
| #define PATH_SEP '/' |
Platform-specific directory separator character.
Definition at line 44 of file filepath.h.
| #define PATH_SEP_STR "/" |
Platform-specific directory separator string.
Definition at line 47 of file filepath.h.
| typedef WalkDirOption(* WalkDirCallback) (const FileAttributes *attr, const char *path, const char *name, void *data) |
Callback function type for directory walking.
| attr | File attributes of the current entry. Never NULL. |
| path | Full path to the entry. Valid only during callback invocation. |
| name | Basename of the entry. Valid only during callback invocation. |
| data | User-provided context pointer passed to dir_walk. |
Definition at line 263 of file filepath.h.
| typedef enum WalkDirOption WalkDirOption |
Control options for directory traversal callbacks.
| enum WalkDirOption |
Control options for directory traversal callbacks.
Definition at line 246 of file filepath.h.
| int dir_chdir | ( | const char * | path | ) |
Changes the current working directory of the process.
| path | Path to the new working directory. Must not be NULL. |
Definition at line 498 of file filepath.c.
| void dir_close | ( | Directory * | dir | ) |
Closes a directory handle and releases all associated resources.
| dir | Directory handle to close. Safe to pass NULL. |
Definition at line 175 of file filepath.c.
References Directory::dir, dir_close(), and Directory::path.
Referenced by dir_close(), dir_list(), dir_list_with_callback(), dir_walk(), and dir_walk_depth_first().
| int dir_create | ( | const char * | path | ) |
Creates a new directory with default permissions.
| path | Path of the directory to create. Must not be NULL. |
Definition at line 306 of file filepath.c.
Referenced by filepath_makedirs().
| WARN_UNUSED_RESULT char ** dir_list | ( | const char * | path, |
| size_t * | count | ||
| ) |
Lists all entries in a directory.
| path | Path to the directory. Must not be NULL. |
| count | Pointer to store the number of entries. Must not be NULL. |
Definition at line 507 of file filepath.c.
References dir_close(), dir_next(), and dir_open().
| void dir_list_with_callback | ( | const char * | path, |
| void(*)(const char *name) | callback | ||
| ) |
Iterates directory entries with a callback function.
| path | Path to the directory. Must not be NULL. |
| callback | Function called for each entry (excluding "." and ".."). Must not be NULL. |
Definition at line 563 of file filepath.c.
References dir_close(), dir_next(), and dir_open().
| WARN_UNUSED_RESULT char * dir_next | ( | Directory * | dir | ) |
Reads the next entry in the directory.
| dir | Directory handle returned by dir_open(). Must not be NULL. |
Definition at line 192 of file filepath.c.
References Directory::dir, and dir_next().
Referenced by dir_list(), dir_list_with_callback(), and dir_next().
| WARN_UNUSED_RESULT Directory * dir_open | ( | const char * | path | ) |
Opens a directory for reading.
| path | Path to the directory to open. Must not be NULL or empty. |
Definition at line 118 of file filepath.c.
References Directory::dir, dir_open(), and Directory::path.
Referenced by dir_list(), dir_list_with_callback(), dir_open(), dir_walk(), and dir_walk_depth_first().
| int dir_remove | ( | const char * | path, |
| bool | recursive | ||
| ) |
Removes a directory.
| path | Path to the directory to remove. Must not be NULL. |
| recursive | If true, recursively removes all contents. If false, fails on non-empty directories. |
Definition at line 473 of file filepath.c.
References dir_walk_depth_first().
| int dir_rename | ( | const char * | oldpath, |
| const char * | newpath | ||
| ) |
Renames or moves a directory.
| oldpath | Current path of the directory. Must not be NULL. |
| newpath | New path for the directory. Must not be NULL. |
Definition at line 489 of file filepath.c.
| ssize_t dir_size | ( | const char * | path | ) |
Calculates the total size of all files in a directory tree.
| path | Root directory to measure. Must not be NULL. |
Definition at line 753 of file filepath.c.
References dir_walk().
Referenced by filepath_split().
| int dir_walk | ( | const char * | path, |
| WalkDirCallback | callback, | ||
| void * | data | ||
| ) |
Walks a directory tree in breadth-first order.
| path | Root directory to walk. Must not be NULL. |
| callback | Function called for each entry. Must not be NULL. |
| data | User context pointer passed to callback. May be NULL. |
Recursively walks a directory tree, invoking a callback for each entry.
| path | Starting directory path. |
| callback | Function to call for each directory entry. |
| data | User-provided data passed to callback. |
Definition at line 649 of file filepath.c.
References FileAttributes::attrs, Directory::dir, dir_close(), dir_open(), dir_walk(), DirError, DirSkip, DirStop, FATTR_NONE, filepath_join_buf(), and populate_file_attrs().
Referenced by dir_size(), and dir_walk().
| int dir_walk_depth_first | ( | const char * | path, |
| WalkDirCallback | callback, | ||
| void * | data | ||
| ) |
Walks a directory tree in depth-first post-order.
| path | Root directory to walk. Must not be NULL. |
| callback | Function called for each entry. Must not be NULL. |
| data | User context pointer passed to callback. May be NULL. |
Recursively walks a directory tree depth-first (post-order), invoking a callback for each entry. Files are processed before their containing directories. Useful for operations like deletion where you need to empty a directory before removing it.
| path | Starting directory path. |
| callback | Function to call for each directory entry. |
| data | User-provided data passed to callback. |
Definition at line 398 of file filepath.c.
References FileAttributes::attrs, Directory::dir, dir_close(), dir_open(), dir_walk_depth_first(), DirError, DirStop, FATTR_NONE, filepath_join_buf(), and populate_file_attrs().
Referenced by dir_remove(), and dir_walk_depth_first().
| WARN_UNUSED_RESULT char * filepath_absolute | ( | const char * | path | ) |
Converts a path to an absolute path.
| path | Path to convert (may be relative or absolute). Must not be NULL. |
Definition at line 1015 of file filepath.c.
| void filepath_basename | ( | const char * | path, |
| char * | basename, | ||
| size_t | size | ||
| ) |
Extracts the basename (filename with extension) from a path.
| path | Full path to parse. Must not be NULL. |
| basename | Buffer to store the result. Must not be NULL. |
| size | Size of the basename buffer. |
Definition at line 948 of file filepath.c.
Referenced by filepath_nameonly().
| void filepath_dirname | ( | const char * | path, |
| char * | dirname, | ||
| size_t | size | ||
| ) |
Extracts the directory portion of a path.
| path | Full path to parse. Must not be NULL. |
| dirname | Buffer to store the result. Must not be NULL. |
| size | Size of the dirname buffer. |
Definition at line 963 of file filepath.c.
| WARN_UNUSED_RESULT char * filepath_expanduser | ( | const char * | path | ) |
Expands tilde (~) in a path to the user's home directory.
| path | Path potentially starting with ~. Must not be NULL. |
Definition at line 1079 of file filepath.c.
References PATH_SEP, and user_home_dir().
| bool filepath_expanduser_buf | ( | const char * | path, |
| char * | expanded, | ||
| size_t | len | ||
| ) |
Expands tilde (~) in a path to the user's home directory into a buffer.
| path | Path potentially starting with ~. Must not be NULL. |
| expanded | Buffer to store the expanded path. Must not be NULL. |
| len | Size of the expanded buffer. |
Definition at line 1118 of file filepath.c.
References user_home_dir().
| void filepath_extension | ( | const char * | path, |
| char * | ext, | ||
| size_t | size | ||
| ) |
Extracts the file extension including the leading dot.
| path | Full path to parse. Must not be NULL. |
| ext | Buffer to store the result. Must not be NULL. |
| size | Size of the ext buffer. |
Definition at line 982 of file filepath.c.
| WARN_UNUSED_RESULT char * filepath_join | ( | const char * | path1, |
| const char * | path2 | ||
| ) |
Joins two path components with the platform-specific separator.
| path1 | First path component. Must not be NULL. |
| path2 | Second path component. Must not be NULL. |
Definition at line 1156 of file filepath.c.
Referenced by make_tempdir(), and make_tempfile().
| bool filepath_join_buf | ( | const char * | path1, |
| const char * | path2, | ||
| char * | abspath, | ||
| size_t | len | ||
| ) |
Joins two path components into a provided buffer.
| path1 | First path component. Must not be NULL. |
| path2 | Second path component. Must not be NULL. |
| abspath | Buffer to store the result. Must not be NULL. |
| len | Size of the abspath buffer. |
Definition at line 1177 of file filepath.c.
Referenced by dir_walk(), and dir_walk_depth_first().
| bool filepath_makedirs | ( | const char * | path | ) |
Creates a directory and all necessary parent directories.
| path | Path to create. Must not be NULL. |
Definition at line 767 of file filepath.c.
References dir_create().
| void filepath_nameonly | ( | const char * | path, |
| char * | name, | ||
| size_t | size | ||
| ) |
Extracts the filename without extension.
| path | Full path to parse. Must not be NULL. |
| name | Buffer to store the result. Must not be NULL. |
| size | Size of the name buffer. |
Definition at line 995 of file filepath.c.
References filepath_basename().
| int filepath_remove | ( | const char * | path | ) |
Deletes a file or empty directory.
| path | Path to remove. Must not be NULL. |
Definition at line 1047 of file filepath.c.
| int filepath_rename | ( | const char * | oldpath, |
| const char * | newpath | ||
| ) |
Renames or moves a file or directory.
| oldpath | Current path. Must not be NULL. |
| newpath | New path. Must not be NULL. |
Definition at line 1061 of file filepath.c.
| void filepath_split | ( | const char * | path, |
| char * | dir, | ||
| char * | name, | ||
| size_t | dir_size, | ||
| size_t | name_size | ||
| ) |
Splits a file path into directory and basename components.
| path | Path to split. Must not be NULL. |
| dir | Buffer to store the directory portion. Must not be NULL. |
| name | Buffer to store the basename. Must not be NULL. |
| dir_size | Size of the dir buffer. |
| name_size | Size of the name buffer. |
Definition at line 1204 of file filepath.c.
References dir_size().
| WARN_UNUSED_RESULT char * get_cwd | ( | void | ) |
Returns the current working directory path.
Definition at line 1037 of file filepath.c.
| WARN_UNUSED_RESULT char * get_tempdir | ( | void | ) |
Returns the path to the system's temporary directory.
Definition at line 809 of file filepath.c.
References GETENV.
Referenced by make_tempdir(), and make_tempfile().
| bool is_dir | ( | const char * | path | ) |
Checks if a path refers to a directory.
| path | Path to check. Must not be NULL. |
Definition at line 583 of file filepath.c.
| bool is_file | ( | const char * | path | ) |
Checks if a path refers to a regular file.
| path | Path to check. Must not be NULL. |
Definition at line 604 of file filepath.c.
| bool is_symlink | ( | const char * | path | ) |
Checks if a path is a symbolic link.
| path | Path to check. Must not be NULL. |
Definition at line 625 of file filepath.c.
| WARN_UNUSED_RESULT char * make_tempdir | ( | void | ) |
Creates a temporary directory with a unique name.
Definition at line 887 of file filepath.c.
References filepath_join(), and get_tempdir().
| WARN_UNUSED_RESULT char * make_tempfile | ( | void | ) |
Creates a temporary file with a unique name.
Definition at line 844 of file filepath.c.
References filepath_join(), and get_tempdir().
| bool path_exists | ( | const char * | path | ) |
Checks if a path exists in the filesystem.
| path | Path to check. Must not be NULL. |
Definition at line 926 of file filepath.c.
| const char * user_home_dir | ( | void | ) |
Returns the user's home directory path.
Definition at line 1070 of file filepath.c.
References GETENV.
Referenced by filepath_expanduser(), and filepath_expanduser_buf().