/*
* Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
* Perez-Rathke and Ram Vedam. All rights reserved.
*
* This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
* Alan Perez-Rathke and Ram Vedam.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
* GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
* GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
/* This will hold all the function definitions
* defined in iso9660_rrip.h
*/
static void cd9660_rrip_initialize_inode(cd9660node *);
static int cd9660_susp_handle_continuation(iso9660_disk *, cd9660node *);
static int cd9660_susp_handle_continuation_common(iso9660_disk *, cd9660node *,
int);
int
cd9660_susp_initialize(iso9660_disk *diskStructure, cd9660node *node,
cd9660node *parent, cd9660node *grandparent)
{
cd9660node *cn;
int r;
/* Make sure the node is not NULL. If it is, there are major problems */
assert(node != NULL);
if (!(node->type & CD9660_TYPE_DOT) &&
!(node->type & CD9660_TYPE_DOTDOT))
TAILQ_INIT(&(node->head));
if (node->dot_record != 0)
TAILQ_INIT(&(node->dot_record->head));
if (node->dot_dot_record != 0)
TAILQ_INIT(&(node->dot_dot_record->head));
/* SUSP specific entries here */
if ((r = cd9660_susp_initialize_node(diskStructure, node)) < 0)
return r;
/* currently called cd9660node_rrip_init_links */
r = cd9660_rrip_initialize_node(diskStructure, node, parent, grandparent);
if (r < 0)
return r;
/*
* See if we need a CE record, and set all of the
* associated counters.
*
* This should be called after all extensions. After
* this is called, no new records should be added.
*/
if ((r = cd9660_susp_handle_continuation(diskStructure, node)) < 0)
return r;
/*
* If we really wanted to speed things up, we could have some sort of
* lookup table on the SUSP entry type that calls a functor. Or, we could
* combine the functions. These functions are kept separate to allow
* easier addition of other extensions.
* For the sake of simplicity and clarity, we won't be doing that for now.
*/
/*
* SUSP needs to update the following types:
* CE (continuation area)
*/
int
cd9660_susp_finalize_node(iso9660_disk *diskStructure, cd9660node *node)
{
struct ISO_SUSP_ATTRIBUTES *t;
/* Handle CE counters */
if (node->susp_entry_ce_length > 0) {
node->susp_entry_ce_start =
diskStructure->susp_continuation_area_current_free;
diskStructure->susp_continuation_area_current_free +=
node->susp_entry_ce_length;
}
int
cd9660_rrip_finalize_node(iso9660_disk *diskStructure __unused,
cd9660node *node)
{
struct ISO_SUSP_ATTRIBUTES *t;
TAILQ_FOREACH(t, &node->head, rr_ll) {
if (t->susp_type != SUSP_TYPE_RRIP)
continue;
switch (t->entry_type) {
case SUSP_ENTRY_RRIP_CL:
/* Look at rr_relocated*/
if (node->rr_relocated == NULL)
return -1;
cd9660_bothendian_dword(
node->rr_relocated->fileDataSector,
(unsigned char *)
t->attr.rr_entry.CL.dir_loc);
break;
case SUSP_ENTRY_RRIP_PL:
/* Look at rr_real_parent */
if (node->parent == NULL ||
node->parent->rr_real_parent == NULL)
return -1;
cd9660_bothendian_dword(
node->parent->rr_real_parent->fileDataSector,
(unsigned char *)
t->attr.rr_entry.PL.dir_loc);
break;
}
}
return 0;
}
static int
cd9660_susp_handle_continuation_common(iso9660_disk *diskStructure,
cd9660node *node, int space)
{
int ca_used, susp_used, susp_used_pre_ce, working;
struct ISO_SUSP_ATTRIBUTES *temp, *pre_ce, *last, *CE, *ST;
pre_ce = last = NULL;
working = 254 - space;
if (node->su_tail_size > 0)
/* Allow 4 bytes for "ST" record. */
working -= node->su_tail_size + 4;
/* printf("There are %i bytes to work with\n",working); */
susp_used_pre_ce = susp_used = 0;
ca_used = 0;
TAILQ_FOREACH(temp, &node->head, rr_ll) {
if (working < 0)
break;
/*
* printf("SUSP Entry found, length is %i\n",
* CD9660_SUSP_ENTRY_SIZE(temp));
*/
working -= CD9660_SUSP_ENTRY_SIZE(temp);
if (working >= 0) {
last = temp;
susp_used += CD9660_SUSP_ENTRY_SIZE(temp);
}
if (working >= 28) {
/*
* Remember the last entry after which we
* could insert a "CE" entry.
*/
pre_ce = last;
susp_used_pre_ce = susp_used;
}
}
/* A CE entry is needed */
if (working <= 0) {
CE = cd9660node_susp_create_node(SUSP_TYPE_SUSP,
SUSP_ENTRY_SUSP_CE, "CE", SUSP_LOC_ENTRY);
cd9660_susp_ce(CE, node);
/* This will automatically insert at the appropriate location */
if (pre_ce != NULL)
TAILQ_INSERT_AFTER(&node->head, pre_ce, CE, rr_ll);
else
TAILQ_INSERT_HEAD(&node->head, CE, rr_ll);
last = CE;
susp_used = susp_used_pre_ce + 28;
/* Count how much CA data is necessary */
for (temp = TAILQ_NEXT(last, rr_ll); temp != NULL;
temp = TAILQ_NEXT(temp, rr_ll)) {
ca_used += CD9660_SUSP_ENTRY_SIZE(temp);
}
}
/* An ST entry is needed */
if (node->su_tail_size > 0) {
ST = cd9660node_susp_create_node(SUSP_TYPE_SUSP,
SUSP_ENTRY_SUSP_ST, "ST", SUSP_LOC_ENTRY);
cd9660_susp_st(ST, node);
if (last != NULL)
TAILQ_INSERT_AFTER(&node->head, last, ST, rr_ll);
else
TAILQ_INSERT_HEAD(&node->head, ST, rr_ll);
last = ST;
susp_used += 4;
}
if (last != NULL)
last->last_in_suf = 1;
/* See if a continuation entry is needed for each of the different types */
static int
cd9660_susp_handle_continuation(iso9660_disk *diskStructure, cd9660node *node)
{
assert (node != NULL);
/* Entry */
if (cd9660_susp_handle_continuation_common(diskStructure,
node,(int)(node->isoDirRecord->length[0])) < 0)
return 0;
return 1;
}
int
cd9660_susp_initialize_node(iso9660_disk *diskStructure, cd9660node *node)
{
struct ISO_SUSP_ATTRIBUTES *temp;
/*
* Requirements/notes:
* CE: is added for us where needed
* ST: not sure if it is even required, but if so, should be
* handled by the CE code
* PD: isnt needed (though might be added for testing)
* SP: is stored ONLY on the . record of the root directory
* ES: not sure
*/
/* Check for root directory, add SP and ER if needed. */
if (node->type & CD9660_TYPE_DOT) {
if (node->parent == diskStructure->rootNode) {
temp = cd9660node_susp_create_node(SUSP_TYPE_SUSP,
SUSP_ENTRY_SUSP_SP, "SP", SUSP_LOC_DOT);
cd9660_susp_sp(temp, node);
/* Should be first entry. */
TAILQ_INSERT_HEAD(&node->head, temp, rr_ll);
}
}
return 1;
}
/* SL - Symbolic link */
/* ?????????? Dan - why is this here? */
if (TAILQ_EMPTY(&node->cn_children) &&
node->node->inode != NULL &&
S_ISLNK(node->node->inode->st.st_mode))
cd9660_createSL(node);
if (node->type & CD9660_TYPE_DOT) {
/*
* Handle ER - should be the only entry to appear on
* a "." record
*/
if (node->parent == diskStructure->rootNode) {
cd9660_susp_ER(node, 1, SUSP_RRIP_ER_EXT_ID,
SUSP_RRIP_ER_EXT_DES, SUSP_RRIP_ER_EXT_SRC);
}
if (parent != NULL && parent->node != NULL &&
parent->node->inode != NULL) {
/* PX - POSIX attributes */
current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
SUSP_ENTRY_RRIP_PX, "PX", SUSP_LOC_ENTRY);
cd9660node_rrip_px(current, parent->node);
TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
}
} else if (node->type & CD9660_TYPE_DOTDOT) {
if (grandparent != NULL && grandparent->node != NULL &&
grandparent->node->inode != NULL) {
/* PX - POSIX attributes */
current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
SUSP_ENTRY_RRIP_PX, "PX", SUSP_LOC_ENTRY);
cd9660node_rrip_px(current, grandparent->node);
TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
}
/* Handle PL */
if (parent != NULL && parent->rr_real_parent != NULL) {
current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
SUSP_ENTRY_RRIP_PL, "PL", SUSP_LOC_DOTDOT);
cd9660_rrip_PL(current,node);
TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
}
} else {
cd9660_rrip_initialize_inode(node);
if (node == diskStructure->rr_moved_dir) {
cd9660_rrip_add_NM(node, RRIP_DEFAULT_MOVE_DIR_NAME);
} else if (node->node != NULL) {
cd9660_rrip_NM(node);
}
/* Rock ridge directory relocation code here. */
/* First handle the CL for the placeholder file. */
if (node->rr_relocated != NULL) {
current = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
SUSP_ENTRY_RRIP_CL, "CL", SUSP_LOC_ENTRY);
cd9660_rrip_CL(current, node);
TAILQ_INSERT_TAIL(&node->head, current, rr_ll);
}
/*
* Since the first four bytes is common, lets go ahead and
* set the type identifier, since we are passing that to this
* function anyhow.
*/
temp->attr.su_entry.SP.h.type[0] = type_id[0];
temp->attr.su_entry.SP.h.type[1] = type_id[1];
return temp;
}
int
cd9660_susp_ce(struct ISO_SUSP_ATTRIBUTES *p, cd9660node *spinfo __unused)
{
p->attr.su_entry.CE.h.length[0] = 28;
p->attr.su_entry.CE.h.version[0] = 1;
/* Other attributes dont matter right now, will be updated later */
return 1;
}
int
cd9660_susp_pd(struct ISO_SUSP_ATTRIBUTES *p __unused, int length __unused)
{
return 1;
}
/*
* Each NM record has 254 bytes to work with. This means that
* the name data itself only has 249 bytes to work with. So, a
* name with 251 characters would require two nm records.
*/
p = name;
working = 1;
while (working) {
r = cd9660node_susp_create_node(SUSP_TYPE_RRIP,
SUSP_ENTRY_RRIP_NM, "NM", SUSP_LOC_ENTRY);
r->attr.rr_entry.NM.h.version[0] = 1;
r->attr.rr_entry.NM.flags[0] = RRIP_NM_FLAGS_NONE;
len = strlen(p);
if (len > 249) {
len = 249;
r->attr.rr_entry.NM.flags[0] = RRIP_NM_FLAGS_CONTINUE;
} else {
working = 0;
}
memcpy(r->attr.rr_entry.NM.altname, p, len);
r->attr.rr_entry.NM.h.length[0] = 5 + len;