1 /*	$NetBSD: iso9660_rrip.c,v 1.8 2009/01/10 22:06:29 bjh21 Exp $	*/
2 
3 /*
4  * Copyright (c) 2009, 2010
5  *	Thorsten Glaser <tg@mirbsd.org>
6  * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
7  * Perez-Rathke and Ram Vedam.  All rights reserved.
8  *
9  * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
10  * Alan Perez-Rathke and Ram Vedam.
11  *
12  * Redistribution and use in source and binary forms, with or
13  * without modification, are permitted provided that the following
14  * conditions are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above
18  *    copyright notice, this list of conditions and the following
19  *    disclaimer in the documentation and/or other materials provided
20  *    with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
23  * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
27  * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30  * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34  * OF SUCH DAMAGE.
35  */
36 /* This will hold all the function definitions
37  * defined in iso9660_rrip.h
38  */
39 
40 #include "makefs.h"
41 #include "cd9660.h"
42 #include "iso9660_rrip.h"
43 #include <sys/queue.h>
44 #include <stdio.h>
45 
46 #include <sys/cdefs.h>
47 #if defined(__RCSID) && !defined(__lint)
48 __IDSTRING(mbsdid, "$MirOS: src/usr.sbin/makefs/cd9660/iso9660_rrip.c,v 1.22 2010/03/06 21:29:07 tg Exp $");
49 __RCSID("$NetBSD: iso9660_rrip.c,v 1.8 2009/01/10 22:06:29 bjh21 Exp $");
50 #endif  /* !__lint */
51 
52 static void cd9660_rrip_initialize_inode(cd9660node *);
53 static int cd9660_susp_handle_continuation(cd9660node *);
54 static int cd9660_susp_handle_continuation_common(cd9660node *, int);
55 
56 int
cd9660_susp_initialize(cd9660node * node,cd9660node * parent,cd9660node * grandparent,int shortcut)57 cd9660_susp_initialize(cd9660node *node, cd9660node *parent,
58     cd9660node *grandparent, int shortcut)
59 {
60 	cd9660node *cn;
61 	int r;
62 
63 	/* Make sure the node is not NULL. If it is, there are major problems */
64 	assert(node != NULL);
65 
66 	if (shortcut == 2) {
67 		shortcut = 0;
68 		goto cd9660_susp_initialize_shortcut;
69 	}
70 
71 	if (!(node->type & CD9660_TYPE_DOT) &&
72 	    !(node->type & CD9660_TYPE_DOTDOT))
73 		TAILQ_INIT(&(node->head));
74 	if (node->dot_record != 0)
75 		TAILQ_INIT(&(node->dot_record->head));
76 	if (node->dot_dot_record != 0)
77 		TAILQ_INIT(&(node->dot_dot_record->head));
78 
79 	 /* SUSP specific entries here */
80 	if ((r = cd9660_susp_initialize_node(node)) < 0)
81 		return r;
82 
83 	/* currently called cd9660node_rrip_init_links */
84 	r = cd9660_rrip_initialize_node(node, parent, grandparent);
85 	if (r < 0)
86 		return r;
87 
88 	/*
89 	 * If we are the root node, we need to initialise the entries for
90 	 * our dot node now (out of the below loop) with shortcut set to
91 	 * 1, then move all of its entries (save PX to prevent duplicates)
92 	 * into it, then finalise the dot node out of bounds but after us
93 	 * again. The “shortcut” variable is unused after the label, thus
94 	 * it can be used as temporary to speed up the loop.
95 	 */
96 
97 	if (shortcut == 1)
98 		return 1;
99  cd9660_susp_initialize_shortcut:
100 	/* assert: shortcut == 0 here */
101 
102 	if (node == diskStructure.rootNode) {
103 		/* We are the root node. */
104 		struct ISO_SUSP_ATTRIBUTES *temp;
105 
106 		/* Initialise our dot record. */
107 		if ((r = cd9660_susp_initialize(node->dot_record, node,
108 		    parent, 1)) < 0)
109 			return r;
110 
111 		/* Move SUSP entries. */
112 		while ((temp = TAILQ_FIRST(&node->head))) {
113 			TAILQ_REMOVE(&node->head, temp, rr_ll);
114 			if (temp->attr.su_entry.SP.h.type[0] != 'P' ||
115 			    temp->attr.su_entry.SP.h.type[1] != 'X')
116 				TAILQ_INSERT_TAIL(&node->dot_record->head,
117 				    temp, rr_ll);
118 		}
119 
120 		/* Note that we are the root node. */
121 		shortcut = 1;
122 
123 		/* Now go on process ourselves normally. */
124 	}
125 
126 	/*
127 	 * See if we need a CE record, and set all of the
128 	 * associated counters.
129 	 *
130 	 * This should be called after all extensions. After
131 	 * this is called, no new records should be added.
132 	 */
133 	if ((r = cd9660_susp_handle_continuation(node)) < 0)
134 		return r;
135 
136 	/* Recurse on children. */
137 	TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) {
138 		if ((r = cd9660_susp_initialize(cn, node, parent,
139 		    /* root node? */ shortcut && cn == node->dot_record ?
140 		    /* finish only */ 2 : /* full operation */ 0)) < 0)
141 			return 0;
142 	}
143 	return 1;
144 }
145 
146 int
cd9660_susp_finalize(cd9660node * node)147 cd9660_susp_finalize(cd9660node *node)
148 {
149 	cd9660node *temp;
150 	int r;
151 
152 	assert(node != NULL);
153 
154 	if (node == diskStructure.rootNode)
155 		diskStructure.susp_continuation_area_current_free = 0;
156 
157 	if ((r = cd9660_susp_finalize_node(node)) < 0)
158 		return r;
159 	if ((r = cd9660_rrip_finalize_node(node)) < 0)
160 		return r;
161 
162 	TAILQ_FOREACH(temp, &node->cn_children, cn_next_child) {
163 		if ((r = cd9660_susp_finalize(temp)) < 0)
164 			return r;
165 	}
166 	return 1;
167 }
168 
169 /*
170  * If we really wanted to speed things up, we could have some sort of
171  * lookup table on the SUSP entry type that calls a functor. Or, we could
172  * combine the functions. These functions are kept separate to allow
173  * easier addition of other extensions.
174 
175  * For the sake of simplicity and clarity, we won't be doing that for now.
176  */
177 
178 /*
179  * SUSP needs to update the following types:
180  * CE (continuation area)
181  */
182 int
cd9660_susp_finalize_node(cd9660node * node)183 cd9660_susp_finalize_node(cd9660node *node)
184 {
185 	struct ISO_SUSP_ATTRIBUTES *t;
186 
187 	/* Handle CE counters */
188 	if (node->susp_entry_ce_length > 0) {
189 		node->susp_entry_ce_start =
190 		    diskStructure.susp_continuation_area_current_free;
191 		diskStructure.susp_continuation_area_current_free +=
192 		    node->susp_entry_ce_length;
193 	}
194 
195 	TAILQ_FOREACH(t, &node->head, rr_ll) {
196 		if (t->susp_type != SUSP_TYPE_SUSP ||
197 		    t->entry_type != SUSP_ENTRY_SUSP_CE)
198 			continue;
199 		cd9660_bothendian_dword(
200 			diskStructure.
201 			  susp_continuation_area_start_sector,
202 			t->attr.su_entry.CE.ca_sector);
203 
204 		cd9660_bothendian_dword(
205 			diskStructure.
206 			  susp_continuation_area_start_sector,
207 			t->attr.su_entry.CE.ca_sector);
208 		cd9660_bothendian_dword(node->susp_entry_ce_start,
209 			t->attr.su_entry.CE.offset);
210 		cd9660_bothendian_dword(node->susp_entry_ce_length,
211 			t->attr.su_entry.CE.length);
212 	}
213 	return 0;
214 }
215 
216 int
cd9660_rrip_finalize_node(cd9660node * node)217 cd9660_rrip_finalize_node(cd9660node *node)
218 {
219 	struct ISO_SUSP_ATTRIBUTES *t;
220 
221 	TAILQ_FOREACH(t, &node->head, rr_ll) {
222 		if (t->susp_type != SUSP_TYPE_RRIP)
223 			continue;
224 		switch (t->entry_type) {
225 		case SUSP_ENTRY_RRIP_CL:
226 			/* Look at rr_relocated*/
227 			if (node->rr_relocated == NULL)
228 				return -1;
229 			cd9660_bothendian_dword(
230 			    node->rr_relocated->fileDataSector,
231 			    t->attr.rr_entry.CL.dir_loc);
232 			break;
233 		case SUSP_ENTRY_RRIP_PL:
234 			/* Look at rr_real_parent */
235 			if (node->parent->rr_real_parent == NULL)
236 				return -1;
237 			cd9660_bothendian_dword(
238 			    node->parent->rr_real_parent->fileDataSector,
239 			    t->attr.rr_entry.PL.dir_loc);
240 			break;
241 		}
242 	}
243 	return 0;
244 }
245 
246 static int
cd9660_susp_handle_continuation_common(cd9660node * node,int space)247 cd9660_susp_handle_continuation_common(cd9660node *node, int space)
248 {
249 	int ca_used, susp_used, susp_used_pre_ce, working;
250 	struct ISO_SUSP_ATTRIBUTES *temp, *pre_ce, *last, *CE, *ST;
251 
252 	pre_ce = last = NULL;
253 	working = 254 - space;
254 	if (node->su_tail_size > 0)
255 		/* Allow 4 bytes for "ST" record. */
256 		working -= node->su_tail_size + 4;
257 #ifdef DEBUG
258 	printf("There are %i bytes to work with\n", working);
259 #endif
260 
261 	susp_used_pre_ce = susp_used = 0;
262 	ca_used = 0;
263 	TAILQ_FOREACH(temp, &node->head, rr_ll) {
264 		if (working < 0)
265 			break;
266 #ifdef DEBUG
267 		printf("SUSP entry %c%c found, length %d\n",
268 		    temp->attr.su_entry.SP.h.type[0],
269 		    temp->attr.su_entry.SP.h.type[1],
270 		    temp->attr.su_entry.SP.h.length[0]);
271 #endif
272 		working -= CD9660_SUSP_ENTRY_SIZE(temp);
273 		if (working >= 0) {
274 			last = temp;
275 			susp_used += CD9660_SUSP_ENTRY_SIZE(temp);
276 		}
277 		if (working >= 28) {
278 			/*
279 			 * Remember the last entry after which we
280 			 * could insert a "CE" entry.
281 			 */
282 			pre_ce = last;
283 			susp_used_pre_ce = susp_used;
284 		}
285 	}
286 
287 	/* A CE entry is needed */
288 	if (working <= 0) {
289 		CE = cd9660node_susp_create_node(SUSP_TYPE_SUSP,
290 			SUSP_ENTRY_SUSP_CE, "CE", SUSP_LOC_ENTRY);
291 		cd9660_susp_ce(CE, node);
292 		/* This will automatically insert at the appropriate location */
293 		if (pre_ce != NULL)
294 			TAILQ_INSERT_AFTER(&node->head, pre_ce, CE, rr_ll);
295 		else
296 			TAILQ_INSERT_HEAD(&node->head, CE, rr_ll);
297 		last = CE;
298 		susp_used = susp_used_pre_ce + 28;
299 		/* Count how much CA data is necessary */
300 		for (temp = TAILQ_NEXT(last, rr_ll); temp != NULL;
301 		     temp = TAILQ_NEXT(temp, rr_ll)) {
302 			ca_used += CD9660_SUSP_ENTRY_SIZE(temp);
303 		}
304 	}
305 
306 	/* An ST entry is needed */
307 	if (node->su_tail_size > 0) {
308 		ST = cd9660node_susp_create_node(SUSP_TYPE_SUSP,
309 		    SUSP_ENTRY_SUSP_ST, "ST", SUSP_LOC_ENTRY);
310 		cd9660_susp_st(ST, node);
311 		if (last != NULL)
312 			TAILQ_INSERT_AFTER(&node->head, last, ST, rr_ll);
313 		else
314 			TAILQ_INSERT_HEAD(&node->head, ST, rr_ll);
315 		last = ST;
316 		susp_used += 4;
317 	}
318 	if (last != NULL)
319 		last->last_in_suf = 1;
320 
321 	/* align to even size */
322 	node->susp_entry_size = ((susp_used + 1) >> 1) << 1;
323 	node->susp_entry_ce_length = ca_used;
324 
325 	diskStructure.susp_continuation_area_size += ca_used;
326 	return 1;
327 }
328 
329 /* See if a continuation entry is needed for each of the different types */
330 static int
cd9660_susp_handle_continuation(cd9660node * node)331 cd9660_susp_handle_continuation(cd9660node *node)
332 {
333 	assert (node != NULL);
334 
335 	/* Entry */
336 	if (cd9660_susp_handle_continuation_common(
337 		node,(int)(node->isoDirRecord->length[0])) < 0)
338 		return 0;
339 
340 	return 1;
341 }
342 
343 int
cd9660_susp_initialize_node(cd9660node * node)344 cd9660_susp_initialize_node(cd9660node *node)
345 {
346 	struct ISO_SUSP_ATTRIBUTES *temp;
347 
348 	/*
349 	 * Requirements/notes:
350 	 * CE: is added for us where needed
351 	 * ST: not sure if it is even required, but if so, should be
352 	 *     handled by the CE code
353 	 * PD: isnt needed (though might be added for testing)
354 	 * SP: is stored ONLY on the . record of the root directory
355 	 * ES: not sure
356 	 */
357 
358 	/* Check for root directory, add SP and ER if needed. */
359 	if (node->type & CD9660_TYPE_DOT) {
360 		if (node->parent == diskStructure.rootNode) {
361 			temp = cd9660node_susp_create_node(SUSP_TYPE_SUSP,
362 				SUSP_ENTRY_SUSP_SP, "SP", SUSP_LOC_DOT);
363 			cd9660_susp_sp(temp, node);
364 
365 			/* Should be first entry. */
366 			TAILQ_INSERT_HEAD(&node->head, temp, rr_ll);
367 		}
368 	}
369 	return 1;
370 }
371 
372 static void
cd9660_rrip_initialize_inode(cd9660node * node)373 cd9660_rrip_initialize_inode(cd9660node *node)
374 {
375 	struct ISO_SUSP_ATTRIBUTES *attr;
376 
377 	/*
378 	 * Inode dependent values - this may change,
379 	 * but for now virtual files and directories do
380 	 * not have an inode structure
381 	 */
382 
383 	if ((node->node != NULL) && (node->node->inode != NULL)) {
384 		/* PX - POSIX attributes */
385 		attr = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
386 			SUSP_ENTRY_RRIP_PX, "PX", SUSP_LOC_ENTRY);
387 		cd9660node_rrip_px(attr, node->node);
388 
389 		TAILQ_INSERT_TAIL(&node->head, attr, rr_ll);
390 
391 		/* TF - timestamp */
392 		attr = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
393 			SUSP_ENTRY_RRIP_TF, "TF", SUSP_LOC_ENTRY);
394 		cd9660node_rrip_tf(attr, node->node);
395 		TAILQ_INSERT_TAIL(&node->head, attr, rr_ll);
396 
397 		/* SL - Symbolic link */
398 		/* ?????????? Dan - why is this here? */
399 		if (TAILQ_EMPTY(&node->cn_children) &&
400 		    node->node->inode != NULL &&
401 		    S_ISLNK(node->node->inode->st.st_mode))
402 			cd9660_createSL(node);
403 
404 		/* PN - device number */
405 		if (node->node->inode != NULL &&
406 		    ((S_ISCHR(node->node->inode->st.st_mode) ||
407 		     S_ISBLK(node->node->inode->st.st_mode)))) {
408 			attr =
409 			    cd9660node_susp_create_node(SUSP_TYPE_RRIP,
410 				SUSP_ENTRY_RRIP_PN, "PN",
411 				SUSP_LOC_ENTRY);
412 			cd9660node_rrip_pn(attr, node->node);
413 			TAILQ_INSERT_TAIL(&node->head, attr, rr_ll);
414 		}
415 	}
416 }
417 
418 int
cd9660_rrip_initialize_node(cd9660node * node,cd9660node * parent,cd9660node * grandparent)419 cd9660_rrip_initialize_node(cd9660node *node, cd9660node *parent,
420     cd9660node *grandparent)
421 {
422 	struct ISO_SUSP_ATTRIBUTES *current = NULL;
423 
424 	assert(node != NULL);
425 
426 	if (node->type & CD9660_TYPE_DOT) {
427 		/*
428 		 * Handle ER - should be the only entry to appear on
429 		 * a "." record
430 		 */
431 		if (node->parent == diskStructure.rootNode) {
432 			cd9660_susp_ER(node, 1, SUSP_RRIP_ER_EXT_ID,
433 				SUSP_RRIP_ER_EXT_DES, SUSP_RRIP_ER_EXT_SRC);
434 		}
435 		if (parent != NULL && parent->node != NULL &&
436 		    parent->node->inode != NULL) {
437 			/* PX - POSIX attributes */
438 			current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
439 				SUSP_ENTRY_RRIP_PX, "PX", SUSP_LOC_ENTRY);
440 			cd9660node_rrip_px(current, parent->node);
441 			TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
442 		}
443 	} else if (node->type & CD9660_TYPE_DOTDOT) {
444 		if (grandparent != NULL && grandparent->node != NULL &&
445 		    grandparent->node->inode != NULL) {
446 			/* PX - POSIX attributes */
447 			current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
448 				SUSP_ENTRY_RRIP_PX, "PX", SUSP_LOC_ENTRY);
449 			cd9660node_rrip_px(current, grandparent->node);
450 			TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
451 		}
452 	} else {
453 		cd9660_rrip_initialize_inode(node);
454 
455 		/*
456 		 * Not every node needs a NM set - only if the name is
457 		 * actually different. IE: If a file is TEST -> TEST,
458 		 * no NM. test -> TEST, need a NM
459 		 *
460 		 * The rr_moved_dir needs to be assigned a NM record as well.
461 		 */
462 		if (node == diskStructure.rr_moved_dir) {
463 			cd9660_rrip_add_NM(node, RRIP_DEFAULT_MOVE_DIR_NAME);
464 		}
465 		else if ((node->node != NULL) &&
466 			((strlen(node->node->name) !=
467 			    (int)node->isoDirRecord->name_len[0]) ||
468 			(memcmp(node->node->name,node->isoDirRecord->name,
469 				(int) node->isoDirRecord->name_len[0]) != 0))) {
470 			cd9660_rrip_NM(node);
471 		}
472 
473 
474 
475 		/* Rock ridge directory relocation code here. */
476 
477 		/* First handle the CL for the placeholder file. */
478 		if (node->rr_relocated != NULL) {
479 			current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
480 				SUSP_ENTRY_RRIP_CL, "CL", SUSP_LOC_ENTRY);
481 			cd9660_rrip_CL(current, node);
482 			TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
483 		}
484 
485 		/* Handle RE*/
486 		if (node->rr_real_parent != NULL) {
487 			current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
488 				SUSP_ENTRY_RRIP_RE, "RE", SUSP_LOC_ENTRY);
489 			cd9660_rrip_RE(current,node);
490 			TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
491 
492 			/* Handle PL */
493 			current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
494 				SUSP_ENTRY_RRIP_PL, "PL", SUSP_LOC_DOTDOT);
495 			cd9660_rrip_PL(current,node->dot_dot_record);
496 			TAILQ_INSERT_TAIL(&node->dot_dot_record->head, current,
497 			    rr_ll);
498 		}
499 	}
500 	return 1;
501 }
502 
503 struct ISO_SUSP_ATTRIBUTES*
cd9660node_susp_create_node(int susp_type,int entry_type,const char * type_id,int write_loc)504 cd9660node_susp_create_node(int susp_type, int entry_type, const char *type_id,
505 			    int write_loc)
506 {
507 	struct ISO_SUSP_ATTRIBUTES* temp;
508 
509 	if ((temp = calloc(1, sizeof (struct ISO_SUSP_ATTRIBUTES))) == NULL) {
510 		CD9660_MEM_ALLOC_ERROR("cd9660node_susp_create_node");
511 		exit(1);
512 	}
513 
514 	temp->susp_type = susp_type;
515 	temp->entry_type = entry_type;
516 	temp->last_in_suf = 0;
517 	/* Phase this out */
518 	temp->type_of[0] = type_id[0];
519 	temp->type_of[1] = type_id[1];
520 	temp->write_location = write_loc;
521 
522 	/*
523 	 * Since the first four bytes is common, lets go ahead and
524 	 * set the type identifier, since we are passing that to this
525 	 * function anyhow.
526 	 */
527 	temp->attr.su_entry.SP.h.type[0] = type_id[0];
528 	temp->attr.su_entry.SP.h.type[1] = type_id[1];
529 	return temp;
530 }
531 
532 int
cd9660_rrip_PL(struct ISO_SUSP_ATTRIBUTES * p,cd9660node * node __unused)533 cd9660_rrip_PL(struct ISO_SUSP_ATTRIBUTES* p, cd9660node *node __unused)
534 {
535 	p->attr.rr_entry.PL.h.length[0] = 12;
536 	p->attr.rr_entry.PL.h.version[0] = 1;
537 	return 1;
538 }
539 
540 int
cd9660_rrip_CL(struct ISO_SUSP_ATTRIBUTES * p,cd9660node * node __unused)541 cd9660_rrip_CL(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *node __unused)
542 {
543 	p->attr.rr_entry.CL.h.length[0] = 12;
544 	p->attr.rr_entry.CL.h.version[0] = 1;
545 	return 1;
546 }
547 
548 int
cd9660_rrip_RE(struct ISO_SUSP_ATTRIBUTES * p,cd9660node * node __unused)549 cd9660_rrip_RE(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *node __unused)
550 {
551 	p->attr.rr_entry.RE.h.length[0] = 0;
552 	p->attr.rr_entry.RE.h.version[0] = 1;
553 	return 1;
554 }
555 
556 void
cd9660_createSL(cd9660node * node)557 cd9660_createSL(cd9660node *node)
558 {
559 	struct ISO_SUSP_ATTRIBUTES* current;
560 	int path_count, dir_count, done, i, j, dir_copied;
561 	char temp_cr[255];
562 	char temp_sl[255]; /* used in copying continuation entry*/
563 	char* sl_ptr;
564 
565 	sl_ptr = node->node->symlink;
566 
567 	done = 0;
568 	path_count = 0;
569 	dir_count = 0;
570 	dir_copied = 0;
571 	current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
572 	    SUSP_ENTRY_RRIP_SL, "SL", SUSP_LOC_ENTRY);
573 
574 	current->attr.rr_entry.SL.h.version[0] = 1;
575 	current->attr.rr_entry.SL.flags[0] = SL_FLAGS_NONE;
576 
577 	if (*sl_ptr == '/') {
578 		temp_cr[0] = SL_FLAGS_ROOT;
579 		temp_cr[1] = 0;
580 		memcpy(current->attr.rr_entry.SL.component + path_count,
581 		    temp_cr, 2);
582 		path_count += 2;
583 		sl_ptr++;
584 	}
585 
586 	for (i = 0; i < (dir_count + 2); i++)
587 		temp_cr[i] = '\0';
588 
589 	while (!done) {
590 		while ((*sl_ptr != '/') && (*sl_ptr != '\0')) {
591 			dir_copied = 1;
592 			if (*sl_ptr == '.') {
593 				if ((*(sl_ptr + 1) == '/') || (*(sl_ptr + 1)
594 				     == '\0')) {
595 					temp_cr[0] = SL_FLAGS_CURRENT;
596 					sl_ptr++;
597 				} else if(*(sl_ptr + 1) == '.') {
598 					if ((*(sl_ptr + 2) == '/') ||
599 					    (*(sl_ptr + 2) == '\0')) {
600 						temp_cr[0] = SL_FLAGS_PARENT;
601 						sl_ptr += 2;
602 					}
603 				} else {
604 					temp_cr[dir_count+2] = *sl_ptr;
605 					sl_ptr++;
606 					dir_count++;
607 				}
608 			} else {
609 				temp_cr[dir_count + 2] = *sl_ptr;
610 				sl_ptr++;
611 				dir_count++;
612 			}
613 		}
614 
615 		if ((path_count + dir_count) >= 249) {
616 			current->attr.rr_entry.SL.flags[0] |= SL_FLAGS_CONTINUE;
617 
618 			j = 0;
619 
620 			if (path_count <= 249) {
621 				while(j != (249 - path_count)) {
622 					temp_sl[j] = temp_cr[j];
623 					j++;
624 				}
625 				temp_sl[0] = SL_FLAGS_CONTINUE;
626 				temp_sl[1] = j - 2;
627 				memcpy(
628 				    current->attr.rr_entry.SL.component +
629 					path_count,
630 				    temp_sl, j);
631 			}
632 
633 			path_count += j;
634 			current->attr.rr_entry.SL.h.length[0] = path_count + 5;
635 			TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
636 			current= cd9660node_susp_create_node(SUSP_TYPE_RRIP,
637 			       SUSP_ENTRY_RRIP_SL, "SL", SUSP_LOC_ENTRY);
638 			current->attr.rr_entry.SL.h.version[0] = 1;
639 			current->attr.rr_entry.SL.flags[0] = SL_FLAGS_NONE;
640 
641 			path_count = 0;
642 
643 			if (dir_count > 2) {
644 				while (j != dir_count + 2) {
645 					current->attr.rr_entry.SL.component[
646 					    path_count + 2] = temp_cr[j];
647 					j++;
648 					path_count++;
649 				}
650 				current->attr.rr_entry.SL.component[1]
651 				    = path_count;
652 				path_count+= 2;
653 			} else {
654 				while(j != dir_count) {
655 					current->attr.rr_entry.SL.component[
656 					    path_count+2] = temp_cr[j];
657 					j++;
658 					path_count++;
659 				}
660 			}
661 		} else {
662 			if (dir_copied == 1) {
663 				temp_cr[1] = dir_count;
664 				memcpy(current->attr.rr_entry.SL.component +
665 					path_count,
666 				    temp_cr, dir_count + 2);
667 				path_count += dir_count + 2;
668 			}
669 		}
670 
671 		if (*sl_ptr == '\0') {
672 			done = 1;
673 			current->attr.rr_entry.SL.h.length[0] = path_count + 5;
674 			TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
675 		} else {
676 			sl_ptr++;
677 			dir_count = 0;
678 			dir_copied = 0;
679 			for(i = 0; i < 255; i++) {
680 				temp_cr[i] = '\0';
681 			}
682 		}
683 	}
684 }
685 
686 int
cd9660node_rrip_px(struct ISO_SUSP_ATTRIBUTES * v,fsnode * pxinfo)687 cd9660node_rrip_px(struct ISO_SUSP_ATTRIBUTES *v, fsnode *pxinfo)
688 {
689 	v->attr.rr_entry.PX.h.length[0] = 44;
690 	v->attr.rr_entry.PX.h.version[0] = 1;
691 	cd9660_bothendian_dword(rrip_squash ?
692 	    /* turn on all R bits, and all X bits if one X bit is set */
693 	    (((pxinfo->inode->st.st_mode & 0111) ? 0555 : 0444) |
694 	    /* preserve original mode bits, except W, setugid, sticky */
695 	    pxinfo->inode->st.st_mode) & ~07222 : pxinfo->inode->st.st_mode,
696 	    v->attr.rr_entry.PX.mode);
697 	cd9660_bothendian_dword(pxinfo->inode->st.st_nlink,
698 	    v->attr.rr_entry.PX.links);
699 	cd9660_bothendian_dword(rrip_squash ? 0 : pxinfo->inode->st.st_uid,
700 	    v->attr.rr_entry.PX.uid);
701 	cd9660_bothendian_dword(rrip_squash ? 0 : pxinfo->inode->st.st_gid,
702 	    v->attr.rr_entry.PX.gid);
703 	cd9660_bothendian_dword(pxinfo->inode->serno,
704 	    v->attr.rr_entry.PX.serial);
705 
706 	return 1;
707 }
708 
709 int
cd9660node_rrip_pn(struct ISO_SUSP_ATTRIBUTES * pn_field,fsnode * fnode)710 cd9660node_rrip_pn(struct ISO_SUSP_ATTRIBUTES *pn_field, fsnode *fnode)
711 {
712 	pn_field->attr.rr_entry.PN.h.length[0] = 20;
713 	pn_field->attr.rr_entry.PN.h.version[0] = 1;
714 
715 	if (sizeof (fnode->inode->st.st_rdev) > 32)
716 		cd9660_bothendian_dword((uint64_t)fnode->inode->st.st_rdev >> 32,
717 		    pn_field->attr.rr_entry.PN.high);
718 	else
719 		cd9660_bothendian_dword(0, pn_field->attr.rr_entry.PN.high);
720 
721 	cd9660_bothendian_dword(fnode->inode->st.st_rdev & 0xffffffff,
722 		pn_field->attr.rr_entry.PN.low);
723 	return 1;
724 }
725 
726 #if 0
727 int
728 cd9660node_rrip_nm(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *file_node)
729 {
730 	int nm_length = strlen(file_node->isoDirRecord->name) + 5;
731         p->attr.rr_entry.NM.h.type[0] = 'N';
732 	p->attr.rr_entry.NM.h.type[1] = 'M';
733 	snprintf(p->attr.rr_entry.NM.altname,
734 	    sizeof (p->attr.rr_entry.NM.altname),
735 	    "%s", file_node->isoDirRecord->name);
736 	p->attr.rr_entry.NM.h.length[0] = (unsigned char)nm_length;
737 	p->attr.rr_entry.NM.h.version[0] = (unsigned char)1;
738 	p->attr.rr_entry.NM.flags[0] = (unsigned char) NM_PARENT;
739 	return 1;
740 }
741 #endif
742 
743 int
cd9660node_rrip_tf(struct ISO_SUSP_ATTRIBUTES * p,fsnode * _node)744 cd9660node_rrip_tf(struct ISO_SUSP_ATTRIBUTES *p, fsnode *_node)
745 {
746 	struct cd9660node_rrip_tf_data {
747 		ISO_SUSP_HEADER		 h;
748 		u_char flags		 [ISODCL ( 4, 4)];
749 #if 0
750 		u_char timestamp	 [ISODCL ( 5, 256)];
751 #else
752 		/* using short form */
753 		u_char ts_mtime		 [ISODCL ( 5, 11)];
754 		u_char ts_atime		 [ISODCL (12, 18)];
755 		u_char ts_ctime		 [ISODCL (19, 25)];
756 		u_char _ts_rest		 [ISODCL (26, 256)];
757 #endif
758 	} *tfp;
759 
760 	/* memory alias */
761 	tfp = (struct cd9660node_rrip_tf_data *)(&(p->attr.rr_entry.TF));
762 
763 	tfp->flags[0] = TF_MODIFY | TF_ACCESS | TF_ATTRIBUTES;
764 	tfp->h.length[0] = 4;
765 	tfp->h.version[0] = 1;
766 
767 	/*
768 	 * Need to add creation time, backup time,
769 	 * expiration time, and effective time.
770 	 */
771 
772 	cd9660_time_915(_node->inode->st.st_mtime, tfp->ts_mtime);
773 	tfp->h.length[0] += 7;
774 
775 	cd9660_time_915(_node->inode->st.st_atime, tfp->ts_atime);
776 	tfp->h.length[0] += 7;
777 
778 	cd9660_time_915(_node->inode->st.st_ctime, tfp->ts_ctime);
779 	tfp->h.length[0] += 7;
780 	return 1;
781 }
782 
783 int
cd9660_susp_sp(struct ISO_SUSP_ATTRIBUTES * p,cd9660node * spinfo __unused)784 cd9660_susp_sp(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *spinfo __unused)
785 {
786 	p->attr.su_entry.SP.h.length[0] = 7;
787 	p->attr.su_entry.SP.h.version[0] = 1;
788 	p->attr.su_entry.SP.check[0] = 0xBE;
789 	p->attr.su_entry.SP.check[1] = 0xEF;
790 	p->attr.su_entry.SP.len_skp[0] = 0;
791 	return 1;
792 }
793 
794 int
cd9660_susp_st(struct ISO_SUSP_ATTRIBUTES * p,cd9660node * stinfo __unused)795 cd9660_susp_st(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *stinfo __unused)
796 {
797 	p->attr.su_entry.ST.h.type[0] = 'S';
798 	p->attr.su_entry.ST.h.type[1] = 'T';
799 	p->attr.su_entry.ST.h.length[0] = 4;
800 	p->attr.su_entry.ST.h.version[0] = 1;
801 	return 1;
802 }
803 
804 int
cd9660_susp_ce(struct ISO_SUSP_ATTRIBUTES * p,cd9660node * spinfo __unused)805 cd9660_susp_ce(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *spinfo __unused)
806 {
807 	p->attr.su_entry.CE.h.length[0] = 28;
808 	p->attr.su_entry.CE.h.version[0] = 1;
809 	/* Other attributes dont matter right now, will be updated later */
810 	return 1;
811 }
812 
813 int
cd9660_susp_pd(struct ISO_SUSP_ATTRIBUTES * p __unused,int length __unused)814 cd9660_susp_pd(struct ISO_SUSP_ATTRIBUTES *p __unused, int length __unused)
815 {
816 	return 1;
817 }
818 
819 void
cd9660_rrip_add_NM(cd9660node * node,const char * name)820 cd9660_rrip_add_NM(cd9660node *node, const char *name)
821 {
822 	int working,len;
823 	const char *p;
824 	struct ISO_SUSP_ATTRIBUTES *r;
825 
826 	/*
827 	 * Each NM record has 254 byes to work with. This means that
828 	 * the name data itself only has 249 bytes to work with. So, a
829 	 * name with 251 characters would require two nm records.
830 	 */
831 	p = name;
832 	working = 1;
833 	while (working) {
834 		r = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
835 		    SUSP_ENTRY_RRIP_NM, "NM", SUSP_LOC_ENTRY);
836 		r->attr.rr_entry.NM.h.version[0] = 1;
837 		r->attr.rr_entry.NM.flags[0] = RRIP_NM_FLAGS_NONE;
838 		len = strlen(p);
839 
840 		if (len > 249) {
841 			len = 249;
842 			r->attr.rr_entry.NM.flags[0] = RRIP_NM_FLAGS_CONTINUE;
843 		} else {
844 			working = 0;
845 		}
846 		memcpy(r->attr.rr_entry.NM.altname, p, len);
847 		r->attr.rr_entry.NM.h.length[0] = 5 + len;
848 
849 		TAILQ_INSERT_TAIL(&node->head, r, rr_ll);
850 
851 		p += len;
852 	}
853 }
854 
855 void
cd9660_rrip_NM(cd9660node * node)856 cd9660_rrip_NM(cd9660node *node)
857 {
858 	cd9660_rrip_add_NM(node, node->node->name);
859 }
860 
861 struct ISO_SUSP_ATTRIBUTES*
cd9660_susp_ER(cd9660node * node,u_char ext_version,const char * ext_id,const char * ext_des,const char * ext_src)862 cd9660_susp_ER(cd9660node *node,
863 	       u_char ext_version, const char* ext_id, const char* ext_des,
864 	       const char* ext_src)
865 {
866 	int l;
867 	struct ISO_SUSP_ATTRIBUTES *r;
868 
869 	r = cd9660node_susp_create_node(SUSP_TYPE_SUSP,
870 			SUSP_ENTRY_SUSP_ER, "ER", SUSP_LOC_DOT);
871 
872 	/* Fixed data is 8 bytes */
873 	r->attr.su_entry.ER.h.length[0] = 8;
874 	r->attr.su_entry.ER.h.version[0] = 1;
875 
876 	r->attr.su_entry.ER.len_id[0] = (u_char)strlen(ext_id);
877 	r->attr.su_entry.ER.len_des[0] = (u_char)strlen(ext_des);
878 	r->attr.su_entry.ER.len_src[0] = (u_char)strlen(ext_src);
879 
880 	l = r->attr.su_entry.ER.len_id[0] +
881 		r->attr.su_entry.ER.len_src[0] +
882 		r->attr.su_entry.ER.len_des[0];
883 
884 	/* Everything must fit. */
885 	assert(l + r->attr.su_entry.ER.h.length[0] <= 254);
886 
887 	r->attr.su_entry.ER.h.length[0] += (u_char)l;
888 
889 
890 	r->attr.su_entry.ER.ext_ver[0] = ext_version;
891 	memcpy(r->attr.su_entry.ER.ext_data, ext_id,
892 		(int)r->attr.su_entry.ER.len_id[0]);
893 	l = (int) r->attr.su_entry.ER.len_id[0];
894 	memcpy(r->attr.su_entry.ER.ext_data + l,ext_des,
895 		(int)r->attr.su_entry.ER.len_des[0]);
896 
897 	l += (int)r->attr.su_entry.ER.len_des[0];
898 	memcpy(r->attr.su_entry.ER.ext_data + l,ext_src,
899 		(int)r->attr.su_entry.ER.len_src[0]);
900 
901 	TAILQ_INSERT_TAIL(&node->head, r, rr_ll);
902 	return r;
903 }
904 
905 struct ISO_SUSP_ATTRIBUTES*
cd9660_susp_ES(struct ISO_SUSP_ATTRIBUTES * last __unused,cd9660node * node __unused)906 cd9660_susp_ES(struct ISO_SUSP_ATTRIBUTES *last __unused, cd9660node *node __unused)
907 {
908 	return NULL;
909 }
910