udf.c revision 1.30 1 1.30 reinoud /* $NetBSD: udf.c,v 1.30 2022/05/07 08:54:02 reinoud 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.30 reinoud __RCSID("$NetBSD: udf.c,v 1.30 2022/05/07 08:54:02 reinoud 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.23 reinoud errx(1, "unknown or unimplemented disc format");
255 1.1 reinoud }
256 1.23 reinoud if (emul_mmc_profile != 0x01)
257 1.7 reinoud set_sectorsize = 2048;
258 1.8 reinoud break;
259 1.8 reinoud case 'L':
260 1.8 reinoud if (context.logvol_name) free(context.logvol_name);
261 1.8 reinoud context.logvol_name = strdup(buf);
262 1.8 reinoud break;
263 1.8 reinoud case 'P':
264 1.8 reinoud if ((colon = strstr(buf, ":"))) {
265 1.8 reinoud if (context.volset_name)
266 1.8 reinoud free(context.volset_name);
267 1.8 reinoud *colon = 0;
268 1.8 reinoud context.volset_name = strdup(buf);
269 1.8 reinoud buf = colon+1;
270 1.8 reinoud }
271 1.8 reinoud if (context.primary_name)
272 1.8 reinoud free(context.primary_name);
273 1.23 reinoud if ((strstr(buf, ":")))
274 1.23 reinoud errx(1, "primary name can't have ':' in its name");
275 1.8 reinoud context.primary_name = strdup(buf);
276 1.8 reinoud break;
277 1.9 reinoud case 'v':
278 1.9 reinoud context.min_udf = a_udf_version(buf, "min_udf");
279 1.23 reinoud if (context.min_udf > 0x250)
280 1.23 reinoud errx(1, "maximum supported version is UDF 2.50");
281 1.9 reinoud if (context.min_udf > context.max_udf)
282 1.9 reinoud context.max_udf = context.min_udf;
283 1.9 reinoud break;
284 1.23 reinoud case 'V':
285 1.23 reinoud context.max_udf = a_udf_version(buf, "min_udf");
286 1.23 reinoud if (context.max_udf > 0x250)
287 1.23 reinoud errx(1, "maximum supported version is UDF 2.50");
288 1.23 reinoud if (context.min_udf > context.max_udf)
289 1.23 reinoud context.min_udf = context.max_udf;
290 1.23 reinoud break;
291 1.1 reinoud }
292 1.7 reinoud if (set_sectorsize)
293 1.7 reinoud fsopts->sectorsize = set_sectorsize;
294 1.23 reinoud if (stdsize) {
295 1.23 reinoud if (fsopts->maxsize > 0)
296 1.23 reinoud stdsize = MIN(stdsize, (uint64_t) fsopts->maxsize);
297 1.23 reinoud if (fsopts->minsize > 0)
298 1.23 reinoud stdsize = MAX(stdsize, (uint64_t) fsopts->minsize);
299 1.23 reinoud fsopts->size = fsopts->minsize = fsopts->maxsize = stdsize;
300 1.23 reinoud }
301 1.23 reinoud if (maxsize) {
302 1.23 reinoud if (fsopts->maxsize > 0)
303 1.23 reinoud maxsize = MIN(maxsize, (uint64_t) fsopts->maxsize);
304 1.23 reinoud if (fsopts->minsize > 0)
305 1.23 reinoud maxsize = MAX(maxsize, (uint64_t) fsopts->minsize);
306 1.23 reinoud fsopts->maxsize = maxsize;
307 1.23 reinoud }
308 1.1 reinoud return 1;
309 1.1 reinoud }
310 1.1 reinoud
311 1.23 reinoud /* -
312 1.23 reinoud * -------------------------------------------------------------------- */
313 1.1 reinoud
314 1.1 reinoud struct udf_stats {
315 1.1 reinoud uint32_t nfiles;
316 1.1 reinoud uint32_t ndirs;
317 1.1 reinoud uint32_t ndescr;
318 1.1 reinoud uint32_t nmetadatablocks;
319 1.1 reinoud uint32_t ndatablocks;
320 1.1 reinoud };
321 1.1 reinoud
322 1.1 reinoud
323 1.1 reinoud /* node reference administration */
324 1.1 reinoud static void
325 1.1 reinoud udf_inc_link(union dscrptr *dscr)
326 1.1 reinoud {
327 1.1 reinoud struct file_entry *fe;
328 1.1 reinoud struct extfile_entry *efe;
329 1.1 reinoud
330 1.1 reinoud if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
331 1.1 reinoud fe = &dscr->fe;
332 1.1 reinoud fe->link_cnt = udf_rw16(udf_rw16(fe->link_cnt) + 1);
333 1.1 reinoud } else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
334 1.1 reinoud efe = &dscr->efe;
335 1.1 reinoud efe->link_cnt = udf_rw16(udf_rw16(efe->link_cnt) + 1);
336 1.1 reinoud } else {
337 1.23 reinoud errx(1, "bad tag passed to udf_inc_link");
338 1.1 reinoud }
339 1.1 reinoud }
340 1.1 reinoud
341 1.1 reinoud
342 1.1 reinoud static void
343 1.1 reinoud udf_set_link_cnt(union dscrptr *dscr, int num)
344 1.1 reinoud {
345 1.1 reinoud struct file_entry *fe;
346 1.1 reinoud struct extfile_entry *efe;
347 1.1 reinoud
348 1.1 reinoud if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
349 1.1 reinoud fe = &dscr->fe;
350 1.1 reinoud fe->link_cnt = udf_rw16(num);
351 1.1 reinoud } else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
352 1.1 reinoud efe = &dscr->efe;
353 1.1 reinoud efe->link_cnt = udf_rw16(num);
354 1.1 reinoud } else {
355 1.23 reinoud errx(1, "bad tag passed to udf_set_link_cnt");
356 1.1 reinoud }
357 1.1 reinoud }
358 1.1 reinoud
359 1.1 reinoud
360 1.1 reinoud static uint32_t
361 1.1 reinoud udf_datablocks(off_t sz)
362 1.1 reinoud {
363 1.1 reinoud /* predictor if it can be written inside the node */
364 1.20 reinoud /* XXX the predictor assumes NO extended attributes in the node */
365 1.1 reinoud if (sz < context.sector_size - UDF_EXTFENTRY_SIZE - 16)
366 1.1 reinoud return 0;
367 1.1 reinoud
368 1.1 reinoud return UDF_ROUNDUP(sz, context.sector_size) / context.sector_size;
369 1.1 reinoud }
370 1.1 reinoud
371 1.1 reinoud
372 1.1 reinoud static void
373 1.1 reinoud udf_prepare_fids(struct long_ad *dir_icb, struct long_ad *dirdata_icb,
374 1.1 reinoud uint8_t *dirdata, uint32_t dirdata_size)
375 1.1 reinoud {
376 1.1 reinoud struct fileid_desc *fid;
377 1.1 reinoud struct long_ad *icb;
378 1.1 reinoud uint32_t fidsize, offset;
379 1.1 reinoud uint32_t location;
380 1.1 reinoud
381 1.1 reinoud if (udf_datablocks(dirdata_size) == 0) {
382 1.1 reinoud /* going internal */
383 1.1 reinoud icb = dir_icb;
384 1.1 reinoud } else {
385 1.1 reinoud /* external blocks to write to */
386 1.1 reinoud icb = dirdata_icb;
387 1.1 reinoud }
388 1.1 reinoud
389 1.1 reinoud for (offset = 0; offset < dirdata_size; offset += fidsize) {
390 1.1 reinoud /* for each FID: */
391 1.1 reinoud fid = (struct fileid_desc *) (dirdata + offset);
392 1.1 reinoud assert(udf_rw16(fid->tag.id) == TAGID_FID);
393 1.1 reinoud
394 1.1 reinoud location = udf_rw32(icb->loc.lb_num);
395 1.1 reinoud location += offset / context.sector_size;
396 1.1 reinoud
397 1.1 reinoud fid->tag.tag_loc = udf_rw32(location);
398 1.1 reinoud udf_validate_tag_and_crc_sums((union dscrptr *) fid);
399 1.1 reinoud
400 1.1 reinoud fidsize = udf_fidsize(fid);
401 1.1 reinoud }
402 1.1 reinoud }
403 1.1 reinoud
404 1.23 reinoud
405 1.1 reinoud static int
406 1.11 reinoud udf_file_inject_blob(union dscrptr *dscr, uint8_t *blob, off_t size)
407 1.1 reinoud {
408 1.1 reinoud struct icb_tag *icb;
409 1.1 reinoud struct file_entry *fe;
410 1.1 reinoud struct extfile_entry *efe;
411 1.1 reinoud uint64_t inf_len, obj_size;
412 1.1 reinoud uint32_t l_ea, l_ad;
413 1.1 reinoud uint16_t crclen;
414 1.1 reinoud uint8_t *data, *pos;
415 1.1 reinoud
416 1.1 reinoud fe = NULL;
417 1.1 reinoud efe = NULL;
418 1.1 reinoud if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
419 1.1 reinoud fe = &dscr->fe;
420 1.1 reinoud data = fe->data;
421 1.1 reinoud l_ea = udf_rw32(fe->l_ea);
422 1.1 reinoud l_ad = udf_rw32(fe->l_ad);
423 1.1 reinoud icb = &fe->icbtag;
424 1.1 reinoud inf_len = udf_rw64(fe->inf_len);
425 1.1 reinoud obj_size = 0;
426 1.1 reinoud } else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
427 1.1 reinoud efe = &dscr->efe;
428 1.1 reinoud data = efe->data;
429 1.1 reinoud l_ea = udf_rw32(efe->l_ea);
430 1.1 reinoud l_ad = udf_rw32(efe->l_ad);
431 1.1 reinoud icb = &efe->icbtag;
432 1.1 reinoud inf_len = udf_rw64(efe->inf_len);
433 1.1 reinoud obj_size = udf_rw64(efe->obj_size);
434 1.1 reinoud } else {
435 1.23 reinoud errx(1, "bad tag passed to udf_file_inject_blob");
436 1.1 reinoud }
437 1.1 reinoud crclen = udf_rw16(dscr->tag.desc_crc_len);
438 1.1 reinoud
439 1.23 reinoud /* check if we can go internal */
440 1.23 reinoud if ((udf_rw16(icb->flags) & UDF_ICB_TAG_FLAGS_ALLOC_MASK) !=
441 1.23 reinoud UDF_ICB_INTERN_ALLOC)
442 1.23 reinoud return 1;
443 1.23 reinoud
444 1.20 reinoud /* check if it will fit internally */
445 1.1 reinoud if (udf_datablocks(size)) {
446 1.20 reinoud /* the predictor tells it won't fit internally */
447 1.1 reinoud return 1;
448 1.1 reinoud }
449 1.1 reinoud
450 1.1 reinoud /* going internal */
451 1.6 reinoud assert((udf_rw16(icb->flags) & UDF_ICB_TAG_FLAGS_ALLOC_MASK) ==
452 1.6 reinoud UDF_ICB_INTERN_ALLOC);
453 1.23 reinoud assert(l_ad == 0);
454 1.1 reinoud
455 1.1 reinoud pos = data + l_ea + l_ad;
456 1.1 reinoud memcpy(pos, blob, size);
457 1.1 reinoud l_ad += size;
458 1.1 reinoud crclen += size;
459 1.1 reinoud
460 1.1 reinoud inf_len += size;
461 1.1 reinoud obj_size += size;
462 1.1 reinoud
463 1.1 reinoud if (fe) {
464 1.1 reinoud fe->l_ad = udf_rw32(l_ad);
465 1.1 reinoud fe->inf_len = udf_rw64(inf_len);
466 1.1 reinoud } else if (efe) {
467 1.1 reinoud efe->l_ad = udf_rw32(l_ad);
468 1.1 reinoud efe->inf_len = udf_rw64(inf_len);
469 1.1 reinoud efe->obj_size = udf_rw64(inf_len);
470 1.1 reinoud }
471 1.1 reinoud
472 1.1 reinoud /* make sure the header sums stays correct */
473 1.1 reinoud dscr->tag.desc_crc_len = udf_rw16(crclen);
474 1.1 reinoud udf_validate_tag_and_crc_sums(dscr);
475 1.1 reinoud
476 1.29 reinoud (void) obj_size;
477 1.1 reinoud return 0;
478 1.1 reinoud }
479 1.1 reinoud
480 1.1 reinoud
481 1.1 reinoud /* XXX no sparse file support */
482 1.1 reinoud static void
483 1.1 reinoud udf_append_file_mapping(union dscrptr *dscr, struct long_ad *piece)
484 1.1 reinoud {
485 1.1 reinoud struct icb_tag *icb;
486 1.1 reinoud struct file_entry *fe;
487 1.1 reinoud struct extfile_entry *efe;
488 1.1 reinoud struct long_ad *last_long, last_piece;
489 1.1 reinoud struct short_ad *last_short, new_short;
490 1.1 reinoud uint64_t inf_len, obj_size, logblks_rec;
491 1.1 reinoud uint32_t l_ea, l_ad, size;
492 1.1 reinoud uint32_t last_lb_num, piece_lb_num;
493 1.12 reinoud uint64_t last_len, piece_len, last_flags;
494 1.12 reinoud uint64_t rest_len, merge_len, last_end;
495 1.1 reinoud uint16_t last_part_num, piece_part_num;
496 1.1 reinoud uint16_t crclen, cur_alloc;
497 1.1 reinoud uint8_t *data, *pos;
498 1.1 reinoud const int short_len = sizeof(struct short_ad);
499 1.1 reinoud const int long_len = sizeof(struct long_ad);
500 1.1 reinoud const int sector_size = context.sector_size;
501 1.1 reinoud uint64_t max_len = UDF_ROUNDDOWN(UDF_EXT_MAXLEN, sector_size);
502 1.23 reinoud int use_shorts;
503 1.1 reinoud
504 1.11 reinoud fe = NULL;
505 1.11 reinoud efe = NULL;
506 1.1 reinoud if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
507 1.1 reinoud fe = &dscr->fe;
508 1.11 reinoud data = fe->data;
509 1.26 reinoud l_ea = udf_rw32(fe->l_ea);
510 1.1 reinoud l_ad = udf_rw32(fe->l_ad);
511 1.1 reinoud icb = &fe->icbtag;
512 1.1 reinoud inf_len = udf_rw64(fe->inf_len);
513 1.1 reinoud logblks_rec = udf_rw64(fe->logblks_rec);
514 1.1 reinoud obj_size = 0;
515 1.1 reinoud } else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
516 1.1 reinoud efe = &dscr->efe;
517 1.1 reinoud data = efe->data;
518 1.26 reinoud l_ea = udf_rw32(efe->l_ea);
519 1.1 reinoud l_ad = udf_rw32(efe->l_ad);
520 1.1 reinoud icb = &efe->icbtag;
521 1.1 reinoud inf_len = udf_rw64(efe->inf_len);
522 1.1 reinoud obj_size = udf_rw64(efe->obj_size);
523 1.1 reinoud logblks_rec = udf_rw64(efe->logblks_rec);
524 1.1 reinoud } else {
525 1.23 reinoud errx(1, "bad tag passed to udf_file_append_blob");
526 1.1 reinoud }
527 1.1 reinoud crclen = udf_rw16(dscr->tag.desc_crc_len);
528 1.1 reinoud
529 1.23 reinoud /* we use shorts if referring inside the metadata partition */
530 1.23 reinoud use_shorts = (udf_rw16(piece->loc.part_num) == context.metadata_part);
531 1.23 reinoud
532 1.1 reinoud pos = data + l_ea;
533 1.1 reinoud cur_alloc = udf_rw16(icb->flags);
534 1.1 reinoud size = UDF_EXT_LEN(udf_rw32(piece->len));
535 1.1 reinoud
536 1.1 reinoud /* extract last entry as a long_ad */
537 1.2 reinoud memset(&last_piece, 0, sizeof(last_piece));
538 1.2 reinoud last_len = 0;
539 1.2 reinoud last_lb_num = 0;
540 1.2 reinoud last_part_num = 0;
541 1.11 reinoud last_flags = 0;
542 1.11 reinoud last_short = NULL;
543 1.11 reinoud last_long = NULL;
544 1.1 reinoud if (l_ad != 0) {
545 1.1 reinoud if (use_shorts) {
546 1.1 reinoud assert(cur_alloc == UDF_ICB_SHORT_ALLOC);
547 1.1 reinoud pos += l_ad - short_len;
548 1.1 reinoud last_short = (struct short_ad *) pos;
549 1.1 reinoud last_lb_num = udf_rw32(last_short->lb_num);
550 1.1 reinoud last_part_num = udf_rw16(piece->loc.part_num);
551 1.1 reinoud last_len = UDF_EXT_LEN(udf_rw32(last_short->len));
552 1.1 reinoud last_flags = UDF_EXT_FLAGS(udf_rw32(last_short->len));
553 1.1 reinoud } else {
554 1.1 reinoud assert(cur_alloc == UDF_ICB_LONG_ALLOC);
555 1.1 reinoud pos += l_ad - long_len;
556 1.1 reinoud last_long = (struct long_ad *) pos;
557 1.1 reinoud last_lb_num = udf_rw32(last_long->loc.lb_num);
558 1.1 reinoud last_part_num = udf_rw16(last_long->loc.part_num);
559 1.1 reinoud last_len = UDF_EXT_LEN(udf_rw32(last_long->len));
560 1.1 reinoud last_flags = UDF_EXT_FLAGS(udf_rw32(last_long->len));
561 1.1 reinoud }
562 1.1 reinoud }
563 1.1 reinoud
564 1.1 reinoud piece_len = UDF_EXT_LEN(udf_rw32(piece->len));
565 1.1 reinoud piece_lb_num = udf_rw32(piece->loc.lb_num);
566 1.1 reinoud piece_part_num = udf_rw16(piece->loc.part_num);
567 1.1 reinoud
568 1.1 reinoud /* try merging */
569 1.1 reinoud rest_len = max_len - last_len;
570 1.1 reinoud
571 1.12 reinoud merge_len = MIN(piece_len, rest_len);
572 1.1 reinoud last_end = last_lb_num + (last_len / sector_size);
573 1.1 reinoud if ((piece_lb_num == last_end) && (last_part_num == piece_part_num)) {
574 1.1 reinoud /* we can merge */
575 1.1 reinoud last_len += merge_len;
576 1.1 reinoud piece_len -= merge_len;
577 1.1 reinoud
578 1.1 reinoud /* write back merge result */
579 1.1 reinoud if (use_shorts) {
580 1.1 reinoud last_short->len = udf_rw32(last_len | last_flags);
581 1.1 reinoud } else {
582 1.1 reinoud last_long->len = udf_rw32(last_len | last_flags);
583 1.1 reinoud }
584 1.1 reinoud piece_lb_num += merge_len / sector_size;
585 1.1 reinoud }
586 1.1 reinoud
587 1.1 reinoud if (piece_len) {
588 1.1 reinoud /* append new entry */
589 1.1 reinoud pos = data + l_ea + l_ad;
590 1.1 reinoud if (use_shorts) {
591 1.1 reinoud icb->flags = udf_rw16(UDF_ICB_SHORT_ALLOC);
592 1.1 reinoud memset(&new_short, 0, short_len);
593 1.1 reinoud new_short.len = udf_rw32(piece_len);
594 1.1 reinoud new_short.lb_num = udf_rw32(piece_lb_num);
595 1.1 reinoud memcpy(pos, &new_short, short_len);
596 1.1 reinoud l_ad += short_len;
597 1.1 reinoud crclen += short_len;
598 1.1 reinoud } else {
599 1.1 reinoud icb->flags = udf_rw16(UDF_ICB_LONG_ALLOC);
600 1.1 reinoud piece->len = udf_rw32(piece_len);
601 1.1 reinoud piece->loc.lb_num = udf_rw32(piece_lb_num);
602 1.1 reinoud memcpy(pos, piece, long_len);
603 1.1 reinoud l_ad += long_len;
604 1.1 reinoud crclen += long_len;
605 1.1 reinoud }
606 1.1 reinoud }
607 1.1 reinoud piece->len = udf_rw32(0);
608 1.1 reinoud
609 1.1 reinoud inf_len += size;
610 1.1 reinoud obj_size += size;
611 1.1 reinoud logblks_rec += UDF_ROUNDUP(size, sector_size) / sector_size;
612 1.1 reinoud
613 1.1 reinoud dscr->tag.desc_crc_len = udf_rw16(crclen);
614 1.1 reinoud if (udf_rw16(dscr->tag.id) == TAGID_FENTRY) {
615 1.1 reinoud fe->l_ad = udf_rw32(l_ad);
616 1.1 reinoud fe->inf_len = udf_rw64(inf_len);
617 1.1 reinoud fe->logblks_rec = udf_rw64(logblks_rec);
618 1.1 reinoud } else if (udf_rw16(dscr->tag.id) == TAGID_EXTFENTRY) {
619 1.1 reinoud efe->l_ad = udf_rw32(l_ad);
620 1.1 reinoud efe->inf_len = udf_rw64(inf_len);
621 1.29 reinoud efe->obj_size = udf_rw64(obj_size);
622 1.1 reinoud efe->logblks_rec = udf_rw64(logblks_rec);
623 1.1 reinoud }
624 1.1 reinoud }
625 1.1 reinoud
626 1.1 reinoud
627 1.1 reinoud static int
628 1.1 reinoud udf_append_file_contents(union dscrptr *dscr, struct long_ad *data_icb,
629 1.11 reinoud uint8_t *fdata, off_t flen)
630 1.1 reinoud {
631 1.1 reinoud struct long_ad icb;
632 1.1 reinoud uint32_t location;
633 1.1 reinoud uint16_t vpart;
634 1.23 reinoud int sectors;
635 1.1 reinoud
636 1.1 reinoud if (udf_file_inject_blob(dscr, fdata, flen) == 0)
637 1.1 reinoud return 0;
638 1.1 reinoud
639 1.1 reinoud /* has to be appended in mappings */
640 1.1 reinoud icb = *data_icb;
641 1.1 reinoud icb.len = udf_rw32(flen);
642 1.1 reinoud while (udf_rw32(icb.len) > 0)
643 1.1 reinoud udf_append_file_mapping(dscr, &icb);
644 1.1 reinoud udf_validate_tag_and_crc_sums(dscr);
645 1.1 reinoud
646 1.1 reinoud /* write out data piece */
647 1.1 reinoud vpart = udf_rw16(data_icb->loc.part_num);
648 1.1 reinoud location = udf_rw32(data_icb->loc.lb_num);
649 1.23 reinoud sectors = udf_datablocks(flen);
650 1.23 reinoud
651 1.23 reinoud return udf_write_virt(fdata, location, vpart, sectors);
652 1.1 reinoud }
653 1.1 reinoud
654 1.1 reinoud
655 1.1 reinoud static int
656 1.1 reinoud udf_create_new_file(struct stat *st, union dscrptr **dscr,
657 1.1 reinoud int filetype, struct long_ad *icb)
658 1.1 reinoud {
659 1.1 reinoud struct file_entry *fe;
660 1.1 reinoud struct extfile_entry *efe;
661 1.1 reinoud int error;
662 1.1 reinoud
663 1.1 reinoud fe = NULL;
664 1.1 reinoud efe = NULL;
665 1.1 reinoud if (context.dscrver == 2) {
666 1.1 reinoud error = udf_create_new_fe(&fe, filetype, st);
667 1.1 reinoud if (error)
668 1.1 reinoud errx(error, "can't create fe");
669 1.1 reinoud *dscr = (union dscrptr *) fe;
670 1.28 reinoud icb->longad_uniqueid = udf_rw32(udf_rw64(fe->unique_id));
671 1.1 reinoud } else {
672 1.1 reinoud error = udf_create_new_efe(&efe, filetype, st);
673 1.1 reinoud if (error)
674 1.1 reinoud errx(error, "can't create fe");
675 1.1 reinoud *dscr = (union dscrptr *) efe;
676 1.28 reinoud icb->longad_uniqueid = udf_rw32(udf_rw64(efe->unique_id));
677 1.1 reinoud }
678 1.1 reinoud
679 1.1 reinoud return 0;
680 1.1 reinoud }
681 1.1 reinoud
682 1.1 reinoud
683 1.1 reinoud static void
684 1.1 reinoud udf_estimate_walk(fsinfo_t *fsopts,
685 1.1 reinoud fsnode *root, char *dir, struct udf_stats *stats)
686 1.1 reinoud {
687 1.1 reinoud struct fileid_desc *fid;
688 1.16 joerg struct long_ad dummy_ref;
689 1.1 reinoud fsnode *cur;
690 1.1 reinoud fsinode *fnode;
691 1.1 reinoud size_t pathlen = strlen(dir);
692 1.1 reinoud char *mydir = dir + pathlen;
693 1.1 reinoud off_t sz;
694 1.1 reinoud uint32_t nblk, ddoff;
695 1.1 reinoud uint32_t softlink_len;
696 1.1 reinoud uint8_t *softlink_buf;
697 1.1 reinoud int nentries;
698 1.1 reinoud int error;
699 1.1 reinoud
700 1.1 reinoud stats->ndirs++;
701 1.1 reinoud
702 1.1 reinoud /*
703 1.1 reinoud * Count number of directory entries and count directory size; needed
704 1.1 reinoud * for the reservation of enough space for the directory. Pity we
705 1.1 reinoud * don't keep the FIDs we created. If it turns out to be a issue we
706 1.1 reinoud * can cache it later.
707 1.1 reinoud */
708 1.1 reinoud fid = (struct fileid_desc *) malloc(context.sector_size);
709 1.1 reinoud assert(fid);
710 1.1 reinoud
711 1.1 reinoud ddoff = 40; /* '..' entry */
712 1.1 reinoud for (cur = root, nentries = 0; cur != NULL; cur = cur->next) {
713 1.1 reinoud switch (cur->type & S_IFMT) {
714 1.1 reinoud default:
715 1.1 reinoud /* what kind of nodes? */
716 1.1 reinoud break;
717 1.1 reinoud case S_IFCHR:
718 1.1 reinoud case S_IFBLK:
719 1.1 reinoud /* not supported yet */
720 1.23 reinoud break;
721 1.1 reinoud case S_IFDIR:
722 1.1 reinoud if (strcmp(cur->name, ".") == 0)
723 1.1 reinoud continue;
724 1.19 mrg /* FALLTHROUGH */
725 1.1 reinoud case S_IFLNK:
726 1.1 reinoud case S_IFREG:
727 1.1 reinoud /* create dummy FID to see how long name will become */
728 1.16 joerg memset(&dummy_ref, 0, sizeof(dummy_ref));
729 1.1 reinoud udf_create_fid(ddoff, fid, cur->name, 0, &dummy_ref);
730 1.1 reinoud nentries++;
731 1.1 reinoud ddoff += udf_fidsize(fid);
732 1.23 reinoud break;
733 1.1 reinoud }
734 1.1 reinoud }
735 1.1 reinoud sz = ddoff;
736 1.1 reinoud
737 1.1 reinoud root->inode->st.st_size = sz; /* max now */
738 1.1 reinoud root->inode->flags |= FI_SIZED;
739 1.1 reinoud
740 1.1 reinoud nblk = udf_datablocks(sz);
741 1.1 reinoud stats->nmetadatablocks += nblk;
742 1.1 reinoud
743 1.1 reinoud /* for each entry in the directory, there needs to be a (E)FE */
744 1.1 reinoud stats->nmetadatablocks += nentries + 1;
745 1.1 reinoud
746 1.1 reinoud /* recurse */
747 1.1 reinoud for (cur = root; cur != NULL; cur = cur->next) {
748 1.1 reinoud switch (cur->type & S_IFMT) {
749 1.1 reinoud default:
750 1.1 reinoud /* what kind of nodes? */
751 1.1 reinoud break;
752 1.1 reinoud case S_IFDIR:
753 1.1 reinoud if (strcmp(cur->name, ".") == 0)
754 1.1 reinoud continue;
755 1.1 reinoud /* empty dir? */
756 1.1 reinoud if (!cur->child)
757 1.1 reinoud break;
758 1.1 reinoud mydir[0] = '/';
759 1.1 reinoud strncpy(&mydir[1], cur->name, MAXPATHLEN - pathlen);
760 1.1 reinoud udf_estimate_walk(fsopts, cur->child, dir, stats);
761 1.1 reinoud mydir[0] = '\0';
762 1.1 reinoud break;
763 1.23 reinoud case S_IFCHR:
764 1.23 reinoud case S_IFBLK:
765 1.23 reinoud /* not supported yet */
766 1.23 reinoud // stats->nfiles++;
767 1.23 reinoud break;
768 1.1 reinoud case S_IFREG:
769 1.1 reinoud fnode = cur->inode;
770 1.1 reinoud /* don't double-count hard-links */
771 1.1 reinoud if (!(fnode->flags & FI_SIZED)) {
772 1.1 reinoud sz = fnode->st.st_size;
773 1.1 reinoud nblk = udf_datablocks(sz);
774 1.1 reinoud stats->ndatablocks += nblk;
775 1.1 reinoud /* ... */
776 1.1 reinoud fnode->flags |= FI_SIZED;
777 1.1 reinoud }
778 1.1 reinoud stats->nfiles++;
779 1.1 reinoud break;
780 1.1 reinoud case S_IFLNK:
781 1.1 reinoud /* softlink */
782 1.1 reinoud fnode = cur->inode;
783 1.1 reinoud /* don't double-count hard-links */
784 1.1 reinoud if (!(fnode->flags & FI_SIZED)) {
785 1.1 reinoud error = udf_encode_symlink(&softlink_buf,
786 1.1 reinoud &softlink_len, cur->symlink);
787 1.1 reinoud if (error) {
788 1.1 reinoud printf("SOFTLINK error %d\n", error);
789 1.1 reinoud break;
790 1.1 reinoud }
791 1.1 reinoud nblk = udf_datablocks(softlink_len);
792 1.1 reinoud stats->ndatablocks += nblk;
793 1.1 reinoud fnode->flags |= FI_SIZED;
794 1.1 reinoud
795 1.1 reinoud free(softlink_buf);
796 1.1 reinoud }
797 1.1 reinoud stats->nfiles++;
798 1.1 reinoud break;
799 1.1 reinoud }
800 1.1 reinoud }
801 1.1 reinoud }
802 1.1 reinoud
803 1.1 reinoud
804 1.1 reinoud #define UDF_MAX_CHUNK_SIZE (4*1024*1024)
805 1.1 reinoud static int
806 1.1 reinoud udf_copy_file(struct stat *st, char *path, fsnode *cur, struct fileid_desc *fid,
807 1.1 reinoud struct long_ad *icb)
808 1.1 reinoud {
809 1.1 reinoud union dscrptr *dscr;
810 1.1 reinoud struct long_ad data_icb;
811 1.1 reinoud fsinode *fnode;
812 1.11 reinoud off_t sz, chunk, rd;
813 1.1 reinoud uint8_t *data;
814 1.23 reinoud bool intern;
815 1.1 reinoud int nblk;
816 1.23 reinoud int f;
817 1.5 reinoud int error;
818 1.1 reinoud
819 1.1 reinoud fnode = cur->inode;
820 1.1 reinoud
821 1.3 joerg f = open(path, O_RDONLY);
822 1.1 reinoud if (f < 0) {
823 1.1 reinoud warn("Can't open file %s for reading", cur->name);
824 1.1 reinoud return errno;
825 1.1 reinoud }
826 1.1 reinoud
827 1.1 reinoud /* claim disc space for the (e)fe descriptor for this file */
828 1.1 reinoud udf_metadata_alloc(1, icb);
829 1.1 reinoud udf_create_new_file(st, &dscr, UDF_ICB_FILETYPE_RANDOMACCESS, icb);
830 1.1 reinoud
831 1.1 reinoud sz = fnode->st.st_size;
832 1.1 reinoud
833 1.1 reinoud chunk = MIN(sz, UDF_MAX_CHUNK_SIZE);
834 1.1 reinoud data = malloc(MAX(chunk, context.sector_size));
835 1.1 reinoud assert(data);
836 1.1 reinoud
837 1.23 reinoud intern = (udf_datablocks(chunk) == 0);
838 1.5 reinoud error = 0;
839 1.1 reinoud while (chunk) {
840 1.1 reinoud rd = read(f, data, chunk);
841 1.1 reinoud if (rd != chunk) {
842 1.17 christos warn("Short read of file %s", cur->name);
843 1.5 reinoud error = errno;
844 1.5 reinoud break;
845 1.1 reinoud }
846 1.1 reinoud
847 1.23 reinoud nblk = UDF_ROUNDUP(chunk, context.sector_size) / context.sector_size;
848 1.23 reinoud if (chunk && !intern)
849 1.1 reinoud udf_data_alloc(nblk, &data_icb);
850 1.1 reinoud udf_append_file_contents(dscr, &data_icb, data, chunk);
851 1.1 reinoud
852 1.1 reinoud sz -= chunk;
853 1.1 reinoud chunk = MIN(sz, UDF_MAX_CHUNK_SIZE);
854 1.1 reinoud }
855 1.1 reinoud close(f);
856 1.1 reinoud free(data);
857 1.1 reinoud
858 1.1 reinoud /* write out dscr (e)fe */
859 1.1 reinoud udf_set_link_cnt(dscr, fnode->nlink);
860 1.1 reinoud udf_write_dscr_virt(dscr, udf_rw32(icb->loc.lb_num),
861 1.1 reinoud udf_rw16(icb->loc.part_num), 1);
862 1.5 reinoud free(dscr);
863 1.1 reinoud
864 1.1 reinoud /* remember our location for hardlinks */
865 1.1 reinoud cur->inode->fsuse = malloc(sizeof(struct long_ad));
866 1.1 reinoud memcpy(cur->inode->fsuse, icb, sizeof(struct long_ad));
867 1.1 reinoud
868 1.5 reinoud return error;
869 1.1 reinoud }
870 1.1 reinoud
871 1.1 reinoud
872 1.1 reinoud static int
873 1.1 reinoud udf_populate_walk(fsinfo_t *fsopts, fsnode *root, char *dir,
874 1.1 reinoud struct long_ad *parent_icb, struct long_ad *dir_icb)
875 1.1 reinoud {
876 1.1 reinoud union dscrptr *dir_dscr, *dscr;
877 1.1 reinoud struct fileid_desc *fid;
878 1.1 reinoud struct long_ad icb, data_icb, dirdata_icb;
879 1.1 reinoud fsnode *cur;
880 1.1 reinoud fsinode *fnode;
881 1.1 reinoud size_t pathlen = strlen(dir);
882 1.1 reinoud size_t dirlen;
883 1.1 reinoud char *mydir = dir + pathlen;
884 1.1 reinoud uint32_t nblk, ddoff;
885 1.1 reinoud uint32_t softlink_len;
886 1.1 reinoud uint8_t *softlink_buf;
887 1.1 reinoud uint8_t *dirdata;
888 1.1 reinoud int error, ret, retval;
889 1.1 reinoud
890 1.1 reinoud /* claim disc space for the (e)fe descriptor for this dir */
891 1.1 reinoud udf_metadata_alloc(1, dir_icb);
892 1.1 reinoud
893 1.1 reinoud /* create new e(fe) */
894 1.1 reinoud udf_create_new_file(&root->inode->st, &dir_dscr,
895 1.1 reinoud UDF_ICB_FILETYPE_DIRECTORY, dir_icb);
896 1.1 reinoud
897 1.23 reinoud /* allocate memory for the directory contents */
898 1.1 reinoud dirlen = root->inode->st.st_size;
899 1.23 reinoud nblk = UDF_ROUNDUP(dirlen, context.sector_size) / context.sector_size;
900 1.1 reinoud dirdata = malloc(nblk * context.sector_size);
901 1.1 reinoud assert(dirdata);
902 1.1 reinoud memset(dirdata, 0, nblk * context.sector_size);
903 1.1 reinoud
904 1.1 reinoud /* create and append '..' */
905 1.1 reinoud fid = (struct fileid_desc *) dirdata;
906 1.1 reinoud ddoff = udf_create_parentfid(fid, parent_icb);
907 1.23 reinoud assert(ddoff == 40);
908 1.1 reinoud
909 1.1 reinoud /* for '..' */
910 1.1 reinoud udf_inc_link(dir_dscr);
911 1.1 reinoud
912 1.1 reinoud /* recurse */
913 1.1 reinoud retval = 0;
914 1.1 reinoud for (cur = root; cur != NULL; cur = cur->next) {
915 1.1 reinoud mydir[0] = '/';
916 1.1 reinoud strncpy(&mydir[1], cur->name, MAXPATHLEN - pathlen);
917 1.1 reinoud
918 1.1 reinoud fid = (struct fileid_desc *) (dirdata + ddoff);
919 1.1 reinoud switch (cur->type & S_IFMT) {
920 1.1 reinoud default:
921 1.1 reinoud /* what kind of nodes? */
922 1.1 reinoud retval = 2;
923 1.1 reinoud break;
924 1.1 reinoud case S_IFCHR:
925 1.1 reinoud case S_IFBLK:
926 1.1 reinoud /* not supported */
927 1.1 reinoud retval = 2;
928 1.1 reinoud warnx("device node %s not supported", dir);
929 1.1 reinoud break;
930 1.1 reinoud case S_IFDIR:
931 1.1 reinoud /* not an empty dir? */
932 1.1 reinoud if (strcmp(cur->name, ".") == 0)
933 1.1 reinoud break;
934 1.1 reinoud assert(cur->child);
935 1.1 reinoud if (cur->child) {
936 1.1 reinoud ret = udf_populate_walk(fsopts, cur->child,
937 1.1 reinoud dir, dir_icb, &icb);
938 1.1 reinoud if (ret)
939 1.1 reinoud retval = 2;
940 1.1 reinoud }
941 1.1 reinoud udf_create_fid(ddoff, fid, cur->name,
942 1.1 reinoud UDF_FILE_CHAR_DIR, &icb);
943 1.1 reinoud udf_inc_link(dir_dscr);
944 1.1 reinoud ddoff += udf_fidsize(fid);
945 1.1 reinoud break;
946 1.1 reinoud case S_IFREG:
947 1.1 reinoud fnode = cur->inode;
948 1.1 reinoud /* don't re-copy hard-links */
949 1.1 reinoud if (!(fnode->flags & FI_WRITTEN)) {
950 1.23 reinoud printf("%s\n", dir);
951 1.1 reinoud error = udf_copy_file(&fnode->st, dir, cur,
952 1.1 reinoud fid, &icb);
953 1.1 reinoud if (!error) {
954 1.1 reinoud fnode->flags |= FI_WRITTEN;
955 1.1 reinoud udf_create_fid(ddoff, fid, cur->name,
956 1.1 reinoud 0, &icb);
957 1.1 reinoud ddoff += udf_fidsize(fid);
958 1.1 reinoud } else {
959 1.1 reinoud retval = 2;
960 1.1 reinoud }
961 1.1 reinoud } else {
962 1.1 reinoud /* hardlink! */
963 1.1 reinoud printf("%s (hardlink)\n", dir);
964 1.1 reinoud udf_create_fid(ddoff, fid, cur->name,
965 1.1 reinoud 0, (struct long_ad *) (fnode->fsuse));
966 1.1 reinoud ddoff += udf_fidsize(fid);
967 1.1 reinoud }
968 1.1 reinoud fnode->nlink--;
969 1.1 reinoud if (fnode->nlink == 0)
970 1.1 reinoud free(fnode->fsuse);
971 1.1 reinoud break;
972 1.1 reinoud case S_IFLNK:
973 1.1 reinoud /* softlink */
974 1.1 reinoud fnode = cur->inode;
975 1.1 reinoud printf("%s -> %s\n", dir, cur->symlink);
976 1.1 reinoud error = udf_encode_symlink(&softlink_buf,
977 1.1 reinoud &softlink_len, cur->symlink);
978 1.1 reinoud if (error) {
979 1.1 reinoud printf("SOFTLINK error %d\n", error);
980 1.1 reinoud retval = 2;
981 1.1 reinoud break;
982 1.1 reinoud }
983 1.1 reinoud
984 1.1 reinoud udf_metadata_alloc(1, &icb);
985 1.1 reinoud udf_create_new_file(&fnode->st, &dscr,
986 1.1 reinoud UDF_ICB_FILETYPE_SYMLINK, &icb);
987 1.1 reinoud
988 1.1 reinoud nblk = udf_datablocks(softlink_len);
989 1.1 reinoud if (nblk > 0)
990 1.1 reinoud udf_data_alloc(nblk, &data_icb);
991 1.1 reinoud udf_append_file_contents(dscr, &data_icb,
992 1.1 reinoud softlink_buf, softlink_len);
993 1.1 reinoud
994 1.1 reinoud /* write out dscr (e)fe */
995 1.1 reinoud udf_inc_link(dscr);
996 1.1 reinoud udf_write_dscr_virt(dscr, udf_rw32(icb.loc.lb_num),
997 1.1 reinoud udf_rw16(icb.loc.part_num), 1);
998 1.1 reinoud
999 1.5 reinoud free(dscr);
1000 1.1 reinoud free(softlink_buf);
1001 1.1 reinoud
1002 1.1 reinoud udf_create_fid(ddoff, fid, cur->name, 0, &icb);
1003 1.1 reinoud ddoff += udf_fidsize(fid);
1004 1.1 reinoud break;
1005 1.1 reinoud }
1006 1.1 reinoud mydir[0] = '\0';
1007 1.1 reinoud }
1008 1.23 reinoud assert(dirlen == ddoff);
1009 1.23 reinoud
1010 1.23 reinoud /* pre allocate space for the directory contents */
1011 1.23 reinoud memset(&dirdata_icb, 0, sizeof(dirdata_icb));
1012 1.23 reinoud nblk = udf_datablocks(dirlen);
1013 1.1 reinoud
1014 1.23 reinoud /* claim disc space for the dir contents if needed */
1015 1.23 reinoud if (nblk > 0)
1016 1.23 reinoud udf_fids_alloc(nblk, &dirdata_icb);
1017 1.1 reinoud
1018 1.1 reinoud udf_prepare_fids(dir_icb, &dirdata_icb, dirdata, dirlen);
1019 1.1 reinoud udf_append_file_contents(dir_dscr, &dirdata_icb, dirdata, dirlen);
1020 1.1 reinoud
1021 1.1 reinoud /* write out dir_dscr (e)fe */
1022 1.1 reinoud udf_write_dscr_virt(dir_dscr, udf_rw32(dir_icb->loc.lb_num),
1023 1.1 reinoud udf_rw16(dir_icb->loc.part_num), 1);
1024 1.1 reinoud
1025 1.5 reinoud free(dirdata);
1026 1.5 reinoud free(dir_dscr);
1027 1.1 reinoud return retval;
1028 1.1 reinoud }
1029 1.1 reinoud
1030 1.1 reinoud
1031 1.1 reinoud static int
1032 1.1 reinoud udf_populate(const char *dir, fsnode *root, fsinfo_t *fsopts,
1033 1.1 reinoud struct udf_stats *stats)
1034 1.1 reinoud {
1035 1.1 reinoud struct long_ad rooticb;
1036 1.1 reinoud static char path[MAXPATHLEN+1];
1037 1.1 reinoud int error;
1038 1.1 reinoud
1039 1.1 reinoud strncpy(path, dir, sizeof(path));
1040 1.1 reinoud error = udf_populate_walk(fsopts, root, path, &rooticb, &rooticb);
1041 1.1 reinoud
1042 1.1 reinoud return error;
1043 1.1 reinoud }
1044 1.1 reinoud
1045 1.1 reinoud
1046 1.1 reinoud static void
1047 1.1 reinoud udf_enumerate_and_estimate(const char *dir, fsnode *root, fsinfo_t *fsopts,
1048 1.1 reinoud struct udf_stats *stats)
1049 1.1 reinoud {
1050 1.1 reinoud char path[MAXPATHLEN + 1];
1051 1.7 reinoud off_t proposed_size;
1052 1.23 reinoud uint32_t n, nblk, nmetablk, nbytes;
1053 1.23 reinoud uint32_t spareable_blocks, spareable_blockingnr;
1054 1.1 reinoud
1055 1.1 reinoud strncpy(path, dir, sizeof(path));
1056 1.1 reinoud
1057 1.1 reinoud /* calculate strict minimal size */
1058 1.1 reinoud udf_estimate_walk(fsopts, root, path, stats);
1059 1.23 reinoud #if 0
1060 1.5 reinoud printf("ndirs %d\n", stats->ndirs);
1061 1.5 reinoud printf("nfiles %d\n", stats->nfiles);
1062 1.5 reinoud printf("ndata_blocks %d\n", stats->ndatablocks);
1063 1.5 reinoud printf("nmetadata_blocks %d\n", stats->nmetadatablocks);
1064 1.5 reinoud printf("\n");
1065 1.23 reinoud #endif
1066 1.1 reinoud
1067 1.1 reinoud /* adjust for options : free file nodes */
1068 1.1 reinoud if (fsopts->freefiles) {
1069 1.1 reinoud /* be mercifull and reserve more for the FID */
1070 1.1 reinoud stats->nmetadatablocks += fsopts->freefiles * 1.5;
1071 1.1 reinoud } else if ((n = fsopts->freefilepc)) {
1072 1.1 reinoud stats->nmetadatablocks += (stats->nmetadatablocks*n) / (100-n);
1073 1.1 reinoud }
1074 1.1 reinoud
1075 1.1 reinoud /* adjust for options : free data blocks */
1076 1.1 reinoud if (fsopts->freeblocks) {
1077 1.1 reinoud stats->ndatablocks += fsopts->freeblocks;
1078 1.1 reinoud } else if ((n = fsopts->freeblockpc)) {
1079 1.1 reinoud stats->ndatablocks += (stats->ndatablocks * n) / (100-n);
1080 1.1 reinoud }
1081 1.1 reinoud
1082 1.1 reinoud /* rough predictor of minimum disc size */
1083 1.1 reinoud nblk = stats->ndatablocks + stats->nmetadatablocks;
1084 1.23 reinoud if (context.format_flags & FORMAT_META) {
1085 1.23 reinoud float meta_p;
1086 1.23 reinoud double factor;
1087 1.23 reinoud
1088 1.23 reinoud meta_p = (float) context.meta_perc/100.0;
1089 1.23 reinoud factor = meta_p / (1.0 - meta_p);
1090 1.23 reinoud
1091 1.23 reinoud /* add space for metadata partition including some slack */
1092 1.23 reinoud nmetablk = factor * nblk + 32;
1093 1.23 reinoud nblk = stats->ndatablocks + nmetablk;
1094 1.23 reinoud
1095 1.23 reinoud /* free space maps */
1096 1.23 reinoud nbytes = ceil((double) nblk * (1.0/8.0));
1097 1.23 reinoud nblk += 1 + (nbytes + context.sector_size-1)/context.sector_size;
1098 1.23 reinoud if (!(context.format_flags & FORMAT_READONLY)) {
1099 1.23 reinoud nbytes = ceil((double) nmetablk * (1.0/8.0));
1100 1.23 reinoud nblk += 1 + (nbytes + context.sector_size-1)/context.sector_size;
1101 1.23 reinoud }
1102 1.23 reinoud } else if (context.format_flags & FORMAT_SEQUENTIAL) {
1103 1.23 reinoud /* nothing */
1104 1.23 reinoud } else {
1105 1.23 reinoud if (!(context.format_flags & FORMAT_READONLY)) {
1106 1.23 reinoud nbytes = ceil((double) nblk * (1.0/8.0));
1107 1.23 reinoud nblk += 1 + (nbytes + context.sector_size-1)/
1108 1.23 reinoud context.sector_size;
1109 1.23 reinoud }
1110 1.23 reinoud }
1111 1.23 reinoud
1112 1.23 reinoud /*
1113 1.23 reinoud * Make extra room for spareable table if requested
1114 1.23 reinoud */
1115 1.23 reinoud if (context.format_flags & FORMAT_SPAREABLE) {
1116 1.23 reinoud spareable_blockingnr = udf_spareable_blockingnr();
1117 1.23 reinoud spareable_blocks = udf_spareable_blocks();
1118 1.23 reinoud
1119 1.23 reinoud nblk += spareable_blocks * spareable_blockingnr;
1120 1.23 reinoud nblk += spareable_blockingnr; /* slack */
1121 1.23 reinoud }
1122 1.23 reinoud
1123 1.1 reinoud nblk += 256; /* pre-volume space */
1124 1.1 reinoud nblk += 256; /* post-volume space */
1125 1.23 reinoud nblk += 1024; /* safeguard */
1126 1.1 reinoud
1127 1.1 reinoud /* try to honour minimum size */
1128 1.1 reinoud n = fsopts->minsize / fsopts->sectorsize;
1129 1.1 reinoud if (nblk < n) {
1130 1.1 reinoud stats->ndatablocks += (n - nblk);
1131 1.1 reinoud nblk += n - nblk;
1132 1.1 reinoud }
1133 1.30 reinoud
1134 1.30 reinoud /* keep proposed size a multiple of blockingnr for image creation */
1135 1.30 reinoud if (S_ISREG(dev_fd_stat.st_mode)) {
1136 1.30 reinoud struct mmc_trackinfo ti;
1137 1.30 reinoud int blockingnr;
1138 1.30 reinoud int error;
1139 1.30 reinoud
1140 1.30 reinoud /* adjust proposed size to be a multiple of the blockingnr */
1141 1.30 reinoud udf_update_discinfo();
1142 1.30 reinoud ti.tracknr = mmc_discinfo.first_track_last_session;
1143 1.30 reinoud error = udf_update_trackinfo(&ti);
1144 1.30 reinoud assert(!error);
1145 1.30 reinoud blockingnr = udf_get_blockingnr(&ti);
1146 1.30 reinoud nblk = UDF_ROUNDUP(nblk, blockingnr);
1147 1.30 reinoud }
1148 1.30 reinoud
1149 1.7 reinoud proposed_size = (off_t) nblk * fsopts->sectorsize;
1150 1.30 reinoud
1151 1.7 reinoud /* sanity size */
1152 1.7 reinoud if (proposed_size < 512*1024)
1153 1.7 reinoud proposed_size = 512*1024;
1154 1.1 reinoud
1155 1.7 reinoud if (fsopts->size) {
1156 1.23 reinoud if (fsopts->size < proposed_size)
1157 1.23 reinoud errx(1, "makefs_udf: won't fit on disc!");
1158 1.7 reinoud } else {
1159 1.7 reinoud fsopts->size = proposed_size;
1160 1.7 reinoud }
1161 1.1 reinoud
1162 1.1 reinoud fsopts->inodes = stats->nfiles + stats->ndirs;
1163 1.1 reinoud }
1164 1.1 reinoud
1165 1.1 reinoud
1166 1.1 reinoud void
1167 1.1 reinoud udf_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
1168 1.1 reinoud {
1169 1.1 reinoud struct udf_stats stats;
1170 1.1 reinoud uint64_t truncate_len;
1171 1.23 reinoud uint32_t last_sector, ext;
1172 1.9 reinoud char scrap[255];
1173 1.1 reinoud int error;
1174 1.1 reinoud
1175 1.23 reinoud /* setup */
1176 1.23 reinoud emul_sectorsize = fsopts->sectorsize;
1177 1.23 reinoud emul_size = 0;
1178 1.1 reinoud context.sector_size = fsopts->sectorsize;
1179 1.1 reinoud
1180 1.1 reinoud /* names */
1181 1.1 reinoud error = udf_proces_names();
1182 1.1 reinoud if (error)
1183 1.23 reinoud errx(1, "bad names given");
1184 1.23 reinoud
1185 1.23 reinoud /* open disc device or emulated file */
1186 1.23 reinoud if (udf_opendisc(image, O_CREAT)) {
1187 1.23 reinoud udf_closedisc();
1188 1.23 reinoud errx(1, "can't open %s", image);
1189 1.23 reinoud }
1190 1.23 reinoud fsopts->fd = dev_fd;
1191 1.1 reinoud
1192 1.23 reinoud /* determine format */
1193 1.23 reinoud if (udf_readonly_format())
1194 1.23 reinoud req_enable |= FORMAT_READONLY;
1195 1.23 reinoud // printf("req_enable %d, req_disable %d\n", req_enable, req_disable);
1196 1.23 reinoud error = udf_derive_format(req_enable, req_disable);
1197 1.23 reinoud if (error) {
1198 1.23 reinoud udf_closedisc();
1199 1.23 reinoud errx(1, "can't derive format from media/settings");
1200 1.23 reinoud }
1201 1.1 reinoud
1202 1.1 reinoud /* estimate the amount of space needed */
1203 1.2 reinoud memset(&stats, 0, sizeof(stats));
1204 1.1 reinoud udf_enumerate_and_estimate(dir, root, fsopts, &stats);
1205 1.1 reinoud
1206 1.23 reinoud printf("Calculated size of `%s' is "
1207 1.23 reinoud "%"PRIu64" KiB, %"PRIu64" MiB, %"PRIu64" GiB with %ld inodes\n",
1208 1.23 reinoud image,
1209 1.23 reinoud (uint64_t) fsopts->size/1024,
1210 1.23 reinoud (uint64_t) fsopts->size/1024/1024,
1211 1.23 reinoud (uint64_t) fsopts->size/1024/1024/1024,
1212 1.23 reinoud (long)fsopts->inodes);
1213 1.23 reinoud emul_size = MAX(emul_size, fsopts->size);
1214 1.23 reinoud if ((fsopts->maxsize > 0) && (emul_size > fsopts->maxsize))
1215 1.23 reinoud errx(1, "won't fit due to set maximum disk size");
1216 1.1 reinoud
1217 1.23 reinoud /* prepare disc if necessary (recordables mainly) */
1218 1.23 reinoud error = udf_prepare_disc();
1219 1.23 reinoud if (error) {
1220 1.23 reinoud udf_closedisc();
1221 1.23 reinoud errx(1, "preparing disc failed");
1222 1.1 reinoud }
1223 1.1 reinoud
1224 1.1 reinoud /* update mmc info but now with correct size */
1225 1.23 reinoud udf_update_discinfo();
1226 1.23 reinoud udf_dump_discinfo(&mmc_discinfo);
1227 1.1 reinoud
1228 1.9 reinoud printf("Building disc compatible with UDF version %x to %x\n\n",
1229 1.9 reinoud context.min_udf, context.max_udf);
1230 1.9 reinoud (void)snprintb(scrap, sizeof(scrap), FORMAT_FLAGBITS,
1231 1.23 reinoud (uint64_t) context.format_flags);
1232 1.9 reinoud printf("UDF properties %s\n", scrap);
1233 1.9 reinoud printf("Volume set `%s'\n", context.volset_name);
1234 1.9 reinoud printf("Primary volume `%s`\n", context.primary_name);
1235 1.9 reinoud printf("Logical volume `%s`\n", context.logvol_name);
1236 1.23 reinoud if (context.format_flags & FORMAT_META)
1237 1.23 reinoud printf("Metadata percentage %d%% (%d%% used)\n",
1238 1.23 reinoud context.meta_perc,
1239 1.27 reinoud (int) ceil(100.0*stats.nmetadatablocks/stats.ndatablocks));
1240 1.9 reinoud printf("\n");
1241 1.23 reinoud
1242 1.23 reinoud /* prefix */
1243 1.23 reinoud udf_allow_writing();
1244 1.23 reinoud if (udf_do_newfs_prefix()) {
1245 1.23 reinoud udf_closedisc();
1246 1.23 reinoud errx(1, "basic setup failed");
1247 1.23 reinoud }
1248 1.1 reinoud
1249 1.1 reinoud /* update context */
1250 1.1 reinoud context.unique_id = 0;
1251 1.1 reinoud
1252 1.23 reinoud /* add all directories */
1253 1.1 reinoud error = udf_populate(dir, root, fsopts, &stats);
1254 1.1 reinoud
1255 1.23 reinoud if (!error) {
1256 1.24 wiz /* update values for integrity sequence */
1257 1.23 reinoud context.num_files = stats.nfiles;
1258 1.23 reinoud context.num_directories = stats.ndirs;
1259 1.23 reinoud
1260 1.23 reinoud udf_do_newfs_postfix();
1261 1.23 reinoud
1262 1.23 reinoud if (S_ISREG(dev_fd_stat.st_mode) &&
1263 1.23 reinoud (context.format_flags & FORMAT_VAT)) {
1264 1.23 reinoud udf_translate_vtop(context.alloc_pos[context.data_part],
1265 1.23 reinoud context.data_part,
1266 1.23 reinoud &last_sector, &ext);
1267 1.23 reinoud truncate_len = (uint64_t) last_sector * context.sector_size;
1268 1.23 reinoud
1269 1.23 reinoud printf("\nTruncing the disc-image to allow for VAT\n");
1270 1.23 reinoud printf("Free space left on this volume approx. "
1271 1.23 reinoud "%"PRIu64" KiB, %"PRIu64" MiB\n",
1272 1.23 reinoud (fsopts->size - truncate_len)/1024,
1273 1.23 reinoud (fsopts->size - truncate_len)/1024/1024);
1274 1.23 reinoud ftruncate(dev_fd, truncate_len);
1275 1.23 reinoud }
1276 1.1 reinoud }
1277 1.23 reinoud udf_closedisc();
1278 1.1 reinoud
1279 1.25 riastrad if (error == 2)
1280 1.23 reinoud errx(error, "not all files could be added");
1281 1.23 reinoud if (error == 1)
1282 1.23 reinoud errx(error, "creation of %s failed", image);
1283 1.1 reinoud return;
1284 1.1 reinoud }
1285