[feat][fatfs] add posix api support
This commit is contained in:
parent
a155fb5e6c
commit
19b5a059b0
@ -17,7 +17,7 @@ list(APPEND ADD_SRCS ${sources})
|
|||||||
#######################################################
|
#######################################################
|
||||||
|
|
||||||
########### Add required/dependent components #########
|
########### Add required/dependent components #########
|
||||||
#list(APPEND ADD_REQUIREMENTS xxx)
|
list(APPEND ADD_REQUIREMENTS common)
|
||||||
#######################################################
|
#######################################################
|
||||||
|
|
||||||
############ Add static libs ##########################
|
############ Add static libs ##########################
|
||||||
|
622
components/fatfs/fatfs_posix_api.c
Normal file
622
components/fatfs/fatfs_posix_api.c
Normal file
@ -0,0 +1,622 @@
|
|||||||
|
/**
|
||||||
|
* @file fatfs_posix_api.c
|
||||||
|
* @brief
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Bouffalolab team
|
||||||
|
*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership. The
|
||||||
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance with the
|
||||||
|
* License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief POSIX Common File System Interface,
|
||||||
|
* Based on the fatfs.
|
||||||
|
* Only part of the interface is implemented
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "fatfs_posix_api.h"
|
||||||
|
#include "ff.h"
|
||||||
|
#include "stdlib.h"
|
||||||
|
|
||||||
|
/*Memory request interface*/
|
||||||
|
static void *(*vfs_malloc)(size_t size) = mmheap_alloc; //默认的内存申请接口
|
||||||
|
static void (*vfs_free)(void *ptr) = mmheap_free; //默认的内存释放接口
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief fs init
|
||||||
|
*
|
||||||
|
* @return 0 on successful, -1 on failed.
|
||||||
|
*/
|
||||||
|
int ff_vfs_init(void)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief FATF error conversion,Used for compatible Linux system error codes
|
||||||
|
*
|
||||||
|
* @param Status
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
int ff_vfs_error(FRESULT Status)
|
||||||
|
{
|
||||||
|
switch (Status) {
|
||||||
|
case FR_OK:
|
||||||
|
return 0;
|
||||||
|
case FR_DISK_ERR:
|
||||||
|
return -EIO; /* (1) A hard error occurred in the low level disk I/O layer */
|
||||||
|
case FR_INT_ERR:
|
||||||
|
return -EPIPE; /* (2) Assertion failed */
|
||||||
|
case FR_NOT_READY:
|
||||||
|
return -EIO; /* (3) The physical drive cannot work */
|
||||||
|
case FR_NO_FILE:
|
||||||
|
return -ENOENT; /* (4) Could not find the file */
|
||||||
|
case FR_NO_PATH:
|
||||||
|
return -ENOENT; /* (5) Could not find the path */
|
||||||
|
case FR_INVALID_NAME:
|
||||||
|
return -ENOEXEC; /* (6) The path name format is invalid */
|
||||||
|
case FR_DENIED:
|
||||||
|
return -ENOSPC; /* (7) Access denied due to prohibited access or directory full */
|
||||||
|
case FR_EXIST:
|
||||||
|
return -EACCES; /* (8) Access denied due to prohibited access */
|
||||||
|
case FR_INVALID_OBJECT:
|
||||||
|
return -ENXIO; /* (9) The file/directory object is invalid */
|
||||||
|
case FR_WRITE_PROTECTED:
|
||||||
|
return -EROFS; /* (10) The physical drive is write protected */
|
||||||
|
case FR_INVALID_DRIVE:
|
||||||
|
return -ENXIO; /* (11) The logical drive number is invalid */
|
||||||
|
case FR_NOT_ENABLED:
|
||||||
|
return -ENXIO; /* (12) The volume has no work area */
|
||||||
|
case FR_NO_FILESYSTEM:
|
||||||
|
return -EPERM; /* (13) There is no valid FAT volume */
|
||||||
|
case FR_MKFS_ABORTED:
|
||||||
|
return -EPERM; /* (14) The f_mkfs() aborted due to any problem */
|
||||||
|
case FR_TIMEOUT:
|
||||||
|
return -EBUSY; /* (15) Could not get a grant to access the volume within defined period */
|
||||||
|
case FR_LOCKED:
|
||||||
|
return -EACCES; /* (16) The operation is rejected according to the file sharing policy */
|
||||||
|
case FR_NOT_ENOUGH_CORE:
|
||||||
|
return -ENOMEM; /* (17) LFN working buffer could not be allocated */
|
||||||
|
case FR_TOO_MANY_OPEN_FILES:
|
||||||
|
return -EMFILE; /* (18) Number of open files > FF_FS_LOCK */
|
||||||
|
case FR_INVALID_PARAMETER:
|
||||||
|
return -EINVAL; /* (19) Given parameter is invalid */
|
||||||
|
default:
|
||||||
|
return -EPERM;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Open the file described in PATH
|
||||||
|
*
|
||||||
|
* @param path The file path
|
||||||
|
* @param flags The file's parameters:
|
||||||
|
* O_RDONLY: read-only
|
||||||
|
* O_WRONLY: write-only
|
||||||
|
* O_RDWR: readable and writable
|
||||||
|
* O_CREAT: Create a file when it doesn't exist
|
||||||
|
* O_EXCL: When the file is opened, returned -EEXIST if the file exists and the flag bit O_CREAT is also specified.
|
||||||
|
* If the file does not exist, the file is created
|
||||||
|
* O_TRUNC: When opening the file, if the length of the file is not 0, the length of the file will be truncated to 0,
|
||||||
|
* and the next time the file is written, the data will be written from the file
|
||||||
|
* O_APPEND: After opening the file, set the write point of the file to the end of the file. The next time the file is written,
|
||||||
|
* the newly written data will be appended to the end of the file
|
||||||
|
* @param mode The permissions, not currently supported, ignored
|
||||||
|
* @return int Positive: File descriptor (actually a pointer); Negative: Error execution, return error code
|
||||||
|
*/
|
||||||
|
int ff_open(const char *path, int flags, int mode)
|
||||||
|
{
|
||||||
|
uint8_t OpenMode = 0;
|
||||||
|
FIL *pFile;
|
||||||
|
FRESULT Status;
|
||||||
|
|
||||||
|
pFile = (FIL *)vfs_malloc(sizeof(FIL));
|
||||||
|
if (pFile == NULL) {
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flags & O_WRONLY) {
|
||||||
|
OpenMode |= FA_WRITE;
|
||||||
|
}
|
||||||
|
if (flags & O_RDWR) {
|
||||||
|
OpenMode |= FA_WRITE | FA_READ;
|
||||||
|
}
|
||||||
|
if ((OpenMode & (FA_WRITE | FA_READ)) == 0) /*If no read-write is specified, the default is read-only*/
|
||||||
|
{
|
||||||
|
OpenMode |= FA_READ;
|
||||||
|
}
|
||||||
|
if ((flags & O_CREAT) && (flags & O_EXCL)) {
|
||||||
|
OpenMode |= FA_CREATE_NEW;
|
||||||
|
} else if (flags & O_CREAT) {
|
||||||
|
OpenMode |= FA_OPEN_ALWAYS;
|
||||||
|
}
|
||||||
|
if (flags & O_TRUNC) {
|
||||||
|
OpenMode |= FA_CREATE_ALWAYS;
|
||||||
|
}
|
||||||
|
if (flags & O_APPEND) {
|
||||||
|
OpenMode |= FA_OPEN_APPEND;
|
||||||
|
}
|
||||||
|
|
||||||
|
Status = f_open(pFile, path, OpenMode);
|
||||||
|
if (Status != FR_OK) {
|
||||||
|
vfs_free(pFile);
|
||||||
|
pFile = NULL;
|
||||||
|
|
||||||
|
if ((flags & O_CREAT) && (flags & O_EXCL) && (Status == FR_EXIST)) {
|
||||||
|
return -EEXIST;
|
||||||
|
}
|
||||||
|
return ff_vfs_error(Status);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (int)pFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Close the file
|
||||||
|
*
|
||||||
|
* @param fd The descriptor of the file
|
||||||
|
* @return int 0: success; Negative: Error execution, return error code
|
||||||
|
*/
|
||||||
|
int ff_close(int fd)
|
||||||
|
{
|
||||||
|
FRESULT Status;
|
||||||
|
|
||||||
|
if (fd <= 0)
|
||||||
|
return -ENXIO;
|
||||||
|
Status = f_close((FIL *)fd);
|
||||||
|
if (Status == FR_OK) {
|
||||||
|
vfs_free((void *)fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ff_vfs_error(Status);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Read file data
|
||||||
|
*
|
||||||
|
* @param fd The descriptor of the file
|
||||||
|
* @param buf The cache of data read by the file
|
||||||
|
* @param nbytes Number of bytes to read
|
||||||
|
* @return int Read successfully, return the number of bytes read;
|
||||||
|
* Returns 0 if the file offset reached the end of the file when it was read.
|
||||||
|
* Other error conditions, return the error code
|
||||||
|
*/
|
||||||
|
int ff_read(int fd, void *buf, size_t nbytes)
|
||||||
|
{
|
||||||
|
FRESULT Status;
|
||||||
|
UINT cnt;
|
||||||
|
|
||||||
|
if (fd <= 0)
|
||||||
|
return -ENXIO;
|
||||||
|
Status = f_read((FIL *)fd, buf, nbytes, (UINT *)&cnt);
|
||||||
|
if (Status != FR_OK) {
|
||||||
|
return ff_vfs_error(Status);
|
||||||
|
}
|
||||||
|
return cnt;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Writes data to a file
|
||||||
|
*
|
||||||
|
* @param fd The descriptor of the file
|
||||||
|
* @param buf The cache of data to write to the file
|
||||||
|
* @param nbytes The length of the file written
|
||||||
|
* @return int When successfully written, the number of bytes written to the file is returned.
|
||||||
|
* The actual number of bytes written may be less than nbytes (insufficient file space).
|
||||||
|
* Returns an error code (negative) when writing fails
|
||||||
|
*/
|
||||||
|
int ff_write(int fd, const void *buf, size_t nbytes)
|
||||||
|
{
|
||||||
|
FRESULT Status;
|
||||||
|
UINT cnt;
|
||||||
|
|
||||||
|
if (fd <= 0)
|
||||||
|
return -ENXIO;
|
||||||
|
Status = f_write((FIL *)fd, buf, nbytes, (UINT *)&cnt);
|
||||||
|
if (Status != FR_OK) {
|
||||||
|
return ff_vfs_error(Status);
|
||||||
|
}
|
||||||
|
return cnt;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Resets the offset of the open file
|
||||||
|
*
|
||||||
|
* @param fd The descriptor of the file
|
||||||
|
* @param offset Offset from the third parameter whence
|
||||||
|
* @param whence File offset basics
|
||||||
|
* VFS_SEEK_SET: Based on file headers
|
||||||
|
* VFS_SEEK_CUR: Based on the current location
|
||||||
|
* VFS_SEEK_END: Based on file end
|
||||||
|
* @return ff_off_t Non-negative: on success, returns the number of offset bytes relative to the start of the file;
|
||||||
|
* Negative: Failure, return error code
|
||||||
|
*/
|
||||||
|
ff_off_t ff_lseek(int fd, ff_off_t offset, int whence)
|
||||||
|
{
|
||||||
|
FRESULT Status;
|
||||||
|
long long temp;
|
||||||
|
|
||||||
|
if (fd <= 0)
|
||||||
|
return -ENXIO;
|
||||||
|
|
||||||
|
switch (whence) {
|
||||||
|
case VFS_SEEK_SET: {
|
||||||
|
Status = f_lseek((FIL *)fd, offset);
|
||||||
|
if (Status == FR_OK) {
|
||||||
|
return (ff_off_t)((FIL *)fd)->fptr;
|
||||||
|
}
|
||||||
|
return ff_vfs_error(Status);
|
||||||
|
}
|
||||||
|
case VFS_SEEK_CUR: {
|
||||||
|
temp = (ff_off_t)((FIL *)fd)->fptr;
|
||||||
|
temp += offset;
|
||||||
|
if (temp >= 0xffffffff)
|
||||||
|
temp = 0xffffffff - 1;
|
||||||
|
Status = f_lseek((FIL *)fd, temp);
|
||||||
|
if (Status == FR_OK) {
|
||||||
|
return (ff_off_t)((FIL *)fd)->fptr;
|
||||||
|
}
|
||||||
|
return ff_vfs_error(Status);
|
||||||
|
}
|
||||||
|
case VFS_SEEK_END: {
|
||||||
|
temp = (ff_off_t)((FIL *)fd)->obj.objsize;
|
||||||
|
temp -= offset;
|
||||||
|
if (temp <= 0)
|
||||||
|
temp = 0;
|
||||||
|
Status = f_lseek((FIL *)fd, temp);
|
||||||
|
if (Status == FR_OK) {
|
||||||
|
return (ff_off_t)((FIL *)fd)->fptr;
|
||||||
|
}
|
||||||
|
return ff_vfs_error(Status);
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Update the cache to memory
|
||||||
|
*
|
||||||
|
* @param fd The descriptor of the file
|
||||||
|
* @return int 0: success;
|
||||||
|
* Negative: Data synchronization failure, return error code
|
||||||
|
*/
|
||||||
|
int ff_sync(int fd)
|
||||||
|
{
|
||||||
|
FRESULT Status;
|
||||||
|
|
||||||
|
if (fd <= 0)
|
||||||
|
return -ENXIO; //文件指针为空,直接返回
|
||||||
|
Status = f_sync((FIL *)fd); //存储文件
|
||||||
|
return ff_vfs_error(Status); //错误代码转义
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the attributes of the file(path)
|
||||||
|
*
|
||||||
|
* @param path The file path
|
||||||
|
* @param st Pointer to file status information structure
|
||||||
|
* @return int 0: success;
|
||||||
|
* Negative: Data synchronization failure, return error code
|
||||||
|
*/
|
||||||
|
int ff_stat(const char *path, struct stat *st)
|
||||||
|
{
|
||||||
|
FRESULT Status;
|
||||||
|
FILINFO *pInfo;
|
||||||
|
|
||||||
|
if (path == NULL || st == NULL) {
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
pInfo = vfs_malloc(sizeof(FILINFO));
|
||||||
|
if (pInfo == NULL) {
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
Status = f_stat(path, pInfo);
|
||||||
|
if (Status == FR_OK) {
|
||||||
|
st->st_size = pInfo->fsize;
|
||||||
|
st->st_mode = pInfo->fattrib;
|
||||||
|
st->st_actime = 0;
|
||||||
|
st->st_modtime = 0;
|
||||||
|
}
|
||||||
|
vfs_free(pInfo);
|
||||||
|
return ff_vfs_error(Status);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Get the property information of the file
|
||||||
|
*
|
||||||
|
* @param fd The descriptor of the file
|
||||||
|
* @param st Pointer to file status information structure
|
||||||
|
* @return int 0: success;
|
||||||
|
* Negative: Data synchronization failure, return error code
|
||||||
|
*/
|
||||||
|
int ff_fstat(int fd, struct stat *st)
|
||||||
|
{
|
||||||
|
if (fd <= 0)
|
||||||
|
return -ENXIO;
|
||||||
|
if (st == NULL)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
st->st_size = ((FIL *)fd)->obj.objsize;
|
||||||
|
st->st_mode = 0777;
|
||||||
|
st->st_actime = 0;
|
||||||
|
st->st_modtime = 0;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Checks whether the current program has access to PATH
|
||||||
|
*
|
||||||
|
* @param path The descriptor of the file
|
||||||
|
* @param amode The permissions, not currently supported, ignored
|
||||||
|
* @return int 0: success;
|
||||||
|
* Negative: Data synchronization failure, return error code
|
||||||
|
*/
|
||||||
|
int ff_access(const char *path, int amode)
|
||||||
|
{
|
||||||
|
struct stat st;
|
||||||
|
|
||||||
|
return ff_stat(path, &st);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Retutn the current working directory,
|
||||||
|
* If the buf parameter is not empty, the current working directory string is also copied into the buf
|
||||||
|
*
|
||||||
|
* @param buf Path string buffer
|
||||||
|
* @param size Path buffer size
|
||||||
|
* @return char* Path string buffer
|
||||||
|
*/
|
||||||
|
char *ff_getcwd(char *buf, size_t size)
|
||||||
|
{
|
||||||
|
FRESULT Status;
|
||||||
|
|
||||||
|
if (buf == NULL || size == 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
Status = f_getcwd(buf, size);
|
||||||
|
if (Status == FR_OK) {
|
||||||
|
return buf;
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*************************************************************************************************************************
|
||||||
|
* 函数 : int ff_ftruncate(int fd, ff_off_t length)
|
||||||
|
* 功能 : 指定文件大小
|
||||||
|
* 参数 : fd:已经打开的文件描述符,必须由写入权限;length:指定的文件大小
|
||||||
|
* 返回 : 0:成功;负数:返回错误码
|
||||||
|
* 依赖 : FATFS
|
||||||
|
* 作者 : cp1300@139.com
|
||||||
|
* 时间 : 2020-09-29
|
||||||
|
* 最后修改时间 : 2020-09-29
|
||||||
|
* 说明 :
|
||||||
|
*************************************************************************************************************************/
|
||||||
|
/**
|
||||||
|
* @brief
|
||||||
|
*
|
||||||
|
* @param fd Specify file size,Automatically truncate or fill
|
||||||
|
* @param length File descriptor that is already open. Must have write permission
|
||||||
|
* @return int 0: success;
|
||||||
|
* Negative: Data synchronization failure, return error code
|
||||||
|
*/
|
||||||
|
int ff_ftruncate(int fd, ff_off_t length)
|
||||||
|
{
|
||||||
|
FRESULT Status;
|
||||||
|
UINT cnt;
|
||||||
|
uint32_t PackCnt;
|
||||||
|
uint16_t EndPackSize;
|
||||||
|
uint8_t *pBuff;
|
||||||
|
uint32_t i;
|
||||||
|
|
||||||
|
if (fd <= 0)
|
||||||
|
return -ENXIO;
|
||||||
|
if (length <= ((FIL *)fd)->obj.objsize) {
|
||||||
|
Status = f_lseek((FIL *)fd, length);
|
||||||
|
if (Status == FR_OK) {
|
||||||
|
Status = f_truncate((FIL *)fd);
|
||||||
|
if (Status == FR_OK) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
return ff_vfs_error(Status);
|
||||||
|
} else {
|
||||||
|
Status = f_lseek((FIL *)fd, ((FIL *)fd)->obj.objsize);
|
||||||
|
if (Status == FR_OK) {
|
||||||
|
PackCnt = (length - ((FIL *)fd)->obj.objsize) / 512;
|
||||||
|
EndPackSize = (length - ((FIL *)fd)->obj.objsize) % 512;
|
||||||
|
pBuff = vfs_malloc(512);
|
||||||
|
if (pBuff == NULL) {
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
memset(pBuff, 0, 512);
|
||||||
|
|
||||||
|
for (i = 0; i < PackCnt; i++) {
|
||||||
|
Status = f_write((FIL *)fd, pBuff, 512, (UINT *)&cnt);
|
||||||
|
if (Status != FR_OK) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ((Status == FR_OK) && EndPackSize) {
|
||||||
|
Status = f_write((FIL *)fd, pBuff, EndPackSize, (UINT *)&cnt);
|
||||||
|
if (Status != FR_OK) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vfs_free(pBuff);
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ff_vfs_error(Status);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief
|
||||||
|
*
|
||||||
|
* @param fd
|
||||||
|
* @param cmd
|
||||||
|
* @param lock
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
int ff_fcntl(int fd, int cmd, struct flock *lock)
|
||||||
|
{
|
||||||
|
if (fd <= 0)
|
||||||
|
return -ENXIO; //文件指针为空,直接返回
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Change access to existing files
|
||||||
|
*
|
||||||
|
* @param fd The file descriptor
|
||||||
|
* @param mode
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
int ff_fchmod(int fd, ff_mode_t mode)
|
||||||
|
{
|
||||||
|
if (fd <= 0)
|
||||||
|
return -ENXIO; //文件指针为空,直接返回
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Delete the file
|
||||||
|
*
|
||||||
|
* @param path The file path
|
||||||
|
* @return int 0: success;
|
||||||
|
* Negative: Data synchronization failure, return error code
|
||||||
|
*/
|
||||||
|
int ff_unlink(const char *path)
|
||||||
|
{
|
||||||
|
FRESULT Status;
|
||||||
|
FILINFO *pInfo;
|
||||||
|
|
||||||
|
if (path == NULL)
|
||||||
|
return -ENXIO;
|
||||||
|
pInfo = vfs_malloc(sizeof(FILINFO));
|
||||||
|
if (pInfo == NULL) {
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
Status = f_stat(path, pInfo);
|
||||||
|
if (Status == FR_OK) {
|
||||||
|
Status = f_unlink(path);
|
||||||
|
if (Status != FR_OK) {
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
vfs_free(pInfo);
|
||||||
|
|
||||||
|
return ff_vfs_error(Status);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Create a directory, If it already exists, the creation fails
|
||||||
|
*
|
||||||
|
* @param path The directory path
|
||||||
|
* @return int 0: success;
|
||||||
|
* Negative: Data synchronization failure, return error code
|
||||||
|
*/
|
||||||
|
int ff_mkdir(const char *path)
|
||||||
|
{
|
||||||
|
FRESULT Status;
|
||||||
|
|
||||||
|
if (path == NULL)
|
||||||
|
return -ENXIO;
|
||||||
|
Status = f_mkdir(path);
|
||||||
|
|
||||||
|
return ff_vfs_error(Status);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Delete a directory,When you delete a directory, the directory must be empty
|
||||||
|
*
|
||||||
|
* @param path Directory path
|
||||||
|
* @return int 0: success;
|
||||||
|
* Negative: Data synchronization failure, return error code
|
||||||
|
*/
|
||||||
|
int ff_rmdir(const char *path)
|
||||||
|
{
|
||||||
|
FRESULT Status;
|
||||||
|
FILINFO *pInfo;
|
||||||
|
|
||||||
|
if (path == NULL)
|
||||||
|
return -ENXIO;
|
||||||
|
pInfo = vfs_malloc(sizeof(FILINFO));
|
||||||
|
if (pInfo == NULL) {
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
Status = f_stat(path, pInfo);
|
||||||
|
if (Status == FR_INVALID_NAME) {
|
||||||
|
Status = f_unlink(path);
|
||||||
|
if (Status != FR_OK) {
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (Status != FR_OK) {
|
||||||
|
} else {
|
||||||
|
vfs_free(pInfo);
|
||||||
|
return -ENOTDIR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vfs_free(pInfo);
|
||||||
|
|
||||||
|
return ff_vfs_error(Status);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief
|
||||||
|
*
|
||||||
|
* @param fd
|
||||||
|
* @param owner
|
||||||
|
* @param group
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
|
||||||
|
int ff_fchown(int fd, ff_uid_t owner, ff_gid_t group)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief
|
||||||
|
*
|
||||||
|
* @return ff_uid_t
|
||||||
|
*/
|
||||||
|
ff_uid_t ff_geteuid(void)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief
|
||||||
|
*
|
||||||
|
* @param path
|
||||||
|
* @param times
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
int ff_utimes(const char *path, const struct ff_timeval times[2])
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
239
components/fatfs/fatfs_posix_api.h
Normal file
239
components/fatfs/fatfs_posix_api.h
Normal file
@ -0,0 +1,239 @@
|
|||||||
|
/**
|
||||||
|
* @file fatfs_posix_api.h
|
||||||
|
* @brief
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 Bouffalolab team
|
||||||
|
*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
* this work for additional information regarding copyright ownership. The
|
||||||
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance with the
|
||||||
|
* License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||||
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||||
|
* License for the specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*************************************************************************************************************
|
||||||
|
* 文件名 : ff_vfs.h
|
||||||
|
* 功能 : VFS虚拟文件系统支持
|
||||||
|
* 作者 : cp1300@139.com
|
||||||
|
* 创建时间 : 2020-09-29
|
||||||
|
* 最后修改时间 : 2020-09-29
|
||||||
|
* 详细 : 兼容linux的vfs文件系统API支持,需要先初始化fatfs
|
||||||
|
*************************************************************************************************************/
|
||||||
|
#ifndef _FF_VFS_H_
|
||||||
|
#define _FF_VFS_H_
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "ff.h"
|
||||||
|
#include "drv_mmheap.h"
|
||||||
|
|
||||||
|
typedef int ff_dev_t; //32为int数据,高12位主设备号,低20位次设备号
|
||||||
|
|
||||||
|
typedef int ff_off_t; //文件偏移此处使用32bit格式,由于FATFS最大只支持4G大小文件,此处设置为只支持2GB文件大小
|
||||||
|
|
||||||
|
typedef uint32_t ff_time_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t dd_vfs_fd;
|
||||||
|
uint32_t dd_rsv;
|
||||||
|
} ff_dir_t;
|
||||||
|
|
||||||
|
struct stat {
|
||||||
|
ff_dev_t st_dev;
|
||||||
|
uint32_t st_size;
|
||||||
|
uint32_t st_mode;
|
||||||
|
ff_time_t st_actime;
|
||||||
|
ff_time_t st_modtime;
|
||||||
|
uint64_t st_ino;
|
||||||
|
uint32_t st_nlink;
|
||||||
|
uint32_t st_blksize;
|
||||||
|
uint32_t st_uid;
|
||||||
|
uint32_t st_gid;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t d_ino; /* file number */
|
||||||
|
uint8_t d_type; /* type of file */
|
||||||
|
char d_name[]; /* file name */
|
||||||
|
} ff_dirent_t;
|
||||||
|
|
||||||
|
typedef const struct fs_ops fs_ops_t;
|
||||||
|
|
||||||
|
struct ff_utimbuf {
|
||||||
|
ff_time_t actime; /**< time of last access */
|
||||||
|
ff_time_t modtime; /**< time of last modification */
|
||||||
|
};
|
||||||
|
//用于 utimes 修改文件的访问时间,未实现
|
||||||
|
struct ff_timeval {
|
||||||
|
ff_time_t tv_sec; /* seconds */
|
||||||
|
ff_time_t tv_usec; /* microseconds */
|
||||||
|
};
|
||||||
|
|
||||||
|
/* for posix fcntl() and lockf() */
|
||||||
|
#define F_RDLCK 0
|
||||||
|
#define F_WRLCK 1
|
||||||
|
#define F_UNLCK 2
|
||||||
|
|
||||||
|
//打开文件的参数flags定义
|
||||||
|
#define O_RDONLY (1 << 0) //以只读方式打开文件
|
||||||
|
#define O_WRONLY (1 << 1) //以只写方式打开
|
||||||
|
#define O_RDWR (O_RDONLY | O_WRONLY) //以可读可写方式打开
|
||||||
|
#define O_ACCMODE (O_RDWR)
|
||||||
|
#define O_CREAT (1 << 8) //文件不存在时创建文件
|
||||||
|
#define O_EXCL (1 << 9) //在打开文件时,如果文件存在而且同时指定的标志位O_CREAT,则返回-EEXIST;如果文件不存在,则创建文件
|
||||||
|
#define O_NOCTTY (1 << 10)
|
||||||
|
#define O_TRUNC (1 << 11) //打开文件时,如果文件的长度不为0,则将文件的长度截短为0,下次写入文件时,从文件开始出写入数据
|
||||||
|
#define O_APPEND (1 << 12) //打开文件后,将文件的写入点设置为文件末尾,下次写入文件时,新写入的数据会追加到文件末尾
|
||||||
|
#define O_DSYNC (1 << 13)
|
||||||
|
#define O_NONBLOCK (1 << 14)
|
||||||
|
#define O_SYNC (1 << 15)
|
||||||
|
|
||||||
|
//文件偏移参数
|
||||||
|
#define VFS_SEEK_SET (0) //文件偏移设置为从文件开始处offset字节处
|
||||||
|
#define VFS_SEEK_CUR (1) //文件偏移设置为相对于当前文件位置的offset字节处
|
||||||
|
#define VFS_SEEK_END (2) //文件偏移摄者为文件末尾的offset字节处
|
||||||
|
|
||||||
|
//访问权限测试值
|
||||||
|
#define R_OK (1 << 2)
|
||||||
|
#define W_OK (1 << 1)
|
||||||
|
#define X_OK (1 << 0)
|
||||||
|
|
||||||
|
//文件特性,并未实现
|
||||||
|
struct flock {
|
||||||
|
short l_type;
|
||||||
|
short l_whence;
|
||||||
|
short l_start;
|
||||||
|
short l_len;
|
||||||
|
};
|
||||||
|
#define F_DUPFD 0 /* dup */
|
||||||
|
#define F_GETFD 1 /* get close_on_exec */
|
||||||
|
#define F_SETFD 2 /* set/clear close_on_exec */
|
||||||
|
#define F_GETFL 3 /* get file->f_flags */
|
||||||
|
#define F_SETFL 4 /* set file->f_flags */
|
||||||
|
#ifndef F_GETLK
|
||||||
|
#define F_GETLK 5
|
||||||
|
#define F_SETLK 6
|
||||||
|
#define F_SETLKW 7
|
||||||
|
#endif //F_GETLK
|
||||||
|
|
||||||
|
//文件权限模式,并未实现
|
||||||
|
typedef unsigned int ff_mode_t;
|
||||||
|
|
||||||
|
//用户与组id,并未实现
|
||||||
|
typedef unsigned int ff_uid_t;
|
||||||
|
typedef unsigned int ff_gid_t;
|
||||||
|
|
||||||
|
#define F_OK 0
|
||||||
|
#define EPERM 1 /* Operation not permitted */
|
||||||
|
#define ENOENT 2 /* No such file or directory */
|
||||||
|
#define ESRCH 3 /* No such process */
|
||||||
|
#define EINTR 4 /* Interrupted system call */
|
||||||
|
#define EIO 5 /* I/O error */
|
||||||
|
#define ENXIO 6 /* No such device or address */
|
||||||
|
#define E2BIG 7 /* Argument list too long */
|
||||||
|
#define ENOEXEC 8 /* Exec format error */
|
||||||
|
#define EBADF 9 /* Bad file number */
|
||||||
|
#define ECHILD 10 /* No child processes */
|
||||||
|
#define EAGAIN 11 /* Try again */
|
||||||
|
#define ENOMEM 12 /* Out of memory */
|
||||||
|
#define EACCES 13 /* Permission denied */
|
||||||
|
#define EFAULT 14 /* Bad address */
|
||||||
|
#define ENOTBLK 15 /* Block device required */
|
||||||
|
#define EBUSY 16 /* Device or resource busy */
|
||||||
|
#define EEXIST 17 /* File exists */
|
||||||
|
#define EXDEV 18 /* Cross-device link */
|
||||||
|
#define ENODEV 19 /* No such device */
|
||||||
|
#define ENOTDIR 20 /* Not a directory */
|
||||||
|
#define EISDIR 21 /* Is a directory */
|
||||||
|
#define EINVAL 22 /* Invalid argument */
|
||||||
|
#define ENFILE 23 /* File table overflow */
|
||||||
|
#define EMFILE 24 /* Too many open files */
|
||||||
|
#define ENOTTY 25 /* Not a typewriter */
|
||||||
|
#define ETXTBSY 26 /* Text file busy */
|
||||||
|
#define EFBIG 27 /* File too large */
|
||||||
|
#define ENOSPC 28 /* No space left on device */
|
||||||
|
#define ESPIPE 29 /* Illegal seek */
|
||||||
|
#define EROFS 30 /* Read-only file system */
|
||||||
|
#define EMLINK 31 /* Too many links */
|
||||||
|
#define EPIPE 32 /* Broken pipe */
|
||||||
|
#define EDOM 33 /* Math argument out of domain of func */
|
||||||
|
#define ERANGE 34 /* Math result not representable */
|
||||||
|
|
||||||
|
int ff_vfs_init(void); //初始化VFS系统。系统启动流程中组件初始化函数
|
||||||
|
int ff_open(const char *path, int flags, int mode); //打开path所描述的文件或者设备
|
||||||
|
int ff_close(int fd); //关闭已打开的文件描述符fd
|
||||||
|
int ff_read(int fd, void *buf, size_t nbytes); //从打开的文件中读取若干字节到buffer
|
||||||
|
int ff_write(int fd, const void *buf, size_t nbytes); //将buffer开始出的nbytes字节数据写入到已经打开的文件
|
||||||
|
ff_off_t ff_lseek(int fd, ff_off_t offset, int whence); //重新设置已打开文件的偏移
|
||||||
|
int ff_sync(int fd); //会将对文件系统元数据(metadata)的修改以及文件系统内部缓存中的数据写入底层文件系统中
|
||||||
|
int ff_stat(const char *path, struct stat *st); //读取文件的属性信息,如文件大小、文件模式(权限、文件/文件夹)
|
||||||
|
int ff_fstat(int fd, struct stat *st); //读取文件的属性信息,如文件大小、文件模式(权限、文件/文件夹)等信息到st指针指向的buffer
|
||||||
|
int ff_link(const char *oldpath, const char *newpath); //为文件创建一个新的链接文件。如果目标文件存在,则不会覆盖
|
||||||
|
int ff_unlink(const char *path); //从文件系统中删除文件名
|
||||||
|
int ff_remove(const char *path); //从文件系统中删除文件名,它既可以删除文件,也可以删除目录。
|
||||||
|
int ff_rename(const char *oldpath, const char *newpath); //重命名一个文件,可以将文件从一个文件夹下通过重命名移动到另外一个文件夹下
|
||||||
|
ff_dir_t *ff_opendir(const char *path); //打开目录名path对应的目录流,并返回目录流指针
|
||||||
|
int ff_closedir(ff_dir_t *dirp); //关闭目录流。调用ff_closedir()后,目录流指针不再可用
|
||||||
|
ff_dirent_t *ff_readdir(ff_dir_t *dirp); //返回一个指向ff_dirent_t的指针,该指针关联的目录流dirp的成员指针指向下一个成员
|
||||||
|
int ff_mkdir(const char *path); //创建名为path的目录。如果已经存在path目录,则创建失败
|
||||||
|
int ff_rmdir(const char *path); //删除一个目录。删除目录时,目录必须为空。
|
||||||
|
void ff_rewinddir(ff_dir_t *dirp); //将目录流dirp的位置重置为目录的开始处。
|
||||||
|
int ff_telldir(ff_dir_t *dirp); //返回与目录流dirp相关联的目录流的当前位置。
|
||||||
|
void ff_seekdir(ff_dir_t *dir, long loc); //设置目录流的位置,下次调用ff_readdir()时目录流将从设置的位置开始读取目录
|
||||||
|
int ff_access(const char *path, int amode); //检查当前程序是否可以访问path文件
|
||||||
|
int ff_chdir(const char *path); //改变当前程序的工作目录到path路径。
|
||||||
|
char *ff_getcwd(char *buf, size_t size); //返回当当前程序的绝对工作目录字符串指针,同时如果buf参数不为空,也将当前工作目录字符串复制到buf中
|
||||||
|
long ff_pathconf(const char *path, int name); //函数返回配置文件的限制值,是与文件或目录相关联的运行时限制。
|
||||||
|
long ff_fpathconf(int fd, int name); //返回配置文件的限制值
|
||||||
|
int ff_utime(const char *path, const struct ff_utimbuf *times); //通过参数times的actime和modtime成员改变文件的访问时间和修改时间
|
||||||
|
int ff_ftruncate(int fd, ff_off_t length); //指定文件大小
|
||||||
|
int ff_fcntl(int fd, int cmd, struct flock *lock); //根据文件描述词来操作文件的特性
|
||||||
|
int ff_fchmod(int fd, ff_mode_t mode); //改变现有文件的访问权限
|
||||||
|
int ff_fchown(int fd, ff_uid_t owner, ff_gid_t group); //更改文件的用户ID和组ID
|
||||||
|
ff_uid_t ff_geteuid(void); //获取用户有效 UID 值
|
||||||
|
int ff_utimes(const char *path, const struct ff_timeval times[2]); //修改一个文件的访问时间和修改时间
|
||||||
|
|
||||||
|
#define S_ISREG(st_mode) 0 //是否是一个常规文件-未实现
|
||||||
|
#define S_ISDIR(st_mode) 0 //是否是一个目录-未实现
|
||||||
|
|
||||||
|
//标准VFS文件接口
|
||||||
|
#define open ff_open //打开文件
|
||||||
|
#define close ff_close //关闭已打开的文件
|
||||||
|
#define read ff_read //读取已打开的文件
|
||||||
|
#define write ff_write //写入已打开的文件
|
||||||
|
#define lseek ff_lseek //重新设置已打开文件的偏移
|
||||||
|
//#define sync ff_sync //同步文件,刷新缓存
|
||||||
|
#define fsync ff_sync //同步文件,刷新缓存
|
||||||
|
__inline int stat(const char *path, struct stat *st)
|
||||||
|
{
|
||||||
|
return ff_stat(path, st);
|
||||||
|
} //获取文件信息,有重名的结构体,这个地方只能使用内联函数
|
||||||
|
#define fstat ff_fstat //获取文件信息
|
||||||
|
#define access ff_access //测试访问权限
|
||||||
|
#define getcwd ff_getcwd //返回当当前程序的绝对工作目录
|
||||||
|
#define ftruncate ff_ftruncate //指定文件大小
|
||||||
|
#define fcntl ff_fcntl //根据文件描述词来操作文件的特性
|
||||||
|
#define fchmod ff_fchmod //改变现有文件的访问权限
|
||||||
|
#define unlink ff_unlink //从文件系统中删除文件
|
||||||
|
#define mkdir ff_mkdir //创建名为path的目录
|
||||||
|
#define rmdir ff_rmdir //删除一个目录
|
||||||
|
//#define fchown ff_fchown //更改文件的用户ID和组ID
|
||||||
|
//#define geteuid ff_geteuid //获取用户有效 UID 值
|
||||||
|
#define utimes ff_utimes //修改一个文件的访问时间和修改时间
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif //_FF_VFS_H_
|
Reference in New Issue
Block a user