lfs_api.c 7.8 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 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 "lfs_api.h"
#include "iCunit.h"

lfs_t g_lfs;
FileDirInfo g_lfsDir[LFS_MAX_OPEN_DIRS];

FileOpInfo g_fsOp;
static LittleFsHandleStruct g_handle[LITTLE_FS_MAX_OPEN_FILES];
struct dirent g_nameValue;
struct fsmap_t g_fsmap[MAX_FILE_SYSTEM_LEN];
static pthread_mutex_t g_FslocalMutex = PTHREAD_MUTEX_INITIALIZER;

FileOpInfo GetFsOpInfo(void)
{
	return g_fsOp;
}

LittleFsHandleStruct *GetFreeFd(int *fd)
{
	for (int i = 0; i < LITTLE_FS_MAX_OPEN_FILES; i++) {
		if (g_handle[i].useFlag == 0) {
			*fd = i;
			g_handle[i].useFlag = 1;
			return &(g_handle[i]);
		}		
	}

	*fd = INVALID_FD;
	return NULL;
}

lfs_dir_t *GetFreeDir()
{
	for (int i = 0; i < LFS_MAX_OPEN_DIRS; i++) {
		if (g_lfsDir[i].useFlag == 0) {
			g_lfsDir[i].useFlag = 1;
			return &(g_lfsDir[i].dir);
		}
	}

	return NULL;
}

int InitMountInfo(const char *fileSystemType, const struct MountOps *fsMops)
{
	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;
}


const struct fsmap_t *mount_finds(const char*fileSystemtype)
{
	struct fsmap_t *m = NULL;

	for (int i = 0; i < MAX_FILE_SYSTEM_LEN; i++) {
		m = &(g_fsmap[i]);
		if (m->fileSystemtype && strcmp(fileSystemtype, m->fileSystemtype) == 0) {
			return m;
		}
	}

	return (const struct fsmap_t *)NULL;
}

const struct MountOps g_fsMnt = {
	
};

struct FileOps lfs_vops = {
	
};

int LfsMount(const char * source, const char * target, const char * fileSystemType, unsigned long mountflags,
	const void * data)
{
	int ret;

	pthread_mutex_lock(&g_FslocalMutex);
	g_fsOp.fsVops = &lfs_vops;
	ret = lfs_mount(&g_lfs, (struct lfs_config*)data);
	pthread_mutex_unlock(&g_FslocalMutex);

	return ret;
}

int LfsUmount(const char * target)
{
	int ret;

	pthread_mutex_lock(&g_FslocalMutex);
	ret = lfs_unmount(&g_lfs);
	pthread_mutex_unlock(&g_FslocalMutex);

	return ret;	
}

int LfsUnlink(const char * fileName)
{
	int ret;

	pthread_mutex_lock(&g_FslocalMutex);
	ret = lfs_remove(&g_lfs, fileName);
	pthread_mutex_unlock(&g_FslocalMutex);

	return ret;
}

int LfsMkdir(const char * dirName, mode_t mode)
{
	int ret;

	pthread_mutex_lock(&g_FslocalMutex);
	ret = lfs_mkdir(&g_lfs, dirName);
	pthread_mutex_unlock(&g_FslocalMutex);

	return ret;
}

int LfsRmdir(const char * dirName)
{
	int ret;

	pthread_mutex_lock(&g_FslocalMutex);
	ret = lfs_remove(&g_lfs, dirName);
	pthread_mutex_unlock(&g_FslocalMutex);

	return ret;
}

DIR *LfsOpendir(const char * dirName)
{
	int ret;

	lfs_dir_t *dir = GetFreeDir();
	if (dir == NULL) {
		return NULL;
	}

	pthread_mutex_lock(&g_FslocalMutex);
	ret = lfs_dir_open(&g_lfs, dir, dirName);
	pthread_mutex_unlock(&g_FslocalMutex);

	if (ret == 0) {
		return (DIR *)dir;
	} else {
		return NULL;
	}
}

struct direct *LfsReaddir(DIR * dir)
{
	int ret;
	struct lfs_info lfsInfo;

	pthread_mutex_lock(&g_FslocalMutex);
	ret = lfs_dir_read(&g_lfs, (lfs_dir_t *)dir, &lfsInfo);
	if (ret == 0) {
		(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;
	}

	pthread_mutex_unlock(&g_FslocalMutex);
	return NULL;
}

int LfsCloseDir(DIR * dir)
{
	int ret;

	pthread_mutex_lock(&g_FslocalMutex);
	ret = lfs_remove(&g_lfs, dirName);
	pthread_mutex_unlock(&g_FslocalMutex);

	return ret;
}

int LfsOpen(const char * path, int openFlag, int mode)
{
	int fd = INVALID_FD;

	pthread_mutex_lock(&g_FslocalMutex);
	LittleFsHandleStruct *fsHandle = GetFreeDir(&fd);
	if (fd == INVALID_FD) {
		goto errout;
	}

	int err = lfs_file_open(lfs_t * lfs, lfs_file_t * file, const char * path, int flags);
	if (err != 0) {
		goto errout;
	}

	pthread_mutex_unlock(&g_FslocalMutex);
	return fd;
errout:
	pthread_mutex_unlock(&g_FslocalMutex);
	return INVALID_FD;	
}

int LfsRead(int fd, void * buf, unsigned int len)
{
	int ret = VFS_ERROR;

	if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
		return ret;
	}

	pthread_mutex_lock(&g_FslocalMutex);
	ret = lfs_file_read(&g_lfs, &(g_handle[fd].file), buf, len);
	pthread_mutex_unlock(&g_FslocalMutex);

	return ret;
}

int LfsWrite(int fd, const void * buf, unsigned int len)
{
	int ret = VFS_ERROR;

	if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
		return ret;
	}

	pthread_mutex_lock(&g_FslocalMutex);
	ret = lfs_file_write(&g_lfs, &(g_handle[fd].file), buf, len);
	pthread_mutex_unlock(&g_FslocalMutex);

	return ret;
}

int LfsSeek(int fd, off_t offset, int whence)
{
	int ret = VFS_ERROR;

	if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
		return ret;
	}

	pthread_mutex_lock(&g_FslocalMutex);
	ret = lfs_file_seek(&g_lfs, &(g_handle[fd].file), offset, whence);
	pthread_mutex_unlock(&g_FslocalMutex);

	return ret;
}

int LfsClose(int fd)
{
	int ret = VFS_ERROR;

	if (fd >= LITTLE_FS_MAX_OPEN_FILES && fd < 0) {
		return ret;
	}

	pthread_mutex_lock(&g_FslocalMutex);
	ret = lfs_file_close(&g_lfs, &(g_handle[fd].file));
	pthread_mutex_unlock(&g_FslocalMutex);

	return ret;	
}

int LfsRename(const char * oldName, const char * newName)
{
	int ret;

	pthread_mutex_lock(&g_FslocalMutex);
	ret = lfs_rename(oldName, newName);
	pthread_mutex_unlock(&g_FslocalMutex);

	return ret;
}

int LfsStat(const char * path, struct stat * buf)
{
	int ret;
	struct lfs_info info;

	pthread_mutex_lock(&g_FslocalMutex);
	ret = lfs_stat(&g_lfs, path, &info);
	if (ret = 0) {
		buf->st_size = info.size;
	}
	pthread_mutex_unlock(&g_FslocalMutex);

	return ret;	
}

int LfsFsync(int fd)
{
	int ret;

	pthread_mutex_lock(&g_FslocalMutex);
	ret = lfs_file_sync(&g_lfs, &(g_handle[fd].file));
	pthread_mutex_unlock(&g_FslocalMutex);

	return ret;
}