lfs_api.c 7.6 KB
Newer Older
L
li_zan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * 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 "lfs_api.h"
#include "iCunit.h"

lfs_t g_lfs;
L
li_zan 已提交
36
FileDirInfo g_lfsDir[LFS_MAX_OPEN_DIRS] = {0};
L
li_zan 已提交
37 38

FileOpInfo g_fsOp;
L
li_zan 已提交
39
static LittleFsHandleStruct g_handle[LITTLE_FS_MAX_OPEN_FILES] = {0};
L
li_zan 已提交
40
struct dirent g_nameValue;
L
li_zan 已提交
41
struct fsmap_t g_fsmap[MAX_FILE_SYSTEM_LEN] = {0};
L
li_zan 已提交
42 43 44 45
static pthread_mutex_t g_FslocalMutex = PTHREAD_MUTEX_INITIALIZER;

FileOpInfo GetFsOpInfo(void)
{
L
li_zan 已提交
46
    return g_fsOp;
L
li_zan 已提交
47 48 49 50
}

LittleFsHandleStruct *GetFreeFd(int *fd)
{
L
li_zan 已提交
51 52 53 54 55 56 57 58 59 60 61 62
    pthread_mutex_lock(&g_FslocalMutex);
    for (int i = 0; i < LITTLE_FS_MAX_OPEN_FILES; i++) {
        if (g_handle[i].useFlag == 0) {
            *fd = i;
            g_handle[i].useFlag = 1;
            pthread_mutex_unlock(&g_FslocalMutex);
            return &(g_handle[i]);
        }        
    }
    pthread_mutex_unlock(&g_FslocalMutex);
    *fd = INVALID_FD;
    return NULL;
L
li_zan 已提交
63 64 65 66
}

lfs_dir_t *GetFreeDir()
{
L
li_zan 已提交
67 68 69 70 71 72 73 74 75 76
    pthread_mutex_lock(&g_FslocalMutex);
    for (int i = 0; i < LFS_MAX_OPEN_DIRS; i++) {
        if (g_lfsDir[i].useFlag == 0) {
            g_lfsDir[i].useFlag = 1;
            pthread_mutex_unlock(&g_FslocalMutex);
            return &(g_lfsDir[i].dir);
        }
    }
    pthread_mutex_unlock(&g_FslocalMutex);
    return NULL;
L
li_zan 已提交
77 78 79 80
}

int InitMountInfo(const char *fileSystemType, const struct MountOps *fsMops)
{
L
li_zan 已提交
81 82 83 84 85 86 87 88 89 90 91
    int len = strlen(fileSystemType) + 1;
    for (int i = 0; i < MAX_FILE_SYSTEM_LEN; i++) {
        if (g_fsmap[i].fileSystemtype == NULL) {
            g_fsmap[i].fileSystemtype = (char*)malloc(len);
            memcpy_s(g_fsmap[i].fileSystemtype, len, fileSystemType, len);
            g_fsmap[i].fs_mops = fsMops;
            return VFS_OK;
        }
    }

    return VFS_ERROR;
L
li_zan 已提交
92 93
}


L
li_zan 已提交
94
const struct fsmap_t *MountFindfs(const char*fileSystemtype)
L
li_zan 已提交
95
{
L
li_zan 已提交
96
    struct fsmap_t *m = NULL;
L
li_zan 已提交
97

L
li_zan 已提交
98 99 100 101 102 103
    for (int i = 0; i < MAX_FILE_SYSTEM_LEN; i++) {
        m = &(g_fsmap[i]);
        if (m->fileSystemtype && strcmp(fileSystemtype, m->fileSystemtype) == 0) {
            return m;
        }
    }
L
li_zan 已提交
104

L
li_zan 已提交
105
    return (const struct fsmap_t *)NULL;
L
li_zan 已提交
106 107 108
}

const struct MountOps g_fsMnt = {
L
li_zan 已提交
109 110
    .Mount = LfsMount,
    .Umount = LfsUmount,
L
li_zan 已提交
111 112
};

L
li_zan 已提交
113
const struct FileOps lfs_vops = {
L
li_zan 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127
    .Mkdir = LfsMkdir,
    .Unlink = LfsUnlink,
    .Rmdir = LfsRmdir,
    .Opendir = LfsOpendir,
    .Readdir = LfsReaddir,
    .Closedir = LfsClosedir,
    .Open = LfsOpen,
    .Close = LfsClose,
    .Write = LfsWrite,
    .Read = LfsRead,
    .Seek = LfsSeek,
    .Rename = LfsRename,
    .Getattr = LfsStat,
    .Fsync = LfsFsync,
L
li_zan 已提交
128 129 130
};

int LfsMount(const char * source, const char * target, const char * fileSystemType, unsigned long mountflags,
L
li_zan 已提交
131
    const void * data)
L
li_zan 已提交
132
{
L
li_zan 已提交
133
    int ret;
L
li_zan 已提交
134

L
li_zan 已提交
135 136
    g_fsOp.fsVops = &lfs_vops;
    ret = lfs_mount(&g_lfs, (struct lfs_config*)data);
L
li_zan 已提交
137

L
li_zan 已提交
138
    return ret;
L
li_zan 已提交
139 140 141 142
}

int LfsUmount(const char * target)
{
L
li_zan 已提交
143
    return lfs_unmount(&g_lfs);
L
li_zan 已提交
144 145 146 147
}

int LfsUnlink(const char * fileName)
{
L
li_zan 已提交
148
    return lfs_remove(&g_lfs, fileName);
L
li_zan 已提交
149 150 151 152
}

int LfsMkdir(const char * dirName, mode_t mode)
{
L
li_zan 已提交
153
    return lfs_mkdir(&g_lfs, dirName);
L
li_zan 已提交
154 155 156 157
}

int LfsRmdir(const char * dirName)
{
L
li_zan 已提交
158
    return lfs_remove(&g_lfs, dirName);
L
li_zan 已提交
159 160 161 162
}

DIR *LfsOpendir(const char * dirName)
{
L
li_zan 已提交
163
    int ret;
L
li_zan 已提交
164

L
li_zan 已提交
165 166 167 168
    lfs_dir_t *dir = GetFreeDir();
    if (dir == NULL) {
        return NULL;
    }
L
li_zan 已提交
169

L
li_zan 已提交
170
    ret = lfs_dir_open(&g_lfs, dir, dirName);
L
li_zan 已提交
171

L
li_zan 已提交
172 173 174 175 176
    if (ret == 0) {
        return (DIR *)dir;
    } else {
        return NULL;
    }
L
li_zan 已提交
177 178
}

L
li_zan 已提交
179
struct dirent *LfsReaddir(DIR * dir)
L
li_zan 已提交
180
{
L
li_zan 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    int ret;
    struct lfs_info lfsInfo;

    ret = lfs_dir_read(&g_lfs, (lfs_dir_t *)dir, &lfsInfo);
    if (ret == 0) {
        pthread_mutex_lock(&g_FslocalMutex);
        (void)memcpy_s(g_nameValue.d_name, sizeof(g_nameValue.d_name), lfsInfo.name, strlen(lfsInfo.name) + 1);
        if (lfsInfo.type == LFS_TYPE_DIR) {
            g_nameValue.d_type = DT_DIR;
        } else if (lfsInfo.type == LFS_TYPE_REG) {
            g_nameValue.d_type = DT_REG;
        }

        g_nameValue.d_reclen = lfsInfo.size;
        pthread_mutex_unlock(&g_FslocalMutex);

        return &g_nameValue;
    }

    return NULL;
L
li_zan 已提交
201 202
}

L
li_zan 已提交
203
int LfsClosedir(DIR * dir)
L
li_zan 已提交
204
{
L
li_zan 已提交
205
    return lfs_dir_close(&g_lfs, (lfs_dir_t *)dir);
L
li_zan 已提交
206 207 208 209
}

int LfsOpen(const char * path, int openFlag, int mode)
{
L
li_zan 已提交
210
    int fd = INVALID_FD;
L
li_zan 已提交
211

L
li_zan 已提交
212 213 214 215
    LittleFsHandleStruct *fsHandle = GetFreeFd(&fd);
    if (fd == INVALID_FD) {
        goto errout;
    }
L
li_zan 已提交
216

L
li_zan 已提交
217 218 219 220
    int err = lfs_file_open(&g_lfs, &(fsHandle->file), path, openFlag);
    if (err != 0) {
        goto errout;
    }
L
li_zan 已提交
221

L
li_zan 已提交
222
    return fd;
L
li_zan 已提交
223
errout:
L
li_zan 已提交
224
    return INVALID_FD;    
L
li_zan 已提交
225 226 227 228
}

int LfsRead(int fd, void * buf, unsigned int len)
{
L
li_zan 已提交
229 230 231
    if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
        return VFS_ERROR;
    }
L
li_zan 已提交
232

L
li_zan 已提交
233
    return lfs_file_read(&g_lfs, &(g_handle[fd].file), buf, len);
L
li_zan 已提交
234 235 236 237
}

int LfsWrite(int fd, const void * buf, unsigned int len)
{
L
li_zan 已提交
238 239 240
    if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
        return VFS_ERROR;
    }
L
li_zan 已提交
241

L
li_zan 已提交
242
    return lfs_file_write(&g_lfs, &(g_handle[fd].file), buf, len);
L
li_zan 已提交
243 244 245 246
}

int LfsSeek(int fd, off_t offset, int whence)
{
L
li_zan 已提交
247 248 249
    if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
        return VFS_ERROR;
    }
L
li_zan 已提交
250

L
li_zan 已提交
251
    return lfs_file_seek(&g_lfs, &(g_handle[fd].file), offset, whence);
L
li_zan 已提交
252 253 254 255
}

int LfsClose(int fd)
{
L
li_zan 已提交
256
    int ret = VFS_ERROR;
L
li_zan 已提交
257

L
li_zan 已提交
258 259 260
    if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
        return ret;
    }
L
li_zan 已提交
261

L
li_zan 已提交
262 263 264 265
    ret = lfs_file_close(&g_lfs, &(g_handle[fd].file));
    pthread_mutex_lock(&g_FslocalMutex);
    g_handle[fd].useFlag = 0;
    pthread_mutex_unlock(&g_FslocalMutex);
L
li_zan 已提交
266

L
li_zan 已提交
267
    return ret;    
L
li_zan 已提交
268 269 270 271
}

int LfsRename(const char * oldName, const char * newName)
{
L
li_zan 已提交
272
    return lfs_rename(&g_lfs, oldName, newName);
L
li_zan 已提交
273 274 275 276
}

int LfsStat(const char * path, struct stat * buf)
{
L
li_zan 已提交
277 278
    int ret;
    struct lfs_info info;
L
li_zan 已提交
279

L
li_zan 已提交
280 281 282 283
    ret = lfs_stat(&g_lfs, path, &info);
    if (ret == 0) {
        buf->st_size = info.size;
    }
L
li_zan 已提交
284

L
li_zan 已提交
285
    return ret;    
L
li_zan 已提交
286 287 288 289
}

int LfsFsync(int fd)
{
L
li_zan 已提交
290
    return lfs_file_sync(&g_lfs, &(g_handle[fd].file));
L
li_zan 已提交
291 292
}