F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Os::FilePathUtils Namespace Reference

Utility for canonicalizing file paths and checking directory containment. More...

Enumerations

enum  Status { VALID, OUTSIDE_SANDBOX, INVALID_PATH, TOO_LONG }
 

Functions

static bool isValidCString (FwSizeType measuredLen, FwSizeType bufferLen)
 
static Status buildAbsolutePath (const char *path, const char *baseDir, char *resolvedOut, FwSizeType resolvedSize)
 
static Status resolveInPlace (char *resolvedOut)
 
Status resolvePath (const char *path, const char *baseDir, char *resolvedOut, FwSizeType resolvedSize)
 Resolve a path by collapsing . and .. segments. More...
 
Status resolvePath (const Fw::ConstStringBase &path, const Fw::ConstStringBase &baseDir, Fw::StringBase &resolvedOut)
 Resolve a path (Fw::StringBase overload) More...
 
Status resolveFromCwd (const char *path, char *resolvedOut, FwSizeType resolvedSize)
 Resolve a path using CWD as the base for relative paths. More...
 
Status checkContainment (const char *resolvedPath, const char *allowedDirectory)
 Check whether an already-resolved path is within an allowed directory. More...
 

Variables

static constexpr FwSizeType MAX_PATH_LENGTH = FileNameStringSize
 Maximum supported path length for resolution buffers. More...
 

Detailed Description

Utility for canonicalizing file paths and checking directory containment.

Provides standalone functions for resolving relative paths (collapsing . and .. segments) and verifying that a resolved path falls within an allowed directory prefix. These utilities can be used independently of SandboxedFile for any path-validation need.

Enumeration Type Documentation

◆ Status

Enumerator
VALID 

Path is valid and within the allowed directory.

OUTSIDE_SANDBOX 

Resolved path falls outside the allowed directory.

INVALID_PATH 

Path is malformed or cannot be resolved.

TOO_LONG 

Combined path length exceeds the output buffer size.

Definition at line 22 of file FilePathUtils.hpp.

Function Documentation

◆ buildAbsolutePath()

static Status Os::FilePathUtils::buildAbsolutePath ( const char *  path,
const char *  baseDir,
char *  resolvedOut,
FwSizeType  resolvedSize 
)
static

Definition at line 22 of file FilePathUtils.cpp.

◆ checkContainment()

Status Os::FilePathUtils::checkContainment ( const char *  resolvedPath,
const char *  allowedDirectory 
)

Check whether an already-resolved path is within an allowed directory.

Both arguments must already be canonical absolute paths (output of resolvePath or resolveFromCwd). Use this when you have already resolved paths and want to check containment without redundant resolution.

Parameters
resolvedPathcanonical absolute path (output of resolvePath)
allowedDirectorycanonical allowed directory prefix (must end with /)
Returns
VALID if contained, OUTSIDE_SANDBOX or INVALID_PATH otherwise

Definition at line 178 of file FilePathUtils.cpp.

◆ isValidCString()

static bool Os::FilePathUtils::isValidCString ( FwSizeType  measuredLen,
FwSizeType  bufferLen 
)
static

Definition at line 16 of file FilePathUtils.cpp.

◆ resolveFromCwd()

Status Os::FilePathUtils::resolveFromCwd ( const char *  path,
char *  resolvedOut,
FwSizeType  resolvedSize 
)

Resolve a path using CWD as the base for relative paths.

Convenience wrapper: relative paths are resolved against the process's current working directory; absolute paths are resolved as-is. Returns INVALID_PATH if CWD cannot be determined.

Parameters
pathinput path to resolve (may be relative or absolute)
resolvedOutoutput buffer for the resolved path
resolvedSizesize of the output buffer
Returns
VALID on success, INVALID_PATH or TOO_LONG on failure

Definition at line 160 of file FilePathUtils.cpp.

◆ resolveInPlace()

static Status Os::FilePathUtils::resolveInPlace ( char *  resolvedOut)
static

Definition at line 77 of file FilePathUtils.cpp.

◆ resolvePath() [1/2]

Status Os::FilePathUtils::resolvePath ( const char *  path,
const char *  baseDir,
char *  resolvedOut,
FwSizeType  resolvedSize 
)

Resolve a path by collapsing . and .. segments.

Performs purely textual path resolution without filesystem access. Handles ./, ../, and redundant / separators. The result is always an absolute path; relative inputs are resolved against the provided base directory.

Parameters
pathinput path to resolve (may be relative or absolute)
baseDirbase directory for resolving relative paths (must be absolute, null-terminated; ignored if path is absolute)
resolvedOutoutput buffer for the resolved path
resolvedSizesize of the output buffer
Returns
VALID on success, INVALID_PATH if the path cannot be resolved, TOO_LONG if the buffer is too small

Definition at line 136 of file FilePathUtils.cpp.

◆ resolvePath() [2/2]

Status Os::FilePathUtils::resolvePath ( const Fw::ConstStringBase path,
const Fw::ConstStringBase baseDir,
Fw::StringBase resolvedOut 
)

Resolve a path (Fw::StringBase overload)

Parameters
pathinput path string
baseDirbase directory for relative paths
resolvedOutoutput string (resized to hold result)
Returns
VALID on success, INVALID_PATH or TOO_LONG on failure

Definition at line 149 of file FilePathUtils.cpp.

Variable Documentation

◆ MAX_PATH_LENGTH

constexpr FwSizeType Os::FilePathUtils::MAX_PATH_LENGTH = FileNameStringSize
static

Maximum supported path length for resolution buffers.

Definition at line 30 of file FilePathUtils.hpp.