Milos RTOS v0.3.4a
Real Time Operating System
File functions
Collaboration diagram for File functions:

Functions

__PFILE __fileOpen (__CONST __PSTRING file, u8 mode)
 Opens a file.
u32 __fileRead (__PFILE file, __PVOID buf, u32 qty)
 Reads file content.
__FILE_RES __fileSeek (__PFILE file, u32 offs)
 Seeks to an absolute offset.
__FILE_RES __fileClose (__PFILE file)
__FILE_RES __fileGetStatus (__CONST __PSTRING file, __PFILEINFO fi)
 Get the file status.
u32 __fileWrite (__PFILE file, __PVOID buf, u32 qty)
 Writes data to a file.
__FILE_RES __fileFlush (__PFILE file)
 Flushes unwritten data.
__FILE_RES __fileTruncate (__PFILE file)
 Truncates the file size.
__FILE_RES __fileDelete (__CONST __PSTRING file)
 Deletes a file.
__FILE_RES __fileChmod (__CONST __PSTRING file, u8 value, u8 mask)
 Changes the attributes of a file or directory.
__FILE_RES __fileChangeTime (__CONST __PSTRING file, __CONST __PFILEINFO fi)
 Changes the timestamp of a file or directory.
__FILE_RES __fileRename (__CONST __PSTRING name1, __CONST __PSTRING name2)
 Renames a file or directory.

Detailed Description

File/Directory functions.


Function Documentation

__PFILE __fileOpen ( __CONST __PSTRING  file,
u8  mode 
)

Opens a file.

The pointer returned is dynamically allocated, and it will be destroyed in __fileClose().

Parameters:
fileFile name.
modeOpen mode.
  • __FILE_READ Specifies read access to the object. Data can be read from the file. Combine with __FILE WRITE for read-write access.
  • __FILE_OPEN_EXISTING Opens the file. The function fails if the file is not existing.
  • __FILE_WRITE Specifies write access to the object. Data can be written to the file. Combine with FA_READ for read-write access.
  • __FILE_CREATE_NEW Creates a new file. If the file is existing, it is truncated and overwritten.
  • __FILE_CREATE_ALWAYS Creates a new file. The function fails with FR_EXIST if the file exists.
  • __FILE_OPEN_ALWAYS Opens the file if it exists. If not, a new file is created. To append data to the file, use __fileSeek() after file open. in this method.
Returns:
A pointer to the file, to be used in consequent calls to the file system functions. Returns __NULL on failure.

Definition at line 470 of file fat.c.

{
    __PFILE ptr;

    ptr = (__PFILE) __heapAllocZero(sizeof(__FILE));
    if (!ptr) return __NULL;

    if (f_open(ptr, file, mode) != FR_OK)
    {
        __heapFree(ptr);
        return __NULL;
    }

    return ptr;
}

Here is the call graph for this function:

u32 __fileRead ( __PFILE  file,
__PVOID  buf,
u32  qty 
)

Reads file content.

Parameters:
fileFile opened with __fileOpen().
bufBuffer to receive the read data.
qtyQuantity of bytes to read.
Returns:
The quantity of bytes reads. The read operation is successfully completed when the returned value equals qty.

Definition at line 496 of file fat.c.

{
    UINT ret = 0;

    f_read(file, buf, qty, &ret);
    return ret;
}
__FILE_RES __fileSeek ( __PFILE  file,
u32  offs 
)

Seeks to an absolute offset.

From FatFS documentation: The f_lseek function moves the file read/write pointer of an open file. The offset can be specified in only origin from top of the file. When an offset above the file size is specified in write mode, the file size is increased and the data in the expanded area is undefined. This is suitable to create a large file quickly, for fast write operation. After the f_lseek function succeeded, member fptr in the file object should be checked in order to make sure the read/write pointer has been moved correctly. In case of fptr is not the expected value, either of followings has been occurred. End of file. The specified Offset was clipped at the file size because the file has been opened in read-only mode. Disk full. There is insufficient free space on the volume to expand the file size.

When _USE_FASTSEEK is set to 1 and cltbl member in the file object is not NULL, the fast seek feature is enabled. This feature enables fast backward/long seek operations without FAT access by cluster link information stored on the user defined table. The cluster link information must be created prior to do the fast seek. The required size of the table is (number of fragments + 1) * 2 items. For example, when the file is fragmented in 5, 12 items will be required to store the cluster link information. The file size cannot be expanded when the fast seek feature is enabled.

Parameters:
fileFile opened with __fileOpen().
offsOffset
Returns:
One of the following codes:
  • FR_OK (0) The function succeeded.
  • FR_DISK_ERR The function failed due to an error in the disk function.
  • FR_INT_ERR The function failed due to a wrong FAT structure or an internal error.
  • FR_NOT_READY The disk drive cannot work due to no medium in the drive or any other reason.
  • FR_INVALID_OBJECT The file object is invalid.
  • FR_NOT_ENOUGH_CORE Insufficient size of link map table for the file.

Definition at line 538 of file fat.c.

{
    return f_lseek(file, offs);
}

Closes a opened file.

From FatFS documentation: The f_close function closes an open file object. If any data has been written to the file, the cached information of the file is written back to the disk. After the function succeeded, the file object is no longer valid and it can be discarded.

Parameters:
filePointer to a file created with __fileOpen().
Returns:
One of the following codes:
  • FR_OK (0) The file object has been closed successfuly.
  • FR_DISK_ERR The function failed due to an error in the disk function.
  • FR_INT_ERR The function failed due to a wrong FAT structure or an internal error.
  • FR_NOT_READY The disk drive cannot work due to no medium in the drive or any other reason.
  • FR_INVALID_OBJECT The file object is invalid.

Definition at line 560 of file fat.c.

{
    __FILE_RES ret;
    ret = f_close(file);
    if (ret == FR_OK) {
        __heapFree(file);
    }

    return ret;
}

Here is the call graph for this function:

Get the file status.

From FatFS documentation: The f_stat gets the information of a file or directory. For details of the information, refer to the FILINFO structure and f_readdir function. This function is not supported in minimization level of >= 1.

Parameters:
fileFile name.
fiPointer to a __FILEINFO structure to receive the file information.
Returns:
One of the following values:
  • FR_OK (0) The function succeeded.
  • FR_NO_FILE Could not find the file or directory.
  • FR_NO_PATH Could not find the path.
  • FR_INVALID_NAME The file name is invalid.
  • FR_INVALID_DRIVE The drive number is invalid.
  • FR_NOT_READY The disk drive cannot work due to no medium in the drive or any other reason.
  • FR_DISK_ERR The function failed due to an error in the disk function.
  • FR_INT_ERR The function failed due to a wrong FAT structure or an internal error.
  • FR_NOT_ENABLED The logical drive has no work area.
  • FR_NO_FILESYSTEM There is no valid FAT volume on the drive.

Definition at line 595 of file fat.c.

{
    return f_stat(file, fi);
}
u32 __fileWrite ( __PFILE  file,
__PVOID  buf,
u32  qty 
)

Writes data to a file.

From FatFS documentation: The read/write pointer in the file object is increased in number of bytes written. After the function succeeded, *ByteWritten should be checked to detect the disk full. In case of *ByteWritten < ByteToWrite, it means the volume got full during the write operation. The function can take a time when the volume is full or close to full.

Parameters:
filePointer to a opened file.
bufPointer to the buffer to write.
qtyQuantity to write, in bytes.
Returns:
The quantity of bytes written.

Definition at line 616 of file fat.c.

{
    UINT ret;
    f_write(file, buf, qty, &ret);
    return ret;
}

Flushes unwritten data.

From FatFS documentation: The __fileFlush (f_sync) function performs the same process as __fileClose (f_close) function but the file is left opened and can continue read/write/seek operations to the file. This is suitable for the applications that open files for a long time in write mode, such as data logger. Performing f_sync of periodic or immediately after __fileFlush (f_write) can minimize the risk of data loss due to a sudden blackout or an unintentional disk removal. However f_sync immediately before f_close has no advantage because f_close performs f_sync in it. In other words, the difference between those functions is that the file object is invalidated or not.

Parameters:
filePointer to opened file.
Returns:
Any of the following values:
  • FR_OK (0) The function succeeded.
  • FR_DISK_ERR The function failed due to an error in the disk function.
  • FR_INT_ERR The function failed due to a wrong FAT structure or an internal error.
  • FR_NOT_READY The disk drive cannot work due to no medium in the drive or any other reason.
  • FR_INVALID_OBJECT The file object is invalid.

Definition at line 644 of file fat.c.

{
    return f_sync(file);
}

Truncates the file size.

From FatFS documentation The f_truncate function truncates the file size to the current file read/write point. This function has no effect if the file read/write pointer is already pointing end of the file.

Parameters:
filePointer to an opened file.
Returns:
Any of the following values:
  • FR_OK (0) The function succeeded.
  • FR_DENIED The file has been opened in read-only mode.
  • FR_DISK_ERR The function failed due to an error in the disk function.
  • FR_INT_ERR The function failed due to a wrong FAT structure or an internal error.
  • FR_NOT_READY The disk drive cannot work due to no medium in the drive or any other reason.
  • FR_INVALID_OBJECT The file object is invalid.

Definition at line 666 of file fat.c.

{
    return f_truncate(file);
}

Deletes a file.

From FatFS documentation: The f_unlink function removes an object. Do not remove open objects.

Parameters:
filePointer to a null-terminated string containing the file name.
Returns:
Any of the following values:
  • FR_OK (0) The function succeeded.
  • FR_NO_FILE Could not find the file or directory.
  • FR_NO_PATH Could not find the path.
  • FR_INVALID_NAME The path name is invalid.
  • FR_INVALID_DRIVE The drive number is invalid.
  • FR_DENIED The function was denied due to either of following reasons: The object has read-only attribute Not empty directory Current directory
  • FR_NOT_READY The disk drive cannot work due to no medium in the drive or any other reason.
  • FR_WRITE_PROTECTED The medium is write protected.
  • FR_DISK_ERR The function failed due to an error in the disk function.
  • FR_INT_ERR The function failed due to a wrong FAT structure or an internal error.
  • FR_NOT_ENABLED The logical drive has no work area.
  • FR_NO_FILESYSTEM There is no valid FAT volume on the drive.
  • FR_LOCKED The function was rejected due to file sharing policy (_FS_SHARE)

Definition at line 697 of file fat.c.

{
    return f_unlink(file);
}
__FILE_RES __fileChmod ( __CONST __PSTRING  file,
u8  value,
u8  mask 
)

Changes the attributes of a file or directory.

Parameters:
filePointer to a null-terminated string containing the file name.
valueA combination of the following flags:
  • AM_RDO Read only
  • AM_ARC Archive
  • AM_SYS System
  • AM_HID Hidden
maskAttribute mask that specifies which attribute is changed. The specified attributes are set or cleared.
Returns:
Ant of the following values
  • FR_OK (0) The function succeeded.
  • FR_NO_FILE Could not find the file.
  • FR_NO_PATH Could not find the path.
  • FR_INVALID_NAME The file name is invalid.
  • FR_INVALID_DRIVE The drive number is invalid.
  • FR_NOT_READY The disk drive cannot work due to no medium in the drive or any other reason.
  • FR_WRITE_PROTECTED The medium is write protected.
  • FR_DISK_ERR The function failed due to an error in the disk function.
  • FR_INT_ERR The function failed due to a wrong FAT structure or an internal error.
  • FR_NOT_ENABLED The logical drive has no work area.
  • FR_NO_FILESYSTEM There is no valid FAT volume on the drive.

Definition at line 728 of file fat.c.

{
    return f_chmod(file, value, mask);
}

Changes the timestamp of a file or directory.

Parameters:
filePointer to a null-terminated string containing the file name.
fiPointer to the file information structure that has a timestamp to be set in member fdate and ftime. Other members will be ignored.
Returns:
Any of the following values:
  • FR_OK (0) The function succeeded.
  • FR_NO_FILE Could not find the file.
  • FR_NO_PATH Could not find the path.
  • FR_INVALID_NAME The file name is invalid.
  • FR_INVALID_DRIVE The drive number is invalid.
  • FR_NOT_READY The disk drive cannot work due to no medium in the drive or any other reason.
  • FR_WRITE_PROTECTED The medium is write protected.
  • FR_DISK_ERR The function failed due to an error in the disk function.
  • FR_INT_ERR The function failed due to a wrong FAT structure or an internal error.
  • FR_NOT_ENABLED The logical drive has no work area.
  • FR_NO_FILESYSTEM There is no valid FAT volume on the drive.

Definition at line 754 of file fat.c.

{
    return f_utime(file, fi);
}

Renames a file or directory.

Parameters:
name1Pointer to a null-terminated string containing the new file name.
name2Pointer to a null-terminated string containing the old file name.
Returns:
Any of the following values:
  • FR_OK (0) The function succeeded.
  • FR_NO_FILE Could not find the old name.
  • FR_NO_PATH Could not find the path.
  • FR_INVALID_NAME The file name is invalid.
  • FR_INVALID_DRIVE The drive number is invalid.
  • FR_NOT_READY The disk drive cannot work due to no medium in the drive or any other reason.
  • FR_EXIST The new name is colliding with an existing name.
  • FR_DENIED The new name could not be created due to any reason.
  • FR_WRITE_PROTECTED The medium is write protected.
  • FR_DISK_ERR The function failed due to an error in the disk function.
  • FR_INT_ERR The function failed due to a wrong FAT structure or an internal error.
  • FR_NOT_ENABLED The logical drive has no work area.
  • FR_NO_FILESYSTEM There is no valid FAT volume on the drive.
  • FR_LOCKEDThe function was rejected due to file shareing policy (_FS_SHARE).
 All Data Structures Files Functions Variables Typedefs Defines