]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - FUSEISO/vtoy_fuse_iso.c
1.0.07 release
[Ventoy.git] / FUSEISO / vtoy_fuse_iso.c
1 /******************************************************************************
2 * vtoy_fuse_iso.c
3 *
4 * Copyright (c) 2020, longpanda <admin@ventoy.net>
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 #define FUSE_USE_VERSION 26
22
23 #include <fuse.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <fcntl.h>
30
31 typedef unsigned int uint32_t;
32
33 typedef struct dmtable_entry
34 {
35 uint32_t isoSector;
36 uint32_t sectorNum;
37 unsigned long long diskSector;
38 }dmtable_entry;
39
40 #define MAX_ENTRY_NUM (1024 * 1024 / sizeof(dmtable_entry))
41
42 static int verbose = 0;
43 #define debug(fmt, ...) if(verbose) printf(fmt, ##__VA_ARGS__)
44
45 static int g_disk_fd = -1;
46 static uint64_t g_iso_file_size;
47 static char g_mnt_point[512];
48 static char g_iso_file_name[512];
49 static dmtable_entry *g_disk_entry_list = NULL;
50 static int g_disk_entry_num = 0;
51
52 static int ventoy_iso_getattr(const char *path, struct stat *statinfo)
53 {
54 int ret = -ENOENT;
55
56 if (path && statinfo)
57 {
58 memset(statinfo, 0, sizeof(struct stat));
59
60 if (path[0] == '/' && path[1] == 0)
61 {
62 statinfo->st_mode = S_IFDIR | 0755;
63 statinfo->st_nlink = 2;
64 ret = 0;
65 }
66 else if (strcmp(path, g_iso_file_name) == 0)
67 {
68 statinfo->st_mode = S_IFREG | 0444;
69 statinfo->st_nlink = 1;
70 statinfo->st_size = g_iso_file_size;
71 ret = 0;
72 }
73 }
74
75 return ret;
76 }
77
78 static int ventoy_iso_readdir
79 (
80 const char *path,
81 void *buf,
82 fuse_fill_dir_t filler,
83 off_t offset,
84 struct fuse_file_info *file
85 )
86 {
87 (void)offset;
88 (void)file;
89
90 if (path[0] != '/' || path[1] != 0)
91 {
92 return -ENOENT;
93 }
94
95 filler(buf, ".", NULL, 0);
96 filler(buf, "..", NULL, 0);
97 filler(buf, g_iso_file_name + 1, NULL, 0);
98
99 return 0;
100 }
101
102 static int ventoy_iso_open(const char *path, struct fuse_file_info *file)
103 {
104 if (strcmp(path, g_iso_file_name) != 0)
105 {
106 return -ENOENT;
107 }
108
109 if ((file->flags & 3) != O_RDONLY)
110 {
111 return -EACCES;
112 }
113
114 return 0;
115 }
116
117 static int ventoy_read_iso_sector(uint32_t sector, uint32_t num, void *buf)
118 {
119 uint32_t i = 0;
120 uint32_t leftSec = 0;
121 uint32_t readSec = 0;
122 dmtable_entry *entry = NULL;
123
124 for (i = 0; i < g_disk_entry_num && num > 0; i++)
125 {
126 entry = g_disk_entry_list + i;
127
128 if (sector >= entry->isoSector && sector < entry->isoSector + entry->sectorNum)
129 {
130 lseek(g_disk_fd, (entry->diskSector + (sector - entry->isoSector)) * 512, SEEK_SET);
131
132 leftSec = entry->sectorNum - (sector - entry->isoSector);
133 readSec = (leftSec > num) ? num : leftSec;
134
135 read(g_disk_fd, buf, readSec * 512);
136
137 sector += readSec;
138 num -= readSec;
139 }
140 }
141
142 return 0;
143 }
144
145 static int ventoy_iso_read
146 (
147 const char *path, char *buf,
148 size_t size, off_t offset,
149 struct fuse_file_info *file
150 )
151 {
152 uint32_t mod = 0;
153 uint32_t align = 0;
154 uint32_t sector = 0;
155 uint32_t number = 0;
156 size_t leftsize = 0;
157 char secbuf[512];
158
159 (void)file;
160
161 if(strcmp(path, g_iso_file_name) != 0)
162 {
163 return -ENOENT;
164 }
165
166 if (offset >= g_iso_file_size)
167 {
168 return 0;
169 }
170
171 if (offset + size > g_iso_file_size)
172 {
173 size = g_iso_file_size - offset;
174 }
175
176 leftsize = size;
177 sector = offset / 512;
178
179 mod = offset % 512;
180 if (mod > 0)
181 {
182 align = 512 - mod;
183 ventoy_read_iso_sector(sector, 1, secbuf);
184
185 if (leftsize > align)
186 {
187 memcpy(buf, secbuf + mod, align);
188 buf += align;
189 offset += align;
190 sector++;
191 leftsize -= align;
192 }
193 else
194 {
195 memcpy(buf, secbuf + mod, leftsize);
196 return size;
197 }
198 }
199
200 number = leftsize / 512;
201 ventoy_read_iso_sector(sector, number, buf);
202 buf += number * 512;
203
204 mod = leftsize % 512;
205 if (mod > 0)
206 {
207 ventoy_read_iso_sector(sector + number, 1, secbuf);
208 memcpy(buf, secbuf, mod);
209 }
210
211 return size;
212 }
213
214 static struct fuse_operations ventoy_op =
215 {
216 .getattr = ventoy_iso_getattr,
217 .readdir = ventoy_iso_readdir,
218 .open = ventoy_iso_open,
219 .read = ventoy_iso_read,
220 };
221
222 static int ventoy_parse_dmtable(const char *filename)
223 {
224 FILE *fp = NULL;
225 char diskname[128] = {0};
226 char line[256] = {0};
227 dmtable_entry *entry= g_disk_entry_list;
228
229 fp = fopen(filename, "r");
230 if (NULL == fp)
231 {
232 printf("Failed to open file %s\n", filename);
233 return 1;
234 }
235
236 /* read untill the last line */
237 while (fgets(line, sizeof(line), fp) && g_disk_entry_num < MAX_ENTRY_NUM)
238 {
239 sscanf(line, "%u %u linear %s %llu",
240 &entry->isoSector, &entry->sectorNum,
241 diskname, &entry->diskSector);
242
243 g_iso_file_size += (uint64_t)entry->sectorNum * 512ULL;
244 g_disk_entry_num++;
245 entry++;
246 }
247 fclose(fp);
248
249 if (g_disk_entry_num >= MAX_ENTRY_NUM)
250 {
251 fprintf(stderr, "ISO file has too many fragments ( more than %u )\n", MAX_ENTRY_NUM);
252 return 1;
253 }
254
255 debug("iso file size: %llu disk name %s\n", g_iso_file_size, diskname);
256
257 g_disk_fd = open(diskname, O_RDONLY);
258 if (g_disk_fd < 0)
259 {
260 debug("Failed to open %s\n", diskname);
261 return 1;
262 }
263
264 return 0;
265 }
266
267 int main(int argc, char **argv)
268 {
269 int rc;
270 int ch;
271 char filename[512] = {0};
272
273 /* Avoid to be killed by systemd */
274 if (access("/etc/initrd-release", F_OK) >= 0)
275 {
276 argv[0][0] = '@';
277 }
278
279 g_iso_file_name[0] = '/';
280
281 while ((ch = getopt(argc, argv, "f:s:m:v::t::")) != -1)
282 {
283 if (ch == 'f')
284 {
285 strncpy(filename, optarg, sizeof(filename) - 1);
286 }
287 else if (ch == 'm')
288 {
289 strncpy(g_mnt_point, optarg, sizeof(g_mnt_point) - 1);
290 }
291 else if (ch == 's')
292 {
293 strncpy(g_iso_file_name + 1, optarg, sizeof(g_iso_file_name) - 2);
294 }
295 else if (ch == 'v')
296 {
297 verbose = 1;
298 }
299 else if (ch == 't') // for test
300 {
301 return 0;
302 }
303 }
304
305 if (filename[0] == 0)
306 {
307 fprintf(stderr, "Must input dmsetup table file with -f\n");
308 return 1;
309 }
310
311 if (g_mnt_point[0] == 0)
312 {
313 fprintf(stderr, "Must input mount point with -m\n");
314 return 1;
315 }
316
317 if (g_iso_file_name[1] == 0)
318 {
319 strncpy(g_iso_file_name + 1, "ventoy.iso", sizeof(g_iso_file_name) - 2);
320 }
321
322 debug("ventoy fuse iso: %s %s %s\n", filename, g_iso_file_name, g_mnt_point);
323
324 g_disk_entry_list = malloc(MAX_ENTRY_NUM * sizeof(dmtable_entry));
325 if (NULL == g_disk_entry_list)
326 {
327 return 1;
328 }
329
330 rc = ventoy_parse_dmtable(filename);
331 if (rc)
332 {
333 free(g_disk_entry_list);
334 return rc;
335 }
336
337 argv[1] = g_mnt_point;
338 argv[2] = NULL;
339 rc = fuse_main(2, argv, &ventoy_op, NULL);
340
341 close(g_disk_fd);
342
343 free(g_disk_entry_list);
344 return rc;
345 }
346