update openharmony 1.0.1
This commit is contained in:
47
components/fs/fatfs/BUILD.gn
Normal file
47
components/fs/fatfs/BUILD.gn
Normal file
@@ -0,0 +1,47 @@
|
||||
# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved.
|
||||
# Copyright (c) 2020-2021, Huawei Device Co., Ltd. All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification,
|
||||
# are permitted provided that the following conditions are met:
|
||||
#
|
||||
# 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
# conditions and the following disclaimer.
|
||||
#
|
||||
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
# of conditions and the following disclaimer in the documentation and/or other materials
|
||||
# provided with the distribution.
|
||||
#
|
||||
# 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
# to endorse or promote products derived from this software without specific prior written
|
||||
# permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
static_library("fatfs") {
|
||||
sources = [
|
||||
"fatfs.c",
|
||||
"fs.c",
|
||||
]
|
||||
|
||||
include_dirs = [
|
||||
"../../../kernel/arch/include",
|
||||
"../../../kernel/include",
|
||||
"../../../utils",
|
||||
"../../../kal/cmsis",
|
||||
"../../../kal",
|
||||
"../../../kal/posix/include",
|
||||
"./",
|
||||
"//third_party/bounds_checking_function/include",
|
||||
"//third_party/FatFs/source/",
|
||||
]
|
||||
}
|
||||
148
components/fs/fatfs/fatfs.c
Normal file → Executable file
148
components/fs/fatfs/fatfs.c
Normal file → Executable file
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
@@ -29,21 +29,18 @@
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "los_compiler.h"
|
||||
#include "fcntl.h"
|
||||
#include "unistd.h"
|
||||
#include "sys/mount.h"
|
||||
#include "sys/stat.h"
|
||||
#include "sys/statfs.h"
|
||||
#include "dirent.h"
|
||||
#include "stdio.h"
|
||||
#include "ff.h"
|
||||
#if FF_USE_EXPAND
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
#include "fatfs.h"
|
||||
#include "errno.h"
|
||||
#include "limits.h"
|
||||
#include "pthread.h"
|
||||
#include "time.h"
|
||||
#include "securec.h"
|
||||
#include "ff.h"
|
||||
#include "los_compiler.h"
|
||||
#include "los_debug.h"
|
||||
#include "fatfs.h"
|
||||
#include "cmsis_os.h"
|
||||
|
||||
#define FS_SUCCESS 0
|
||||
@@ -55,10 +52,6 @@
|
||||
#define FAT_MAX_OPEN_DIRS 8
|
||||
#endif /* FAT_MAX_OPEN_DIRS */
|
||||
|
||||
#ifndef FAT_MAX_OPEN_FILES
|
||||
#define FAT_MAX_OPEN_FILES 50
|
||||
#endif /* FAT_MAX_OPEN_FILES */
|
||||
|
||||
#ifndef FS_LOCK_TIMEMOUT_SEC
|
||||
#define FS_LOCK_TIMEMOUT_SEC 15
|
||||
#endif /* FS_LOCK_TIMEMOUT_SEC */
|
||||
@@ -327,9 +320,9 @@ static int FatfsErrno(int result)
|
||||
return status;
|
||||
}
|
||||
|
||||
int mount(const char *source, const char *target,
|
||||
const char *filesystemtype, unsigned long mountflags,
|
||||
const void *data)
|
||||
int fatfs_mount(const char *source, const char *target,
|
||||
const char *filesystemtype, unsigned long mountflags,
|
||||
const void *data)
|
||||
{
|
||||
INT32 index;
|
||||
FRESULT res;
|
||||
@@ -386,7 +379,7 @@ OUT:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int umount(const char *target)
|
||||
int fatfs_umount(const char *target)
|
||||
{
|
||||
FRESULT res;
|
||||
INT32 ret;
|
||||
@@ -482,7 +475,7 @@ static int CloseAll(int index)
|
||||
return FS_SUCCESS;
|
||||
}
|
||||
|
||||
int umount2(const char *target, int flag)
|
||||
int fatfs_umount2(const char *target, int flag)
|
||||
{
|
||||
INT32 index;
|
||||
INT32 ret;
|
||||
@@ -546,7 +539,7 @@ OUT:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int open(const char *path, int oflag, ...)
|
||||
int fatfs_open(const char *path, int oflag, ...)
|
||||
{
|
||||
FRESULT res;
|
||||
UINT32 i;
|
||||
@@ -626,7 +619,7 @@ OUT:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int close(int fd)
|
||||
int fatfs_close(int fd)
|
||||
{
|
||||
FRESULT res;
|
||||
INT32 ret;
|
||||
@@ -667,7 +660,7 @@ int close(int fd)
|
||||
return FS_SUCCESS;
|
||||
}
|
||||
|
||||
ssize_t read(int fd, void *buf, size_t nbyte)
|
||||
ssize_t fatfs_read(int fd, void *buf, size_t nbyte)
|
||||
{
|
||||
FRESULT res;
|
||||
INT32 ret;
|
||||
@@ -699,7 +692,7 @@ ssize_t read(int fd, void *buf, size_t nbyte)
|
||||
return (ssize_t)lenRead;
|
||||
}
|
||||
|
||||
ssize_t write(int fd, const void *buf, size_t nbyte)
|
||||
ssize_t fatfs_write(int fd, const void *buf, size_t nbyte)
|
||||
{
|
||||
FRESULT res;
|
||||
INT32 ret;
|
||||
@@ -743,7 +736,7 @@ ERROUT:
|
||||
return FS_FAILURE;
|
||||
}
|
||||
|
||||
off_t lseek(int fd, off_t offset, int whence)
|
||||
off_t fatfs_lseek(int fd, off_t offset, int whence)
|
||||
{
|
||||
FRESULT res;
|
||||
INT32 ret;
|
||||
@@ -787,7 +780,7 @@ ERROUT:
|
||||
}
|
||||
|
||||
/* Remove the specified FILE */
|
||||
int unlink(const char *path)
|
||||
int fatfs_unlink(const char *path)
|
||||
{
|
||||
FRESULT res;
|
||||
INT32 ret;
|
||||
@@ -833,7 +826,7 @@ OUT:
|
||||
}
|
||||
|
||||
/* Return information about a file */
|
||||
int fstat(int fd, struct stat *buf)
|
||||
int fatfs_fstat(int fd, struct stat *buf)
|
||||
{
|
||||
INT32 ret;
|
||||
|
||||
@@ -864,7 +857,7 @@ int fstat(int fd, struct stat *buf)
|
||||
return FS_SUCCESS;
|
||||
}
|
||||
|
||||
int stat(const char *__restrict path, struct stat *__restrict buf)
|
||||
int fatfs_stat(const char *path, struct stat *buf)
|
||||
{
|
||||
FRESULT res;
|
||||
FILINFO fileInfo = {0};
|
||||
@@ -918,7 +911,7 @@ OUT:
|
||||
}
|
||||
|
||||
/* Synchronize all changes to Flash */
|
||||
int fsync(int fd)
|
||||
int fatfs_fsync(int fd)
|
||||
{
|
||||
FRESULT res;
|
||||
INT32 ret;
|
||||
@@ -952,7 +945,7 @@ OUT:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int mkdir(const char *path, mode_t mode)
|
||||
int fatfs_mkdir(const char *path, mode_t mode)
|
||||
{
|
||||
FRESULT res;
|
||||
INT32 ret;
|
||||
@@ -996,7 +989,7 @@ OUT:
|
||||
return ret;
|
||||
}
|
||||
|
||||
DIR *opendir(const char *dirName)
|
||||
DIR *fatfs_opendir(const char *dirName)
|
||||
{
|
||||
FRESULT res;
|
||||
UINT32 openNum = 0;
|
||||
@@ -1058,7 +1051,7 @@ ERROUT:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct dirent *readdir(DIR *dir)
|
||||
struct dirent *fatfs_readdir(DIR *dir)
|
||||
{
|
||||
FRESULT res;
|
||||
INT32 ret;
|
||||
@@ -1095,7 +1088,7 @@ struct dirent *readdir(DIR *dir)
|
||||
return &g_retValue;
|
||||
}
|
||||
|
||||
int closedir(DIR *dir)
|
||||
int fatfs_closedir(DIR *dir)
|
||||
{
|
||||
FRESULT res;
|
||||
INT32 ret;
|
||||
@@ -1126,7 +1119,7 @@ int closedir(DIR *dir)
|
||||
return FS_SUCCESS;
|
||||
}
|
||||
|
||||
int rmdir(const char *path)
|
||||
int fatfs_rmdir(const char *path)
|
||||
{
|
||||
FRESULT res;
|
||||
INT32 ret;
|
||||
@@ -1170,7 +1163,7 @@ OUT:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int rename(const char *oldName, const char *newName)
|
||||
int fatfs_rename(const char *oldName, const char *newName)
|
||||
{
|
||||
FRESULT res;
|
||||
INT32 ret;
|
||||
@@ -1214,7 +1207,7 @@ OUT:
|
||||
return ret;
|
||||
}
|
||||
|
||||
int statfs(const char *path, struct statfs *buf)
|
||||
int fatfs_statfs(const char *path, struct statfs *buf)
|
||||
{
|
||||
FATFS *fs = NULL;
|
||||
UINT32 freeClust;
|
||||
@@ -1264,6 +1257,85 @@ OUT:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int do_truncate(int fd, off_t length, UINT count)
|
||||
{
|
||||
FRESULT res;
|
||||
INT32 ret = FR_OK;
|
||||
DWORD csz;
|
||||
|
||||
csz = (DWORD)(g_handle[fd].fil.obj.fs)->csize * SS(g_handle[fd].fil.obj.fs); /* Cluster size */
|
||||
if (length > csz * count) {
|
||||
#if FF_USE_EXPAND
|
||||
res = f_expand(&g_handle[fd].fil, 0, (FSIZE_t)(length), FALLOC_FL_KEEP_SIZE);
|
||||
#else
|
||||
errno = ENOSYS;
|
||||
ret = FS_FAILURE;
|
||||
return ret;
|
||||
#endif
|
||||
} else if (length < csz * count) {
|
||||
res = f_truncate(&g_handle[fd].fil, (FSIZE_t)length);
|
||||
}
|
||||
|
||||
if (res != FR_OK) {
|
||||
errno = FatfsErrno(res);
|
||||
ret = FS_FAILURE;
|
||||
return ret;
|
||||
}
|
||||
|
||||
g_handle[fd].fil.obj.objsize = length; /* Set file size to length */
|
||||
g_handle[fd].fil.flag |= 0x40; /* Set modified flag */
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int fatfs_ftruncate(int fd, off_t length)
|
||||
{
|
||||
FRESULT res;
|
||||
INT32 ret;
|
||||
UINT count;
|
||||
DWORD fclust;
|
||||
|
||||
if (!IsValidFd(fd)) {
|
||||
errno = EBADF;
|
||||
return FS_FAILURE;
|
||||
}
|
||||
|
||||
if ((length < 0) || (length > UINT_MAX)) {
|
||||
errno = EINVAL;
|
||||
return FS_FAILURE;
|
||||
}
|
||||
|
||||
ret = FsLock();
|
||||
if (ret != 0) {
|
||||
errno = ret;
|
||||
return FS_FAILURE;
|
||||
}
|
||||
|
||||
if (!FsCheckByID(g_handle[fd].fil.obj.fs->id)) {
|
||||
errno = EACCES;
|
||||
ret = FS_FAILURE;
|
||||
goto OUT;
|
||||
}
|
||||
|
||||
res = f_getclustinfo(&g_handle[fd].fil, &fclust, &count);
|
||||
if (res != FR_OK) {
|
||||
errno = FatfsErrno(res);
|
||||
ret = FS_FAILURE;
|
||||
goto OUT;
|
||||
}
|
||||
|
||||
ret = do_truncate(fd, length, count);
|
||||
if (ret != FR_OK) {
|
||||
goto OUT;
|
||||
}
|
||||
|
||||
ret = FS_SUCCESS;
|
||||
|
||||
OUT:
|
||||
FsUnlock();
|
||||
return ret;
|
||||
}
|
||||
|
||||
int fatfs_fdisk(int pdrv, const unsigned int *partTbl)
|
||||
{
|
||||
INT32 index;
|
||||
@@ -1348,4 +1420,4 @@ int fatfs_format(const char *dev, int sectors, int option)
|
||||
OUT:
|
||||
FsUnlock();
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
40
components/fs/fatfs/fatfs.h
Normal file → Executable file
40
components/fs/fatfs/fatfs.h
Normal file → Executable file
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
@@ -32,17 +32,51 @@
|
||||
#ifndef _FATFS_H
|
||||
#define _FATFS_H
|
||||
|
||||
#include "fcntl.h"
|
||||
#include "dirent.h"
|
||||
#include "unistd.h"
|
||||
#include "sys/mount.h"
|
||||
#include "sys/stat.h"
|
||||
#include "sys/statfs.h"
|
||||
#include "fs_config.h"
|
||||
#ifdef __cplusplus
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#ifndef FAT_MAX_OPEN_FILES
|
||||
#define FAT_MAX_OPEN_FILES 50
|
||||
#endif /* FAT_MAX_OPEN_FILES */
|
||||
|
||||
/* Format options */
|
||||
#define FMT_FAT 0x01
|
||||
#define FMT_FAT32 0x02
|
||||
#define FMT_ANY 0x07
|
||||
|
||||
int fatfs_mount(const char *source, const char *target,
|
||||
const char *filesystemtype, unsigned long mountflags,
|
||||
const void *data);
|
||||
int fatfs_umount(const char *target);
|
||||
int fatfs_umount2(const char *target, int flag);
|
||||
int fatfs_open(const char *path, int oflag, ...);
|
||||
int fatfs_close(int fd);
|
||||
ssize_t fatfs_read(int fd, void *buf, size_t nbyte);
|
||||
ssize_t fatfs_write(int fd, const void *buf, size_t nbyte);
|
||||
off_t fatfs_lseek(int fd, off_t offset, int whence);
|
||||
int fatfs_unlink(const char *path);
|
||||
int fatfs_fstat(int fd, struct stat *buf);
|
||||
int fatfs_stat(const char *path, struct stat *buf);
|
||||
int fatfs_fsync(int fd);
|
||||
int fatfs_mkdir(const char *path, mode_t mode);
|
||||
DIR *fatfs_opendir(const char *dirName);
|
||||
struct dirent *fatfs_readdir(DIR *dir);
|
||||
int fatfs_closedir(DIR *dir);
|
||||
int fatfs_rmdir(const char *path);
|
||||
int fatfs_rename(const char *oldName, const char *newName);
|
||||
int fatfs_statfs(const char *path, struct statfs *buf);
|
||||
int fatfs_ftruncate(int fd, off_t length);
|
||||
|
||||
/**
|
||||
* @brief divide a physical drive (SD card, U disk, and MMC card), this function is OHOS-specific
|
||||
* @param pdrv physical drive number.
|
||||
@@ -77,4 +111,4 @@ int fatfs_format(const char *dev, int sectors, int option);
|
||||
#endif /* __cplusplus */
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* _FATFS_H */
|
||||
#endif /* _FATFS_H */
|
||||
|
||||
343
components/fs/fatfs/fs.c
Executable file
343
components/fs/fatfs/fs.c
Executable file
@@ -0,0 +1,343 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
||||
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "fatfs.h"
|
||||
#include "dirent.h"
|
||||
#include "errno.h"
|
||||
#include "fcntl.h"
|
||||
#include "securec.h"
|
||||
#include "stdio.h"
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
#include "sys/mount.h"
|
||||
#include "sys/statfs.h"
|
||||
#include "sys/stat.h"
|
||||
#include "unistd.h"
|
||||
|
||||
#ifdef LOSCFG_NET_LWIP_SACK
|
||||
#include "lwip/lwipopts.h"
|
||||
#define CONFIG_NSOCKET_DESCRIPTORS LWIP_CONFIG_NUM_SOCKETS
|
||||
#else
|
||||
#define CONFIG_NSOCKET_DESCRIPTORS 0
|
||||
#endif
|
||||
|
||||
#define CONFIG_NFILE_DESCRIPTORS FAT_MAX_OPEN_FILES /* only for random currently */
|
||||
|
||||
#ifdef LOSCFG_RANDOM_DEV
|
||||
#include "hks_client.h"
|
||||
#define RANDOM_DEV_FD CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS
|
||||
#define RANDOM_DEV_PATH "/dev/random"
|
||||
#endif
|
||||
|
||||
#define FREE_AND_SET_NULL(ptr) do { \
|
||||
free(ptr); \
|
||||
ptr = NULL; \
|
||||
} while (0)
|
||||
|
||||
#ifdef LOSCFG_RANDOM_DEV
|
||||
/**
|
||||
* @brief Get canonical form of a given path based on cwd(Current working directory).
|
||||
*
|
||||
* @param cwd Indicates the current working directory.
|
||||
* @param path Indicates the path to be canonicalization.
|
||||
* @param buf Indicates the pointer to the buffer where the result will be return.
|
||||
* @param bufSize Indicates the size of the buffer.
|
||||
* @return Returns the length of the canonical path.
|
||||
*
|
||||
* @attention if path is an absolute path, cwd is ignored. if cwd if not specified, it is assumed to be root('/').
|
||||
* if the buffer is not big enough the result will be truncated, but the return value will always be the
|
||||
* length of the canonical path.
|
||||
*/
|
||||
static size_t GetCanonicalPath(const char *cwd, const char *path, char *buf, size_t bufSize)
|
||||
{
|
||||
size_t offset;
|
||||
if (!path) {
|
||||
path = "";
|
||||
}
|
||||
|
||||
if (!cwd || path[0] == '/') {
|
||||
cwd = "";
|
||||
}
|
||||
|
||||
offset = strlen("///") + 1; // three '/' and one '\0'
|
||||
size_t tmpLen = strlen(cwd) + strlen(path) + offset;
|
||||
char *tmpBuf = (char *)malloc(tmpLen);
|
||||
if (tmpBuf == NULL) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (-1 == sprintf_s(tmpBuf, tmpLen, "/%s/%s/", cwd, path)) {
|
||||
free(tmpBuf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *p;
|
||||
/* replace /./ to / */
|
||||
offset = strlen("/./") - 1;
|
||||
while ((p = strstr(tmpBuf, "/./")) != NULL) {
|
||||
if (EOK != memmove_s(p, tmpLen - (p - tmpBuf), p + offset, tmpLen - (p - tmpBuf) - offset)) {
|
||||
free(tmpBuf);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* replace // to / */
|
||||
while ((p = strstr(tmpBuf, "//")) != NULL) {
|
||||
if (EOK != memmove_s(p, tmpLen - (p - tmpBuf), p + 1, tmpLen - (p - tmpBuf) - 1)) {
|
||||
free(tmpBuf);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* handle /../ (e.g., replace /aa/bb/../ to /aa/) */
|
||||
offset = strlen("/../") - 1;
|
||||
while ((p = strstr(tmpBuf, "/../")) != NULL) {
|
||||
char *start = p;
|
||||
while (start > tmpBuf && *(start - 1) != '/') {
|
||||
--start;
|
||||
}
|
||||
if (EOK != memmove_s(start, tmpLen - (start - tmpBuf), p + offset, tmpLen - (p - tmpBuf) - offset)) {
|
||||
free(tmpBuf);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
size_t totalLen = strlen(tmpBuf);
|
||||
/* strip the last / */
|
||||
if (totalLen > 1 && tmpBuf[totalLen - 1] == '/') {
|
||||
tmpBuf[--totalLen] = 0;
|
||||
}
|
||||
|
||||
if (!buf || bufSize == 0) {
|
||||
free(tmpBuf);
|
||||
return totalLen;
|
||||
}
|
||||
|
||||
if (EOK != memcpy_s(buf, bufSize, tmpBuf, (((totalLen + 1) > bufSize) ? bufSize : (totalLen + 1)))) {
|
||||
free(tmpBuf);
|
||||
return 0;
|
||||
}
|
||||
|
||||
buf[bufSize - 1] = 0;
|
||||
free(tmpBuf);
|
||||
return totalLen;
|
||||
}
|
||||
#endif
|
||||
|
||||
int mount(const char *source, const char *target,
|
||||
const char *filesystemtype, unsigned long mountflags,
|
||||
const void *data)
|
||||
{
|
||||
return fatfs_mount(source, target, filesystemtype, mountflags, data);
|
||||
}
|
||||
|
||||
int umount(const char *target)
|
||||
{
|
||||
return fatfs_umount(target);
|
||||
}
|
||||
|
||||
int umount2(const char *target, int flag)
|
||||
{
|
||||
return fatfs_umount2(target, flag);
|
||||
}
|
||||
|
||||
int open(const char *path, int oflag, ...)
|
||||
{
|
||||
#ifdef LOSCFG_RANDOM_DEV
|
||||
unsigned flags = O_RDONLY | O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_LARGEFILE | O_TRUNC | O_EXCL | O_DIRECTORY;
|
||||
if ((unsigned)oflag & ~flags) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t pathLen = strlen(path) + 1;
|
||||
char *canonicalPath = (char *)malloc(pathLen);
|
||||
if (!canonicalPath) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
if (GetCanonicalPath(NULL, path, canonicalPath, pathLen) == 0) {
|
||||
FREE_AND_SET_NULL(canonicalPath);
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strcmp(canonicalPath, RANDOM_DEV_PATH) == 0) {
|
||||
FREE_AND_SET_NULL(canonicalPath);
|
||||
if ((O_ACCMODE & (unsigned)oflag) != O_RDONLY) {
|
||||
errno = EPERM;
|
||||
return -1;
|
||||
}
|
||||
if ((unsigned)oflag & O_DIRECTORY) {
|
||||
errno = ENOTDIR;
|
||||
return -1;
|
||||
}
|
||||
return RANDOM_DEV_FD;
|
||||
}
|
||||
if (strcmp(canonicalPath, "/") == 0 || strcmp(canonicalPath, "/dev") == 0) {
|
||||
FREE_AND_SET_NULL(canonicalPath);
|
||||
if ((unsigned)oflag & O_DIRECTORY) {
|
||||
errno = EPERM;
|
||||
return -1;
|
||||
}
|
||||
errno = EISDIR;
|
||||
return -1;
|
||||
}
|
||||
FREE_AND_SET_NULL(canonicalPath);
|
||||
#endif
|
||||
return fatfs_open(path, oflag);
|
||||
}
|
||||
|
||||
int close(int fd)
|
||||
{
|
||||
#ifdef LOSCFG_RANDOM_DEV
|
||||
if (fd == RANDOM_DEV_FD) {
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#ifdef LOSCFG_NET_LWIP_SACK
|
||||
if (fd >= CONFIG_NFILE_DESCRIPTORS && fd < (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS)) {
|
||||
return closesocket(fd);
|
||||
}
|
||||
#endif
|
||||
return fatfs_close(fd);
|
||||
}
|
||||
|
||||
ssize_t read(int fd, void *buf, size_t nbyte)
|
||||
{
|
||||
#ifdef LOSCFG_RANDOM_DEV
|
||||
if (fd == RANDOM_DEV_FD) {
|
||||
if (nbyte == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (buf == NULL) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
if (nbyte > 1024) {
|
||||
nbyte = 1024; /* hks_generate_random: random_size must <= 1024 */
|
||||
}
|
||||
struct hks_blob key = {HKS_BLOB_TYPE_RAW, (uint8_t *)buf, nbyte};
|
||||
if (hks_generate_random(&key) != 0) {
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
return (ssize_t)nbyte;
|
||||
}
|
||||
#endif
|
||||
#ifdef LOSCFG_NET_LWIP_SACK
|
||||
if (fd >= CONFIG_NFILE_DESCRIPTORS && fd < (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS)) {
|
||||
return recv(fd, buf, nbyte, 0);
|
||||
}
|
||||
#endif
|
||||
return fatfs_read(fd, buf, nbyte);
|
||||
}
|
||||
|
||||
ssize_t write(int fd, const void *buf, size_t nbyte)
|
||||
{
|
||||
#ifdef LOSCFG_RANDOM_DEV
|
||||
if (fd == RANDOM_DEV_FD) {
|
||||
errno = EBADF; /* "/dev/random" is readonly */
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
#ifdef LOSCFG_NET_LWIP_SACK
|
||||
if (fd >= CONFIG_NFILE_DESCRIPTORS && fd < (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS)) {
|
||||
return send(fd, buf, nbyte, 0);
|
||||
}
|
||||
#endif
|
||||
return fatfs_write(fd, buf, nbyte);
|
||||
}
|
||||
|
||||
off_t lseek(int fd, off_t offset, int whence)
|
||||
{
|
||||
return fatfs_lseek(fd, offset, whence);
|
||||
}
|
||||
|
||||
int unlink(const char *path)
|
||||
{
|
||||
return fatfs_unlink(path);
|
||||
}
|
||||
|
||||
int fstat(int fd, struct stat *buf)
|
||||
{
|
||||
return fatfs_fstat(fd, buf);
|
||||
}
|
||||
|
||||
int stat(const char *path, struct stat *buf)
|
||||
{
|
||||
return fatfs_stat(path, buf);
|
||||
}
|
||||
|
||||
int fsync(int fd)
|
||||
{
|
||||
return fatfs_fsync(fd);
|
||||
}
|
||||
|
||||
int mkdir(const char *path, mode_t mode)
|
||||
{
|
||||
return fatfs_mkdir(path, mode);
|
||||
}
|
||||
|
||||
DIR *opendir(const char *dirName)
|
||||
{
|
||||
return fatfs_opendir(dirName);
|
||||
}
|
||||
|
||||
struct dirent *readdir(DIR *dir)
|
||||
{
|
||||
return fatfs_readdir(dir);
|
||||
}
|
||||
|
||||
int closedir(DIR *dir)
|
||||
{
|
||||
return fatfs_closedir(dir);
|
||||
}
|
||||
|
||||
int rmdir(const char *path)
|
||||
{
|
||||
return fatfs_rmdir(path);
|
||||
}
|
||||
|
||||
int rename(const char *oldName, const char *newName)
|
||||
{
|
||||
return fatfs_rename(oldName, newName);
|
||||
}
|
||||
|
||||
int statfs(const char *path, struct statfs *buf)
|
||||
{
|
||||
return fatfs_statfs(path, buf);
|
||||
}
|
||||
|
||||
int ftruncate(int fd, off_t length)
|
||||
{
|
||||
return fatfs_ftruncate(fd, length);
|
||||
}
|
||||
Reference in New Issue
Block a user