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
);
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
)
244 ventoy_http_writeback_pf callback
= (ventoy_http_writeback_pf
)data
;
248 pthread_mutex_lock(&g_writeback_mutex
);
249 pthread_cond_wait(&g_writeback_cond
, &g_writeback_mutex
);
253 pthread_mutex_unlock(&g_writeback_mutex
);
259 pthread_mutex_unlock(&g_writeback_mutex
);
266 void ventoy_set_writeback_event(void)
268 pthread_cond_signal(&g_writeback_cond
);
271 int ventoy_start_writeback_thread(ventoy_http_writeback_pf callback
)
274 pthread_mutex_init(&g_writeback_mutex
, NULL
);
275 pthread_cond_init(&g_writeback_cond
, NULL
);
277 pthread_create(&g_writeback_thread
, NULL
, ventoy_local_thread_run
, callback
);
282 void ventoy_stop_writeback_thread(void)
285 pthread_cond_signal(&g_writeback_cond
);
287 pthread_join(g_writeback_thread
, NULL
);
290 pthread_cond_destroy(&g_writeback_cond
);
291 pthread_mutex_destroy(&g_writeback_mutex
);
295 int ventoy_copy_file(const char *a
, const char *b
)
300 if (0 == ventoy_read_file_to_buf(a
, 0, (void **)&buf
, &len
))
302 ventoy_write_buf_to_file(b
, buf
, len
);