• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020-2021 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 
31 #include "osal_file.h"
32 #include "fs/file.h"
33 #include "unistd.h"
34 #include "limits.h"
35 #include "sys/stat.h"
36 #include "hdf_log.h"
37 
38 #define HDF_LOG_TAG osal_file
39 
OsalFileOpen(OsalFile * file,const char * path,int flags,uint32_t rights)40 int32_t OsalFileOpen(OsalFile *file, const char *path, int flags, uint32_t rights)
41 {
42     int32_t fd = -1;
43     char pathName[PATH_MAX + 1] = {0};
44 
45     if (file == NULL || path == NULL) {
46         HDF_LOGE("%s invalid param", __func__);
47         return HDF_ERR_INVALID_PARAM;
48     }
49 
50     file->realFile = (void *)(uintptr_t)fd;
51 
52     if (realpath(path, pathName) == NULL) {
53         HDF_LOGE("%s file name is invalid\n", __func__);
54         return HDF_FAILURE;
55     }
56 
57     fd = open(pathName, flags, rights);
58     if (fd < 0) {
59         HDF_LOGE("%s open file fail %d %d %d", __func__, flags, rights, errno);
60         return HDF_FAILURE;
61     }
62 
63     file->realFile = (void *)(uintptr_t)fd;
64     return HDF_SUCCESS;
65 }
66 
OsalFileWrite(OsalFile * file,const void * string,uint32_t length)67 ssize_t OsalFileWrite(OsalFile *file, const void *string, uint32_t length)
68 {
69     ssize_t ret;
70 
71     if (file == NULL || (int32_t)(uintptr_t)file->realFile == -1 || string == NULL) {
72         HDF_LOGE("%s invalid param", __func__);
73         return HDF_ERR_INVALID_PARAM;
74     }
75 
76     ret = write((int32_t)(uintptr_t)file->realFile, string, length);
77     if (ret < 0) {
78         HDF_LOGE("%s write file fail %d %d", __func__, length, errno);
79         return HDF_FAILURE;
80     }
81 
82     return ret;
83 }
84 
OsalFileClose(OsalFile * file)85 void OsalFileClose(OsalFile *file)
86 {
87     if (file == NULL || (int32_t)(uintptr_t)file->realFile == -1) {
88         HDF_LOGE("%s invalid param", __func__);
89         return;
90     }
91 
92     close((int32_t)(uintptr_t)file->realFile);
93     file->realFile = (void *)(uintptr_t)-1;
94 }
95 
OsalFileRead(OsalFile * file,void * buf,uint32_t length)96 ssize_t OsalFileRead(OsalFile *file, void *buf, uint32_t length)
97 {
98     ssize_t ret;
99 
100     if (file == NULL || (int32_t)(uintptr_t)file->realFile == -1 || buf == NULL) {
101         HDF_LOGE("%s invalid param", __func__);
102         return HDF_ERR_INVALID_PARAM;
103     }
104 
105     ret = read((int32_t)(uintptr_t)file->realFile, buf, length);
106     if (ret < 0) {
107         HDF_LOGE("%s read file fail %d %d", __func__, length, errno);
108         return HDF_FAILURE;
109     }
110 
111     return ret;
112 }
113 
OsalFileLseek(OsalFile * file,off_t offset,int32_t whence)114 off_t OsalFileLseek(OsalFile *file, off_t offset, int32_t whence)
115 {
116     off_t ret;
117 
118     if (file == NULL || (int32_t)(uintptr_t)file->realFile == -1) {
119         HDF_LOGE("%s invalid param", __func__);
120         return HDF_ERR_INVALID_PARAM;
121     }
122 
123     ret = lseek((int32_t)(uintptr_t)file->realFile, offset, whence);
124     if (ret < 0) {
125         HDF_LOGE("%s lseek file fail %lld %d %d", __func__, offset, whence, errno);
126         return HDF_FAILURE;
127     }
128 
129     return ret;
130 }
131 
132