1 /*
2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved.
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 */
15 #include <string.h>
16 #include "utils_file.h"
17 #define LOG_TAG "UtilsFile"
18 #include "log.h"
19
20 #define SEEK_COUNT 3
21 #define FILE_SIZE 64
22 #define READ_SIZE 4
23 #define SEEK_POS 2
24
25
utils_file_test(void)26 void utils_file_test(void)
27 {
28 const char *path = "/data/test.txt";
29 const char *data = "utils_file_test";
30 const int whence[3] = {SEEK_SET_FS, SEEK_CUR_FS, SEEK_END_FS};
31 char buf[FILE_SIZE] = {0};
32 unsigned int fileSize = 0;
33
34 int ret;
35 int fd = UtilsFileOpen(path, O_WRONLY_FS | O_CREAT_FS, 0664);
36 if (fd < 0) {
37 HILOG_ERROR(HILOG_MODULE_APP, "%d: UtilsFileOpen failed", __LINE__);
38 return;
39 }
40 ret = UtilsFileWrite(fd, data, strlen(data));
41 if (ret != strlen(data)) {
42 HILOG_ERROR(HILOG_MODULE_APP, "%d: UtilsFileWrite failed", __LINE__);
43 return;
44 }
45 ret = UtilsFileClose(fd);
46 if (ret != 0) {
47 HILOG_ERROR(HILOG_MODULE_APP, "%d: UtilsFileClose failed", __LINE__);
48 return;
49 }
50 fd = UtilsFileOpen(path, O_RDONLY_FS, 0);
51 if (fd < 0) {
52 HILOG_ERROR(HILOG_MODULE_APP, "%d: UtilsFileOpen failed", __LINE__);
53 return;
54 }
55 ret = UtilsFileStat(path, &fileSize);
56 if (ret != 0) {
57 HILOG_ERROR(HILOG_MODULE_APP, "%d: UtilsFileStat failed", __LINE__);
58 return;
59 }
60 HILOG_DEBUG(HILOG_MODULE_APP, "fileSize %u", fileSize);
61
62 for (int i = 0; i < SEEK_COUNT; i++) {
63 ret = UtilsFileSeek(fd, 0, whence[i]);
64 if (ret < 0) {
65 HILOG_ERROR(HILOG_MODULE_APP, "%d: UtilsFileSeek failed", __LINE__);
66 goto ERR;
67 }
68 }
69
70 UtilsFileSeek(fd, 0, SEEK_SET_FS);
71 ret = UtilsFileRead(fd, buf, READ_SIZE);
72 if (ret != READ_SIZE) {
73 HILOG_ERROR(HILOG_MODULE_APP, "%d: UtilsFileRead failed", __LINE__);
74 return;
75 }
76 buf[READ_SIZE] = '\0';
77 HILOG_DEBUG(HILOG_MODULE_APP, "read: %s", buf);
78 UtilsFileSeek(fd, SEEK_POS, SEEK_SET_FS);
79 ret = UtilsFileRead(fd, buf, READ_SIZE);
80 if (ret != READ_SIZE) {
81 HILOG_ERROR(HILOG_MODULE_APP, "%d: UtilsFileRead failed", __LINE__);
82 return;
83 }
84 buf[READ_SIZE] = '\0';
85 HILOG_DEBUG(HILOG_MODULE_APP, "read: %s", buf);
86
87 ERR:
88 ret = UtilsFileClose(fd);
89 if (ret != 0) {
90 HILOG_ERROR(HILOG_MODULE_APP, "%d: UtilsFileClose failed", __LINE__);
91 return;
92 }
93 ret = UtilsFileDelete(path);
94 if (ret != 0) {
95 HILOG_ERROR(HILOG_MODULE_APP, "%d: UtilsFileDelete failed", __LINE__);
96 return;
97 }
98 }
99