Uniot Core
0.8.1
Loading...
Searching...
No Matches
uniot::Storage Class Reference

#include <Storage.h>

Inheritance diagram for uniot::Storage:
[legend]

Public Member Functions

 Storage (const String &path)
 Constructs a Storage object for a specific file.
 
virtual ~Storage ()
 Destructor.
 
virtual bool store ()
 Writes the current data to the file system.
 
virtual bool restore ()
 Loads data from the file system into memory.
 
virtual bool clean ()
 Clears data and removes the file from the file system.
 

Static Public Member Functions

static void unmount ()
 Explicitly unmounts the filesystem.
 

Protected Member Functions

void setPath (const String &path)
 Sets the file path, ensuring it starts with "/".
 

Protected Attributes

Bytes mData
 The byte array containing the data to be stored or the loaded data.
 
String mPath
 The file path where data is stored.
 

Constructor & Destructor Documentation

◆ Storage()

uniot::Storage::Storage ( const String & path)
inline

Constructs a Storage object for a specific file.

Creates a Storage instance associated with a specific file path. The constructor automatically mounts the filesystem if this is the first Storage instance created. File path will be prepended with "/" if not already present.

Parameters
pathThe file path to use for storage (limited to 31 characters)

◆ ~Storage()

virtual uniot::Storage::~Storage ( )
inlinevirtual

Destructor.

Decrements the instance counter and unmounts the filesystem when the last instance is destroyed

Member Function Documentation

◆ clean()

virtual bool uniot::Storage::clean ( )
inlinevirtual

Clears data and removes the file from the file system.

Cleans up by both clearing the in-memory data and removing the associated file from the filesystem.

Return values
trueFile was successfully removed
falseFailed to remove the file

Reimplemented in uniot::CBORStorage, uniot::CrashStorage, and uniot::WifiStorage.

◆ restore()

virtual bool uniot::Storage::restore ( )
inlinevirtual

Loads data from the file system into memory.

Reads the content of the file specified by mPath into mData. If the file doesn't exist (which is normal on first use), it logs a warning but doesn't treat it as an error.

Return values
trueData was successfully loaded into mData
falseFailed to open the file for reading

Reimplemented in uniot::CBORStorage, uniot::Credentials, uniot::Date, uniot::NetworkController, and uniot::WifiStorage.

◆ setPath()

void uniot::Storage::setPath ( const String & path)
inlineprotected

Sets the file path, ensuring it starts with "/".

Parameters
pathThe file path to set (limited to 31 characters total)

◆ store()

virtual bool uniot::Storage::store ( )
inlinevirtual

Writes the current data to the file system.

Saves the content of mData to the file specified by mPath. On ESP8266 with SPIFFS, it attempts to run garbage collection to prevent filesystem fragmentation issues.

Return values
trueData was successfully written to the file
falseFailed to open the file for writing

Reimplemented in uniot::CBORStorage, uniot::CrashStorage, uniot::Credentials, uniot::Date, uniot::LispDevice, uniot::NetworkController, and uniot::WifiStorage.

◆ unmount()

static void uniot::Storage::unmount ( )
inlinestatic

Explicitly unmounts the filesystem.

This method forcibly unmounts the filesystem regardless of how many Storage instances exist. Use with extreme caution as it will affect all other Storage instances.

Warning
This can cause other Storage instances to fail as they may attempt to access an unmounted filesystem

Member Data Documentation

◆ mData

Bytes uniot::Storage::mData
protected

The byte array containing the data to be stored or the loaded data.

This member stores the actual data that will be written to the file system or that has been read from the file system.

◆ mPath

String uniot::Storage::mPath
protected

The file path where data is stored.

This is the full path to the file in the filesystem.


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