lfs_api.c 7.5 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
/*
 * 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"

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

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

FileOpInfo GetFsOpInfo(void)
{
L
li_zan 已提交
44
    return g_fsOp;
L
li_zan 已提交
45 46
}

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

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

L
li_zan 已提交
69
BOOL CheckFileIsOpen(const char *fileName)
L
li_zan 已提交
70
{
L
li_zan 已提交
71 72 73
    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 已提交
74
                return TRUE;
L
li_zan 已提交
75 76 77 78
            }
        }
    }

L
li_zan 已提交
79
    return FALSE;
L
li_zan 已提交
80 81
}

L
li_zan 已提交
82 83
lfs_dir_t *GetFreeDir()
{
L
li_zan 已提交
84 85 86 87 88 89 90 91 92 93
    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 已提交
94 95
}

C
chenjing 已提交
96
const struct MountOps g_lfsMnt = {
L
li_zan 已提交
97 98
    .Mount = LfsMount,
    .Umount = LfsUmount,
L
li_zan 已提交
99 100
};

C
chenjing 已提交
101
const struct FileOps g_lfsFops = {
L
li_zan 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115
    .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 已提交
116 117
};

L
li_zan 已提交
118 119
int LfsMount(const char *source, const char *target, const char *fileSystemType, unsigned long mountflags,
    const void *data)
L
li_zan 已提交
120
{
L
li_zan 已提交
121
    int ret;
L
li_zan 已提交
122

L
li_zan 已提交
123
    g_fsOp.fsVops = &g_lfsVops;
L
li_zan 已提交
124
    ret = lfs_mount(&g_lfs, (struct lfs_config*)data);
L
li_zan 已提交
125

L
li_zan 已提交
126
    return ret;
L
li_zan 已提交
127 128
}

L
li_zan 已提交
129
int LfsUmount(const char *target)
L
li_zan 已提交
130
{
L
li_zan 已提交
131
    return lfs_unmount(&g_lfs);
L
li_zan 已提交
132 133
}

L
li_zan 已提交
134
int LfsUnlink(const char *fileName)
L
li_zan 已提交
135
{
L
li_zan 已提交
136
    return lfs_remove(&g_lfs, fileName);
L
li_zan 已提交
137 138
}

L
li_zan 已提交
139
int LfsMkdir(const char *dirName, mode_t mode)
L
li_zan 已提交
140
{
L
li_zan 已提交
141
    return lfs_mkdir(&g_lfs, dirName);
L
li_zan 已提交
142 143
}

L
li_zan 已提交
144
int LfsRmdir(const char *dirName)
L
li_zan 已提交
145
{
L
li_zan 已提交
146
    return lfs_remove(&g_lfs, dirName);
L
li_zan 已提交
147 148
}

L
li_zan 已提交
149
DIR *LfsOpendir(const char *dirName)
L
li_zan 已提交
150
{
L
li_zan 已提交
151
    int ret;
L
li_zan 已提交
152

L
li_zan 已提交
153 154 155 156
    lfs_dir_t *dir = GetFreeDir();
    if (dir == NULL) {
        return NULL;
    }
L
li_zan 已提交
157

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

L
li_zan 已提交
160 161 162 163 164
    if (ret == 0) {
        return (DIR *)dir;
    } else {
        return NULL;
    }
L
li_zan 已提交
165 166
}

L
li_zan 已提交
167
struct dirent *LfsReaddir(DIR *dir)
L
li_zan 已提交
168
{
L
li_zan 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    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 已提交
189 190
}

L
li_zan 已提交
191
int LfsClosedir(const DIR *dir)
L
li_zan 已提交
192
{
L
li_zan 已提交
193
    return lfs_dir_close(&g_lfs, (lfs_dir_t *)dir);
L
li_zan 已提交
194 195
}

L
li_zan 已提交
196
int LfsOpen(const char *pathName, int openFlag, int mode)
L
li_zan 已提交
197
{
L
li_zan 已提交
198
    int fd = INVALID_FD;
L
li_zan 已提交
199

L
li_zan 已提交
200
    // if file is already open, return invalid fd
L
li_zan 已提交
201
    if (pathName == NULL || CheckFileIsOpen(pathName)) {
L
li_zan 已提交
202
        goto errout;
L
li_zan 已提交
203 204
    }

L
li_zan 已提交
205
    LittleFsHandleStruct *fsHandle = LfsAllocFd(pathName, &fd);
L
li_zan 已提交
206 207 208
    if (fd == INVALID_FD) {
        goto errout;
    }
L
li_zan 已提交
209

L
li_zan 已提交
210
    int err = lfs_file_open(&g_lfs, &(fsHandle->file), pathName, openFlag);
L
li_zan 已提交
211 212 213
    if (err != 0) {
        goto errout;
    }
L
li_zan 已提交
214

L
li_zan 已提交
215
    return fd;
L
li_zan 已提交
216
errout:
L
li_zan 已提交
217
    return INVALID_FD;
L
li_zan 已提交
218 219
}

L
li_zan 已提交
220
int LfsRead(int fd, void *buf, unsigned int len)
L
li_zan 已提交
221
{
L
li_zan 已提交
222 223 224
    if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
        return VFS_ERROR;
    }
L
li_zan 已提交
225

L
li_zan 已提交
226
    return lfs_file_read(&g_lfs, &(g_handle[fd].file), buf, len);
L
li_zan 已提交
227 228
}

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

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

int LfsSeek(int fd, off_t offset, int whence)
{
L
li_zan 已提交
240 241 242
    if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
        return VFS_ERROR;
    }
L
li_zan 已提交
243

L
li_zan 已提交
244
    return lfs_file_seek(&g_lfs, &(g_handle[fd].file), offset, whence);
L
li_zan 已提交
245 246 247 248
}

int LfsClose(int fd)
{
L
li_zan 已提交
249
    int ret = VFS_ERROR;
L
li_zan 已提交
250

L
li_zan 已提交
251 252 253
    if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
        return ret;
    }
L
li_zan 已提交
254

L
li_zan 已提交
255
    pthread_mutex_lock(&g_FslocalMutex);
L
li_zan 已提交
256
    ret = lfs_file_close(&g_lfs, &(g_handle[fd].file));
L
li_zan 已提交
257
    g_handle[fd].useFlag = 0;
C
chenjing 已提交
258 259
    if (g_handle[fd].pathName != NULL) {

L
li_zan 已提交
260
        free(g_handle[fd].pathName);
L
li_zan 已提交
261
        g_handle[fd].pathName = NULL;
L
li_zan 已提交
262
    }
L
li_zan 已提交
263
    pthread_mutex_unlock(&g_FslocalMutex);
L
li_zan 已提交
264

L
li_zan 已提交
265
    return ret;    
L
li_zan 已提交
266 267
}

L
li_zan 已提交
268
int LfsRename(const char *oldName, const char *newName)
L
li_zan 已提交
269
{
L
li_zan 已提交
270
    return lfs_rename(&g_lfs, oldName, newName);
L
li_zan 已提交
271 272
}

L
li_zan 已提交
273
int LfsStat(const char *path, struct stat *buf)
L
li_zan 已提交
274
{
L
li_zan 已提交
275 276
    int ret;
    struct lfs_info info;
L
li_zan 已提交
277

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

L
li_zan 已提交
283
    return ret;    
L
li_zan 已提交
284 285 286 287
}

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