1 /*
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 * Description: fileops
15 *
16 * Create: 2021-12-16
17 */
18
19 #include <fcntl.h>
20 #ifdef LOSCFG_FS_VFS
21 #include <fs/fs.h>
22 #endif
23 #include <unistd.h>
24
25 #include "los_memory.h"
26 #include "soc_osal.h"
27 #include "osal_errno.h"
28 #include "osal_inner.h"
29
30 char *g_klib_store_path = NULL;
osal_klib_fopen(const char * filename,int flags,int mode)31 void *osal_klib_fopen(const char *filename, int flags, int mode)
32 {
33 int *filp = NULL;
34 if (filename == NULL) {
35 return NULL;
36 }
37 int fd = open(filename, flags, mode);
38 if (fd < 0) {
39 osal_log("open failed! fd = %d, errno = %d\n", fd, get_errno());
40 return NULL;
41 } else {
42 filp = (int *)LOS_MemAlloc((void*)m_aucSysMem0, sizeof(int));
43 if (filp == NULL) {
44 osal_log("LOS_MemAlloc failed!\n");
45 close(fd);
46 return NULL;
47 }
48 *filp = fd;
49 }
50 return (void *)filp;
51 }
52
osal_klib_fclose(void * filp)53 void osal_klib_fclose(void *filp)
54 {
55 if (filp == NULL) {
56 osal_log("parameter invalid!\n");
57 return;
58 }
59 close(*(int *)filp);
60 LOS_MemFree((void*)m_aucSysMem0, filp);
61 }
62
osal_klib_fwrite(const char * buf,unsigned long size,void * filp)63 int osal_klib_fwrite(const char *buf, unsigned long size, void *filp)
64 {
65 if (filp == NULL || buf == NULL) {
66 osal_log("parameter invalid!\n");
67 return OSAL_FAILURE;
68 }
69 return write(*(int *)filp, buf, size);
70 }
71
osal_klib_fread(char * buf,unsigned long size,void * filp)72 int osal_klib_fread(char *buf, unsigned long size, void *filp)
73 {
74 if (filp == NULL || buf == NULL) {
75 osal_log("parameter invalid!\n");
76 return OSAL_FAILURE;
77 }
78 return read(*(int *)filp, buf, size);
79 }
80
osal_klib_fseek(long long offset,int whence,void * filp)81 int osal_klib_fseek(long long offset, int whence, void *filp)
82 {
83 int ret;
84 off_t res;
85 if (filp == NULL || offset > INT32_MAX) {
86 osal_log("parameter invalid!\n");
87 return OSAL_FAILURE;
88 }
89 res = lseek(*(int *)filp, (off_t)offset, whence);
90 ret = (int)res;
91 if (res != (off_t)ret) {
92 ret = OSAL_EOVERFLOW;
93 }
94 return ret;
95 }
96
osal_klib_fsync(void * filp)97 void osal_klib_fsync(void *filp)
98 {
99 if (filp == NULL) {
100 osal_log("parameter invalid!\n");
101 return;
102 }
103 fsync(*(int *)filp);
104 }
105