distortos  v0.7.0
object-oriented C++ RTOS for microcontrollers
distortos::File Class Referenceabstract

#include "distortos/FileSystem/File.hpp"

Inheritance diagram for distortos::File:
[legend]

Public Types

enum  Whence { Whence::beginning, Whence::current, Whence::end }
 seek() mode of operation More...
 

Public Member Functions

virtual ~File ()=default
 File's destructor. More...
 
virtual int close ()=0
 Closes file. More...
 
virtual std::pair< int, off_t > getPosition ()=0
 Returns current file offset. More...
 
virtual std::pair< int, off_t > getSize ()=0
 Returns size of file. More...
 
virtual int getStatus (struct stat &status)=0
 Returns status of file. More...
 
virtual std::pair< int, bool > isATerminal ()=0
 Tells whether the file is a terminal. More...
 
virtual void lock ()=0
 Locks the file for exclusive use by current thread. More...
 
virtual std::pair< int, size_t > read (void *buffer, size_t size)=0
 Reads data from file. More...
 
virtual int rewind ()=0
 Resets current file offset. More...
 
virtual std::pair< int, off_t > seek (Whence whence, off_t offset)=0
 Moves file offset. More...
 
virtual int synchronize ()=0
 Synchronizes state of a file, ensuring all cached writes are finished. More...
 
virtual void unlock ()=0
 Unlocks the file which was previously locked by current thread. More...
 
virtual std::pair< int, size_t > write (const void *buffer, size_t size)=0
 Writes data to file. More...
 
 File (const File &)=delete
 
Fileoperator= (const File &)=delete
 

Detailed Description

File class is an interface for a file.

Member Enumeration Documentation

◆ Whence

seek() mode of operation

Enumerator
beginning 

set file offset to provided argument, similar to SEEK_SET

current 

set file offset to its current value plus provided argument, similar to SEEK_CUR

end 

set file offset to the size of the file plus provided argument, similar to SEEK_END

Constructor & Destructor Documentation

◆ ~File()

virtual distortos::File::~File ( )
virtualdefault

File's destructor.

Precondition
File is closed.

Member Function Documentation

◆ close()

virtual int distortos::File::close ( )
pure virtual

Closes file.

Similar to close()

Note
Even if error code is returned, the file must not be used.
Precondition
File is opened.
Postcondition
File is closed.
Returns
0 on success, error code otherwise

Implemented in distortos::Littlefs1File.

◆ getPosition()

virtual std::pair<int, off_t> distortos::File::getPosition ( )
pure virtual

Returns current file offset.

Similar to ftello()

Precondition
File is opened.
Returns
pair with return code (0 on success, error code otherwise) and current file offset, bytes

Implemented in distortos::Littlefs1File.

◆ getSize()

virtual std::pair<int, off_t> distortos::File::getSize ( )
pure virtual

Returns size of file.

Precondition
File is opened.
Returns
pair with return code (0 on success, error code otherwise) and size of file, bytes

Implemented in distortos::Littlefs1File.

◆ getStatus()

virtual int distortos::File::getStatus ( struct stat &  status)
pure virtual

Returns status of file.

Similar to fstat()

Precondition
File is opened.
Parameters
[out]statusis a reference to stat struct into which status of file will be written
Returns
0 on success, error code otherwise

Implemented in distortos::Littlefs1File.

◆ isATerminal()

virtual std::pair<int, bool> distortos::File::isATerminal ( )
pure virtual

Tells whether the file is a terminal.

Similar to isatty()

Precondition
File is opened.
Returns
pair with return code (0 on success, error code otherwise) and bool telling whether the file is a terminal (true) or not (false)

Implemented in distortos::Littlefs1File.

◆ lock()

virtual void distortos::File::lock ( )
pure virtual

Locks the file for exclusive use by current thread.

When the object is locked, any call to any member function from other thread will be blocked until the object is unlocked. Locking is optional, but may be useful when more than one operation must be done atomically.

Note
Locks are recursive.
Warning
This function must not be called from interrupt context!
Precondition
The number of recursive locks of file is less than 65535.
Postcondition
File is locked.

Implemented in distortos::Littlefs1File.

◆ read()

virtual std::pair<int, size_t> distortos::File::read ( void *  buffer,
size_t  size 
)
pure virtual

Reads data from file.

Similar to read()

Precondition
File is opened.
buffer is valid.
Parameters
[out]bufferis the buffer into which the data will be read, must be valid
[in]sizeis the size of buffer, bytes
Returns
pair with return code (0 on success, error code otherwise) and number of read bytes (valid even when error code is returned)

Implemented in distortos::Littlefs1File.

◆ rewind()

virtual int distortos::File::rewind ( )
pure virtual

Resets current file offset.

Similar to rewind()

Precondition
File is opened.
Returns
0 on success, error code otherwise

Implemented in distortos::Littlefs1File.

◆ seek()

virtual std::pair<int, off_t> distortos::File::seek ( Whence  whence,
off_t  offset 
)
pure virtual

Moves file offset.

Similar to lseek()

Precondition
File is opened.
Parameters
[in]whenceselects the mode of operation: Whence::beginning will set file offset to offset, Whence::current will set file offset to its current value plus offset, Whence::end will set file offset to the size of the file plus offset
[in]offsetis the value of offset, bytes
Returns
pair with return code (0 on success, error code otherwise) and current file offset, bytes; error codes:
  • EINVAL - resulting file offset would be negative;

Implemented in distortos::Littlefs1File.

◆ synchronize()

virtual int distortos::File::synchronize ( )
pure virtual

Synchronizes state of a file, ensuring all cached writes are finished.

Similar to fsync()

Precondition
File is opened.
Returns
0 on success, error code otherwise

Implemented in distortos::Littlefs1File.

◆ unlock()

virtual void distortos::File::unlock ( )
pure virtual

Unlocks the file which was previously locked by current thread.

Note
Locks are recursive.
Warning
This function must not be called from interrupt context!
Precondition
This function is called by the thread that locked the file.

Implemented in distortos::Littlefs1File.

◆ write()

virtual std::pair<int, size_t> distortos::File::write ( const void *  buffer,
size_t  size 
)
pure virtual

Writes data to file.

Similar to write()

Precondition
File is opened.
buffer is valid.
Parameters
[in]bufferis the buffer with data that will be written, must be valid
[in]sizeis the size of buffer, bytes
Returns
pair with return code (0 on success, error code otherwise) and number of written bytes (valid even when error code is returned); error codes:
  • ENOSPC - no space left on the device containing the file;

Implemented in distortos::Littlefs1File.


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