diff --git a/stm32f4xx/sdk/hals/utils/file/hal_file.c b/stm32f4xx/sdk/hals/utils/file/hal_file.c index 1703013..d07113d 100755 --- a/stm32f4xx/sdk/hals/utils/file/hal_file.c +++ b/stm32f4xx/sdk/hals/utils/file/hal_file.c @@ -13,58 +13,130 @@ * limitations under the License. */ -#include -#include -#include -#include "errno.h" -#include "fcntl.h" +#include "securec.h" #include "sys/stat.h" -#include "los_fs.h" +#include "littlefs.h" +#include "lfs.h" +#include "utils_file.h" +#include "fcntl.h" + #define LITTLEFS_MAX_LFN_LEN 120 +#define OFFSET_FD 3 + +static int sHalFileGetPath(char *tmpPath, const char* path) +{ + if(!path || !tmpPath) return -1; + for(;'.' == path[0]; path++); + for(;'/' == path[0]; path++); + (void)snprintf_s(tmpPath, LITTLEFS_MAX_LFN_LEN, LITTLEFS_MAX_LFN_LEN, "/talkweb/%s", path); + return 0; +} int HalFileOpen(const char* path, int oflag, int mode) { - char tmpPath[LITTLEFS_MAX_LFN_LEN] = {0}; - (void)snprintf_s(tmpPath, LITTLEFS_MAX_LFN_LEN, LITTLEFS_MAX_LFN_LEN, "/talkweb/%s", path); - - return LOS_Open(tmpPath, oflag); + int fd, tflag = 0; + char tmpPath[LITTLEFS_MAX_LFN_LEN]; + if(sHalFileGetPath(tmpPath, path)) return -1; + + if (O_CREAT_FS == (oflag & O_CREAT_FS)) tflag |= O_CREAT; + + if (O_RDWR_FS == (oflag & O_RDWR_FS)) tflag |= O_RDWR; + #if O_WRONLY_FS != 0 + else if (O_WRONLY_FS == (oflag & O_WRONLY_FS)) tflag |= O_WRONLY; + else tflag |= O_RDONLY; + #else + else if (O_RDONLY_FS == (oflag & O_RDONLY_FS)) tflag |= O_RDONLY; + else tflag |= O_WRONLY; + #endif + if (O_APPEND_FS == (oflag & O_APPEND_FS)) tflag |= O_APPEND; + + if (O_EXCL_FS == (oflag & O_EXCL_FS)) tflag |= O_EXCL; + if (O_TRUNC_FS == (oflag & O_TRUNC_FS)) tflag |= O_TRUNC; + + fd = _open(tmpPath, tflag); + if(fd < 0) { + // printf("Error HalFileOpen.%s oflag:0x%X\n", path, oflag); + return fd; + } + return fd + OFFSET_FD; } int HalFileClose(int fd) { - return LOS_Close(fd); + if(fd < OFFSET_FD) return -1; + return _close(fd - OFFSET_FD); } int HalFileRead(int fd, char *buf, unsigned int len) { - return LOS_Read(fd, buf, len); + if(fd < OFFSET_FD) return -1; + return _read(fd - OFFSET_FD, buf, len); } int HalFileWrite(int fd, const char *buf, unsigned int len) { - return LOS_Write(fd, buf, len); + if(fd < OFFSET_FD) return -1; + return _write(fd - OFFSET_FD, buf, len); } int HalFileDelete(const char *path) { - char tmpPath[LITTLEFS_MAX_LFN_LEN] = {0}; - (void)snprintf_s(tmpPath, LITTLEFS_MAX_LFN_LEN, LITTLEFS_MAX_LFN_LEN, "/littlefs/%s", path); - return LOS_Unlink(tmpPath); + char tmpPath[LITTLEFS_MAX_LFN_LEN]; + if(sHalFileGetPath(tmpPath, path)) return -1; + return _unlink(tmpPath); } int HalFileStat(const char *path, unsigned int *fileSize) { - char tmpPath[LITTLEFS_MAX_LFN_LEN] = {0}; - struct stat halStat = {0}; - int ret = 0; - (void)snprintf_s(tmpPath, LITTLEFS_MAX_LFN_LEN, LITTLEFS_MAX_LFN_LEN, "/littlefs/%s", path); - ret = LOS_Stat(tmpPath, &halStat); - *fileSize = halStat.st_size; - return ret; + #if 1 + off_t len; + int fd; + char tmpPath[LITTLEFS_MAX_LFN_LEN]; + if(sHalFileGetPath(tmpPath, path)) return -1; + fd = _open(tmpPath, O_RDONLY); + if(fd < 0) { + // printf("Error HalFileStat.%s\n", path); + return -1; + } + len = _lseek( fd , 0 ,SEEK_END); + _close(fd); + if (fileSize) *fileSize = len; + #else + char tmpPath[LITTLEFS_MAX_LFN_LEN]; + if(sHalFileGetPath(tmpPath, path)) return -1; + struct stat st_buf = {0}; + if (_stat(tmpPath, &st_buf) != 0) { + printf("Error HalFileStat.%s\n", path); + return -1; + } + // printf("OK HalFileStat.%s\n", path); + if (fileSize) *fileSize = st_buf.st_size; + #endif + return 0; } int HalFileSeek(int fd, int offset, unsigned int whence) { - return LOS_Lseek(fd, (off_t)offset, whence); + if(fd < OFFSET_FD) + return -1; + switch (whence) { + case SEEK_SET_FS: whence = SEEK_SET;break; + case SEEK_CUR_FS: whence = SEEK_CUR;break; + case SEEK_END_FS: whence = SEEK_END;break; + default: return -1; + } + fd -= OFFSET_FD; + if ((SEEK_SET == whence) || (SEEK_CUR == whence)) { + off_t len, len2; + len = _lseek( fd, 0, SEEK_CUR); + len2 = _lseek( fd , 0 ,SEEK_END); + if (SEEK_CUR == whence) offset += len; + if(offset > len2) { + _lseek( fd, len, SEEK_SET); + return -1; + } + return _lseek( fd, (off_t)offset, SEEK_SET); + } + return _lseek(fd, (off_t)offset, whence); }