]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Plugson/src/Core/ventoy_util_linux.c
1.0.70 release
[Ventoy.git] / Plugson / src / Core / ventoy_util_linux.c
1 /******************************************************************************
2 * ventoy_util_linux.c ---- ventoy util
3 * Copyright (c) 2021, longpanda <admin@ventoy.net>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <stdarg.h>
24 #include <errno.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/types.h>
29 #include <sys/ioctl.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <sys/mount.h>
33 #include <linux/fs.h>
34 #include <dirent.h>
35 #include <time.h>
36 #include <ventoy_define.h>
37 #include <ventoy_util.h>
38
39 void ventoy_gen_preudo_uuid(void *uuid)
40 {
41 int i;
42 int fd;
43
44 fd = open("/dev/urandom", O_RDONLY | O_BINARY);
45 if (fd < 0)
46 {
47 srand(time(NULL));
48 for (i = 0; i < 8; i++)
49 {
50 *((uint16_t *)uuid + i) = (uint16_t)(rand() & 0xFFFF);
51 }
52 }
53 else
54 {
55 read(fd, uuid, 16);
56 close(fd);
57 }
58 }
59
60 int ventoy_get_sys_file_line(char *buffer, int buflen, const char *fmt, ...)
61 {
62 int len;
63 char c;
64 char path[256];
65 va_list arg;
66
67 va_start(arg, fmt);
68 vsnprintf(path, 256, fmt, arg);
69 va_end(arg);
70
71 if (access(path, F_OK) >= 0)
72 {
73 FILE *fp = fopen(path, "r");
74 memset(buffer, 0, buflen);
75 len = (int)fread(buffer, 1, buflen - 1, fp);
76 fclose(fp);
77
78 while (len > 0)
79 {
80 c = buffer[len - 1];
81 if (c == '\r' || c == '\n' || c == ' ' || c == '\t')
82 {
83 buffer[len - 1] = 0;
84 len--;
85 }
86 else
87 {
88 break;
89 }
90 }
91
92 return 0;
93 }
94 else
95 {
96 vdebug("%s not exist \n", path);
97 return 1;
98 }
99 }
100
101 int ventoy_is_disk_mounted(const char *devpath)
102 {
103 int len;
104 int mount = 0;
105 char line[512];
106 FILE *fp = NULL;
107
108 fp = fopen("/proc/mounts", "r");
109 if (!fp)
110 {
111 return 0;
112 }
113
114 len = (int)strlen(devpath);
115 while (fgets(line, sizeof(line), fp))
116 {
117 if (strncmp(line, devpath, len) == 0)
118 {
119 mount = 1;
120 vdebug("%s mounted <%s>\n", devpath, line);
121 goto end;
122 }
123 }
124
125 end:
126 fclose(fp);
127 return mount;
128 }
129
130 const char * ventoy_get_os_language(void)
131 {
132 const char *env = getenv("LANG");
133
134 if (env && strncasecmp(env, "zh_CN", 5) == 0)
135 {
136 return "cn";
137 }
138 else
139 {
140 return "en";
141 }
142 }
143
144 int ventoy_is_file_exist(const char *fmt, ...)
145 {
146 va_list ap;
147 struct stat sb;
148 char fullpath[MAX_PATH];
149
150 va_start (ap, fmt);
151 vsnprintf(fullpath, MAX_PATH, fmt, ap);
152 va_end (ap);
153
154 if (stat(fullpath, &sb))
155 {
156 return 0;
157 }
158
159 if (S_ISREG(sb.st_mode))
160 {
161 return 1;
162 }
163
164 return 0;
165 }
166
167 int ventoy_is_directory_exist(const char *fmt, ...)
168 {
169 va_list ap;
170 struct stat sb;
171 char fullpath[MAX_PATH];
172
173 va_start (ap, fmt);
174 vsnprintf(fullpath, MAX_PATH, fmt, ap);
175 va_end (ap);
176
177 if (stat(fullpath, &sb))
178 {
179 return 0;
180 }
181
182 if (S_ISDIR(sb.st_mode))
183 {
184 return 1;
185 }
186
187 return 0;
188 }
189
190 int ventoy_get_file_size(const char *file)
191 {
192 int Size = -1;
193 struct stat stStat;
194
195 if (stat(file, &stStat) >= 0)
196 {
197 Size = (int)(stStat.st_size);
198 }
199
200 return Size;
201 }
202
203
204 int ventoy_write_buf_to_file(const char *FileName, void *Bufer, int BufLen)
205 {
206 int fd;
207 int rc;
208 ssize_t size;
209
210 fd = open(FileName, O_CREAT | O_RDWR | O_TRUNC, 0755);
211 if (fd < 0)
212 {
213 vlog("Failed to open file %s %d\n", FileName, errno);
214 return 1;
215 }
216
217 rc = fchmod(fd, 0755);
218 if (rc)
219 {
220 vlog("Failed to chmod <%s> %d\n", FileName, errno);
221 }
222
223 size = write(fd, Bufer, BufLen);
224 if ((int)size != BufLen)
225 {
226 close(fd);
227 vlog("write file %s failed %d err:%d\n", FileName, (int)size, errno);
228 return 1;
229 }
230
231 fsync(fd);
232 close(fd);
233
234 return 0;
235 }
236
237
238 static volatile int g_thread_stop = 0;
239 static pthread_t g_writeback_thread;
240 static pthread_mutex_t g_writeback_mutex;
241 static pthread_cond_t g_writeback_cond;
242 static void * ventoy_local_thread_run(void* data)
243 {
244 ventoy_http_writeback_pf callback = (ventoy_http_writeback_pf)data;
245
246 while (1)
247 {
248 pthread_mutex_lock(&g_writeback_mutex);
249 pthread_cond_wait(&g_writeback_cond, &g_writeback_mutex);
250
251 if (g_thread_stop)
252 {
253 pthread_mutex_unlock(&g_writeback_mutex);
254 break;
255 }
256 else
257 {
258 callback();
259 pthread_mutex_unlock(&g_writeback_mutex);
260 }
261 }
262
263 return NULL;
264 }
265
266 void ventoy_set_writeback_event(void)
267 {
268 pthread_cond_signal(&g_writeback_cond);
269 }
270
271 int ventoy_start_writeback_thread(ventoy_http_writeback_pf callback)
272 {
273 g_thread_stop = 0;
274 pthread_mutex_init(&g_writeback_mutex, NULL);
275 pthread_cond_init(&g_writeback_cond, NULL);
276
277 pthread_create(&g_writeback_thread, NULL, ventoy_local_thread_run, callback);
278
279 return 0;
280 }
281
282 void ventoy_stop_writeback_thread(void)
283 {
284 g_thread_stop = 1;
285 pthread_cond_signal(&g_writeback_cond);
286
287 pthread_join(g_writeback_thread, NULL);
288
289
290 pthread_cond_destroy(&g_writeback_cond);
291 pthread_mutex_destroy(&g_writeback_mutex);
292 }
293
294
295
296 int ventoy_read_file_to_buf(const char *FileName, int ExtLen, void **Bufer, int *BufLen)
297 {
298 int FileSize;
299 FILE *fp = NULL;
300 void *Data = NULL;
301
302 fp = fopen(FileName, "rb");
303 if (fp == NULL)
304 {
305 vlog("Failed to open file %s", FileName);
306 return 1;
307 }
308
309 fseek(fp, 0, SEEK_END);
310 FileSize = (int)ftell(fp);
311
312 Data = malloc(FileSize + ExtLen);
313 if (!Data)
314 {
315 fclose(fp);
316 return 1;
317 }
318
319 fseek(fp, 0, SEEK_SET);
320 fread(Data, 1, FileSize, fp);
321
322 fclose(fp);
323
324 *Bufer = Data;
325 *BufLen = FileSize;
326
327 return 0;
328 }
329
330 int ventoy_copy_file(const char *a, const char *b)
331 {
332 int len = 0;
333 char *buf = NULL;
334
335 if (0 == ventoy_read_file_to_buf(a, 0, (void **)&buf, &len))
336 {
337 ventoy_write_buf_to_file(b, buf, len);
338 free(buf);
339 }
340
341 return 0;
342 }
343