1 /******************************************************************************
2 * ventoy_util_linux.c ---- ventoy util
3 * Copyright (c) 2021, longpanda <admin@ventoy.net>
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.
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.
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/>.
28 #include <sys/types.h>
29 #include <sys/ioctl.h>
31 #include <sys/types.h>
32 #include <sys/mount.h>
36 #include <ventoy_define.h>
37 #include <ventoy_util.h>
39 void ventoy_gen_preudo_uuid(void *uuid
)
44 fd
= open("/dev/urandom", O_RDONLY
| O_BINARY
);
48 for (i
= 0; i
< 8; i
++)
50 *((uint16_t *)uuid
+ i
) = (uint16_t)(rand() & 0xFFFF);
60 int ventoy_get_sys_file_line(char *buffer
, int buflen
, const char *fmt
, ...)
68 vsnprintf(path
, 256, fmt
, arg
);
71 if (access(path
, F_OK
) >= 0)
73 FILE *fp
= fopen(path
, "r");
74 memset(buffer
, 0, buflen
);
75 len
= (int)fread(buffer
, 1, buflen
- 1, fp
);
81 if (c
== '\r' || c
== '\n' || c
== ' ' || c
== '\t')
96 vdebug("%s not exist \n", path
);
101 int ventoy_is_disk_mounted(const char *devpath
)
108 fp
= fopen("/proc/mounts", "r");
114 len
= (int)strlen(devpath
);
115 while (fgets(line
, sizeof(line
), fp
))
117 if (strncmp(line
, devpath
, len
) == 0)
120 vdebug("%s mounted <%s>\n", devpath
, line
);
130 const char * ventoy_get_os_language(void)
132 const char *env
= getenv("LANG");
134 if (env
&& strncasecmp(env
, "zh_CN", 5) == 0)
144 int ventoy_is_file_exist(const char *fmt
, ...)
148 char fullpath
[MAX_PATH
];
151 vsnprintf(fullpath
, MAX_PATH
, fmt
, ap
);
154 if (stat(fullpath
, &sb
))
159 if (S_ISREG(sb
.st_mode
))
167 int ventoy_is_directory_exist(const char *fmt
, ...)
171 char fullpath
[MAX_PATH
];
174 vsnprintf(fullpath
, MAX_PATH
, fmt
, ap
);
177 if (stat(fullpath
, &sb
))
182 if (S_ISDIR(sb
.st_mode
))
190 int ventoy_get_file_size(const char *file
)
195 if (stat(file
, &stStat
) >= 0)
197 Size
= (int)(stStat
.st_size
);
204 int ventoy_write_buf_to_file(const char *FileName
, void *Bufer
, int BufLen
)
210 fd
= open(FileName
, O_CREAT
| O_RDWR
| O_TRUNC
, 0755);
213 vlog("Failed to open file %s %d\n", FileName
, errno
);
217 rc
= fchmod(fd
, 0755);
220 vlog("Failed to chmod <%s> %d\n", FileName
, errno
);
223 size
= write(fd
, Bufer
, BufLen
);
224 if ((int)size
!= BufLen
)
227 vlog("write file %s failed %d err:%d\n", FileName
, (int)size
, errno
);
237 int ventoy_decompress_tar(char *tarbuf
, int buflen
, int *tarsize
)
242 unsigned char *buffer
= NULL
;
243 char tarxz
[MAX_PATH
];
245 scnprintf(tarxz
, sizeof(tarxz
), "%s/tool/plugson.tar.xz", g_ventoy_dir
);
246 if (ventoy_read_file_to_buf(tarxz
, 0, (void **)&buffer
, &BufLen
))
248 vlog("Failed to read file <%s>\n", tarxz
);
252 g_unxz_buffer
= (unsigned char *)tarbuf
;
255 unxz(buffer
, BufLen
, NULL
, unxz_flush
, NULL
, &inused
, unxz_error
);
256 vlog("xzlen:%u rawdata size:%d\n", BufLen
, g_unxz_len
);
258 if (inused
!= BufLen
)
260 vlog("Failed to unxz data %d %d\n", inused
, BufLen
);
265 *tarsize
= g_unxz_len
;
274 static volatile int g_thread_stop
= 0;
275 static pthread_t g_writeback_thread
;
276 static pthread_mutex_t g_writeback_mutex
;
277 static pthread_cond_t g_writeback_cond
;
278 static void * ventoy_local_thread_run(void* data
)
280 ventoy_http_writeback_pf callback
= (ventoy_http_writeback_pf
)data
;
284 pthread_mutex_lock(&g_writeback_mutex
);
285 pthread_cond_wait(&g_writeback_cond
, &g_writeback_mutex
);
289 pthread_mutex_unlock(&g_writeback_mutex
);
295 pthread_mutex_unlock(&g_writeback_mutex
);
302 void ventoy_set_writeback_event(void)
304 pthread_cond_signal(&g_writeback_cond
);
307 int ventoy_start_writeback_thread(ventoy_http_writeback_pf callback
)
310 pthread_mutex_init(&g_writeback_mutex
, NULL
);
311 pthread_cond_init(&g_writeback_cond
, NULL
);
313 pthread_create(&g_writeback_thread
, NULL
, ventoy_local_thread_run
, callback
);
318 void ventoy_stop_writeback_thread(void)
321 pthread_cond_signal(&g_writeback_cond
);
323 pthread_join(g_writeback_thread
, NULL
);
326 pthread_cond_destroy(&g_writeback_cond
);
327 pthread_mutex_destroy(&g_writeback_mutex
);
331 int ventoy_copy_file(const char *a
, const char *b
)
336 if (0 == ventoy_read_file_to_buf(a
, 0, (void **)&buf
, &len
))
338 ventoy_write_buf_to_file(b
, buf
, len
);