blob: 235ccd253fd5f2cf8d5dbdb867a2a9755713d7a3 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001#include "mbtk_file.h"
2
3/**
4* Return TRUE if file exist,FLASE or not.
5*/
6bool file_exist(const char *path)
7{
8 return (access(path, F_OK) == 0) ? true : false;
9}
10
11/*
12 * Return file descriptor if open file success, return -1 or not.
13 *
14 * flag : File open flag.
15 * O_RDONLY 以只读方式打开文件
16 * O_WRONLY 以只写方式打开文件
17 * O_RDWR 以可读写方式打开文件
18 * 上述三种旗标是互斥
19 *
20 * O_CREAT 若欲打开的文件不存在则自动建立该文件。
21 * O_EXCL 如果 O_CREAT 也被设置,此指令会去检查文件是否存在。文件若不存在则
22 * 建立该文件,否则将导致打开文件错误。此外,若 O_CREAT 与 O_EXCL 同时设置,
23 * 并且欲打开的文件为符号连接,则会打开文件失败。
24 * O_NOCTTY 如果欲打开的文件为终端机设备时,则不会将该终端机当成进程控制终端机。
25 * O_TRUNC 若文件存在并且以可写的方式打开时,此旗标会令文件长度清为 0,而原来存于该文件的资料也会消失。
26 * O_APPEND 当读写文件时会从文件尾开始移动,也就是所写入的数据会以附加的方式加入到文件后面。
27 * O_NONBLOCK 以不可阻断的方式打开文件,也就是无论有无数据读取或等待,都会立即返回进程之中。
28 * O_NDELAY 同 O_NONBLOCK。
29 * O_SYNC 以同步的方式打开文件。
30 * O_NOFOLLOW 如果参数 pathname 所指的文件为一符号连接,则会令打开文件失败。
31 * O_DIRECTORY 如果参数 pathname 所指的文件并非为一目录,则会令打开文件失败
32 */
33int file_open(const char *path, int flag)
34{
35 // Only for create file : rwxrw-rx-
36 int result = open(path, flag, 0766);
37 if(result == -1) {
38 LOGE("Open file[%s] fail:%d", path, errno);
39 }
40 return result;
41}
42
43/*
44* Return file size,or -1 if get file size fail.
45*/
46int file_length(int fd)
47{
48 int cur= lseek(fd, 0, SEEK_CUR);
49 if(cur < 0)
50 {
51 LOGE("lseek(SEEK_CUR) error: %d", errno);
52 return -1;
53 }
54 int len = lseek(fd, 0, SEEK_END);
55 if (len < 0)
56 {
57 LOGE("lseek(SEEK_END) error: %d", errno);
58 return -1;
59 }
60
61 // Reset position
62 if(cur != len) {
63 if(lseek(fd, cur, SEEK_SET))
64 {
65 LOGE("lseek(SEEK_SET) error: %d", errno);
66 return -1;
67 }
68 }
69 return len;
70}
71
72// Read data of specified length.
73int file_read(int fd, void *buf, int nbyte)
74{
75 int count = 0;
76 int len = 0;
77 while (true)
78 {
79 len = read(fd, buf + count, nbyte - count);
80 if (len > 0)
81 {
82 count += len;
83 }
84 else
85 {
86 break;
87 }
88
89 if (count == nbyte)
90 break;
91 }
92
93 return count;
94}
95
96// Write data of specified length.
97int file_write(int fd, void *buf, int nbyte)
98{
99 int count = 0;
100 int len = 0;
101 while (true)
102 {
103 len = write(fd, buf + count, nbyte - count);
104 if (len > 0)
105 {
106 count += len;
107 }
108 else
109 {
110 LOGE("write() fail,ret = %d,errno = %d", len, errno);
111 break;
112 }
113
114 if (count == nbyte)
115 break;
116 }
117
118 return count;
119}
120
121int file_close(int fd)
122{
123 int result = close(fd);
124 if(result == -1) {
125 LOGE("Close file fail:%d", errno);
126 }
127 return result;
128}
b.liufd87baf2024-11-15 15:30:38 +0800129
130/*===========================================================================
131FUNCTION file_link()
132
133DESCRIPTION:
134 Create soft link.
135
136PARAMETERS:
137 oldpath [IN]: Local original file,it may not exist.
138 newpath [IN]: Soft link file. If the file exists, it will be replaced.
139
140RETURN VALUE:
141 Return 0 if success,other for error.
142
143===========================================================================*/
144int file_link(const void* oldpath, const void* newpath)
145{
146 if(oldpath == NULL || newpath == NULL) {
147 LOGE("File not be NULL.");
148 return -1;
149 }
150
151 struct stat file_stat;
152 int ret = lstat((const char*)newpath, &file_stat);
153 if (ret) {
154 if (errno != ENOENT) {
155 LOGE("lstat(%s) fail:%d", (char*)newpath, errno);
156 return -1;
157 }
158
159 // Link file not exist, Create link file directly.
160 } else {
161 if (S_ISLNK(file_stat.st_mode)) {
162 // The file is a symbolic link
163 char buf[1024] = {0};
164 ssize_t len = readlink((char*)newpath, buf, sizeof(buf));
165 if (len == -1) {
166 LOGE("Error reading the link file, errno - %d", errno);
167 return -1;
168 }
169 buf[len] = '\0';
170 LOGD("Link target is: %s", buf);
171 if(strcmp(buf, (const char*)oldpath) == 0) {
172 LOGD("The link target is same,do nothing.");
173 return 0;
174 } else {
175 ret = unlink((const char*)newpath);
176 if(ret) {
177 LOGE("unlink(%s) fail:%d", (char*)newpath, errno);
178 return -1;
179 }
180
181 // Delete link file success.
182 }
183 } else {
184 LOGE("%s exist,but no link file.", (char*)newpath);
185 return -1;
186 }
187 }
188
189 // Start create link file.
190 ret = symlink((const char*)oldpath, (const char*)newpath);
191 if(ret){
192 LOGE("symlink(%s->%s) fail:%d", (char*)newpath, (const char*)newpath, errno);
193 return -1;
194 } else {
195 LOGE("symlink(%s->%s) success.", (char*)newpath, (const char*)newpath);
196 return 0;
197 }
198}