1 /******************************************************************************
4 * Copyright (c) 2021, longpanda <admin@ventoy.net>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 3 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #ifndef __VENTOY_UTIL_H__
21 #define __VENTOY_UTIL_H__
23 #define PLUGSON_TXZ "plugson.tar.xz"
25 #define check_free(p) if (p) free(p)
26 #define vtoy_safe_close_fd(fd) \
35 extern char g_cur_dir
[MAX_PATH
];
36 extern char g_ventoy_dir
[MAX_PATH
];
38 #if defined(_MSC_VER) || defined(WIN32)
40 typedef HANDLE pthread_mutex_t
;
42 static __inline
int pthread_mutex_init(pthread_mutex_t
*mutex
, void *unused
)
45 *mutex
= CreateMutex(NULL
, FALSE
, NULL
);
46 return *mutex
== NULL
? -1 : 0;
49 static __inline
int pthread_mutex_destroy(pthread_mutex_t
*mutex
)
51 return CloseHandle(*mutex
) == 0 ? -1 : 0;
54 static __inline
int pthread_mutex_unlock(pthread_mutex_t
*mutex
)
56 return ReleaseMutex(*mutex
) == 0 ? -1 : 0;
59 static __inline
int pthread_mutex_lock(pthread_mutex_t
*mutex
)
61 return WaitForSingleObject(*mutex
, INFINITE
) == WAIT_OBJECT_0
? 0 : -1;
64 int ventoy_path_case(char *path
, int slash
);
67 int ventoy_get_sys_file_line(char *buffer
, int buflen
, const char *fmt
, ...);
70 #define UINT16 uint16_t
71 #define UINT32 uint32_t
72 #define UINT64 uint64_t
74 static inline int ventoy_path_case(char *path
, int slash
)
86 typedef struct SYSINFO
94 char cur_capacity
[64];
96 char cur_ventoy_ver
[64];
104 extern SYSINFO g_sysinfo
;
109 #define TMAGIC "ustar"
112 #define AREGTYPE '\0'
122 typedef struct tag_tar_head
146 #define VENTOY_UP_ALIGN(N, align) (((N) + ((align) - 1)) / (align) * (align))
147 #define VENTOY_FILE_MAX 2048
150 #if defined(_MSC_VER) || defined(WIN32)
151 #define million_sleep(a) Sleep(a)
153 #define million_sleep(a) usleep((a) * 1000)
157 typedef struct ventoy_file
167 int ventoy_is_file_exist(const char *fmt
, ...);
168 int ventoy_is_directory_exist(const char *fmt
, ...);
169 void ventoy_gen_preudo_uuid(void *uuid
);
170 uint64_t ventoy_get_human_readable_gb(uint64_t SizeBytes
);
171 void ventoy_md5(const void *data
, uint32_t len
, uint8_t *md5
);
172 int ventoy_is_disk_mounted(const char *devpath
);
173 int unxz(unsigned char *in
, int in_size
,
174 int (*fill
)(void *dest
, unsigned int size
),
175 int (*flush
)(void *src
, unsigned int size
),
176 unsigned char *out
, int *in_used
,
177 void (*error
)(char *x
));
178 int ventoy_read_file_to_buf(const char *FileName
, int ExtLen
, void **Bufer
, int *BufLen
);
179 int ventoy_write_buf_to_file(const char *FileName
, void *Bufer
, int BufLen
);
180 const char * ventoy_get_os_language(void);
181 int ventoy_get_file_size(const char *file
);
182 int ventoy_www_init(void);
183 void ventoy_www_exit(void);
184 int ventoy_decompress_tar(char *tarbuf
, int buflen
, int *tarsize
);
185 ventoy_file
* ventoy_tar_find_file(const char *path
);
186 void ventoy_get_json_path(char *path
, char *backup
);
187 int ventoy_copy_file(const char *a
, const char *b
);
189 typedef int (*ventoy_http_writeback_pf
)(void);
191 int ventoy_start_writeback_thread(ventoy_http_writeback_pf callback
);
192 void ventoy_stop_writeback_thread(void);
193 void ventoy_set_writeback_event(void);
196 extern unsigned char *g_unxz_buffer
;
197 extern int g_unxz_len
;
198 void unxz_error(char *x
);
199 int unxz_flush(void *src
, unsigned int size
);
201 #endif /* __VENTOY_UTIL_H__ */