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