• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2022 Huawei Technologies Co., Ltd. All rights reserved.
3  *
4  * UniProton is licensed under Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *          http://license.coscl.org.cn/MulanPSL2
8  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
9  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
10  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
11  * See the Mulan PSL v2 for more details.
12  * Create: 2022-09-21
13  * Description: 文件系统vfs层
14  */
15 #include "errno.h"
16 #include "stdio.h"
17 #include "stdlib.h"
18 #include "string.h"
19 #include "vfs_config.h"
20 #include "vfs_files.h"
21 #include "vfs_mount.h"
22 #include "vfs_operations.h"
23 #include "prt_fs.h"
24 
25 static struct TagFile g_files[NR_OPEN_DEFAULT];
26 
OsFileToFd(struct TagFile * file)27 S32 OsFileToFd(struct TagFile *file)
28 {
29     if (file == NULL) {
30         return FS_NOK;
31     }
32     return file - g_files + MIN_START_FD;
33 }
34 
OsFdToFile(S32 fd)35 struct TagFile *OsFdToFile(S32 fd)
36 {
37     if ((fd < MIN_START_FD) || (fd >= CONFIG_NFILE_DESCRIPTORS)) {
38         return NULL;
39     }
40     return &g_files[fd - MIN_START_FD];
41 }
42 
OsVfsGetFile(void)43 struct TagFile *OsVfsGetFile(void)
44 {
45     /* protected by g_fsMutex */
46     for (S32 i = 0; i < NR_OPEN_DEFAULT; i++) {
47         if (g_files[i].fStatus == FILE_STATUS_NOT_USED) {
48             g_files[i].fStatus = FILE_STATUS_INITING;
49             return &g_files[i];
50         }
51     }
52 
53     return NULL;
54 }
55 
OsVfsGetFileSpec(S32 fd)56 struct TagFile *OsVfsGetFileSpec(S32 fd)
57 {
58     if ((fd < MIN_START_FD) || (fd >= CONFIG_NFILE_DESCRIPTORS)) {
59         return NULL;
60     }
61     if (g_files[fd - MIN_START_FD].fStatus == FILE_STATUS_NOT_USED) {
62         g_files[fd - MIN_START_FD].fStatus = FILE_STATUS_INITING;
63         return &g_files[fd - MIN_START_FD];
64     }
65 
66     return NULL;
67 }
68 
OsVfsPutFile(struct TagFile * file)69 void OsVfsPutFile(struct TagFile *file)
70 {
71     if (file == NULL) {
72         return;
73     }
74     file->fFlags = 0;
75     file->fFops = NULL;
76     file->fData = NULL;
77     file->fMp = NULL;
78     file->fOffset = 0;
79     file->fOwner = -1;
80     file->fullPath = NULL;
81     file->fStatus = FILE_STATUS_NOT_USED;
82 }
83