udf.c revision 1.31 1 1.31 tsutsui /* $NetBSD: udf.c,v 1.31 2023/12/28 12:13:55 tsutsui Exp $ */
2 1.1 reinoud
3 1.1 reinoud /*
4 1.23 reinoud * Copyright (c) 2006, 2008, 2013, 2021, 2022 Reinoud Zandijk
5 1.1 reinoud * All rights reserved.
6 1.25 riastrad *
7 1.1 reinoud * Redistribution and use in source and binary forms, with or without
8 1.1 reinoud * modification, are permitted provided that the following conditions
9 1.1 reinoud * are met:
10 1.1 reinoud * 1. Redistributions of source code must retain the above copyright
11 1.1 reinoud * notice, this list of conditions and the following disclaimer.
12 1.1 reinoud * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 reinoud * notice, this list of conditions and the following disclaimer in the
14 1.1 reinoud * documentation and/or other materials provided with the distribution.
15 1.25 riastrad *
16 1.1 reinoud * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 reinoud * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 reinoud * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 reinoud * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 reinoud * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 reinoud * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 reinoud * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 reinoud * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 reinoud * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 reinoud * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.25 riastrad *
27 1.1 reinoud */
28 1.3 joerg #if HAVE_NBTOOL_CONFIG_H
29 1.3 joerg #include "nbtool_config.h"
30 1.3 joerg #endif
31 1.1 reinoud
32 1.1 reinoud #include <sys/cdefs.h>
33 1.31 tsutsui __RCSID("$NetBSD: udf.c,v 1.31 2023/12/28 12:13:55 tsutsui Exp $");
34 1.1 reinoud
35 1.1 reinoud #include <stdio.h>
36 1.1 reinoud #include <stdlib.h>
37 1.1 reinoud #include <string.h>
38 1.1 reinoud #include <errno.h>
39 1.1 reinoud #include <time.h>
40 1.1 reinoud #include <assert.h>
41 1.1 reinoud #include <err.h>
42 1.1 reinoud #include <unistd.h>
43 1.1 reinoud #include <fcntl.h>
44 1.23 reinoud #include <math.h>
45 1.1 reinoud #include <sys/types.h>
46 1.1 reinoud #include <sys/param.h>
47 1.4 reinoud #include <sys/stat.h>
48 1.4 reinoud #include <util.h>
49 1.4 reinoud
50 1.3 joerg #if !HAVE_NBTOOL_CONFIG_H
51 1.4 reinoud #define _EXPOSE_MMC
52 1.1 reinoud #include <sys/cdio.h>
53 1.4 reinoud #else
54 1.4 reinoud #include "udf/cdio_mmc_structs.h"
55 1.3 joerg #endif
56 1.1 reinoud
57 1.13 jmcneill #if !HAVE_NBTOOL_CONFIG_H
58 1.13 jmcneill #define HAVE_STRUCT_TM_TM_GMTOFF
59 1.13 jmcneill #endif
60 1.13 jmcneill
61 1.1 reinoud #include "makefs.h"
62 1.23 reinoud #include "udf_core.h"
63 1.1 reinoud #include "newfs_udf.h"
64 1.1 reinoud
65 1.23 reinoud /* identification */
66 1.23 reinoud #define IMPL_NAME "*NetBSD makefs 10.0"
67 1.23 reinoud #define APP_VERSION_MAIN 0
68 1.23 reinoud #define APP_VERSION_SUB 5
69 1.1 reinoud
70 1.1 reinoud /*
71 1.1 reinoud * Note: due to the setup of the newfs code, the current state of the program
72 1.22 andvar * and its options are held in a few global variables. The FS specific parts
73 1.23 reinoud * are in global `context' and 'layout' structures.
74 1.1 reinoud */
75 1.1 reinoud
76 1.1 reinoud /* global variables describing disc and format requests */
77 1.1 reinoud int req_enable, req_disable;
78 1.1 reinoud
79 1.1 reinoud
80 1.1 reinoud /* --------------------------------------------------------------------- */
81 1.1 reinoud
82 1.1 reinoud static int
83 1.23 reinoud udf_readonly_format(void)
84 1.1 reinoud {
85 1.23 reinoud /*
86 1.23 reinoud * we choose the emulated profile to determine this since the media
87 1.23 reinoud * might be different from the format we create. Say creating a CDROM
88 1.23 reinoud * on a CD-R media.
89 1.23 reinoud */
90 1.23 reinoud switch (emul_mmc_profile) {
91 1.1 reinoud case 0x00: /* unknown, treat as CDROM */
92 1.1 reinoud case 0x08: /* CDROM */
93 1.7 reinoud case 0x10: /* DVDROM */
94 1.7 reinoud case 0x40: /* BDROM */
95 1.23 reinoud return true;
96 1.1 reinoud }
97 1.23 reinoud return false;
98 1.1 reinoud }
99 1.1 reinoud
100 1.1 reinoud
101 1.1 reinoud #define OPT_STR(letter, name, desc) \
102 1.1 reinoud { letter, name, NULL, OPT_STRBUF, 0, 0, desc }
103 1.1 reinoud
104 1.1 reinoud #define OPT_NUM(letter, name, field, min, max, desc) \
105 1.9 reinoud { letter, name, &context.field, \
106 1.9 reinoud sizeof(context.field) == 8 ? OPT_INT64 : \
107 1.9 reinoud (sizeof(context.field) == 4 ? OPT_INT32 : \
108 1.9 reinoud (sizeof(context.field) == 2 ? OPT_INT16 : OPT_INT8)), \
109 1.1 reinoud min, max, desc }
110 1.1 reinoud
111 1.1 reinoud #define OPT_BOOL(letter, name, field, desc) \
112 1.1 reinoud OPT_NUM(letter, name, field, 0, 1, desc)
113 1.1 reinoud
114 1.1 reinoud void
115 1.1 reinoud udf_prep_opts(fsinfo_t *fsopts)
116 1.1 reinoud {
117 1.1 reinoud const option_t udf_options[] = {
118 1.8 reinoud OPT_STR('T', "disctype", "disc type (cdrom,dvdrom,bdrom,"
119 1.8 reinoud "dvdram,bdre,disk,cdr,dvdr,bdr,cdrw,dvdrw)"),
120 1.8 reinoud OPT_STR('L', "loglabel", "\"logical volume name\""),
121 1.9 reinoud OPT_STR('P', "discid", "\"[volset name ':']"
122 1.9 reinoud "physical volume name\""),
123 1.9 reinoud OPT_NUM('t', "tz", gmtoff, -24, 24, "timezone"),
124 1.9 reinoud OPT_STR('v', "minver", "minimum UDF version in either "
125 1.9 reinoud "``0x201'' or ``2.01'' format"),
126 1.9 reinoud OPT_STR('V', "maxver", "maximum UDF version in either "
127 1.9 reinoud "``0x201'' or ``2.01'' format"),
128 1.23 reinoud OPT_NUM('p', "metaperc", meta_perc, 1, 99,
129 1.23 reinoud "minimum free metadata percentage"),
130 1.23 reinoud OPT_BOOL('c', "checksurface", check_surface,
131 1.23 reinoud "perform crude surface check on rewritable media"),
132 1.23 reinoud OPT_BOOL('F', "forceformat", create_new_session,
133 1.24 wiz "force file system construction on non-empty recordable media"),
134 1.1 reinoud { .name = NULL }
135 1.1 reinoud };
136 1.1 reinoud
137 1.1 reinoud /* initialise */
138 1.23 reinoud req_enable = req_disable = 0;
139 1.1 reinoud fsopts->sectorsize = 512; /* minimum allowed sector size */
140 1.1 reinoud
141 1.1 reinoud srandom((unsigned long) time(NULL));
142 1.1 reinoud
143 1.1 reinoud udf_init_create_context();
144 1.23 reinoud context.app_name = "*NetBSD UDF";
145 1.1 reinoud context.app_version_main = APP_VERSION_MAIN;
146 1.1 reinoud context.app_version_sub = APP_VERSION_SUB;
147 1.10 reinoud context.impl_name = IMPL_NAME;
148 1.1 reinoud
149 1.1 reinoud /* minimum and maximum UDF versions we advise */
150 1.1 reinoud context.min_udf = 0x102;
151 1.23 reinoud context.max_udf = 0x250; /* 0x260 is not ready */
152 1.23 reinoud
153 1.23 reinoud /* defaults for disc/files */
154 1.23 reinoud emul_mmc_profile = -1; /* invalid->no emulation */
155 1.23 reinoud emul_packetsize = 1; /* reasonable default */
156 1.23 reinoud emul_sectorsize = 512; /* minimum allowed sector size */
157 1.23 reinoud emul_size = 0; /* empty */
158 1.1 reinoud
159 1.1 reinoud /* use user's time zone as default */
160 1.13 jmcneill #ifdef HAVE_STRUCT_TM_TM_GMTOFF
161 1.18 christos if (!stampst.st_ino) {
162 1.18 christos struct tm tm;
163 1.18 christos time_t now;
164 1.18 christos (void)time(&now);
165 1.18 christos (void)localtime_r(&now, &tm);
166 1.18 christos context.gmtoff = tm.tm_gmtoff;
167 1.18 christos } else
168 1.13 jmcneill #endif
169 1.18 christos context.gmtoff = 0;
170 1.1 reinoud
171 1.1 reinoud /* return info */
172 1.1 reinoud fsopts->fs_specific = NULL;
173 1.1 reinoud fsopts->fs_options = copy_opts(udf_options);
174 1.1 reinoud }
175 1.1 reinoud
176 1.1 reinoud
177 1.1 reinoud void
178 1.1 reinoud udf_cleanup_opts(fsinfo_t *fsopts)
179 1.1 reinoud {
180 1.1 reinoud free(fsopts->fs_options);
181 1.1 reinoud }
182 1.1 reinoud
183 1.1 reinoud
184 1.9 reinoud /* ----- included from newfs_udf.c ------ */
185 1.9 reinoud
186 1.7 reinoud #define CDRSIZE ((uint64_t) 700*1024*1024) /* small approx */
187 1.7 reinoud #define CDRWSIZE ((uint64_t) 576*1024*1024) /* small approx */
188 1.7 reinoud #define DVDRSIZE ((uint64_t) 4488*1024*1024) /* small approx */
189 1.7 reinoud #define DVDRAMSIZE ((uint64_t) 4330*1024*1024) /* small approx with spare */
190 1.7 reinoud #define DVDRWSIZE ((uint64_t) 4482*1024*1024) /* small approx */
191 1.7 reinoud #define BDRSIZE ((uint64_t) 23866*1024*1024) /* small approx */
192 1.7 reinoud #define BDRESIZE ((uint64_t) 23098*1024*1024) /* small approx */
193 1.1 reinoud int
194 1.1 reinoud udf_parse_opts(const char *option, fsinfo_t *fsopts)
195 1.1 reinoud {
196 1.1 reinoud option_t *udf_options = fsopts->fs_options;
197 1.23 reinoud uint64_t stdsize, maxsize;
198 1.7 reinoud uint32_t set_sectorsize;
199 1.8 reinoud char buffer[1024], *buf, *colon;
200 1.1 reinoud int i;
201 1.1 reinoud
202 1.1 reinoud assert(option != NULL);
203 1.1 reinoud
204 1.1 reinoud if (debug & DEBUG_FS_PARSE_OPTS)
205 1.1 reinoud printf("udf_parse_opts: got `%s'\n", option);
206 1.1 reinoud
207 1.8 reinoud i = set_option(udf_options, option, buffer, sizeof(buffer));
208 1.1 reinoud if (i == -1)
209 1.1 reinoud return 0;
210 1.1 reinoud
211 1.1 reinoud if (udf_options[i].name == NULL)
212 1.1 reinoud abort();
213 1.1 reinoud
214 1.7 reinoud set_sectorsize = 0;
215 1.7 reinoud stdsize = 0;
216 1.23 reinoud maxsize = 0;
217 1.7 reinoud
218 1.8 reinoud buf = buffer;
219 1.1 reinoud switch (udf_options[i].letter) {
220 1.1 reinoud case 'T':
221 1.1 reinoud if (strcmp(buf, "cdrom") == 0) {
222 1.23 reinoud emul_mmc_profile = 0x00;
223 1.23 reinoud maxsize = CDRSIZE;
224 1.1 reinoud } else if (strcmp(buf, "dvdrom") == 0) {
225 1.23 reinoud emul_mmc_profile = 0x10;
226 1.23 reinoud maxsize = DVDRSIZE;
227 1.7 reinoud } else if (strcmp(buf, "bdrom") == 0) {
228 1.23 reinoud emul_mmc_profile = 0x40;
229 1.23 reinoud maxsize = BDRSIZE;
230 1.1 reinoud } else if (strcmp(buf, "dvdram") == 0) {
231 1.23 reinoud emul_mmc_profile = 0x12;
232 1.7 reinoud stdsize = DVDRAMSIZE;
233 1.1 reinoud } else if (strcmp(buf, "bdre") == 0) {
234 1.23 reinoud emul_mmc_profile = 0x43;
235 1.7 reinoud stdsize = BDRESIZE;
236 1.1 reinoud } else if (strcmp(buf, "disk") == 0) {
237 1.23 reinoud emul_mmc_profile = 0x01;
238 1.1 reinoud } else if (strcmp(buf, "cdr") == 0) {
239 1.23 reinoud emul_mmc_profile = 0x09;
240 1.7 reinoud stdsize = CDRSIZE;
241 1.1 reinoud } else if (strcmp(buf, "dvdr") == 0) {
242 1.23 reinoud emul_mmc_profile = 0x1b;
243 1.7 reinoud stdsize = DVDRSIZE;
244 1.7 reinoud } else if (strcmp(buf, "bdr") == 0) {
245 1.23 reinoud emul_mmc_profile = 0x41;
246 1.7 reinoud stdsize = BDRSIZE;
247 1.1 reinoud } else if (strcmp(buf, "cdrw") == 0) {
248 1.23 reinoud emul_mmc_profile = 0x0a;
249 1.7 reinoud stdsize = CDRWSIZE;
250 1.1 reinoud } else if (strcmp(buf, "dvdrw") == 0) {
251 1.23 reinoud emul_mmc_profile = 0x1a;
252 1.7 reinoud stdsize = DVDRWSIZE;
253 1.1 reinoud } else {
254 1.31 tsutsui errx(EXIT_FAILURE,
255 1.31 tsutsui "unknown or unimplemented disc format");
256 1.1 reinoud }
257 1.23 reinoud if (emul_mmc_profile != 0x01)
258 1.7 reinoud set_sectorsize = 2048;
259 1.8 reinoud break;
260 1.8 reinoud case 'L':
261 1.8 reinoud if (context.logvol_name) free(context.logvol_name);
262 1.8 reinoud context.logvol_name = strdup(buf);
263 1.8 reinoud break;
264 1.8 reinoud case 'P':
265 1.8 reinoud if ((colon = strstr(buf, ":"))) {
266 1.8 reinoud if (context.volset_name)
267 1.8 reinoud free(context.volset_name);
268 1.8 reinoud *colon = 0;
269 1.8 reinoud context.volset_name = strdup(buf);
270 1.8 reinoud buf = colon+1;
271 1.8 reinoud }
272 1.8 reinoud if (context.primary_name)
273 1.8 reinoud free(context.primary_name);
274 1.23 reinoud if ((strstr(buf, ":")))
275 1.31 tsutsui errx(EXIT_FAILURE,
276 1.31 tsutsui "primary name can't have ':' in its name");
277 1.8 reinoud context.primary_name = strdup(buf);
278 1.8 reinoud break;
279 1.9 reinoud case 'v':
280 1.9 reinoud context.min_udf = a_udf_version(buf, "min_udf");
281 1.23 reinoud if (context.min_udf > 0x250)
282 1.31 tsutsui errx(EXIT_FAILURE,
283 1.31 tsutsui "maximum supported version is UDF 2.50");
284 1.9 reinoud if (context.min_udf > context.max_udf)
285 1.9 reinoud context.max_udf = context.min_udf;
286 1.9 reinoud break;
287 1.23 reinoud case 'V':
288 1.23 reinoud context.max_udf = a_udf_version(buf, "min_udf");
289 1.23 reinoud if (context.max_udf > 0x250)
290 1.31 tsutsui errx(EXIT_FAILURE,
291 1.31 tsutsui "maximum supported version is UDF 2.50");
292 1.23 reinoud if (context.min_udf > context.max_udf)
293 1.23 reinoud context.min_udf = context.max_udf;
294 1.23 reinoud break;
295 1.1 reinoud }
296 1.7 reinoud if (set_sectorsize)
297 1.7 reinoud fsopts->sectorsize = set_sectorsize;
298 1.23 reinoud if (stdsize) {
299 1.23 reinoud if (fsopts->maxsize > 0)
300 1.23 reinoud stdsize = MIN(stdsize, (uint64_t) fsopts->maxsize);
301 1.23 reinoud if (fsopts->minsize > 0)
302 1.23 reinoud stdsize = MAX(stdsize, (uint64_t) fsopts->minsize);
303 1.23 reinoud fsopts->size = fsopts->minsize = fsopts->maxsize = stdsize;
304 1.23 reinoud }
305 1.23 reinoud if (maxsize) {
306 1.23 reinoud if (fsopts->maxsize > 0)
307 1.23 reinoud maxsize = MIN(maxsize, (uint64_t) fsopts->maxsize);
308 1.23 reinoud if (fsopts->minsize > 0)
309 1.23 reinoud maxsize = MAX(maxsize, (uint64_t) fsopts->minsize);
310 1.23 reinoud fsopts->maxsize = maxsize;
311 1.23 reinoud }
312 1.1 reinoud return 1;
313 1.1 reinoud }
314 1.1 reinoud
315 1.23 reinoud /* -
316 1.23 reinoud * -------------------------------------------------------------------- */
317 1.1 reinoud
318 1.1 reinoud struct udf_stats {
319 1.1 reinoud uint32_t nfiles;
320 1.1 reinoud uint32_t ndirs;
321 1.1 reinoud uint32_t ndescr;
322 1.1 reinoud uint32_t nmetadatablocks;
323 1.1 reinoud uint32_t ndatablocks;
324 1.1 reinoud };
325 1.1 reinoud
326 1.1 reinoud
327 1.1 reinoud /* node reference administration */
328 1.1 reinoud static void
329 1.1 reinoud udf_inc_link(union dscrptr *dscr)
330 1.1 reinoud {
331 1.1 reinoud struct file_entry *fe;
332 1.1 reinoud struct extfile_entry *efe;
333 1.1 reinoud
334 1.1 reinoud if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
335 1.1 reinoud fe = &dscr->fe;
336 1.1 reinoud fe->link_cnt = udf_rw16(udf_rw16(fe->link_cnt) + 1);
337 1.1 reinoud } else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
338 1.1 reinoud efe = &dscr->efe;
339 1.1 reinoud efe->link_cnt = udf_rw16(udf_rw16(efe->link_cnt) + 1);
340 1.1 reinoud } else {
341 1.31 tsutsui errx(EXIT_FAILURE, "bad tag passed to udf_inc_link");
342 1.1 reinoud }
343 1.1 reinoud }
344 1.1 reinoud
345 1.1 reinoud
346 1.1 reinoud static void
347 1.1 reinoud udf_set_link_cnt(union dscrptr *dscr, int num)
348 1.1 reinoud {
349 1.1 reinoud struct file_entry *fe;
350 1.1 reinoud struct extfile_entry *efe;
351 1.1 reinoud
352 1.1 reinoud if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
353 1.1 reinoud fe = &dscr->fe;
354 1.1 reinoud fe->link_cnt = udf_rw16(num);
355 1.1 reinoud } else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
356 1.1 reinoud efe = &dscr->efe;
357 1.1 reinoud efe->link_cnt = udf_rw16(num);
358 1.1 reinoud } else {
359 1.31 tsutsui errx(EXIT_FAILURE, "bad tag passed to udf_set_link_cnt");
360 1.1 reinoud }
361 1.1 reinoud }
362 1.1 reinoud
363 1.1 reinoud
364 1.1 reinoud static uint32_t
365 1.1 reinoud udf_datablocks(off_t sz)
366 1.1 reinoud {
367 1.1 reinoud /* predictor if it can be written inside the node */
368 1.20 reinoud /* XXX the predictor assumes NO extended attributes in the node */
369 1.1 reinoud if (sz < context.sector_size - UDF_EXTFENTRY_SIZE - 16)
370 1.1 reinoud return 0;
371 1.1 reinoud
372 1.1 reinoud return UDF_ROUNDUP(sz, context.sector_size) / context.sector_size;
373 1.1 reinoud }
374 1.1 reinoud
375 1.1 reinoud
376 1.1 reinoud static void
377 1.1 reinoud udf_prepare_fids(struct long_ad *dir_icb, struct long_ad *dirdata_icb,
378 1.1 reinoud uint8_t *dirdata, uint32_t dirdata_size)
379 1.1 reinoud {
380 1.1 reinoud struct fileid_desc *fid;
381 1.1 reinoud struct long_ad *icb;
382 1.1 reinoud uint32_t fidsize, offset;
383 1.1 reinoud uint32_t location;
384 1.1 reinoud
385 1.1 reinoud if (udf_datablocks(dirdata_size) == 0) {
386 1.1 reinoud /* going internal */
387 1.1 reinoud icb = dir_icb;
388 1.1 reinoud } else {
389 1.1 reinoud /* external blocks to write to */
390 1.1 reinoud icb = dirdata_icb;
391 1.1 reinoud }
392 1.1 reinoud
393 1.1 reinoud for (offset = 0; offset < dirdata_size; offset += fidsize) {
394 1.1 reinoud /* for each FID: */
395 1.1 reinoud fid = (struct fileid_desc *) (dirdata + offset);
396 1.1 reinoud assert(udf_rw16(fid->tag.id) == TAGID_FID);
397 1.1 reinoud
398 1.1 reinoud location = udf_rw32(icb->loc.lb_num);
399 1.1 reinoud location += offset / context.sector_size;
400 1.1 reinoud
401 1.1 reinoud fid->tag.tag_loc = udf_rw32(location);
402 1.1 reinoud udf_validate_tag_and_crc_sums((union dscrptr *) fid);
403 1.1 reinoud
404 1.1 reinoud fidsize = udf_fidsize(fid);
405 1.1 reinoud }
406 1.1 reinoud }
407 1.1 reinoud
408 1.23 reinoud
409 1.1 reinoud static int
410 1.11 reinoud udf_file_inject_blob(union dscrptr *dscr, uint8_t *blob, off_t size)
411 1.1 reinoud {
412 1.1 reinoud struct icb_tag *icb;
413 1.1 reinoud struct file_entry *fe;
414 1.1 reinoud struct extfile_entry *efe;
415 1.1 reinoud uint64_t inf_len, obj_size;
416 1.1 reinoud uint32_t l_ea, l_ad;
417 1.1 reinoud uint16_t crclen;
418 1.1 reinoud uint8_t *data, *pos;
419 1.1 reinoud
420 1.1 reinoud fe = NULL;
421 1.1 reinoud efe = NULL;
422 1.1 reinoud if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
423 1.1 reinoud fe = &dscr->fe;
424 1.1 reinoud data = fe->data;
425 1.1 reinoud l_ea = udf_rw32(fe->l_ea);
426 1.1 reinoud l_ad = udf_rw32(fe->l_ad);
427 1.1 reinoud icb = &fe->icbtag;
428 1.1 reinoud inf_len = udf_rw64(fe->inf_len);
429 1.1 reinoud obj_size = 0;
430 1.1 reinoud } else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
431 1.1 reinoud efe = &dscr->efe;
432 1.1 reinoud data = efe->data;
433 1.1 reinoud l_ea = udf_rw32(efe->l_ea);
434 1.1 reinoud l_ad = udf_rw32(efe->l_ad);
435 1.1 reinoud icb = &efe->icbtag;
436 1.1 reinoud inf_len = udf_rw64(efe->inf_len);
437 1.1 reinoud obj_size = udf_rw64(efe->obj_size);
438 1.1 reinoud } else {
439 1.31 tsutsui errx(EXIT_FAILURE, "bad tag passed to udf_file_inject_blob");
440 1.1 reinoud }
441 1.1 reinoud crclen = udf_rw16(dscr->tag.desc_crc_len);
442 1.1 reinoud
443 1.23 reinoud /* check if we can go internal */
444 1.23 reinoud if ((udf_rw16(icb->flags) & UDF_ICB_TAG_FLAGS_ALLOC_MASK) !=
445 1.23 reinoud UDF_ICB_INTERN_ALLOC)
446 1.23 reinoud return 1;
447 1.23 reinoud
448 1.20 reinoud /* check if it will fit internally */
449 1.1 reinoud if (udf_datablocks(size)) {
450 1.20 reinoud /* the predictor tells it won't fit internally */
451 1.1 reinoud return 1;
452 1.1 reinoud }
453 1.1 reinoud
454 1.1 reinoud /* going internal */
455 1.6 reinoud assert((udf_rw16(icb->flags) & UDF_ICB_TAG_FLAGS_ALLOC_MASK) ==
456 1.6 reinoud UDF_ICB_INTERN_ALLOC);
457 1.23 reinoud assert(l_ad == 0);
458 1.1 reinoud
459 1.1 reinoud pos = data + l_ea + l_ad;
460 1.1 reinoud memcpy(pos, blob, size);
461 1.1 reinoud l_ad += size;
462 1.1 reinoud crclen += size;
463 1.1 reinoud
464 1.1 reinoud inf_len += size;
465 1.1 reinoud obj_size += size;
466 1.1 reinoud
467 1.1 reinoud if (fe) {
468 1.1 reinoud fe->l_ad = udf_rw32(l_ad);
469 1.1 reinoud fe->inf_len = udf_rw64(inf_len);
470 1.1 reinoud } else if (efe) {
471 1.1 reinoud efe->l_ad = udf_rw32(l_ad);
472 1.1 reinoud efe->inf_len = udf_rw64(inf_len);
473 1.1 reinoud efe->obj_size = udf_rw64(inf_len);
474 1.1 reinoud }
475 1.1 reinoud
476 1.1 reinoud /* make sure the header sums stays correct */
477 1.1 reinoud dscr->tag.desc_crc_len = udf_rw16(crclen);
478 1.1 reinoud udf_validate_tag_and_crc_sums(dscr);
479 1.1 reinoud
480 1.29 reinoud (void) obj_size;
481 1.1 reinoud return 0;
482 1.1 reinoud }
483 1.1 reinoud
484 1.1 reinoud
485 1.1 reinoud /* XXX no sparse file support */
486 1.1 reinoud static void
487 1.1 reinoud udf_append_file_mapping(union dscrptr *dscr, struct long_ad *piece)
488 1.1 reinoud {
489 1.1 reinoud struct icb_tag *icb;
490 1.1 reinoud struct file_entry *fe;
491 1.1 reinoud struct extfile_entry *efe;
492 1.1 reinoud struct long_ad *last_long, last_piece;
493 1.1 reinoud struct short_ad *last_short, new_short;
494 1.1 reinoud uint64_t inf_len, obj_size, logblks_rec;
495 1.1 reinoud uint32_t l_ea, l_ad, size;
496 1.1 reinoud uint32_t last_lb_num, piece_lb_num;
497 1.12 reinoud uint64_t last_len, piece_len, last_flags;
498 1.12 reinoud uint64_t rest_len, merge_len, last_end;
499 1.1 reinoud uint16_t last_part_num, piece_part_num;
500 1.1 reinoud uint16_t crclen, cur_alloc;
501 1.1 reinoud uint8_t *data, *pos;
502 1.1 reinoud const int short_len = sizeof(struct short_ad);
503 1.1 reinoud const int long_len = sizeof(struct long_ad);
504 1.1 reinoud const int sector_size = context.sector_size;
505 1.1 reinoud uint64_t max_len = UDF_ROUNDDOWN(UDF_EXT_MAXLEN, sector_size);
506 1.23 reinoud int use_shorts;
507 1.1 reinoud
508 1.11 reinoud fe = NULL;
509 1.11 reinoud efe = NULL;
510 1.1 reinoud if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
511 1.1 reinoud fe = &dscr->fe;
512 1.11 reinoud data = fe->data;
513 1.26 reinoud l_ea = udf_rw32(fe->l_ea);
514 1.1 reinoud l_ad = udf_rw32(fe->l_ad);
515 1.1 reinoud icb = &fe->icbtag;
516 1.1 reinoud inf_len = udf_rw64(fe->inf_len);
517 1.1 reinoud logblks_rec = udf_rw64(fe->logblks_rec);
518 1.1 reinoud obj_size = 0;
519 1.1 reinoud } else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
520 1.1 reinoud efe = &dscr->efe;
521 1.1 reinoud data = efe->data;
522 1.26 reinoud l_ea = udf_rw32(efe->l_ea);
523 1.1 reinoud l_ad = udf_rw32(efe->l_ad);
524 1.1 reinoud icb = &efe->icbtag;
525 1.1 reinoud inf_len = udf_rw64(efe->inf_len);
526 1.1 reinoud obj_size = udf_rw64(efe->obj_size);
527 1.1 reinoud logblks_rec = udf_rw64(efe->logblks_rec);
528 1.1 reinoud } else {
529 1.31 tsutsui errx(EXIT_FAILURE, "bad tag passed to udf_file_append_blob");
530 1.1 reinoud }
531 1.1 reinoud crclen = udf_rw16(dscr->tag.desc_crc_len);
532 1.1 reinoud
533 1.23 reinoud /* we use shorts if referring inside the metadata partition */
534 1.23 reinoud use_shorts = (udf_rw16(piece->loc.part_num) == context.metadata_part);
535 1.23 reinoud
536 1.1 reinoud pos = data + l_ea;
537 1.1 reinoud cur_alloc = udf_rw16(icb->flags);
538 1.1 reinoud size = UDF_EXT_LEN(udf_rw32(piece->len));
539 1.1 reinoud
540 1.1 reinoud /* extract last entry as a long_ad */
541 1.2 reinoud memset(&last_piece, 0, sizeof(last_piece));
542 1.2 reinoud last_len = 0;
543 1.2 reinoud last_lb_num = 0;
544 1.2 reinoud last_part_num = 0;
545 1.11 reinoud last_flags = 0;
546 1.11 reinoud last_short = NULL;
547 1.11 reinoud last_long = NULL;
548 1.1 reinoud if (l_ad != 0) {
549 1.1 reinoud if (use_shorts) {
550 1.1 reinoud assert(cur_alloc == UDF_ICB_SHORT_ALLOC);
551 1.1 reinoud pos += l_ad - short_len;
552 1.1 reinoud last_short = (struct short_ad *) pos;
553 1.1 reinoud last_lb_num = udf_rw32(last_short->lb_num);
554 1.1 reinoud last_part_num = udf_rw16(piece->loc.part_num);
555 1.1 reinoud last_len = UDF_EXT_LEN(udf_rw32(last_short->len));
556 1.1 reinoud last_flags = UDF_EXT_FLAGS(udf_rw32(last_short->len));
557 1.1 reinoud } else {
558 1.1 reinoud assert(cur_alloc == UDF_ICB_LONG_ALLOC);
559 1.1 reinoud pos += l_ad - long_len;
560 1.1 reinoud last_long = (struct long_ad *) pos;
561 1.1 reinoud last_lb_num = udf_rw32(last_long->loc.lb_num);
562 1.1 reinoud last_part_num = udf_rw16(last_long->loc.part_num);
563 1.1 reinoud last_len = UDF_EXT_LEN(udf_rw32(last_long->len));
564 1.1 reinoud last_flags = UDF_EXT_FLAGS(udf_rw32(last_long->len));
565 1.1 reinoud }
566 1.1 reinoud }
567 1.1 reinoud
568 1.1 reinoud piece_len = UDF_EXT_LEN(udf_rw32(piece->len));
569 1.1 reinoud piece_lb_num = udf_rw32(piece->loc.lb_num);
570 1.1 reinoud piece_part_num = udf_rw16(piece->loc.part_num);
571 1.1 reinoud
572 1.1 reinoud /* try merging */
573 1.1 reinoud rest_len = max_len - last_len;
574 1.1 reinoud
575 1.12 reinoud merge_len = MIN(piece_len, rest_len);
576 1.1 reinoud last_end = last_lb_num + (last_len / sector_size);
577 1.1 reinoud if ((piece_lb_num == last_end) && (last_part_num == piece_part_num)) {
578 1.1 reinoud /* we can merge */
579 1.1 reinoud last_len += merge_len;
580 1.1 reinoud piece_len -= merge_len;
581 1.1 reinoud
582 1.1 reinoud /* write back merge result */
583 1.1 reinoud if (use_shorts) {
584 1.1 reinoud last_short->len = udf_rw32(last_len | last_flags);
585 1.1 reinoud } else {
586 1.1 reinoud last_long->len = udf_rw32(last_len | last_flags);
587 1.1 reinoud }
588 1.1 reinoud piece_lb_num += merge_len / sector_size;
589 1.1 reinoud }
590 1.1 reinoud
591 1.1 reinoud if (piece_len) {
592 1.1 reinoud /* append new entry */
593 1.1 reinoud pos = data + l_ea + l_ad;
594 1.1 reinoud if (use_shorts) {
595 1.1 reinoud icb->flags = udf_rw16(UDF_ICB_SHORT_ALLOC);
596 1.1 reinoud memset(&new_short, 0, short_len);
597 1.1 reinoud new_short.len = udf_rw32(piece_len);
598 1.1 reinoud new_short.lb_num = udf_rw32(piece_lb_num);
599 1.1 reinoud memcpy(pos, &new_short, short_len);
600 1.1 reinoud l_ad += short_len;
601 1.1 reinoud crclen += short_len;
602 1.1 reinoud } else {
603 1.1 reinoud icb->flags = udf_rw16(UDF_ICB_LONG_ALLOC);
604 1.1 reinoud piece->len = udf_rw32(piece_len);
605 1.1 reinoud piece->loc.lb_num = udf_rw32(piece_lb_num);
606 1.1 reinoud memcpy(pos, piece, long_len);
607 1.1 reinoud l_ad += long_len;
608 1.1 reinoud crclen += long_len;
609 1.1 reinoud }
610 1.1 reinoud }
611 1.1 reinoud piece->len = udf_rw32(0);
612 1.1 reinoud
613 1.1 reinoud inf_len += size;
614 1.1 reinoud obj_size += size;
615 1.1 reinoud logblks_rec += UDF_ROUNDUP(size, sector_size) / sector_size;
616 1.1 reinoud
617 1.1 reinoud dscr->tag.desc_crc_len = udf_rw16(crclen);
618 1.1 reinoud if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
619 1.1 reinoud fe->l_ad = udf_rw32(l_ad);
620 1.1 reinoud fe->inf_len = udf_rw64(inf_len);
621 1.1 reinoud fe->logblks_rec = udf_rw64(logblks_rec);
622 1.1 reinoud } else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
623 1.1 reinoud efe->l_ad = udf_rw32(l_ad);
624 1.1 reinoud efe->inf_len = udf_rw64(inf_len);
625 1.29 reinoud efe->obj_size = udf_rw64(obj_size);
626 1.1 reinoud efe->logblks_rec = udf_rw64(logblks_rec);
627 1.1 reinoud }
628 1.1 reinoud }
629 1.1 reinoud
630 1.1 reinoud
631 1.1 reinoud static int
632 1.1 reinoud udf_append_file_contents(union dscrptr *dscr, struct long_ad *data_icb,
633 1.11 reinoud uint8_t *fdata, off_t flen)
634 1.1 reinoud {
635 1.1 reinoud struct long_ad icb;
636 1.1 reinoud uint32_t location;
637 1.1 reinoud uint16_t vpart;
638 1.23 reinoud int sectors;
639 1.1 reinoud
640 1.1 reinoud if (udf_file_inject_blob(dscr, fdata, flen) == 0)
641 1.1 reinoud return 0;
642 1.1 reinoud
643 1.1 reinoud /* has to be appended in mappings */
644 1.1 reinoud icb = *data_icb;
645 1.1 reinoud icb.len = udf_rw32(flen);
646 1.1 reinoud while (udf_rw32(icb.len) > 0)
647 1.1 reinoud udf_append_file_mapping(dscr, &icb);
648 1.1 reinoud udf_validate_tag_and_crc_sums(dscr);
649 1.1 reinoud
650 1.1 reinoud /* write out data piece */
651 1.1 reinoud vpart = udf_rw16(data_icb->loc.part_num);
652 1.1 reinoud location = udf_rw32(data_icb->loc.lb_num);
653 1.23 reinoud sectors = udf_datablocks(flen);
654 1.23 reinoud
655 1.23 reinoud return udf_write_virt(fdata, location, vpart, sectors);
656 1.1 reinoud }
657 1.1 reinoud
658 1.1 reinoud
659 1.1 reinoud static int
660 1.1 reinoud udf_create_new_file(struct stat *st, union dscrptr **dscr,
661 1.1 reinoud int filetype, struct long_ad *icb)
662 1.1 reinoud {
663 1.1 reinoud struct file_entry *fe;
664 1.1 reinoud struct extfile_entry *efe;
665 1.1 reinoud int error;
666 1.1 reinoud
667 1.1 reinoud fe = NULL;
668 1.1 reinoud efe = NULL;
669 1.1 reinoud if (context.dscrver == 2) {
670 1.1 reinoud error = udf_create_new_fe(&fe, filetype, st);
671 1.1 reinoud if (error)
672 1.1 reinoud errx(error, "can't create fe");
673 1.1 reinoud *dscr = (union dscrptr *) fe;
674 1.28 reinoud icb->longad_uniqueid = udf_rw32(udf_rw64(fe->unique_id));
675 1.1 reinoud } else {
676 1.1 reinoud error = udf_create_new_efe(&efe, filetype, st);
677 1.1 reinoud if (error)
678 1.1 reinoud errx(error, "can't create fe");
679 1.1 reinoud *dscr = (union dscrptr *) efe;
680 1.28 reinoud icb->longad_uniqueid = udf_rw32(udf_rw64(efe->unique_id));
681 1.1 reinoud }
682 1.1 reinoud
683 1.1 reinoud return 0;
684 1.1 reinoud }
685 1.1 reinoud
686 1.1 reinoud
687 1.1 reinoud static void
688 1.1 reinoud udf_estimate_walk(fsinfo_t *fsopts,
689 1.1 reinoud fsnode *root, char *dir, struct udf_stats *stats)
690 1.1 reinoud {
691 1.1 reinoud struct fileid_desc *fid;
692 1.16 joerg struct long_ad dummy_ref;
693 1.1 reinoud fsnode *cur;
694 1.1 reinoud fsinode *fnode;
695 1.1 reinoud size_t pathlen = strlen(dir);
696 1.1 reinoud char *mydir = dir + pathlen;
697 1.1 reinoud off_t sz;
698 1.1 reinoud uint32_t nblk, ddoff;
699 1.1 reinoud uint32_t softlink_len;
700 1.1 reinoud uint8_t *softlink_buf;
701 1.1 reinoud int nentries;
702 1.1 reinoud int error;
703 1.1 reinoud
704 1.1 reinoud stats->ndirs++;
705 1.1 reinoud
706 1.1 reinoud /*
707 1.1 reinoud * Count number of directory entries and count directory size; needed
708 1.1 reinoud * for the reservation of enough space for the directory. Pity we
709 1.1 reinoud * don't keep the FIDs we created. If it turns out to be a issue we
710 1.1 reinoud * can cache it later.
711 1.1 reinoud */
712 1.1 reinoud fid = (struct fileid_desc *) malloc(context.sector_size);
713 1.1 reinoud assert(fid);
714 1.1 reinoud
715 1.1 reinoud ddoff = 40; /* '..' entry */
716 1.1 reinoud for (cur = root, nentries = 0; cur != NULL; cur = cur->next) {
717 1.1 reinoud switch (cur->type & S_IFMT) {
718 1.1 reinoud default:
719 1.1 reinoud /* what kind of nodes? */
720 1.1 reinoud break;
721 1.1 reinoud case S_IFCHR:
722 1.1 reinoud case S_IFBLK:
723 1.1 reinoud /* not supported yet */
724 1.23 reinoud break;
725 1.1 reinoud case S_IFDIR:
726 1.1 reinoud if (strcmp(cur->name, ".") == 0)
727 1.1 reinoud continue;
728 1.19 mrg /* FALLTHROUGH */
729 1.1 reinoud case S_IFLNK:
730 1.1 reinoud case S_IFREG:
731 1.1 reinoud /* create dummy FID to see how long name will become */
732 1.16 joerg memset(&dummy_ref, 0, sizeof(dummy_ref));
733 1.1 reinoud udf_create_fid(ddoff, fid, cur->name, 0, &dummy_ref);
734 1.1 reinoud nentries++;
735 1.1 reinoud ddoff += udf_fidsize(fid);
736 1.23 reinoud break;
737 1.1 reinoud }
738 1.1 reinoud }
739 1.1 reinoud sz = ddoff;
740 1.1 reinoud
741 1.1 reinoud root->inode->st.st_size = sz; /* max now */
742 1.1 reinoud root->inode->flags |= FI_SIZED;
743 1.1 reinoud
744 1.1 reinoud nblk = udf_datablocks(sz);
745 1.1 reinoud stats->nmetadatablocks += nblk;
746 1.1 reinoud
747 1.1 reinoud /* for each entry in the directory, there needs to be a (E)FE */
748 1.1 reinoud stats->nmetadatablocks += nentries + 1;
749 1.1 reinoud
750 1.1 reinoud /* recurse */
751 1.1 reinoud for (cur = root; cur != NULL; cur = cur->next) {
752 1.1 reinoud switch (cur->type & S_IFMT) {
753 1.1 reinoud default:
754 1.1 reinoud /* what kind of nodes? */
755 1.1 reinoud break;
756 1.1 reinoud case S_IFDIR:
757 1.1 reinoud if (strcmp(cur->name, ".") == 0)
758 1.1 reinoud continue;
759 1.1 reinoud /* empty dir? */
760 1.1 reinoud if (!cur->child)
761 1.1 reinoud break;
762 1.1 reinoud mydir[0] = '/';
763 1.1 reinoud strncpy(&mydir[1], cur->name, MAXPATHLEN - pathlen);
764 1.1 reinoud udf_estimate_walk(fsopts, cur->child, dir, stats);
765 1.1 reinoud mydir[0] = '\0';
766 1.1 reinoud break;
767 1.23 reinoud case S_IFCHR:
768 1.23 reinoud case S_IFBLK:
769 1.23 reinoud /* not supported yet */
770 1.23 reinoud // stats->nfiles++;
771 1.23 reinoud break;
772 1.1 reinoud case S_IFREG:
773 1.1 reinoud fnode = cur->inode;
774 1.1 reinoud /* don't double-count hard-links */
775 1.1 reinoud if (!(fnode->flags & FI_SIZED)) {
776 1.1 reinoud sz = fnode->st.st_size;
777 1.1 reinoud nblk = udf_datablocks(sz);
778 1.1 reinoud stats->ndatablocks += nblk;
779 1.1 reinoud /* ... */
780 1.1 reinoud fnode->flags |= FI_SIZED;
781 1.1 reinoud }
782 1.1 reinoud stats->nfiles++;
783 1.1 reinoud break;
784 1.1 reinoud case S_IFLNK:
785 1.1 reinoud /* softlink */
786 1.1 reinoud fnode = cur->inode;
787 1.1 reinoud /* don't double-count hard-links */
788 1.1 reinoud if (!(fnode->flags & FI_SIZED)) {
789 1.1 reinoud error = udf_encode_symlink(&softlink_buf,
790 1.1 reinoud &softlink_len, cur->symlink);
791 1.1 reinoud if (error) {
792 1.1 reinoud printf("SOFTLINK error %d\n", error);
793 1.1 reinoud break;
794 1.1 reinoud }
795 1.1 reinoud nblk = udf_datablocks(softlink_len);
796 1.1 reinoud stats->ndatablocks += nblk;
797 1.1 reinoud fnode->flags |= FI_SIZED;
798 1.1 reinoud
799 1.1 reinoud free(softlink_buf);
800 1.1 reinoud }
801 1.1 reinoud stats->nfiles++;
802 1.1 reinoud break;
803 1.1 reinoud }
804 1.1 reinoud }
805 1.1 reinoud }
806 1.1 reinoud
807 1.1 reinoud
808 1.1 reinoud #define UDF_MAX_CHUNK_SIZE (4*1024*1024)
809 1.1 reinoud static int
810 1.1 reinoud udf_copy_file(struct stat *st, char *path, fsnode *cur, struct fileid_desc *fid,
811 1.1 reinoud struct long_ad *icb)
812 1.1 reinoud {
813 1.1 reinoud union dscrptr *dscr;
814 1.1 reinoud struct long_ad data_icb;
815 1.1 reinoud fsinode *fnode;
816 1.11 reinoud off_t sz, chunk, rd;
817 1.1 reinoud uint8_t *data;
818 1.23 reinoud bool intern;
819 1.1 reinoud int nblk;
820 1.23 reinoud int f;
821 1.5 reinoud int error;
822 1.1 reinoud
823 1.1 reinoud fnode = cur->inode;
824 1.1 reinoud
825 1.3 joerg f = open(path, O_RDONLY);
826 1.1 reinoud if (f < 0) {
827 1.1 reinoud warn("Can't open file %s for reading", cur->name);
828 1.1 reinoud return errno;
829 1.1 reinoud }
830 1.1 reinoud
831 1.1 reinoud /* claim disc space for the (e)fe descriptor for this file */
832 1.1 reinoud udf_metadata_alloc(1, icb);
833 1.1 reinoud udf_create_new_file(st, &dscr, UDF_ICB_FILETYPE_RANDOMACCESS, icb);
834 1.1 reinoud
835 1.1 reinoud sz = fnode->st.st_size;
836 1.1 reinoud
837 1.1 reinoud chunk = MIN(sz, UDF_MAX_CHUNK_SIZE);
838 1.1 reinoud data = malloc(MAX(chunk, context.sector_size));
839 1.1 reinoud assert(data);
840 1.1 reinoud
841 1.23 reinoud intern = (udf_datablocks(chunk) == 0);
842 1.5 reinoud error = 0;
843 1.1 reinoud while (chunk) {
844 1.1 reinoud rd = read(f, data, chunk);
845 1.1 reinoud if (rd != chunk) {
846 1.17 christos warn("Short read of file %s", cur->name);
847 1.5 reinoud error = errno;
848 1.5 reinoud break;
849 1.1 reinoud }
850 1.1 reinoud
851 1.23 reinoud nblk = UDF_ROUNDUP(chunk, context.sector_size) / context.sector_size;
852 1.23 reinoud if (chunk && !intern)
853 1.1 reinoud udf_data_alloc(nblk, &data_icb);
854 1.1 reinoud udf_append_file_contents(dscr, &data_icb, data, chunk);
855 1.1 reinoud
856 1.1 reinoud sz -= chunk;
857 1.1 reinoud chunk = MIN(sz, UDF_MAX_CHUNK_SIZE);
858 1.1 reinoud }
859 1.1 reinoud close(f);
860 1.1 reinoud free(data);
861 1.1 reinoud
862 1.1 reinoud /* write out dscr (e)fe */
863 1.1 reinoud udf_set_link_cnt(dscr, fnode->nlink);
864 1.1 reinoud udf_write_dscr_virt(dscr, udf_rw32(icb->loc.lb_num),
865 1.1 reinoud udf_rw16(icb->loc.part_num), 1);
866 1.5 reinoud free(dscr);
867 1.1 reinoud
868 1.1 reinoud /* remember our location for hardlinks */
869 1.1 reinoud cur->inode->fsuse = malloc(sizeof(struct long_ad));
870 1.1 reinoud memcpy(cur->inode->fsuse, icb, sizeof(struct long_ad));
871 1.1 reinoud
872 1.5 reinoud return error;
873 1.1 reinoud }
874 1.1 reinoud
875 1.1 reinoud
876 1.1 reinoud static int
877 1.1 reinoud udf_populate_walk(fsinfo_t *fsopts, fsnode *root, char *dir,
878 1.1 reinoud struct long_ad *parent_icb, struct long_ad *dir_icb)
879 1.1 reinoud {
880 1.1 reinoud union dscrptr *dir_dscr, *dscr;
881 1.1 reinoud struct fileid_desc *fid;
882 1.1 reinoud struct long_ad icb, data_icb, dirdata_icb;
883 1.1 reinoud fsnode *cur;
884 1.1 reinoud fsinode *fnode;
885 1.1 reinoud size_t pathlen = strlen(dir);
886 1.1 reinoud size_t dirlen;
887 1.1 reinoud char *mydir = dir + pathlen;
888 1.1 reinoud uint32_t nblk, ddoff;
889 1.1 reinoud uint32_t softlink_len;
890 1.1 reinoud uint8_t *softlink_buf;
891 1.1 reinoud uint8_t *dirdata;
892 1.1 reinoud int error, ret, retval;
893 1.1 reinoud
894 1.1 reinoud /* claim disc space for the (e)fe descriptor for this dir */
895 1.1 reinoud udf_metadata_alloc(1, dir_icb);
896 1.1 reinoud
897 1.1 reinoud /* create new e(fe) */
898 1.1 reinoud udf_create_new_file(&root->inode->st, &dir_dscr,
899 1.1 reinoud UDF_ICB_FILETYPE_DIRECTORY, dir_icb);
900 1.1 reinoud
901 1.23 reinoud /* allocate memory for the directory contents */
902 1.1 reinoud dirlen = root->inode->st.st_size;
903 1.23 reinoud nblk = UDF_ROUNDUP(dirlen, context.sector_size) / context.sector_size;
904 1.1 reinoud dirdata = malloc(nblk * context.sector_size);
905 1.1 reinoud assert(dirdata);
906 1.1 reinoud memset(dirdata, 0, nblk * context.sector_size);
907 1.1 reinoud
908 1.1 reinoud /* create and append '..' */
909 1.1 reinoud fid = (struct fileid_desc *) dirdata;
910 1.1 reinoud ddoff = udf_create_parentfid(fid, parent_icb);
911 1.23 reinoud assert(ddoff == 40);
912 1.1 reinoud
913 1.1 reinoud /* for '..' */
914 1.1 reinoud udf_inc_link(dir_dscr);
915 1.1 reinoud
916 1.1 reinoud /* recurse */
917 1.1 reinoud retval = 0;
918 1.1 reinoud for (cur = root; cur != NULL; cur = cur->next) {
919 1.1 reinoud mydir[0] = '/';
920 1.1 reinoud strncpy(&mydir[1], cur->name, MAXPATHLEN - pathlen);
921 1.1 reinoud
922 1.1 reinoud fid = (struct fileid_desc *) (dirdata + ddoff);
923 1.1 reinoud switch (cur->type & S_IFMT) {
924 1.1 reinoud default:
925 1.1 reinoud /* what kind of nodes? */
926 1.1 reinoud retval = 2;
927 1.1 reinoud break;
928 1.1 reinoud case S_IFCHR:
929 1.1 reinoud case S_IFBLK:
930 1.1 reinoud /* not supported */
931 1.1 reinoud retval = 2;
932 1.1 reinoud warnx("device node %s not supported", dir);
933 1.1 reinoud break;
934 1.1 reinoud case S_IFDIR:
935 1.1 reinoud /* not an empty dir? */
936 1.1 reinoud if (strcmp(cur->name, ".") == 0)
937 1.1 reinoud break;
938 1.1 reinoud assert(cur->child);
939 1.1 reinoud if (cur->child) {
940 1.1 reinoud ret = udf_populate_walk(fsopts, cur->child,
941 1.1 reinoud dir, dir_icb, &icb);
942 1.1 reinoud if (ret)
943 1.1 reinoud retval = 2;
944 1.1 reinoud }
945 1.1 reinoud udf_create_fid(ddoff, fid, cur->name,
946 1.1 reinoud UDF_FILE_CHAR_DIR, &icb);
947 1.1 reinoud udf_inc_link(dir_dscr);
948 1.1 reinoud ddoff += udf_fidsize(fid);
949 1.1 reinoud break;
950 1.1 reinoud case S_IFREG:
951 1.1 reinoud fnode = cur->inode;
952 1.1 reinoud /* don't re-copy hard-links */
953 1.1 reinoud if (!(fnode->flags & FI_WRITTEN)) {
954 1.23 reinoud printf("%s\n", dir);
955 1.1 reinoud error = udf_copy_file(&fnode->st, dir, cur,
956 1.1 reinoud fid, &icb);
957 1.1 reinoud if (!error) {
958 1.1 reinoud fnode->flags |= FI_WRITTEN;
959 1.1 reinoud udf_create_fid(ddoff, fid, cur->name,
960 1.1 reinoud 0, &icb);
961 1.1 reinoud ddoff += udf_fidsize(fid);
962 1.1 reinoud } else {
963 1.1 reinoud retval = 2;
964 1.1 reinoud }
965 1.1 reinoud } else {
966 1.1 reinoud /* hardlink! */
967 1.1 reinoud printf("%s (hardlink)\n", dir);
968 1.1 reinoud udf_create_fid(ddoff, fid, cur->name,
969 1.1 reinoud 0, (struct long_ad *) (fnode->fsuse));
970 1.1 reinoud ddoff += udf_fidsize(fid);
971 1.1 reinoud }
972 1.1 reinoud fnode->nlink--;
973 1.1 reinoud if (fnode->nlink == 0)
974 1.1 reinoud free(fnode->fsuse);
975 1.1 reinoud break;
976 1.1 reinoud case S_IFLNK:
977 1.1 reinoud /* softlink */
978 1.1 reinoud fnode = cur->inode;
979 1.1 reinoud printf("%s -> %s\n", dir, cur->symlink);
980 1.1 reinoud error = udf_encode_symlink(&softlink_buf,
981 1.1 reinoud &softlink_len, cur->symlink);
982 1.1 reinoud if (error) {
983 1.1 reinoud printf("SOFTLINK error %d\n", error);
984 1.1 reinoud retval = 2;
985 1.1 reinoud break;
986 1.1 reinoud }
987 1.1 reinoud
988 1.1 reinoud udf_metadata_alloc(1, &icb);
989 1.1 reinoud udf_create_new_file(&fnode->st, &dscr,
990 1.1 reinoud UDF_ICB_FILETYPE_SYMLINK, &icb);
991 1.1 reinoud
992 1.1 reinoud nblk = udf_datablocks(softlink_len);
993 1.1 reinoud if (nblk > 0)
994 1.1 reinoud udf_data_alloc(nblk, &data_icb);
995 1.1 reinoud udf_append_file_contents(dscr, &data_icb,
996 1.1 reinoud softlink_buf, softlink_len);
997 1.1 reinoud
998 1.1 reinoud /* write out dscr (e)fe */
999 1.1 reinoud udf_inc_link(dscr);
1000 1.1 reinoud udf_write_dscr_virt(dscr, udf_rw32(icb.loc.lb_num),
1001 1.1 reinoud udf_rw16(icb.loc.part_num), 1);
1002 1.1 reinoud
1003 1.5 reinoud free(dscr);
1004 1.1 reinoud free(softlink_buf);
1005 1.1 reinoud
1006 1.1 reinoud udf_create_fid(ddoff, fid, cur->name, 0, &icb);
1007 1.1 reinoud ddoff += udf_fidsize(fid);
1008 1.1 reinoud break;
1009 1.1 reinoud }
1010 1.1 reinoud mydir[0] = '\0';
1011 1.1 reinoud }
1012 1.23 reinoud assert(dirlen == ddoff);
1013 1.23 reinoud
1014 1.23 reinoud /* pre allocate space for the directory contents */
1015 1.23 reinoud memset(&dirdata_icb, 0, sizeof(dirdata_icb));
1016 1.23 reinoud nblk = udf_datablocks(dirlen);
1017 1.1 reinoud
1018 1.23 reinoud /* claim disc space for the dir contents if needed */
1019 1.23 reinoud if (nblk > 0)
1020 1.23 reinoud udf_fids_alloc(nblk, &dirdata_icb);
1021 1.1 reinoud
1022 1.1 reinoud udf_prepare_fids(dir_icb, &dirdata_icb, dirdata, dirlen);
1023 1.1 reinoud udf_append_file_contents(dir_dscr, &dirdata_icb, dirdata, dirlen);
1024 1.1 reinoud
1025 1.1 reinoud /* write out dir_dscr (e)fe */
1026 1.1 reinoud udf_write_dscr_virt(dir_dscr, udf_rw32(dir_icb->loc.lb_num),
1027 1.1 reinoud udf_rw16(dir_icb->loc.part_num), 1);
1028 1.1 reinoud
1029 1.5 reinoud free(dirdata);
1030 1.5 reinoud free(dir_dscr);
1031 1.1 reinoud return retval;
1032 1.1 reinoud }
1033 1.1 reinoud
1034 1.1 reinoud
1035 1.1 reinoud static int
1036 1.1 reinoud udf_populate(const char *dir, fsnode *root, fsinfo_t *fsopts,
1037 1.1 reinoud struct udf_stats *stats)
1038 1.1 reinoud {
1039 1.1 reinoud struct long_ad rooticb;
1040 1.1 reinoud static char path[MAXPATHLEN+1];
1041 1.1 reinoud int error;
1042 1.1 reinoud
1043 1.1 reinoud strncpy(path, dir, sizeof(path));
1044 1.1 reinoud error = udf_populate_walk(fsopts, root, path, &rooticb, &rooticb);
1045 1.1 reinoud
1046 1.1 reinoud return error;
1047 1.1 reinoud }
1048 1.1 reinoud
1049 1.1 reinoud
1050 1.1 reinoud static void
1051 1.1 reinoud udf_enumerate_and_estimate(const char *dir, fsnode *root, fsinfo_t *fsopts,
1052 1.1 reinoud struct udf_stats *stats)
1053 1.1 reinoud {
1054 1.1 reinoud char path[MAXPATHLEN + 1];
1055 1.7 reinoud off_t proposed_size;
1056 1.23 reinoud uint32_t n, nblk, nmetablk, nbytes;
1057 1.23 reinoud uint32_t spareable_blocks, spareable_blockingnr;
1058 1.1 reinoud
1059 1.1 reinoud strncpy(path, dir, sizeof(path));
1060 1.1 reinoud
1061 1.1 reinoud /* calculate strict minimal size */
1062 1.1 reinoud udf_estimate_walk(fsopts, root, path, stats);
1063 1.23 reinoud #if 0
1064 1.5 reinoud printf("ndirs %d\n", stats->ndirs);
1065 1.5 reinoud printf("nfiles %d\n", stats->nfiles);
1066 1.5 reinoud printf("ndata_blocks %d\n", stats->ndatablocks);
1067 1.5 reinoud printf("nmetadata_blocks %d\n", stats->nmetadatablocks);
1068 1.5 reinoud printf("\n");
1069 1.23 reinoud #endif
1070 1.1 reinoud
1071 1.1 reinoud /* adjust for options : free file nodes */
1072 1.1 reinoud if (fsopts->freefiles) {
1073 1.1 reinoud /* be mercifull and reserve more for the FID */
1074 1.1 reinoud stats->nmetadatablocks += fsopts->freefiles * 1.5;
1075 1.1 reinoud } else if ((n = fsopts->freefilepc)) {
1076 1.1 reinoud stats->nmetadatablocks += (stats->nmetadatablocks*n) / (100-n);
1077 1.1 reinoud }
1078 1.1 reinoud
1079 1.1 reinoud /* adjust for options : free data blocks */
1080 1.1 reinoud if (fsopts->freeblocks) {
1081 1.1 reinoud stats->ndatablocks += fsopts->freeblocks;
1082 1.1 reinoud } else if ((n = fsopts->freeblockpc)) {
1083 1.1 reinoud stats->ndatablocks += (stats->ndatablocks * n) / (100-n);
1084 1.1 reinoud }
1085 1.1 reinoud
1086 1.1 reinoud /* rough predictor of minimum disc size */
1087 1.1 reinoud nblk = stats->ndatablocks + stats->nmetadatablocks;
1088 1.23 reinoud if (context.format_flags & FORMAT_META) {
1089 1.23 reinoud float meta_p;
1090 1.23 reinoud double factor;
1091 1.23 reinoud
1092 1.23 reinoud meta_p = (float) context.meta_perc/100.0;
1093 1.23 reinoud factor = meta_p / (1.0 - meta_p);
1094 1.23 reinoud
1095 1.23 reinoud /* add space for metadata partition including some slack */
1096 1.23 reinoud nmetablk = factor * nblk + 32;
1097 1.23 reinoud nblk = stats->ndatablocks + nmetablk;
1098 1.23 reinoud
1099 1.23 reinoud /* free space maps */
1100 1.23 reinoud nbytes = ceil((double) nblk * (1.0/8.0));
1101 1.23 reinoud nblk += 1 + (nbytes + context.sector_size-1)/context.sector_size;
1102 1.23 reinoud if (!(context.format_flags & FORMAT_READONLY)) {
1103 1.23 reinoud nbytes = ceil((double) nmetablk * (1.0/8.0));
1104 1.23 reinoud nblk += 1 + (nbytes + context.sector_size-1)/context.sector_size;
1105 1.23 reinoud }
1106 1.23 reinoud } else if (context.format_flags & FORMAT_SEQUENTIAL) {
1107 1.23 reinoud /* nothing */
1108 1.23 reinoud } else {
1109 1.23 reinoud if (!(context.format_flags & FORMAT_READONLY)) {
1110 1.23 reinoud nbytes = ceil((double) nblk * (1.0/8.0));
1111 1.23 reinoud nblk += 1 + (nbytes + context.sector_size-1)/
1112 1.23 reinoud context.sector_size;
1113 1.23 reinoud }
1114 1.23 reinoud }
1115 1.23 reinoud
1116 1.23 reinoud /*
1117 1.23 reinoud * Make extra room for spareable table if requested
1118 1.23 reinoud */
1119 1.23 reinoud if (context.format_flags & FORMAT_SPAREABLE) {
1120 1.23 reinoud spareable_blockingnr = udf_spareable_blockingnr();
1121 1.23 reinoud spareable_blocks = udf_spareable_blocks();
1122 1.23 reinoud
1123 1.23 reinoud nblk += spareable_blocks * spareable_blockingnr;
1124 1.23 reinoud nblk += spareable_blockingnr; /* slack */
1125 1.23 reinoud }
1126 1.23 reinoud
1127 1.1 reinoud nblk += 256; /* pre-volume space */
1128 1.1 reinoud nblk += 256; /* post-volume space */
1129 1.23 reinoud nblk += 1024; /* safeguard */
1130 1.1 reinoud
1131 1.1 reinoud /* try to honour minimum size */
1132 1.1 reinoud n = fsopts->minsize / fsopts->sectorsize;
1133 1.1 reinoud if (nblk < n) {
1134 1.1 reinoud stats->ndatablocks += (n - nblk);
1135 1.1 reinoud nblk += n - nblk;
1136 1.1 reinoud }
1137 1.30 reinoud
1138 1.30 reinoud /* keep proposed size a multiple of blockingnr for image creation */
1139 1.30 reinoud if (S_ISREG(dev_fd_stat.st_mode)) {
1140 1.30 reinoud struct mmc_trackinfo ti;
1141 1.30 reinoud int blockingnr;
1142 1.30 reinoud int error;
1143 1.30 reinoud
1144 1.30 reinoud /* adjust proposed size to be a multiple of the blockingnr */
1145 1.30 reinoud udf_update_discinfo();
1146 1.30 reinoud ti.tracknr = mmc_discinfo.first_track_last_session;
1147 1.30 reinoud error = udf_update_trackinfo(&ti);
1148 1.30 reinoud assert(!error);
1149 1.30 reinoud blockingnr = udf_get_blockingnr(&ti);
1150 1.30 reinoud nblk = UDF_ROUNDUP(nblk, blockingnr);
1151 1.30 reinoud }
1152 1.30 reinoud
1153 1.7 reinoud proposed_size = (off_t) nblk * fsopts->sectorsize;
1154 1.30 reinoud
1155 1.7 reinoud /* sanity size */
1156 1.7 reinoud if (proposed_size < 512*1024)
1157 1.7 reinoud proposed_size = 512*1024;
1158 1.1 reinoud
1159 1.7 reinoud if (fsopts->size) {
1160 1.23 reinoud if (fsopts->size < proposed_size)
1161 1.31 tsutsui errx(EXIT_FAILURE, "makefs_udf: won't fit on disc!");
1162 1.7 reinoud } else {
1163 1.7 reinoud fsopts->size = proposed_size;
1164 1.7 reinoud }
1165 1.1 reinoud
1166 1.1 reinoud fsopts->inodes = stats->nfiles + stats->ndirs;
1167 1.1 reinoud }
1168 1.1 reinoud
1169 1.1 reinoud
1170 1.1 reinoud void
1171 1.1 reinoud udf_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
1172 1.1 reinoud {
1173 1.1 reinoud struct udf_stats stats;
1174 1.1 reinoud uint64_t truncate_len;
1175 1.23 reinoud uint32_t last_sector, ext;
1176 1.9 reinoud char scrap[255];
1177 1.1 reinoud int error;
1178 1.1 reinoud
1179 1.23 reinoud /* setup */
1180 1.23 reinoud emul_sectorsize = fsopts->sectorsize;
1181 1.23 reinoud emul_size = 0;
1182 1.1 reinoud context.sector_size = fsopts->sectorsize;
1183 1.1 reinoud
1184 1.1 reinoud /* names */
1185 1.1 reinoud error = udf_proces_names();
1186 1.1 reinoud if (error)
1187 1.31 tsutsui errx(EXIT_FAILURE, "bad names given");
1188 1.23 reinoud
1189 1.23 reinoud /* open disc device or emulated file */
1190 1.23 reinoud if (udf_opendisc(image, O_CREAT)) {
1191 1.23 reinoud udf_closedisc();
1192 1.31 tsutsui errx(EXIT_FAILURE, "can't open %s", image);
1193 1.23 reinoud }
1194 1.23 reinoud fsopts->fd = dev_fd;
1195 1.1 reinoud
1196 1.23 reinoud /* determine format */
1197 1.23 reinoud if (udf_readonly_format())
1198 1.23 reinoud req_enable |= FORMAT_READONLY;
1199 1.23 reinoud // printf("req_enable %d, req_disable %d\n", req_enable, req_disable);
1200 1.23 reinoud error = udf_derive_format(req_enable, req_disable);
1201 1.23 reinoud if (error) {
1202 1.23 reinoud udf_closedisc();
1203 1.31 tsutsui errx(EXIT_FAILURE, "can't derive format from media/settings");
1204 1.23 reinoud }
1205 1.1 reinoud
1206 1.1 reinoud /* estimate the amount of space needed */
1207 1.2 reinoud memset(&stats, 0, sizeof(stats));
1208 1.1 reinoud udf_enumerate_and_estimate(dir, root, fsopts, &stats);
1209 1.1 reinoud
1210 1.23 reinoud printf("Calculated size of `%s' is "
1211 1.23 reinoud "%"PRIu64" KiB, %"PRIu64" MiB, %"PRIu64" GiB with %ld inodes\n",
1212 1.23 reinoud image,
1213 1.23 reinoud (uint64_t) fsopts->size/1024,
1214 1.23 reinoud (uint64_t) fsopts->size/1024/1024,
1215 1.23 reinoud (uint64_t) fsopts->size/1024/1024/1024,
1216 1.23 reinoud (long)fsopts->inodes);
1217 1.23 reinoud emul_size = MAX(emul_size, fsopts->size);
1218 1.23 reinoud if ((fsopts->maxsize > 0) && (emul_size > fsopts->maxsize))
1219 1.31 tsutsui errx(EXIT_FAILURE, "won't fit due to set maximum disk size");
1220 1.1 reinoud
1221 1.23 reinoud /* prepare disc if necessary (recordables mainly) */
1222 1.23 reinoud error = udf_prepare_disc();
1223 1.23 reinoud if (error) {
1224 1.23 reinoud udf_closedisc();
1225 1.31 tsutsui errx(EXIT_FAILURE, "preparing disc failed");
1226 1.1 reinoud }
1227 1.1 reinoud
1228 1.1 reinoud /* update mmc info but now with correct size */
1229 1.23 reinoud udf_update_discinfo();
1230 1.23 reinoud udf_dump_discinfo(&mmc_discinfo);
1231 1.1 reinoud
1232 1.9 reinoud printf("Building disc compatible with UDF version %x to %x\n\n",
1233 1.9 reinoud context.min_udf, context.max_udf);
1234 1.9 reinoud (void)snprintb(scrap, sizeof(scrap), FORMAT_FLAGBITS,
1235 1.23 reinoud (uint64_t) context.format_flags);
1236 1.9 reinoud printf("UDF properties %s\n", scrap);
1237 1.9 reinoud printf("Volume set `%s'\n", context.volset_name);
1238 1.9 reinoud printf("Primary volume `%s`\n", context.primary_name);
1239 1.9 reinoud printf("Logical volume `%s`\n", context.logvol_name);
1240 1.23 reinoud if (context.format_flags & FORMAT_META)
1241 1.23 reinoud printf("Metadata percentage %d%% (%d%% used)\n",
1242 1.23 reinoud context.meta_perc,
1243 1.27 reinoud (int) ceil(100.0*stats.nmetadatablocks/stats.ndatablocks));
1244 1.9 reinoud printf("\n");
1245 1.23 reinoud
1246 1.23 reinoud /* prefix */
1247 1.23 reinoud udf_allow_writing();
1248 1.23 reinoud if (udf_do_newfs_prefix()) {
1249 1.23 reinoud udf_closedisc();
1250 1.31 tsutsui errx(EXIT_FAILURE, "basic setup failed");
1251 1.23 reinoud }
1252 1.1 reinoud
1253 1.1 reinoud /* update context */
1254 1.1 reinoud context.unique_id = 0;
1255 1.1 reinoud
1256 1.23 reinoud /* add all directories */
1257 1.1 reinoud error = udf_populate(dir, root, fsopts, &stats);
1258 1.1 reinoud
1259 1.23 reinoud if (!error) {
1260 1.24 wiz /* update values for integrity sequence */
1261 1.23 reinoud context.num_files = stats.nfiles;
1262 1.23 reinoud context.num_directories = stats.ndirs;
1263 1.23 reinoud
1264 1.23 reinoud udf_do_newfs_postfix();
1265 1.23 reinoud
1266 1.23 reinoud if (S_ISREG(dev_fd_stat.st_mode) &&
1267 1.23 reinoud (context.format_flags & FORMAT_VAT)) {
1268 1.23 reinoud udf_translate_vtop(context.alloc_pos[context.data_part],
1269 1.23 reinoud context.data_part,
1270 1.23 reinoud &last_sector, &ext);
1271 1.23 reinoud truncate_len = (uint64_t) last_sector * context.sector_size;
1272 1.23 reinoud
1273 1.23 reinoud printf("\nTruncing the disc-image to allow for VAT\n");
1274 1.23 reinoud printf("Free space left on this volume approx. "
1275 1.23 reinoud "%"PRIu64" KiB, %"PRIu64" MiB\n",
1276 1.23 reinoud (fsopts->size - truncate_len)/1024,
1277 1.23 reinoud (fsopts->size - truncate_len)/1024/1024);
1278 1.23 reinoud ftruncate(dev_fd, truncate_len);
1279 1.23 reinoud }
1280 1.1 reinoud }
1281 1.23 reinoud udf_closedisc();
1282 1.1 reinoud
1283 1.25 riastrad if (error == 2)
1284 1.23 reinoud errx(error, "not all files could be added");
1285 1.23 reinoud if (error == 1)
1286 1.23 reinoud errx(error, "creation of %s failed", image);
1287 1.1 reinoud return;
1288 1.1 reinoud }
1289