WE310F5  39.00.000
M2MB FS POSIX

This section describes the M2MB APIs to perform various File system posix operations. More...

#define M2MB_O_RDONLY   00
 
#define M2MB_O_WRONLY   01
 
#define M2MB_O_RDWR   02
 
#define M2MB_O_CREAT   0100
 
#define M2MB_O_EXCL   0200
 
#define M2MB_O_TRUNC   01000
 
#define M2MB_O_APPEND   02000
 
#define M2MB_SEEK_SET   0
 
#define M2MB_SEEK_CUR   1
 
#define M2MB_SEEK_END   2
 
enum  M2MB_FR_E {
  M2MB_FR_ERROR = -1,
  M2MB_FR_OK = 0,
  M2MB_FR_DISK_ERR,
  M2MB_FR_INT_ERR,
  M2MB_FR_NOT_READY,
  M2MB_FR_NO_FILE,
  M2MB_FR_NO_PATH,
  M2MB_FR_INVALID_NAME,
  M2MB_FR_DENIED,
  M2MB_FR_EXIST,
  M2MB_FR_INVALID_OBJECT,
  M2MB_FR_WRITE_PROTECTED,
  M2MB_FR_INVALID_DRIVE,
  M2MB_FR_NOT_ENABLED,
  M2MB_FR_NO_FILESYSTEM,
  M2MB_FR_MKFS_ABORTED,
  M2MB_FR_TIMEOUT,
  M2MB_FR_LOCKED,
  M2MB_FR_NOT_ENOUGH_CORE,
  M2MB_FR_TOO_MANY_OPEN_FILES,
  M2MB_FR_INVALID_PARAMETER
}
 
typedef struct M2MB_STAT M2MB_STAT_T
 
typedef struct M2MB_LS_ENTRY M2MB_LS_ENTRY_T
 
M2MB_STATUS_T m2mb_fs_init (VOID)
 File system initialization. More...
 
INT32 m2mb_fs_open (const CHAR *path, INT32 oflag,...)
 Open file. More...
 
INT32 m2mb_fs_secure_open (const CHAR *path, INT32 oflag, const char *pwd)
 Open secure file. More...
 
SSIZE_T m2mb_fs_read (INT32 fd, void *buf, UINT32 nbyte)
 Read file. More...
 
SSIZE_T m2mb_fs_write (INT32 fd, const void *buf, UINT32 nbyte)
 Write file. More...
 
SSIZE_T m2mb_fs_length (INT32 fd)
 Write file. More...
 
INT32 m2mb_fs_close (INT32 fd)
 Close file. More...
 
OFF_T m2mb_fs_lseek (INT32 fd, OFF_T offset, INT32 whence)
 Move file offset. More...
 
INT32 m2mb_fs_stat (const CHAR *path, struct M2MB_STAT *buf)
 File status. More...
 
INT32 m2mb_fs_unlink (const CHAR *path)
 Delete file. More...
 
INT32 m2mb_fs_ls_open (const char *path, HANDLE *handle)
 
The function opens the folder whose name is the string pointed to by path.

Depending on the read/write speeds of the underlying flash media, this API can take time in the order of seconds to complete, and clients calling this API should be prepared to have their task blocked for this time duration. More...

 
INT32 m2mb_fs_ls_next (HANDLE ls_hdl, struct M2MB_LS_ENTRY *ls_entry)
 Returns the next file system entry while listing. Depending on the read/write speeds of the underlying flash media, this API can take time in the order of seconds to complete, and clients calling this API should be prepared to have their task blocked for this time duration. More...
 
INT32 m2mb_fs_ls_close (HANDLE ls_hdl)
 File system ls close. More...
 

Detailed Description

This section describes the M2MB APIs to perform various File system posix operations.

Macro Definition Documentation

◆ M2MB_O_APPEND

#define M2MB_O_APPEND   02000

File Append mode, the file offset shall be set to the end of the file prior to each write.

Definition at line 60 of file m2mb_fs_posix.h.

◆ M2MB_O_CREAT

#define M2MB_O_CREAT   0100

File create mode

Definition at line 57 of file m2mb_fs_posix.h.

◆ M2MB_O_EXCL

#define M2MB_O_EXCL   0200

Definition at line 58 of file m2mb_fs_posix.h.

◆ M2MB_O_RDONLY

#define M2MB_O_RDONLY   00

Read only mode

Definition at line 54 of file m2mb_fs_posix.h.

◆ M2MB_O_RDWR

#define M2MB_O_RDWR   02

Read-write mode

Definition at line 56 of file m2mb_fs_posix.h.

◆ M2MB_O_TRUNC

#define M2MB_O_TRUNC   01000

File Truncate mode

Definition at line 59 of file m2mb_fs_posix.h.

◆ M2MB_O_WRONLY

#define M2MB_O_WRONLY   01

write only mode

Definition at line 55 of file m2mb_fs_posix.h.

◆ M2MB_SEEK_CUR

#define M2MB_SEEK_CUR   1

Seek from current position

Definition at line 64 of file m2mb_fs_posix.h.

◆ M2MB_SEEK_END

#define M2MB_SEEK_END   2

Seek from end of file

Definition at line 65 of file m2mb_fs_posix.h.

◆ M2MB_SEEK_SET

#define M2MB_SEEK_SET   0

Seek from beginning of file

Definition at line 63 of file m2mb_fs_posix.h.

Typedef Documentation

◆ M2MB_LS_ENTRY_T

◆ M2MB_STAT_T

typedef struct M2MB_STAT M2MB_STAT_T

Enumeration Type Documentation

◆ M2MB_FR_E

enum M2MB_FR_E
Enumerator
M2MB_FR_ERROR 
M2MB_FR_OK 
M2MB_FR_DISK_ERR 
M2MB_FR_INT_ERR 
M2MB_FR_NOT_READY 
M2MB_FR_NO_FILE 
M2MB_FR_NO_PATH 
M2MB_FR_INVALID_NAME 
M2MB_FR_DENIED 
M2MB_FR_EXIST 
M2MB_FR_INVALID_OBJECT 
M2MB_FR_WRITE_PROTECTED 
M2MB_FR_INVALID_DRIVE 
M2MB_FR_NOT_ENABLED 
M2MB_FR_NO_FILESYSTEM 
M2MB_FR_MKFS_ABORTED 
M2MB_FR_TIMEOUT 
M2MB_FR_LOCKED 
M2MB_FR_NOT_ENOUGH_CORE 
M2MB_FR_TOO_MANY_OPEN_FILES 
M2MB_FR_INVALID_PARAMETER 

Definition at line 68 of file m2mb_fs_posix.h.

Function Documentation

◆ m2mb_fs_close()

INT32 m2mb_fs_close ( INT32  fd)

Close file.

The function closes the file associated with the open file descriptor, fd.

Parameters
[in]fdFile descriptor.
Returns
Upon successful completion, 0 is returned. Otherwise, -1 is returned.
Note
<Notes>

m2mb_fs_close(fd);

int main()
{
INT32 fd;
UINT8 file_name[128];
UINT32 profile_num;
static UINT8 profile_pwd[17];
UINT8 i, *data = NULL;
INT32 len;
INT32 bytes_read;
M2MB_STATUS_T status;
status = m2mb_fs_init( );
if(status == M2MB_ERROR)
{
return M2MB_ERROR;
}
snprintf( file_name, 128, "/spinor/sys/profile%d.json", profile_num );
fd = m2mb_fs_secure_open( file_name,M2MB_O_RDONLY, profile_pwd );
if(fd == -1)
{
return M2MB_ERROR;
}
len = m2mb_fs_lseek( fd, 0, M2MB_SEEK_END );
data = ( UINT8* )MALLOC( len );
if( data != NULL )
{
bytes_read = m2mb_fs_read( fd, data, len );
if( bytes_read != len )
{
FREE( data );
data = NULL;
return_status = M2MB_ERROR;
}
}
INT32 ret = m2mb_fs_close(fd);
return ret;
return M2MB_OK;
}

◆ m2mb_fs_init()

M2MB_STATUS_T m2mb_fs_init ( VOID  )

File system initialization.

The function initialization File system.

Returns
Upon successful completion returns M2MB_OK. Otherwise, M2MB_ERROR is returned.
Note
<Notes>

m2mb_fs_init( );

int main()
{
M2MB_STATUS_T status;
status = m2mb_fs_init( );
if(status == M2MB_ERROR)
{
return M2MB_ERROR;
}
return M2MB_OK;
}

◆ m2mb_fs_length()

SSIZE_T m2mb_fs_length ( INT32  fd)

Write file.

The function returns the length of the file associated with the open file descriptor, fd.

Parameters
[in]fdFile descriptor.
Returns
Upon successful length of the file in bytes. Otherwise, -1 is returned
Note
<Notes>

m2mb_fs_length( fd );

int main()
{
INT32 fd;
UINT8 file_name[128];
UINT32 profile_num;
static UINT8 profile_pwd[17];
UINT8 i, *data = NULL;
INT32 len;
INT32 bytes_read;
M2MB_STATUS_T status;
status = m2mb_fs_init( );
if(status == M2MB_ERROR)
{
return M2MB_ERROR;
}
snprintf( file_name, 128, "/spinor/sys/profile%d.json", profile_num );
fd = m2mb_fs_secure_open( file_name, M2MB_O_CREAT | M2MB_O_WRONLY, profile_pwd );
if( fd == -1 )
{
return M2MB_ERROR;
}
else
{
INT32 size_fd = m2mb_fs_write( fd, (UINT8*)buffer, (UINT32)length );
if( length != size_fd )
{
return M2MB_ERROR;
}
INT32 length = m2mb_fs_length( fd );
if( length == -1 )
{
return M2MB_ERROR;
}
}
return M2MB_OK;
}

◆ m2mb_fs_ls_close()

INT32 m2mb_fs_ls_close ( HANDLE  ls_hdl)

File system ls close.

The function is called to close the ls functionality.

Parameters
[in]ls_hdlFile ls handle.
Returns
Returns 0 on success and -ve error code on failure.

m2mb_fs_ls_close( handle );

int main()
{
HANDLE handle;
UINT8 file_path[64 + sizeof("/spinor")];
M2MB_STATUS_T status;
status = m2mb_fs_init( );
if(status == M2MB_ERROR)
{
return M2MB_ERROR;
}
snprintf(file_path,64 + sizeof("/spinor"), "/spinor%s",path);
if( 0 == m2mb_fs_ls_open( file_path, &handle ))
{
struct M2MB_LS_ENTRY ls_entry;
INT32 ret;
do
{
M2M_S2W_SYSTEM_FILE_LIST_RSP_T rsp;
ret = m2mb_fs_ls_next( handle, &ls_entry );
if( ret == 0 )
{
if( NULL == strstr( ls_entry.file_path, "/spinor/sys" ) &&
NULL == strstr( ls_entry.file_path, "/spinor/ssl" ))
{
rsp.file_name = ls_entry.file_path + strlen("/spinor");
rsp.alen = ls_entry.sbuf.st_size;
rsp.tlen = ls_entry.sbuf.st_blocks * ls_entry.sbuf.st_blksize;
m2m_parser_rsp_handle( pHandle, &rsp );
}
}
}while( 0 == ret );
m2mb_fs_ls_close( handle );
return M2MB_OK;
}
}

◆ m2mb_fs_ls_next()

INT32 m2mb_fs_ls_next ( HANDLE  ls_hdl,
struct M2MB_LS_ENTRY ls_entry 
)

Returns the next file system entry while listing. Depending on the read/write speeds of the underlying flash media, this API can take time in the order of seconds to complete, and clients calling this API should be prepared to have their task blocked for this time duration.

Parameters
[in]ls_hdlHandle to the ls.
[in]ls_entryReturns non NULL ls entry on success, or NULL for an error.
Returns
Returns 0 on success and -ve error code on failure.
Note
<Notes>
  struct M2MB_LS_ENTRY {
      char file_path[100];
      struct M2MB_STAT sbuf;
   };

   struct M2MB_STAT {
        DEV_T st_dev;
        UINT32 st_ino;
        UINT32 st_size;
        UINT32 st_blksize;
        UINT32 st_blocks;
   };

    st_dev     : Unique Device ID
    st_ino     : INode number associated with the file.
    st_size    : File size in bytes.
    st_blksize : Block size, smallest allocation unit of
                 the file system. Unit in which block count
                 is represented.
    st_blocks  : Number of blocks allocated for this file
                 in st_blksize units.

m2mb_fs_ls_next( handle, &ls_entry );

int main()
{
HANDLE handle;
UINT8 file_path[64 + sizeof("/spinor")];
M2MB_STATUS_T status;
status = m2mb_fs_init( );
if(status == M2MB_ERROR)
{
return M2MB_ERROR;
}
snprintf(file_path,64 + sizeof("/spinor"), "/spinor%s",path);
if( 0 == m2mb_fs_ls_open( file_path, &handle ))
{
struct M2MB_LS_ENTRY ls_entry;
INT32 ret;
do
{
M2M_S2W_SYSTEM_FILE_LIST_RSP_T rsp;
ret = m2mb_fs_ls_next( handle, &ls_entry );
if( ret == 0 )
{
if( NULL == strstr( ls_entry.file_path, "/spinor/sys" ) &&
NULL == strstr( ls_entry.file_path, "/spinor/ssl" ))
{
rsp.file_name = ls_entry.file_path + strlen("/spinor");
rsp.alen = ls_entry.sbuf.st_size;
rsp.tlen = ls_entry.sbuf.st_blocks * ls_entry.sbuf.st_blksize;
m2m_parser_rsp_handle( pHandle, &rsp );
}
}
}while( 0 == ret );
m2mb_fs_ls_close( handle );
return M2MB_OK;
}
}

◆ m2mb_fs_ls_open()

INT32 m2mb_fs_ls_open ( const char *  path,
HANDLE handle 
)

The function opens the folder whose name is the string pointed to by path.

Depending on the read/write speeds of the underlying flash media, this API can take time in the order of seconds to complete, and clients calling this API should be prepared to have their task blocked for this time duration.

Parameters
[in]pathpath of the file system to iterate.
[in]handleReturns non NULL iterator handle on success, or NULL for an error.
Returns
Returns 0 on success and -ve error code on failure.

m2mb_fs_ls_open( file_path, &handle ));

int main()
{
HANDLE handle;
UINT8 file_path[64 + sizeof("/spinor")];
M2MB_STATUS_T status;
status = m2mb_fs_init( );
if(status == M2MB_ERROR)
{
return M2MB_ERROR;
}
snprintf(file_path,64 + sizeof("/spinor"), "/spinor%s",path);
if( 0 == m2mb_fs_ls_open( file_path, &handle ))
{
struct M2MB_LS_ENTRY ls_entry;
INT32 ret;
do
{
M2M_S2W_SYSTEM_FILE_LIST_RSP_T rsp;
ret = m2mb_fs_ls_next( handle, &ls_entry );
if( ret == 0 )
{
if( NULL == strstr( ls_entry.file_path, "/spinor/sys" ) &&
NULL == strstr( ls_entry.file_path, "/spinor/ssl" ))
{
rsp.file_name = ls_entry.file_path + strlen("/spinor");
rsp.alen = ls_entry.sbuf.st_size;
rsp.tlen = ls_entry.sbuf.st_blocks * ls_entry.sbuf.st_blksize;
m2m_parser_rsp_handle( pHandle, &rsp );
}
}
}while( 0 == ret );
m2mb_fs_ls_close( handle );
return M2MB_OK;
}
}

◆ m2mb_fs_lseek()

OFF_T m2mb_fs_lseek ( INT32  fd,
OFF_T  offset,
INT32  whence 
)

Move file offset.

The function sets the file offset for the open file description associated with the file descriptor fd. The new offset, measured in bytes, is obtained by adding offset bytes to the position specified by whence.

Parameters
[in]fdFile descriptor.
[in]offsetNumber of bytes to move the file offset from the position specified by whence.
[in]whenceFile position. M2MB_SEEK_SET Beginning of the file. M2MB_SEEK_CUR Current file position. M2MB_SEEK_END End of file.
Returns
Upon successful completion, the resulting offset, as measured in bytes from the beginning of the file, is returned. Otherwise, (OFF_T)-1 is returned.
Note
<Notes>

m2mb_fs_lseek( fd, 0, M2MB_SEEK_END );

int main()
{
INT32 fd;
UINT8 file_name[128];
UINT32 profile_num;
static UINT8 profile_pwd[17];
UINT8 i, *data = NULL;
INT32 len;
INT32 bytes_read;
M2MB_STATUS_T status;
status = m2mb_fs_init( );
if(status == M2MB_ERROR)
{
return M2MB_ERROR;
}
snprintf( file_name, 128, "/spinor/sys/profile%d.json", profile_num );
fd = m2mb_fs_secure_open( file_name,M2MB_O_RDONLY, profile_pwd );
if(fd == -1)
{
return M2MB_ERROR;
}
len = m2mb_fs_lseek( fd, 0, M2MB_SEEK_END );
data = ( UINT8* )MALLOC( len );
if( data != NULL )
{
bytes_read = m2mb_fs_read( fd, data, len );
if( bytes_read != len )
{
FREE( data );
data = NULL;
return_status = M2MB_ERROR;
}
}
return M2MB_OK;
}

◆ m2mb_fs_open()

INT32 m2mb_fs_open ( const CHAR path,
INT32  oflag,
  ... 
)

Open file.

The function opens the file whose name is the string pointed to by path.

Parameters
[in]pathName of file to be open.
[in]oflagFlags for opening file. One of the following three flags must be used: M2MB_O_RDONLY Open for reading only. M2MB_O_WRONLY Open for writing only. M2MB_O_RDWR Open for reading and writing. In addition any of the following flags may be bitwise ORed: M2MB_O_APPEND If set, the file offset shall be set to the end of the file prior to each write. M2MB_O_CREAT If the file exists, this flag has no effect except as noted under M2MB_O_EXCL below. Otherwise, the file shall be created. M2MB_O_EXCL If M2MB_O_CREAT and M2MB_O_EXCL are set, open fails if the file exists. M2MB_O_TRUNC If the file exists and is a regular file, and the file is successfully opened with M2MB_O_RDWR or M2MB_O_WRONLY, its length is truncated to 0, and the mode is unchanged.
Returns
Upon successful completion returns a non-negative integer representing the lowest numbered unused file descriptor. Otherwise, -1 is returned.
Note
<Notes>

m2mb_fs_open( M2MB_PROV_FILE_CUST_JS_PATH, 0 );

#define M2MB_PROV_FILE_CUST_JS_PATH "/spinor/web/js/custom.min.js.gz"
int main()
{
INT32 fd;
M2MB_STATUS_T status;
status = m2mb_fs_init( );
if(status == M2MB_ERROR)
{
return M2MB_ERROR;
}
fd = m2mb_fs_open( M2MB_PROV_FILE_CUST_JS_PATH, 0 );
if(fd == -1)
{
return M2MB_ERROR;
}
return M2MB_OK;
}

◆ m2mb_fs_read()

SSIZE_T m2mb_fs_read ( INT32  fd,
void *  buf,
UINT32  nbyte 
)

Read file.

The function reads nbyte bytes from the file associated with the open file descriptor, fd, into the buffer pointed to by buf.

Parameters
[in]fdFile descriptor.
[out]bufBuffer to fill with read data.
[in]nbyteNumber of bytes to read.
Returns
Upon successful completion returns a non-negative integer indicating the number of bytes read. Otherwise, -1 is returned
Note
<Notes>

m2mb_fs_read( fd, data, len );

int main()
{
INT32 fd;
UINT8 file_name[128];
UINT32 profile_num;
static UINT8 profile_pwd[17];
UINT8 i, *data = NULL;
INT32 len;
INT32 bytes_read;
M2MB_STATUS_T status;
status = m2mb_fs_init( );
if(status == M2MB_ERROR)
{
return M2MB_ERROR;
}
snprintf( file_name, 128, "/spinor/sys/profile%d.json", profile_num );
fd = m2mb_fs_secure_open( file_name,M2MB_O_RDONLY, profile_pwd );
if(fd == -1)
{
return M2MB_ERROR;
}
len = m2mb_fs_lseek( fd, 0, M2MB_SEEK_END );
data = ( UINT8* )MALLOC( len );
if( data != NULL )
{
bytes_read = m2mb_fs_read( fd, data, len );
if( bytes_read != len )
{
FREE( data );
data = NULL;
return M2MB_ERROR;
}
}
return M2MB_OK;
}

◆ m2mb_fs_secure_open()

INT32 m2mb_fs_secure_open ( const CHAR path,
INT32  oflag,
const char *  pwd 
)

Open secure file.

The function opens the file in secured mode, whose name is the string pointed to by path.

Parameters
[in]pathName of file to be open.
[in]oflagFlags for opening files. One of the following three flags must be used: M2MB_O_RDONLY Open for reading only. M2MB_O_WRONLY Open for writing only. M2MB_O_RDWR Open for reading and writing. In addition, any of the following flags may be bitwise ORed: M2MB_O_APPEND If set, the file offset shall be set to the end of the file prior to each write. M2MB_O_CREAT If the file exists, this flag has no effect except as noted under M2MB_O_EXCL below. Otherwise, the file shall be created. M2MB_O_EXCL If M2MB_O_CREAT and M2MB_O_EXCL are set, open fails if the file exists. M2MB_O_TRUNC If the file exists and is a regular file, and the file is successfully opened with M2MB_O_RDWR or M2MB_O_WRONLY, its length is truncated to 0, and the mode is unchanged.
[in]pwdPassword of the file to be opened. The password must of be 16 bytes length
Returns
Upon successful completion returns a non-negative integer representing the lowest numbered unused file descriptor. Otherwise, -1 is returned.
Note
The password must be same whenever a particular file is opened. Else, there will be a mismatch between the written data and the read data.

m2mb_fs_secure_open( file_name,M2MB_O_RDONLY, profile_pwd );

int main()
{
INT32 fd;
UINT8 file_name[128];
UINT32 profile_num;
static UINT8 profile_pwd[17];
M2MB_STATUS_T status;
status = m2mb_fs_init( );
if(status == M2MB_ERROR)
{
return M2MB_ERROR;
}
snprintf( file_name, 128, "/spinor/sys/profile%d.json", profile_num );
fd = m2mb_fs_secure_open( file_name,M2MB_O_RDONLY, profile_pwd );
if(fd == -1)
{
return M2MB_ERROR;
}
return M2MB_OK;
}

◆ m2mb_fs_stat()

INT32 m2mb_fs_stat ( const CHAR path,
struct M2MB_STAT buf 
)

File status.

The function gets status of the file whose name is the string pointed to by path, and writes it to the area pointed to by buf.

Parameters
[in]pathName of file.
[in]bufPointer to a stat structure (see definition) to be filled with the status of the file.
Returns
Upon successful completion, 0 is returned. Otherwise, -1 is returned.
Note
<Notes>

m2mb_fs_stat( M2MB_PROV_FILE_CUST_JS_PATH, &stat );

#define M2MB_PROV_FILE_CUST_JS_PATH "/spinor/web/js/custom.min.js.gz"
int main()
{
INT32 fd,ret;
struct M2MB_STAT stat = {0};
M2MB_STATUS_T status;
status = m2mb_fs_init( );
if(status == M2MB_ERROR)
{
return M2MB_ERROR;
}
ret = m2mb_fs_stat( M2MB_PROV_FILE_CUST_JS_PATH, &stat );
if( !ret )
{
fd = m2mb_fs_open( M2MB_PROV_FILE_CUST_JS_PATH, 0 );
if(fd == -1)
{
return M2MB_ERROR;
}
}
return M2MB_OK;
}

◆ m2mb_fs_unlink()

INT32 m2mb_fs_unlink ( const CHAR path)

Delete file.

The function deletes the file whose name is the string pointed to by path.

Parameters
[in]pathName of file to be deleted.
Returns
Upon successful completion returns 0. Otherwise, -1 is returned.
Note
<Notes>

m2mb_fs_unlink( file_name );

int main()
{
INT32 fd;
UINT8 file_name[128];
UINT32 profile_num;
static UINT8 profile_pwd[17];
UINT8 i, *data = NULL;
INT32 len;
INT32 bytes_read;
M2MB_STATUS_T status;
status = m2mb_fs_init( );
if(status == M2MB_ERROR)
{
return M2MB_ERROR;
}
snprintf( file_name, 128, "/spinor/sys/profile%d.json", profile_num );
fd = m2mb_fs_secure_open( file_name,M2MB_O_RDONLY, profile_pwd );
if(fd == -1)
{
return M2MB_ERROR;
}
len = m2mb_fs_lseek( fd, 0, M2MB_SEEK_END );
data = ( UINT8* )MALLOC( len );
if( data != NULL )
{
bytes_read = m2mb_fs_read( fd, data, len );
if( bytes_read != len )
{
FREE( data );
data = NULL;
return_status = M2MB_ERROR;
}
}
m2mb_fs_unlink( file_name );
return M2MB_OK;
}

◆ m2mb_fs_write()

SSIZE_T m2mb_fs_write ( INT32  fd,
const void *  buf,
UINT32  nbyte 
)

Write file.

The function writes nbyte bytes from the buffer pointed to by buf to the file associated with the open file descriptor, fd.

Parameters
[in]fdFile descriptor.
[in]bufBuffer containing data to be written.
[in]nbyteNumber of bytes to write.
Returns
Upon successful completion returns the number of bytes written. Otherwise, -1 is returned
Note
<Notes>

m2mb_fs_write( fd, (UINT8*)buffer, (UINT32)length );

int main()
{
INT32 fd;
UINT8 file_name[128];
UINT32 profile_num;
static UINT8 profile_pwd[17];
UINT8 i, *data = NULL;
INT32 len;
INT32 bytes_read;
M2MB_STATUS_T status;
status = m2mb_fs_init( );
if(status == M2MB_ERROR)
{
return M2MB_ERROR;
}
snprintf( file_name, 128, "/spinor/sys/profile%d.json", profile_num );
fd = m2mb_fs_secure_open( file_name, M2MB_O_CREAT | M2MB_O_WRONLY, profile_pwd );
if( fd == -1 )
{
return M2MB_ERROR;
}
else
{
INT32 size_fd = m2mb_fs_write( fd, (UINT8*)buffer, (UINT32)length );
if( length != size_fd )
{
return M2MB_ERROR;
}
}
return M2MB_OK;
}