Contiki-NG
Files | Macros | Functions
The Contiki file system interface

The Contiki file system interface (CFS) defines an abstract API for reading directories and for reading and writing files. More...

Files

file  cfs-coffee.h
 
 Header for the Coffee file system.

 

Macros

#define CFS_COFFEE_IO_FLASH_AWARE   0x1
 Instruct Coffee that the access pattern to this file is adapted to flash I/O semantics by design, and Coffee should therefore not invoke its own micro logs when file modifications occur. More...
 
#define CFS_COFFEE_IO_FIRM_SIZE   0x2
 Instruct Coffee not to attempt to extend the file upon a request to write past the reserved file size. More...
 
#define CFS_COFFEE_IO_ENSURE_READ_LENGTH   0x4
 Instruct Coffee to set unused bytes in the destination buffer to zero. More...
 
#define CFS_READ   1
 Specify that cfs_open() should open a file for reading. More...
 
#define CFS_WRITE   2
 Specify that cfs_open() should open a file for writing. More...
 
#define CFS_APPEND   4
 Specify that cfs_open() should append written data to the file rather than overwriting it. More...
 
#define CFS_SEEK_SET   0
 Specify that cfs_seek() should compute the offset from the beginning of the file. More...
 
#define CFS_SEEK_CUR   1
 Specify that cfs_seek() should compute the offset from the current position of the file pointer. More...
 
#define CFS_SEEK_END   2
 Specify that cfs_seek() should compute the offset from the end of the file. More...
 

Functions

int cfs_open (const char *name, int flags)
 Open a file. More...
 
void cfs_close (int fd)
 Close an open file. More...
 
int cfs_read (int fd, void *buf, unsigned int len)
 Read data from an open file. More...
 
int cfs_write (int fd, const void *buf, unsigned int len)
 Write data to an open file. More...
 
cfs_offset_t cfs_seek (int fd, cfs_offset_t offset, int whence)
 Seek to a specified position in an open file. More...
 
int cfs_remove (const char *name)
 Remove a file. More...
 
int cfs_opendir (struct cfs_dir *dirp, const char *name)
 Open a directory for reading directory entries. More...
 
int cfs_readdir (struct cfs_dir *dirp, struct cfs_dirent *dirent)
 Read a directory entry. More...
 
void cfs_closedir (struct cfs_dir *dirp)
 Close a directory opened with cfs_opendir(). More...
 

Functions called from application programs

int cfs_coffee_reserve (const char *name, cfs_offset_t size)
 Reserve space for a file. More...
 
int cfs_coffee_configure_log (const char *file, unsigned log_size, unsigned log_entry_size)
 Configure the on-demand log file. More...
 
int cfs_coffee_set_io_semantics (int fd, unsigned flags)
 Set the I/O semantics for accessing a file. More...
 
int cfs_coffee_format (void)
 Format the storage area assigned to Coffee. More...
 

Detailed Description

The Contiki file system interface (CFS) defines an abstract API for reading directories and for reading and writing files.

The CFS API is intentionally simple. The CFS API is modeled after the POSIX file API, and slightly simplified.

Macro Definition Documentation

◆ CFS_APPEND

#define CFS_APPEND   4

Specify that cfs_open() should append written data to the file rather than overwriting it.

This constant indicates to cfs_open() that a file that should be opened for writing gets written data appended to the end of the file. The default behaviour (without CFS_APPEND) is that the file is overwritten with the new data.

See also
cfs_open()

Definition at line 120 of file cfs.h.

Referenced by cfs_open().

◆ CFS_COFFEE_IO_ENSURE_READ_LENGTH

#define CFS_COFFEE_IO_ENSURE_READ_LENGTH   0x4

Instruct Coffee to set unused bytes in the destination buffer to zero.

Trailing zeros may cause a wrong file size, this option ensures that the corresponding bytes get set, so Coffee does not read unexpected data.

See also
cfs_coffee_set_io_semantics()

Definition at line 73 of file cfs-coffee.h.

◆ CFS_COFFEE_IO_FIRM_SIZE

#define CFS_COFFEE_IO_FIRM_SIZE   0x2

Instruct Coffee not to attempt to extend the file upon a request to write past the reserved file size.

A case when this is necessary is when the file has a firm size limit, and a safeguard is needed to protect against writes beyond this limit.

See also
cfs_coffee_set_io_semantics()

Definition at line 64 of file cfs-coffee.h.

◆ CFS_COFFEE_IO_FLASH_AWARE

#define CFS_COFFEE_IO_FLASH_AWARE   0x1

Instruct Coffee that the access pattern to this file is adapted to flash I/O semantics by design, and Coffee should therefore not invoke its own micro logs when file modifications occur.

This semantical I/O setting is useful when implementing flash storage algorithms such as database indices on top of Coffee.

See also
cfs_coffee_set_io_semantics()

Definition at line 53 of file cfs-coffee.h.

◆ CFS_READ

#define CFS_READ   1

Specify that cfs_open() should open a file for reading.

This constant indicates to cfs_open() that a file should be opened for reading. CFS_WRITE should be used if the file is opened for writing, and CFS_READ + CFS_WRITE indicates that the file is opened for both reading and writing.

See also
cfs_open()

Definition at line 92 of file cfs.h.

◆ CFS_SEEK_CUR

#define CFS_SEEK_CUR   1

Specify that cfs_seek() should compute the offset from the current position of the file pointer.

See also
cfs_seek()

Definition at line 138 of file cfs.h.

Referenced by cfs_seek().

◆ CFS_SEEK_END

#define CFS_SEEK_END   2

Specify that cfs_seek() should compute the offset from the end of the file.

See also
cfs_seek()

Definition at line 147 of file cfs.h.

Referenced by cfs_seek().

◆ CFS_SEEK_SET

#define CFS_SEEK_SET   0

Specify that cfs_seek() should compute the offset from the beginning of the file.

See also
cfs_seek()

Definition at line 129 of file cfs.h.

Referenced by cfs_seek().

◆ CFS_WRITE

#define CFS_WRITE   2

Specify that cfs_open() should open a file for writing.

This constant indicates to cfs_open() that a file should be opened for writing. CFS_READ should be used if the file is opened for reading, and CFS_READ + CFS_WRITE indicates that the file is opened for both reading and writing.

See also
cfs_open()

Definition at line 106 of file cfs.h.

Referenced by cfs_open().

Function Documentation

◆ cfs_close()

void cfs_close ( int  fd)

Close an open file.

Parameters
fdThe file descriptor of the open file.
        This function closes a file that has previously been
        opened with cfs_open().

Definition at line 69 of file cfs-sdcard.c.

◆ cfs_closedir()

void cfs_closedir ( struct cfs_dir *  dirp)

Close a directory opened with cfs_opendir().

Parameters
dirpA pointer to a struct cfs_dir that has been opened with cfs_opendir().
See also
cfs_opendir()
cfs_readdir()

Definition at line 184 of file cfs-sdcard.c.

◆ cfs_coffee_configure_log()

int cfs_coffee_configure_log ( const char *  file,
unsigned  log_size,
unsigned  log_entry_size 
)

Configure the on-demand log file.

Parameters
fileThe file name.
log_sizeThe total log file size.
log_entry_sizeThe log entry size.
Returns
0 on success, -1 on failure.

When file data is first modified, Coffee creates a micro log for the file. The micro log stores a table of modifications whose parameters – the log size and the log entry size – can be modified through the cfs_coffee_configure_log function.

Definition at line 1283 of file cfs-coffee.c.

References COFFEE_PAGE_SIZE.

◆ cfs_coffee_format()

int cfs_coffee_format ( void  )

Format the storage area assigned to Coffee.

Returns
0 on success, -1 on failure.

Coffee formats the underlying storage by setting all bits to zero. Formatting must be done before using Coffee for the first time in a mote.

Notice that the erased bits may be set to 1 on the physical storage when using flash memory. In this case, Coffee requires that the COFFEE_READ and COFFEE_WRITE functions used to access the flash memory invert all bits.

Definition at line 1325 of file cfs-coffee.c.

◆ cfs_coffee_reserve()

int cfs_coffee_reserve ( const char *  name,
cfs_offset_t  size 
)

Reserve space for a file.

Parameters
nameThe file name.
sizeThe initial size to be reserved for the file.
Returns
0 on success, -1 on failure.

Coffee uses sequential page structures for files. The sequential structure can be reserved with a certain size. If a file has not been reserved when it is opened for the first time, it will be allocated with a default size.

Definition at line 1277 of file cfs-coffee.c.

◆ cfs_coffee_set_io_semantics()

int cfs_coffee_set_io_semantics ( int  fd,
unsigned  flags 
)

Set the I/O semantics for accessing a file.

Parameters
fdThe file descriptor through which the file is accessed.
flagsA bit vector of flags.

Coffee is used on a wide range of storage types, and the default I/O file semantics may not be optimal for the access pattern of a certain file. Hence, this function allows programmers to switch the I/O semantics on a file that is accessed through a particular file descriptor.

Definition at line 1313 of file cfs-coffee.c.

◆ cfs_open()

int cfs_open ( const char *  name,
int  flags 
)

Open a file.

Parameters
nameThe name of the file.
flagsCFS_READ, or CFS_WRITE/CFS_APPEND, or both.
Returns
A file descriptor, if the file could be opened, or -1 if the file could not be opened.

This function opens a file and returns a file descriptor for the opened file. If the file could not be opened, the function returns -1. The function can open a file for reading or writing, or both.

An opened file must be closed with cfs_close().

See also
CFS_READ
CFS_WRITE
cfs_close()

Definition at line 41 of file cfs-sdcard.c.

References CFS_APPEND, and CFS_WRITE.

◆ cfs_opendir()

int cfs_opendir ( struct cfs_dir *  dirp,
const char *  name 
)

Open a directory for reading directory entries.

Parameters
dirpA pointer to a struct cfs_dir that is filled in by the function.
nameThe name of the directory.
Returns
0 or -1 if the directory could not be opened.
See also
cfs_readdir()
cfs_closedir()

Definition at line 129 of file cfs-sdcard.c.

◆ cfs_read()

int cfs_read ( int  fd,
void *  buf,
unsigned int  len 
)

Read data from an open file.

Parameters
fdThe file descriptor of the open file.
bufThe buffer in which data should be read from the file.
lenThe number of bytes that should be read.
Returns
The number of bytes that was actually read from the file.
        This function reads data from an open file into a
        buffer. The file must have first been opened with
        cfs_open() and the CFS_READ flag.

Definition at line 78 of file cfs-sdcard.c.

◆ cfs_readdir()

int cfs_readdir ( struct cfs_dir *  dirp,
struct cfs_dirent *  dirent 
)

Read a directory entry.

Parameters
dirpA pointer to a struct cfs_dir that has been opened with cfs_opendir().
direntA pointer to a struct cfs_dirent that is filled in by cfs_readdir()
Return values
0If a directory entry was read.
-1If no more directory entries can be read.
See also
cfs_opendir()
cfs_closedir()

Definition at line 145 of file cfs-sdcard.c.

References start().

◆ cfs_remove()

int cfs_remove ( const char *  name)

Remove a file.

Parameters
nameThe name of the file.
Return values
0If the file was removed.
Returns
-1 If the file could not be removed or if it doesn't exist.

Definition at line 150 of file cfs-cooja.c.

◆ cfs_seek()

cfs_offset_t cfs_seek ( int  fd,
cfs_offset_t  offset,
int  whence 
)

Seek to a specified position in an open file.

Parameters
fdThe file descriptor of the open file.
offsetA position, either relative or absolute, in the file.
whenceDetermines how to interpret the offset parameter.
Returns
The new position in the file, or (cfs_offset_t)-1 if the seek failed.
        This function moves the file position to the specified
        position in the file. The next byte that is read from
        or written to the file will be at the position given 
        determined by the combination of the offset parameter 
        and the whence parameter.
See also
CFS_SEEK_CUR
CFS_SEEK_END
CFS_SEEK_SET

Definition at line 94 of file cfs-sdcard.c.

References CFS_SEEK_CUR, CFS_SEEK_END, and CFS_SEEK_SET.

◆ cfs_write()

int cfs_write ( int  fd,
const void *  buf,
unsigned int  len 
)

Write data to an open file.

Parameters
fdThe file descriptor of the open file.
bufThe buffer from which data should be written to the file.
lenThe number of bytes that should be written.
Returns
The number of bytes that was actually written to the file.
        This function reads writes data from a memory buffer to
        an open file. The file must have been opened with
        cfs_open() and the CFS_WRITE flag.

Definition at line 86 of file cfs-sdcard.c.