WL865E4-P  36.07.001
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
 
#define M2MB_FS_ENOERR   0
 
#define M2MB_FS_EPERM   1
 
#define M2MB_FS_ENOENT   2
 
#define M2MB_FS_EIO   5
 
#define M2MB_FS_EBADF   9
 
#define M2MB_FS_ENOMEM   12
 
#define M2MB_FS_EACCES   13
 
#define M2MB_FS_EBUSY   16
 
#define M2MB_FS_EEXIST   17
 
#define M2MB_FS_EXDEV   18
 
#define M2MB_FS_ENODEV   19
 
#define M2MB_FS_ENOTDIR   20
 
#define M2MB_FS_EISDIR   21
 
#define M2MB_FS_EINVAL   22
 
#define M2MB_FS_EMFILE   24
 
#define M2MB_FS_ETXTBSY   26
 
#define M2MB_FS_ENOSPC   28
 
#define M2MB_FS_ESPIPE   29
 
#define M2MB_FS_ENAMETOOLONG   36
 
#define M2MB_FS_ENOTEMPTY   39
 
#define M2MB_FS_ELOOP   40
 
#define M2MB_FS_EILSEQ   84
 
#define M2MB_FS_ETIMEDOUT   110
 
#define M2MB_FS_ESTALE   116
 
#define M2MB_FS_EDQUOT   122
 
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, SIZE_T nbyte)
 Read file. More...
 
SSIZE_T m2mb_fs_write (INT32 fd, const void *buf, SIZE_T nbyte)
 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_statvfs (const CHAR *path, struct M2MB_STATVFS *buf)
 File system statistics. More...
 
INT32 m2mb_fs_ls_open (const char *path, HANDLE *handle)
 List over all the files in the file system. 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_FS_EACCES

#define M2MB_FS_EACCES   13

Permission denied

Definition at line 74 of file m2mb_fs_posix.h.

◆ M2MB_FS_EBADF

#define M2MB_FS_EBADF   9

Bad file descriptor

Definition at line 72 of file m2mb_fs_posix.h.

◆ M2MB_FS_EBUSY

#define M2MB_FS_EBUSY   16

Device or resource busy

Definition at line 75 of file m2mb_fs_posix.h.

◆ M2MB_FS_EDQUOT

#define M2MB_FS_EDQUOT   122

Quota exceeded

Definition at line 92 of file m2mb_fs_posix.h.

◆ M2MB_FS_EEXIST

#define M2MB_FS_EEXIST   17

File exists

Definition at line 76 of file m2mb_fs_posix.h.

◆ M2MB_FS_EILSEQ

#define M2MB_FS_EILSEQ   84

Illegal byte sequence

Definition at line 89 of file m2mb_fs_posix.h.

◆ M2MB_FS_EINVAL

#define M2MB_FS_EINVAL   22

Invalid argument

Definition at line 81 of file m2mb_fs_posix.h.

◆ M2MB_FS_EIO

#define M2MB_FS_EIO   5

I/O error

Definition at line 71 of file m2mb_fs_posix.h.

◆ M2MB_FS_EISDIR

#define M2MB_FS_EISDIR   21

Is a directory

Definition at line 80 of file m2mb_fs_posix.h.

◆ M2MB_FS_ELOOP

#define M2MB_FS_ELOOP   40

Too many symbolic links encountered

Definition at line 88 of file m2mb_fs_posix.h.

◆ M2MB_FS_EMFILE

#define M2MB_FS_EMFILE   24

Too many open files

Definition at line 82 of file m2mb_fs_posix.h.

◆ M2MB_FS_ENAMETOOLONG

#define M2MB_FS_ENAMETOOLONG   36

File name too long

Definition at line 86 of file m2mb_fs_posix.h.

◆ M2MB_FS_ENODEV

#define M2MB_FS_ENODEV   19

No such device

Definition at line 78 of file m2mb_fs_posix.h.

◆ M2MB_FS_ENOENT

#define M2MB_FS_ENOENT   2

No such file or directory

Definition at line 70 of file m2mb_fs_posix.h.

◆ M2MB_FS_ENOERR

#define M2MB_FS_ENOERR   0

Success

Definition at line 68 of file m2mb_fs_posix.h.

◆ M2MB_FS_ENOMEM

#define M2MB_FS_ENOMEM   12

Out of memory

Definition at line 73 of file m2mb_fs_posix.h.

◆ M2MB_FS_ENOSPC

#define M2MB_FS_ENOSPC   28

No space left on device

Definition at line 84 of file m2mb_fs_posix.h.

◆ M2MB_FS_ENOTDIR

#define M2MB_FS_ENOTDIR   20

Not a directory

Definition at line 79 of file m2mb_fs_posix.h.

◆ M2MB_FS_ENOTEMPTY

#define M2MB_FS_ENOTEMPTY   39

Directory not empty

Definition at line 87 of file m2mb_fs_posix.h.

◆ M2MB_FS_EPERM

#define M2MB_FS_EPERM   1

Operation not permitted

Definition at line 69 of file m2mb_fs_posix.h.

◆ M2MB_FS_ESPIPE

#define M2MB_FS_ESPIPE   29

Illegal seek

Definition at line 85 of file m2mb_fs_posix.h.

◆ M2MB_FS_ESTALE

#define M2MB_FS_ESTALE   116

Stale remote file handle.

Definition at line 91 of file m2mb_fs_posix.h.

◆ M2MB_FS_ETIMEDOUT

#define M2MB_FS_ETIMEDOUT   110

Operation/Connection timed out.

Definition at line 90 of file m2mb_fs_posix.h.

◆ M2MB_FS_ETXTBSY

#define M2MB_FS_ETXTBSY   26

File or directory busy (already open)

Definition at line 83 of file m2mb_fs_posix.h.

◆ M2MB_FS_EXDEV

#define M2MB_FS_EXDEV   18

Cross-device link

Definition at line 77 of file m2mb_fs_posix.h.

◆ 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.

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;
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_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")];
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")];
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 
)

List over all the files in the file system. 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")];
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;
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;
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,
SIZE_T  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;
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];
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};
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_statvfs()

INT32 m2mb_fs_statvfs ( const CHAR path,
struct M2MB_STATVFS buf 
)

File system statistics.

The function gets information about file system specified by the string pointed to by path, and writes it to the area pointed to by buf.

Parameters
[in]pathPathname of any file within the mounted file system.
[in]bufPointer to a statvfs structure (see definition) to be filled with the information about file system.
Returns
Upon successful completion 0 is returned. Otherwise, -1 is returned.
Note
In parameter path, root or any mounted path can be given to get information about file system.

m2mb_fs_statvfs( 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_STATVFS stat = {0};
ret = m2mb_fs_statvfs( 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;
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,
SIZE_T  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;
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;
}