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