
Write a File
116
SWRU455A – February 2017 – Revised March 2017
Copyright © 2017, Texas Instruments Incorporated
File System
The return value of the write function is the number of bytes written, and a negative value is an error
number.
The following is a partial list of errors that might be returned by the write function:
•
SL_ERROR_FS_OFFSET_OUT_OF_RANGE: The file can be written to offsets which are less than the
maximum file size, and trying to write a file to an offset which exceeded the maximum file size results
in an error.
•
SL_ERROR_FS_INVALID_HANDLE: The input file handle is illegal.
•
SL_ERROR_FS_OFFSET_NOT_16_BYTE_ALIGN: For a secure file, when trying to write to an offset,
this is not sequential.
•
SL_ERROR_FS_FILE_ACCESS_IS_DIFFERENT: Trying to read a file which was opened for read.
Example:
_i32
FileHdl;
_i32
Status;
_u32
Offset = 0;
unsigned char
pData[100];
_u32
Len = 0;
Status = sl_FsWrite( FileHdl, Offset, pData, Len );
if
( Status < 0 )
{
/* error */
/* abort */
sl_FsClose(FileHdl,0,'A',1);
}
8.4.6 Close an Opened (for Write) File
Closing or aborting an opened file is mandatory. Closing the file frees the file resources from the device
memory and sets the last file copy (if one exists) as the active one.
If the host application decides not to write an opened file due to an error, use abort instead of close.
The abort function requires the file handle. How to abort a file without using the file handle is described in
the file commit-rollback function.
A file that was not closed or aborted appears in the file system; its storage is allocated, but it might have
no valid copy. Such files can be observed by the SL_FS_INFO_NOT_VALID flag, and the file flags can be
retrieved by the sl_FsGetFileList () or sl_FsGetInfo () functions.
For a file opened with the FAILSAFE flag that has a valid copy (from a previous write operation), invoking
the abort function sets the nonactive copy as the active one, so that the next read operation reads the
valid copy and not the one that has been aborted.
In the case of an unexpected or sudden shutdown, each file opened for write that has not been closed is
treated as if abort has been called for this file.
The function returns 0 for success, and a negative number for an error.
The following is a partial list of errors that might be returned by the close after write function:
•
SL_ERROR_FS_INVALID_HANDLE: The input file handle is illegal.
•
SL_ERROR_FS_CERT_CHAIN_ERROR_SECURITY_ALERT: For a secure signed file, testing the
certificate chain of trust failed, and a security alert is triggered.
•
SL_ERROR_FS_CERT_IN_THE_CHAIN_REVOKED_SECURITY_ALERT: For a secure signed file,
the certificate chain of trust exists in the revoked list, and a security alert is triggered.
•
SL_ERROR_FS_WRONG_SIGNATURE_SECURITY_ALERT: For a secure signed file, the signature
test failed, and a security alert is triggered.
•
SL_ERROR_FS_WRONG_CERTIFICATE_FILE_NAME: For a secure signed file, if one of the
certificates in the chain of trust is missing, it does not trigger security alerts.
Close nonsigned file example:
_i32
FileHdl;