]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - Plugson/src/Core/ventoy_util.c
1 /******************************************************************************
2 * ventoy_util.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/>.
22 #include <ventoy_define.h>
23 #include <ventoy_util.h>
26 static int g_tar_filenum
= 0;
27 static char *g_tar_buffer
= NULL
;
28 static ventoy_file
*g_tar_filelist
= NULL
;
32 unsigned char *g_unxz_buffer
= NULL
;
35 void unxz_error(char *x
)
40 int unxz_flush(void *src
, unsigned int size
)
42 memcpy(g_unxz_buffer
+ g_unxz_len
, src
, size
);
43 g_unxz_len
+= (int)size
;
49 uint64_t ventoy_get_human_readable_gb(uint64_t SizeBytes
)
54 double GB
= SizeBytes
* 1.0 / 1000 / 1000 / 1000;
56 if ((SizeBytes
% SIZE_1GB
) == 0)
58 return (uint64_t)(SizeBytes
/ SIZE_1GB
);
61 for (i
= 0; i
< 12; i
++)
65 Delta
= (Pow2
- GB
) / Pow2
;
69 Delta
= (GB
- Pow2
) / Pow2
;
83 int ventoy_read_file_to_buf(const char *FileName
, int ExtLen
, void **Bufer
, int *BufLen
)
89 #if defined(_MSC_VER) || defined(WIN32)
90 fopen_s(&fp
, FileName
, "rb");
92 fp
= fopen(FileName
, "rb");
96 vlog("Failed to open file %s", FileName
);
100 fseek(fp
, 0, SEEK_END
);
101 FileSize
= (int)ftell(fp
);
103 Data
= malloc(FileSize
+ ExtLen
);
110 fseek(fp
, 0, SEEK_SET
);
111 fread(Data
, 1, FileSize
, fp
);
121 ventoy_file
* ventoy_tar_find_file(const char *path
)
125 ventoy_file
*node
= g_tar_filelist
;
127 len
= (int)strlen(path
);
129 for (i
= 0; i
< g_tar_filenum
; i
++, node
++)
131 if (node
->pathlen
== len
&& memcmp(node
->path
, path
, len
) == 0)
136 if (node
->pathlen
> len
)
146 int ventoy_decompress_tar(char *tarbuf
, int buflen
, int *tarsize
)
151 unsigned char *buffer
= NULL
;
152 char tarxz
[MAX_PATH
];
154 #if defined(_MSC_VER) || defined(WIN32)
155 scnprintf(tarxz
, sizeof(tarxz
), "%s\\ventoy\\%s", g_ventoy_dir
, PLUGSON_TXZ
);
157 scnprintf(tarxz
, sizeof(tarxz
), "%s/tool/%s", g_ventoy_dir
, PLUGSON_TXZ
);
160 if (ventoy_read_file_to_buf(tarxz
, 0, (void **)&buffer
, &BufLen
))
162 vlog("Failed to read file <%s>\n", tarxz
);
166 g_unxz_buffer
= (unsigned char *)tarbuf
;
169 unxz(buffer
, BufLen
, NULL
, unxz_flush
, NULL
, &inused
, unxz_error
);
170 vlog("xzlen:%u rawdata size:%d\n", BufLen
, g_unxz_len
);
172 if (inused
!= BufLen
)
174 vlog("Failed to unxz data %d %d\n", inused
, BufLen
);
179 *tarsize
= g_unxz_len
;
188 int ventoy_www_init(void)
195 ventoy_file
*node
= NULL
;
196 ventoy_file
*node2
= NULL
;
197 VENTOY_TAR_HEAD
*pHead
= NULL
;
202 g_tar_filelist
= malloc(VENTOY_FILE_MAX
* sizeof(ventoy_file
));
203 g_tar_buffer
= malloc(TAR_BUF_MAX
);
207 if ((!g_tar_filelist
) || (!g_tar_buffer
))
212 if (ventoy_decompress_tar(g_tar_buffer
, TAR_BUF_MAX
, &tarsize
))
217 pHead
= (VENTOY_TAR_HEAD
*)g_tar_buffer
;
218 node
= g_tar_filelist
;
220 while (g_tar_filenum
< VENTOY_FILE_MAX
&& size
< tarsize
&& memcmp(pHead
->magic
, TMAGIC
, 5) == 0)
222 if (pHead
->typeflag
== REGTYPE
)
224 node
->size
= (int)strtol(pHead
->size
, NULL
, 8);
225 node
->pathlen
= (int)scnprintf(node
->path
, MAX_PATH
, "%s", pHead
->name
);
226 node
->addr
= pHead
+ 1;
228 if (node
->pathlen
== 13 && strcmp(pHead
->name
, "www/buildtime") == 0)
230 scnprintf(g_sysinfo
.buildtime
, sizeof(g_sysinfo
.buildtime
), "%s", (char *)node
->addr
);
231 vlog("Plugson buildtime %s\n", g_sysinfo
.buildtime
);
234 offset
= 512 + VENTOY_UP_ALIGN(node
->size
, 512);
244 pHead
= (VENTOY_TAR_HEAD
*)((char *)pHead
+ offset
);
250 for (i
= 0; i
< g_tar_filenum
; i
++)
251 for (j
= i
+ 1; j
< g_tar_filenum
; j
++)
253 node
= g_tar_filelist
+ i
;
254 node2
= g_tar_filelist
+ j
;
256 if (node
->pathlen
> node2
->pathlen
)
258 memcpy(&tmpnode
, node
, sizeof(ventoy_file
));
259 memcpy(node
, node2
, sizeof(ventoy_file
));
260 memcpy(node2
, &tmpnode
, sizeof(ventoy_file
));
264 vlog("Total extract %d files from tar file.\n", g_tar_filenum
);
269 void ventoy_www_exit(void)
271 check_free(g_tar_filelist
);
272 check_free(g_tar_buffer
);
273 g_tar_filelist
= NULL
;
279 void ventoy_get_json_path(char *path
, char *backup
)
281 #if defined(_MSC_VER) || defined(WIN32)
282 scnprintf(path
, 64, "%C:\\ventoy\\ventoy.json", g_cur_dir
[0]);
285 scnprintf(backup
, 64, "%C:\\ventoy\\ventoy_backup.json", g_cur_dir
[0]);
288 scnprintf(path
, 64, "%s/ventoy/ventoy.json", g_cur_dir
);
291 scnprintf(backup
, 64, "%s/ventoy/ventoy_backup.json", g_cur_dir
);