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 <semaphore.h>
37 #include <ventoy_define.h>
38 #include <ventoy_util.h>
40 void ventoy_gen_preudo_uuid(void *uuid
)
45 fd
= open("/dev/urandom", O_RDONLY
| O_BINARY
);
49 for (i
= 0; i
< 8; i
++)
51 *((uint16_t *)uuid
+ i
) = (uint16_t)(rand() & 0xFFFF);
61 int ventoy_get_sys_file_line(char *buffer
, int buflen
, const char *fmt
, ...)
69 vsnprintf(path
, 256, fmt
, arg
);
72 if (access(path
, F_OK
) >= 0)
74 FILE *fp
= fopen(path
, "r");
75 memset(buffer
, 0, buflen
);
76 len
= (int)fread(buffer
, 1, buflen
- 1, fp
);
82 if (c
== '\r' || c
== '\n' || c
== ' ' || c
== '\t')
97 vdebug("%s not exist \n", path
);
102 int ventoy_is_disk_mounted(const char *devpath
)
109 fp
= fopen("/proc/mounts", "r");
115 len
= (int)strlen(devpath
);
116 while (fgets(line
, sizeof(line
), fp
))
118 if (strncmp(line
, devpath
, len
) == 0)
121 vdebug("%s mounted <%s>\n", devpath
, line
);
131 const char * ventoy_get_os_language(void)
133 const char *env
= getenv("LANG");
135 if (env
&& strncasecmp(env
, "zh_CN", 5) == 0)
145 int ventoy_is_file_exist(const char *fmt
, ...)
149 char fullpath
[MAX_PATH
];
152 vsnprintf(fullpath
, MAX_PATH
, fmt
, ap
);
155 if (stat(fullpath
, &sb
))
160 if (S_ISREG(sb
.st_mode
))
168 int ventoy_is_directory_exist(const char *fmt
, ...)
172 char fullpath
[MAX_PATH
];
175 vsnprintf(fullpath
, MAX_PATH
, fmt
, ap
);
178 if (stat(fullpath
, &sb
))
183 if (S_ISDIR(sb
.st_mode
))
191 int ventoy_get_file_size(const char *file
)
196 if (stat(file
, &stStat
) >= 0)
198 Size
= (int)(stStat
.st_size
);
205 int ventoy_write_buf_to_file(const char *FileName
, void *Bufer
, int BufLen
)
211 fd
= open(FileName
, O_CREAT
| O_RDWR
| O_TRUNC
, 0755);
214 vlog("Failed to open file %s %d\n", FileName
, errno
);
218 rc
= fchmod(fd
, 0755);
221 vlog("Failed to chmod <%s> %d\n", FileName
, errno
);
224 size
= write(fd
, Bufer
, BufLen
);
225 if ((int)size
!= BufLen
)
228 vlog("write file %s failed %d err:%d\n", FileName
, (int)size
, errno
);
238 static sem_t g_writeback_sem
;
239 static volatile int g_thread_stop
= 0;
240 static pthread_t g_writeback_thread
;
242 static void * ventoy_local_thread_run(void* data
)
244 ventoy_http_writeback_pf callback
= (ventoy_http_writeback_pf
)data
;
246 while (0 == g_thread_stop
)
248 sem_wait(&g_writeback_sem
);
255 void ventoy_set_writeback_event(void)
257 sem_post(&g_writeback_sem
);
260 int ventoy_start_writeback_thread(ventoy_http_writeback_pf callback
)
264 sem_init(&g_writeback_sem
, 0, 0);
265 pthread_create(&g_writeback_thread
, NULL
, ventoy_local_thread_run
, callback
);
270 void ventoy_stop_writeback_thread(void)
274 sem_post(&g_writeback_sem
);
275 pthread_join(g_writeback_thread
, NULL
);
276 sem_destroy(&g_writeback_sem
);
281 int ventoy_read_file_to_buf(const char *FileName
, int ExtLen
, void **Bufer
, int *BufLen
)
287 fp
= fopen(FileName
, "rb");
290 vlog("Failed to open file %s", FileName
);
294 fseek(fp
, 0, SEEK_END
);
295 FileSize
= (int)ftell(fp
);
297 Data
= malloc(FileSize
+ ExtLen
);
304 fseek(fp
, 0, SEEK_SET
);
305 fread(Data
, 1, FileSize
, fp
);
315 int ventoy_copy_file(const char *a
, const char *b
)
320 if (0 == ventoy_read_file_to_buf(a
, 0, (void **)&buf
, &len
))
322 ventoy_write_buf_to_file(b
, buf
, len
);