lfs_api.c 8.2 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 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
}

L
li_zan 已提交
49
LittleFsHandleStruct *GetFreeFd(const char *fileName, int *fd)
L
li_zan 已提交
50
{
L
li_zan 已提交
51 52
    int len = strlen() + 1;

L
li_zan 已提交
53 54 55 56 57
    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;
L
li_zan 已提交
58 59 60 61
            g_handle[i].pathName = (char *)malloc(len);
            if (g_handle[i].pathName) {
                memcpy_s(g_handle[i].pathName, len, fileName, len);
            }
L
li_zan 已提交
62 63
            pthread_mutex_unlock(&g_FslocalMutex);
            return &(g_handle[i]);
L
li_zan 已提交
64
        }
L
li_zan 已提交
65 66 67 68
    }
    pthread_mutex_unlock(&g_FslocalMutex);
    *fd = INVALID_FD;
    return NULL;
L
li_zan 已提交
69 70
}

L
li_zan 已提交
71 72
bool CheckFileIsOpen(const char *fileName, int *fd)
{
L
li_zan 已提交
73 74 75
    for (int i = 0; i < LITTLE_FS_MAX_OPEN_FILES; i++) {
        if (g_handle[i].useFlag == 1) {
            if (strcmp(g_handle[i].pathName, fileName) == 0) {
L
li_zan 已提交
76 77 78 79 80 81 82 83 84 85
                *fd = i;
                return true;
            }
        }
    }

    *fd = INVALID_FD;
    return false;
}

L
li_zan 已提交
86 87
lfs_dir_t *GetFreeDir()
{
L
li_zan 已提交
88 89 90 91 92 93 94 95 96 97
    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 已提交
98 99 100 101
}

int InitMountInfo(const char *fileSystemType, const struct MountOps *fsMops)
{
L
li_zan 已提交
102 103 104 105 106
    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);
L
li_zan 已提交
107
            g_fsmap[i].fsMops = fsMops;
L
li_zan 已提交
108 109 110 111 112
            return VFS_OK;
        }
    }

    return VFS_ERROR;
L
li_zan 已提交
113 114
}


L
li_zan 已提交
115
const struct FsMap *MountFindfs(const char *fileSystemtype)
L
li_zan 已提交
116
{
L
li_zan 已提交
117
    struct FsMap *m = NULL;
L
li_zan 已提交
118

L
li_zan 已提交
119 120 121 122 123 124
    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 已提交
125

L
li_zan 已提交
126
    return (const struct FsMap *)NULL;
L
li_zan 已提交
127 128 129
}

const struct MountOps g_fsMnt = {
L
li_zan 已提交
130 131
    .Mount = LfsMount,
    .Umount = LfsUmount,
L
li_zan 已提交
132 133
};

L
li_zan 已提交
134
const struct FileOps g_lfsVops = {
L
li_zan 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148
    .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 已提交
149 150
};

L
li_zan 已提交
151 152
int LfsMount(const char *source, const char *target, const char *fileSystemType, unsigned long mountflags,
    const void *data)
L
li_zan 已提交
153
{
L
li_zan 已提交
154
    int ret;
L
li_zan 已提交
155

L
li_zan 已提交
156
    g_fsOp.fsVops = &g_lfsVops;
L
li_zan 已提交
157
    ret = lfs_mount(&g_lfs, (struct lfs_config*)data);
L
li_zan 已提交
158

L
li_zan 已提交
159
    return ret;
L
li_zan 已提交
160 161
}

L
li_zan 已提交
162
int LfsUmount(const char *target)
L
li_zan 已提交
163
{
L
li_zan 已提交
164
    return lfs_unmount(&g_lfs);
L
li_zan 已提交
165 166
}

L
li_zan 已提交
167
int LfsUnlink(const char *fileName)
L
li_zan 已提交
168
{
L
li_zan 已提交
169
    return lfs_remove(&g_lfs, fileName);
L
li_zan 已提交
170 171
}

L
li_zan 已提交
172
int LfsMkdir(const char *dirName, mode_t mode)
L
li_zan 已提交
173
{
L
li_zan 已提交
174
    return lfs_mkdir(&g_lfs, dirName);
L
li_zan 已提交
175 176
}

L
li_zan 已提交
177
int LfsRmdir(const char *dirName)
L
li_zan 已提交
178
{
L
li_zan 已提交
179
    return lfs_remove(&g_lfs, dirName);
L
li_zan 已提交
180 181
}

L
li_zan 已提交
182
DIR *LfsOpendir(const char *dirName)
L
li_zan 已提交
183
{
L
li_zan 已提交
184
    int ret;
L
li_zan 已提交
185

L
li_zan 已提交
186 187 188 189
    lfs_dir_t *dir = GetFreeDir();
    if (dir == NULL) {
        return NULL;
    }
L
li_zan 已提交
190

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

L
li_zan 已提交
193 194 195 196 197
    if (ret == 0) {
        return (DIR *)dir;
    } else {
        return NULL;
    }
L
li_zan 已提交
198 199
}

L
li_zan 已提交
200
struct dirent *LfsReaddir(DIR *dir)
L
li_zan 已提交
201
{
L
li_zan 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    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 已提交
222 223
}

L
li_zan 已提交
224
int LfsClosedir(const DIR *dir)
L
li_zan 已提交
225
{
L
li_zan 已提交
226
    return lfs_dir_close(&g_lfs, (lfs_dir_t *)dir);
L
li_zan 已提交
227 228
}

L
li_zan 已提交
229
int LfsOpen(const char *pathName, int openFlag, int mode)
L
li_zan 已提交
230
{
L
li_zan 已提交
231
    int fd = INVALID_FD;
L
li_zan 已提交
232

L
li_zan 已提交
233 234 235 236
    if (CheckFileIsOpen(pathName, &fd)) {
        return fd;
    }

L
li_zan 已提交
237
    LittleFsHandleStruct *fsHandle = GetFreeFd(pathName, &fd);
L
li_zan 已提交
238 239 240
    if (fd == INVALID_FD) {
        goto errout;
    }
L
li_zan 已提交
241

L
li_zan 已提交
242
    int err = lfs_file_open(&g_lfs, &(fsHandle->file), pathName, openFlag);
L
li_zan 已提交
243 244 245
    if (err != 0) {
        goto errout;
    }
L
li_zan 已提交
246

L
li_zan 已提交
247
    return fd;
L
li_zan 已提交
248
errout:
L
li_zan 已提交
249
    return INVALID_FD;    
L
li_zan 已提交
250 251
}

L
li_zan 已提交
252
int LfsRead(int fd, void *buf, unsigned int len)
L
li_zan 已提交
253
{
L
li_zan 已提交
254 255 256
    if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
        return VFS_ERROR;
    }
L
li_zan 已提交
257

L
li_zan 已提交
258
    return lfs_file_read(&g_lfs, &(g_handle[fd].file), buf, len);
L
li_zan 已提交
259 260
}

L
li_zan 已提交
261
int LfsWrite(int fd, const void *buf, unsigned int len)
L
li_zan 已提交
262
{
L
li_zan 已提交
263 264 265
    if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
        return VFS_ERROR;
    }
L
li_zan 已提交
266

L
li_zan 已提交
267
    return lfs_file_write(&g_lfs, &(g_handle[fd].file), buf, len);
L
li_zan 已提交
268 269 270 271
}

int LfsSeek(int fd, off_t offset, int whence)
{
L
li_zan 已提交
272 273 274
    if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
        return VFS_ERROR;
    }
L
li_zan 已提交
275

L
li_zan 已提交
276
    return lfs_file_seek(&g_lfs, &(g_handle[fd].file), offset, whence);
L
li_zan 已提交
277 278 279 280
}

int LfsClose(int fd)
{
L
li_zan 已提交
281
    int ret = VFS_ERROR;
L
li_zan 已提交
282

L
li_zan 已提交
283 284 285
    if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
        return ret;
    }
L
li_zan 已提交
286

L
li_zan 已提交
287 288 289 290
    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 已提交
291

L
li_zan 已提交
292
    return ret;    
L
li_zan 已提交
293 294
}

L
li_zan 已提交
295
int LfsRename(const char *oldName, const char *newName)
L
li_zan 已提交
296
{
L
li_zan 已提交
297
    return lfs_rename(&g_lfs, oldName, newName);
L
li_zan 已提交
298 299
}

L
li_zan 已提交
300
int LfsStat(const char *path, struct stat *buf)
L
li_zan 已提交
301
{
L
li_zan 已提交
302 303
    int ret;
    struct lfs_info info;
L
li_zan 已提交
304

L
li_zan 已提交
305 306 307 308
    ret = lfs_stat(&g_lfs, path, &info);
    if (ret == 0) {
        buf->st_size = info.size;
    }
L
li_zan 已提交
309

L
li_zan 已提交
310
    return ret;    
L
li_zan 已提交
311 312 313 314
}

int LfsFsync(int fd)
{
L
li_zan 已提交
315
    return lfs_file_sync(&g_lfs, &(g_handle[fd].file));
L
li_zan 已提交
316 317
}