Fray Engine
Loading...
Searching...
No Matches
fray::files::FileManager Class Reference

Owns one working directory, its database name and a nonblocking file lock. Project workspaces additionally have archive paths and packaging operations. Operations run on the calling thread; the class has no internal mutex and callers must serialize access. State managers must detach before saving, closing or destroying this manager. More...

#include <file_manager.hpp>

Classes

struct  ProjectLock
 Owns the operating-system handle and path of a project lock file. More...

Public Member Functions

 FileManager ()=default
 Constructs a closed file manager.
 FileManager (const FileManager &)=delete
 Copy construction is disabled; project locks have a single owner.
FileManager & operator= (const FileManager &)=delete
 Copy assignment is disabled; project locks have a single owner.
 ~FileManager ()
 Invalidates the lifetime sentinel and releases project locks. Logs if state managers remain attached; closes lock handles if normal reset fails.
bool isOpen () const
 Returns whether a working directory is associated with this manager.
const std::string & projectName () const
 Returns a borrowed reference to the current project or database name.
std::string databaseFileName () const
 Returns the database filename within the open directory.
bool requiresExistingDatabase () const
 Reports whether state opens must require an existing database.
const std::filesystem::path & workingDirectory () const
 Returns a borrowed reference to the working folder path.
const std::filesystem::path & archivePath () const
 Returns a borrowed reference to the archive path.
F_err openDirectory (const std::filesystem::path &directory, const std::string &name)
 Opens an existing directory for a named state database without project archive operations.
F_err openDatabase (const std::filesystem::path &path)
 Opens an existing named database file without treating its directory as a project.
F_err filePath (const std::string &name, std::filesystem::path &result) const
 Resolves a safe relative project path without accepting symbolic-link components.
F_err open (const std::string &path, ConflictChoice choice=ConflictChoice::Ask, OpenMode mode=OpenMode::OpenOrCreate, bool deferConflictCommit=false)
 Opens or creates a project workspace and acquires its nonblocking project lock. May extract an archive or rename the unselected representation and its database on conflict. A deferred conflict retains rollback state and an extra lock for the owner to resolve.
F_err save ()
 Packages the workspace into its .fry archive without deleting the workspace. Requires an open project and no attached state managers.
F_err close ()
 Releases locks and forgets the open project without saving or deleting its workspace. Requires no attached state managers.

Private Member Functions

bool lockProject (const std::filesystem::path &archive, ProjectLock &lock)
 Acquires a nonblocking exclusive lock on the archive path’s .lock file. Creates the lock file if needed; rejects non-regular, nonempty or replaced lock files where checked.
void restoreLock ()
 Attempts to recreate the primary lock if its file was renamed or removed. On POSIX, closes the stale descriptor before attempting a nonblocking reacquisition; logs failure.
bool reset ()
 Releases both locks and clears the current project and conflict paths.
bool rollbackConflict ()
 Attempts to restore an unselected conflict representation and its database names.
bool commitConflict ()
 Finalizes a conflict rename by releasing its extra lock and clearing rollback state.
bool snapshotDatabase (const std::string &stem)
 Copies an existing workspace database into snapshots with a timestamp and collision suffix. Call while the database is closed; a missing database is not an error.
void removeArchive ()
 Removes the existing archive while the project is open, logging any failure. A later save recreates the archive.
bool acquireRename (const std::string &newName, ProjectLock &lock)
 Locks the proposed archive path after checking that the archive does not exist. Attempts to restore the current lock first; locking does not wait for another owner.
void commitRename (const std::string &newName, const std::filesystem::path &workspace, ProjectLock &lock)
 Transfers a new lock and updates the project name and paths, then releases the old lock.
bool extract ()
 Extracts the archive into a newly created workspace, rejecting unsafe or unsupported entries. Attempts to remove the incomplete workspace on failure.
F_err removeWorkingDirectory ()
 Deletes the workspace tree after checking it does not contain protected locations. Requires an open project and no attached state managers; does not release project locks.
void attach ()
 Registers an attached state manager; callers must balance this with detach().
void detach ()
 Unregisters an attached state manager; requires a prior unmatched attach().
std::weak_ptr< int > lifetime () const
 Returns a weak lifetime sentinel that expires when this manager is destroyed.

Static Private Member Functions

static bool safeRelative (const std::string &name)
 Checks whether a project entry name is a safe relative path.
static bool lockIntact (const ProjectLock &lock)
 Checks that a POSIX lock descriptor still names the same empty regular file as its path.
static bool unlockProject (ProjectLock &lock)
 Removes and releases an owned project lock. On POSIX, only unlinks the path if it still identifies the locked file.
static bool pathExists (const std::filesystem::path &path, bool &exists)
 Inspects a path without following its final symbolic link.
static bool unusedPath (const std::filesystem::path &parent, const std::string &stem, std::filesystem::path &output)
 Finds an unused numbered folder name with no matching .fry archive.
static bool renameExclusive (const std::filesystem::path &from, const std::filesystem::path &to)
 Renames a filesystem entry without replacing an existing destination.
static bool renameFolderDatabase (const std::filesystem::path &folder, const std::string &oldName, const std::string &newName)
 Renames a folder’s database and SQLite sidecars without overwriting destinations. Attempts to reverse completed renames if a later rename fails.
static bool renameArchiveDatabase (const std::filesystem::path &path, const std::string &oldName, const std::string &newName)
 Renames database and SQLite sidecar entries within an archive. Unreadable non-ZIP, inconsistent or truncated archives are left unchanged and treated as success.

Private Attributes

std::filesystem::path _archive
 Path of the project archive associated with the workspace.
std::filesystem::path _workspace
 Working folder path; empty when no project is open.
std::string _name
 Current project name and database stem.
std::string _databaseFile
 Explicit database filename for a directory-backed store; empty for a project.
bool _existingDatabase = false
 Whether opening state must not create a missing database file.
bool _archiveExists = false
 Whether the archive is expected to exist.
bool _extracted = false
 Whether the workspace was extracted from an archive.
ProjectLock _lock
 Lock for the selected project archive path.
ProjectLock _extraLock
 Temporary lock for the renamed, unselected conflict representation.
std::filesystem::path _conflictSource
 Original path of an unselected representation awaiting conflict commit or rollback.
std::filesystem::path _conflictDestination
 Renamed path of the unselected representation awaiting resolution.
std::string _conflictName
 New database stem of the unselected representation.
bool _conflictFolder = false
 Whether the unselected representation is a folder rather than an archive.
unsigned _users = 0
 Number of attached state managers; nonzero prevents save and close.
std::shared_ptr< int > _lifetime = std::make_shared<int>(0)
 Lifetime sentinel observed weakly by attached state managers.

Friends

class fray::state::StateManager
class fray::ProjectManager

Detailed Description

Owns one working directory, its database name and a nonblocking file lock. Project workspaces additionally have archive paths and packaging operations. Operations run on the calling thread; the class has no internal mutex and callers must serialize access. State managers must detach before saving, closing or destroying this manager.

Definition at line 71 of file file_manager.hpp.

Constructor & Destructor Documentation

◆ FileManager() [1/2]

fray::files::FileManager::FileManager ( )
default

Constructs a closed file manager.

◆ FileManager() [2/2]

fray::files::FileManager::FileManager ( const FileManager & )
delete

Copy construction is disabled; project locks have a single owner.

◆ ~FileManager()

fray::files::FileManager::~FileManager ( )
inline

Invalidates the lifetime sentinel and releases project locks. Logs if state managers remain attached; closes lock handles if normal reset fails.

Definition at line 804 of file file_manager.hpp.

Member Function Documentation

◆ acquireRename()

bool fray::files::FileManager::acquireRename ( const std::string & newName,
ProjectLock & lock )
inlineprivate

Locks the proposed archive path after checking that the archive does not exist. Attempts to restore the current lock first; locking does not wait for another owner.

Parameters
newNameProposed project name.
lockReceives the new lock on success.
Returns
True if acquired; false if the path exists, cannot be inspected or cannot be locked.

Definition at line 449 of file file_manager.hpp.

◆ archivePath()

const std::filesystem::path & fray::files::FileManager::archivePath ( ) const
inline

Returns a borrowed reference to the archive path.

Definition at line 853 of file file_manager.hpp.

◆ attach()

void fray::files::FileManager::attach ( )
inlineprivate

Registers an attached state manager; callers must balance this with detach().

Definition at line 1312 of file file_manager.hpp.

◆ close()

F_err fray::files::FileManager::close ( )
inline

Releases locks and forgets the open project without saving or deleting its workspace. Requires no attached state managers.

Returns
NONE on success; FAILED_CLOSE_PROJECT if closed, in use or lock release fails.

Definition at line 1231 of file file_manager.hpp.

◆ commitConflict()

bool fray::files::FileManager::commitConflict ( )
inlineprivate

Finalizes a conflict rename by releasing its extra lock and clearing rollback state.

Returns
True on success; false if the extra lock cannot be released.

Definition at line 365 of file file_manager.hpp.

◆ commitRename()

void fray::files::FileManager::commitRename ( const std::string & newName,
const std::filesystem::path & workspace,
ProjectLock & lock )
inlineprivate

Transfers a new lock and updates the project name and paths, then releases the old lock.

Parameters
newNameNew project name.
workspaceNew working folder path.
lockNew lock, consumed and cleared by this call.

Definition at line 472 of file file_manager.hpp.

◆ databaseFileName()

std::string fray::files::FileManager::databaseFileName ( ) const
inline

Returns the database filename within the open directory.

Returns
Explicit directory-backed filename, or the project name with a SQLite extension.

Definition at line 837 of file file_manager.hpp.

◆ detach()

void fray::files::FileManager::detach ( )
inlineprivate

Unregisters an attached state manager; requires a prior unmatched attach().

Definition at line 1316 of file file_manager.hpp.

◆ extract()

bool fray::files::FileManager::extract ( )
inlineprivate

Extracts the archive into a newly created workspace, rejecting unsafe or unsupported entries. Attempts to remove the incomplete workspace on failure.

Returns
True on successful extraction; false on ZIP, validation or filesystem failure.

Definition at line 695 of file file_manager.hpp.

◆ filePath()

F_err fray::files::FileManager::filePath ( const std::string & name,
std::filesystem::path & result ) const
inline

Resolves a safe relative project path without accepting symbolic-link components.

Parameters
nameRelative project entry name.
resultReceives the resolved path on success; the entry need not exist.
Returns
NONE on success; FAILED_OPEN_PROJECT if closed, invalid or inspection fails.

Definition at line 932 of file file_manager.hpp.

◆ isOpen()

bool fray::files::FileManager::isOpen ( ) const
inline

Returns whether a working directory is associated with this manager.

Definition at line 828 of file file_manager.hpp.

◆ lifetime()

std::weak_ptr< int > fray::files::FileManager::lifetime ( ) const
inlineprivate

Returns a weak lifetime sentinel that expires when this manager is destroyed.

Definition at line 1320 of file file_manager.hpp.

◆ lockIntact()

bool fray::files::FileManager::lockIntact ( const ProjectLock & lock)
inlinestaticprivate

Checks that a POSIX lock descriptor still names the same empty regular file as its path.

Parameters
lockLock to inspect.
Returns
True if the descriptor and path remain valid and match.

Definition at line 240 of file file_manager.hpp.

◆ lockProject()

bool fray::files::FileManager::lockProject ( const std::filesystem::path & archive,
ProjectLock & lock )
inlineprivate

Acquires a nonblocking exclusive lock on the archive path’s .lock file. Creates the lock file if needed; rejects non-regular, nonempty or replaced lock files where checked.

Parameters
archiveArchive path whose extension is replaced with .lock.
lockReceives the owned handle and path on success.
Returns
True if acquired; false on an open, validation or locking failure.

Definition at line 183 of file file_manager.hpp.

◆ open()

F_err fray::files::FileManager::open ( const std::string & path,
ConflictChoice choice = ConflictChoice::Ask,
OpenMode mode = OpenMode::OpenOrCreate,
bool deferConflictCommit = false )
inline

Opens or creates a project workspace and acquires its nonblocking project lock. May extract an archive or rename the unselected representation and its database on conflict. A deferred conflict retains rollback state and an extra lock for the owner to resolve.

Parameters
pathArchive or folder path identifying the project.
choiceWhich representation to select when both exist, or Ask to report a conflict.
modeExistence and conflict requirements for this open.
deferConflictCommitWhether to retain conflict rollback state and its lock.
Returns
NONE on success; PROJECT_CONFLICT for unresolved conflicts; PROJECT_NOT_FOUND when required input is missing; PROJECT_BOTH_EXIST, PROJECT_ARCHIVE_EXISTS or PROJECT_FOLDER_EXISTS for CreateNew collisions; FAILED_OPEN_PROJECT for invalid paths, lock or filesystem failures.

Definition at line 970 of file file_manager.hpp.

◆ openDatabase()

F_err fray::files::FileManager::openDatabase ( const std::filesystem::path & path)
inline

Opens an existing named database file without treating its directory as a project.

Parameters
pathExisting regular database file; symbolic links are rejected.
Returns
NONE on success, otherwise FAILED_OPEN_PROJECT.

Definition at line 892 of file file_manager.hpp.

◆ openDirectory()

F_err fray::files::FileManager::openDirectory ( const std::filesystem::path & directory,
const std::string & name )
inline

Opens an existing directory for a named state database without project archive operations.

Parameters
directoryExisting directory in which to store the database.
nameDatabase stem and lock name; must be a single safe path component.
Returns
NONE on success, otherwise FAILED_OPEN_PROJECT.

Definition at line 861 of file file_manager.hpp.

◆ operator=()

FileManager & fray::files::FileManager::operator= ( const FileManager & )
delete

Copy assignment is disabled; project locks have a single owner.

◆ pathExists()

bool fray::files::FileManager::pathExists ( const std::filesystem::path & path,
bool & exists )
inlinestaticprivate

Inspects a path without following its final symbolic link.

Parameters
pathPath to inspect.
existsReceives whether an entry exists on success.
Returns
True if inspected or absent; false on another filesystem error.

Definition at line 493 of file file_manager.hpp.

◆ projectName()

const std::string & fray::files::FileManager::projectName ( ) const
inline

Returns a borrowed reference to the current project or database name.

Definition at line 832 of file file_manager.hpp.

◆ removeArchive()

void fray::files::FileManager::removeArchive ( )
inlineprivate

Removes the existing archive while the project is open, logging any failure. A later save recreates the archive.

Definition at line 429 of file file_manager.hpp.

◆ removeWorkingDirectory()

F_err fray::files::FileManager::removeWorkingDirectory ( )
inlineprivate

Deletes the workspace tree after checking it does not contain protected locations. Requires an open project and no attached state managers; does not release project locks.

Returns
NONE on success; FAILED_CLOSE_PROJECT if unsafe, in use, closed or removal fails.

Definition at line 1247 of file file_manager.hpp.

◆ renameArchiveDatabase()

bool fray::files::FileManager::renameArchiveDatabase ( const std::filesystem::path & path,
const std::string & oldName,
const std::string & newName )
inlinestaticprivate

Renames database and SQLite sidecar entries within an archive. Unreadable non-ZIP, inconsistent or truncated archives are left unchanged and treated as success.

Parameters
pathArchive to update.
oldNameExisting entry stem.
newNameDestination entry stem.
Returns
True if updated or no update is needed; false on other ZIP errors or collisions.

Definition at line 622 of file file_manager.hpp.

◆ renameExclusive()

bool fray::files::FileManager::renameExclusive ( const std::filesystem::path & from,
const std::filesystem::path & to )
inlinestaticprivate

Renames a filesystem entry without replacing an existing destination.

Parameters
fromExisting source path.
toDestination path, which must not exist.
Returns
True on success; false on failure or an unsupported platform.

Definition at line 543 of file file_manager.hpp.

◆ renameFolderDatabase()

bool fray::files::FileManager::renameFolderDatabase ( const std::filesystem::path & folder,
const std::string & oldName,
const std::string & newName )
inlinestaticprivate

Renames a folder’s database and SQLite sidecars without overwriting destinations. Attempts to reverse completed renames if a later rename fails.

Parameters
folderFolder containing the files.
oldNameExisting database stem.
newNameDestination database stem.
Returns
True if all required renames succeed; false on inspection, collision or rename failure.

Definition at line 575 of file file_manager.hpp.

◆ requiresExistingDatabase()

bool fray::files::FileManager::requiresExistingDatabase ( ) const
inline

Reports whether state opens must require an existing database.

Returns
True for a file selected with openDatabase.

Definition at line 845 of file file_manager.hpp.

◆ reset()

bool fray::files::FileManager::reset ( )
inlineprivate

Releases both locks and clears the current project and conflict paths.

Returns
True on success; false if a lock cannot be released, leaving remaining state uncleared.

Definition at line 311 of file file_manager.hpp.

◆ restoreLock()

void fray::files::FileManager::restoreLock ( )
inlineprivate

Attempts to recreate the primary lock if its file was renamed or removed. On POSIX, closes the stale descriptor before attempting a nonblocking reacquisition; logs failure.

Definition at line 255 of file file_manager.hpp.

◆ rollbackConflict()

bool fray::files::FileManager::rollbackConflict ( )
inlineprivate

Attempts to restore an unselected conflict representation and its database names.

Returns
True if nothing needed restoring or rollback succeeded; false on rename failure.

Definition at line 332 of file file_manager.hpp.

◆ safeRelative()

bool fray::files::FileManager::safeRelative ( const std::string & name)
inlinestaticprivate

Checks whether a project entry name is a safe relative path.

Parameters
nameEntry name to validate.
Returns
True if nonempty, rootless and free of rejected components or characters.

Definition at line 161 of file file_manager.hpp.

◆ save()

F_err fray::files::FileManager::save ( )
inline

Packages the workspace into its .fry archive without deleting the workspace. Requires an open project and no attached state managers.

Returns
NONE on success; FAILED_SAVE_PROJECT if closed, in use or archiving fails.

Definition at line 1153 of file file_manager.hpp.

◆ snapshotDatabase()

bool fray::files::FileManager::snapshotDatabase ( const std::string & stem)
inlineprivate

Copies an existing workspace database into snapshots with a timestamp and collision suffix. Call while the database is closed; a missing database is not an error.

Parameters
stemDatabase filename stem.
Returns
True if absent or copied; false on inspection, directory creation or copy failure.

Definition at line 381 of file file_manager.hpp.

◆ unlockProject()

bool fray::files::FileManager::unlockProject ( ProjectLock & lock)
inlinestaticprivate

Removes and releases an owned project lock. On POSIX, only unlinks the path if it still identifies the locked file.

Parameters
lockLock to release; cleared after successful release.
Returns
True if already unlocked or released; false if removal or release fails.

Definition at line 273 of file file_manager.hpp.

◆ unusedPath()

bool fray::files::FileManager::unusedPath ( const std::filesystem::path & parent,
const std::string & stem,
std::filesystem::path & output )
inlinestaticprivate

Finds an unused numbered folder name with no matching .fry archive.

Parameters
parentParent directory to search.
stemBase name before the numeric suffix.
outputReceives the unused folder path on success.
Returns
True if found; false on inspection error or exhaustion of the search range.

Definition at line 515 of file file_manager.hpp.

◆ workingDirectory()

const std::filesystem::path & fray::files::FileManager::workingDirectory ( ) const
inline

Returns a borrowed reference to the working folder path.

Definition at line 849 of file file_manager.hpp.

◆ fray::ProjectManager

friend class fray::ProjectManager
friend

Definition at line 75 of file file_manager.hpp.

◆ fray::state::StateManager

friend class fray::state::StateManager
friend

Definition at line 74 of file file_manager.hpp.

Member Data Documentation

◆ _archive

std::filesystem::path fray::files::FileManager::_archive
private

Path of the project archive associated with the workspace.

Definition at line 79 of file file_manager.hpp.

◆ _archiveExists

bool fray::files::FileManager::_archiveExists = false
private

Whether the archive is expected to exist.

Definition at line 97 of file file_manager.hpp.

◆ _conflictDestination

std::filesystem::path fray::files::FileManager::_conflictDestination
private

Renamed path of the unselected representation awaiting resolution.

Definition at line 138 of file file_manager.hpp.

◆ _conflictFolder

bool fray::files::FileManager::_conflictFolder = false
private

Whether the unselected representation is a folder rather than an archive.

Definition at line 146 of file file_manager.hpp.

◆ _conflictName

std::string fray::files::FileManager::_conflictName
private

New database stem of the unselected representation.

Definition at line 142 of file file_manager.hpp.

◆ _conflictSource

std::filesystem::path fray::files::FileManager::_conflictSource
private

Original path of an unselected representation awaiting conflict commit or rollback.

Definition at line 134 of file file_manager.hpp.

◆ _databaseFile

std::string fray::files::FileManager::_databaseFile
private

Explicit database filename for a directory-backed store; empty for a project.

Definition at line 91 of file file_manager.hpp.

◆ _existingDatabase

bool fray::files::FileManager::_existingDatabase = false
private

Whether opening state must not create a missing database file.

Definition at line 93 of file file_manager.hpp.

◆ _extracted

bool fray::files::FileManager::_extracted = false
private

Whether the workspace was extracted from an archive.

Definition at line 101 of file file_manager.hpp.

◆ _extraLock

ProjectLock fray::files::FileManager::_extraLock
private

Temporary lock for the renamed, unselected conflict representation.

Definition at line 130 of file file_manager.hpp.

◆ _lifetime

std::shared_ptr<int> fray::files::FileManager::_lifetime = std::make_shared<int>(0)
private

Lifetime sentinel observed weakly by attached state managers.

Definition at line 154 of file file_manager.hpp.

◆ _lock

ProjectLock fray::files::FileManager::_lock
private

Lock for the selected project archive path.

Definition at line 126 of file file_manager.hpp.

◆ _name

std::string fray::files::FileManager::_name
private

Current project name and database stem.

Definition at line 87 of file file_manager.hpp.

◆ _users

unsigned fray::files::FileManager::_users = 0
private

Number of attached state managers; nonzero prevents save and close.

Definition at line 150 of file file_manager.hpp.

◆ _workspace

std::filesystem::path fray::files::FileManager::_workspace
private

Working folder path; empty when no project is open.

Definition at line 83 of file file_manager.hpp.


The documentation for this class was generated from the following file: