• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2022 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  *    conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *    of conditions and the following disclaimer in the documentation and/or other materials
12  *    provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  *    to endorse or promote products derived from this software without specific prior written
16  *    permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #include "vfs_files.h"
31 #include "errno.h"
32 #include "stdio.h"
33 #include "stdlib.h"
34 #include "string.h"
35 #include "los_config.h"
36 #include "vfs_config.h"
37 #include "vfs_mount.h"
38 #include "vfs_operations.h"
39 
40 static struct File g_files[NR_OPEN_DEFAULT];
41 
FileToFd(const struct File * file)42 int FileToFd(const struct File *file)
43 {
44     if (file == NULL) {
45         return (int)LOS_NOK;
46     }
47     return file - g_files + MIN_START_FD;
48 }
49 
FdToFile(int fd)50 struct File *FdToFile(int fd)
51 {
52     if ((fd < MIN_START_FD) || (fd >= CONFIG_NFILE_DESCRIPTORS)) {
53         return NULL;
54     }
55     return &g_files[fd - MIN_START_FD];
56 }
57 
VfsFileGet(void)58 struct File *VfsFileGet(void)
59 {
60     int i;
61     /* protected by g_fsMutex */
62     for (i = 0; i < NR_OPEN_DEFAULT; i++) {
63         if (g_files[i].fStatus == FILE_STATUS_NOT_USED) {
64             g_files[i].fStatus = FILE_STATUS_INITING;
65             return &g_files[i];
66         }
67     }
68 
69     return NULL;
70 }
71 
VfsFileGetSpec(int fd)72 struct File *VfsFileGetSpec(int fd)
73 {
74     if ((fd < MIN_START_FD) || (fd >= CONFIG_NFILE_DESCRIPTORS)) {
75         return NULL;
76     }
77     if (g_files[fd - MIN_START_FD].fStatus == FILE_STATUS_NOT_USED) {
78         g_files[fd - MIN_START_FD].fStatus = FILE_STATUS_INITING;
79         return &g_files[fd - MIN_START_FD];
80     }
81 
82     return NULL;
83 }
84 
VfsFilePut(struct File * file)85 void VfsFilePut(struct File *file)
86 {
87     if (file == NULL) {
88         return;
89     }
90     file->fFlags = 0;
91     file->fFops = NULL;
92     file->fData = NULL;
93     file->fMp = NULL;
94     file->fOffset = 0;
95     file->fOwner = -1;
96     file->fullPath = NULL;
97     file->fStatus = FILE_STATUS_NOT_USED;
98 }
99