Base tree add functions in low level node new functions.

This commit is contained in:
Vreixo Formoso 2008-01-26 16:30:05 +01:00
parent ea45f858cb
commit bd893a8cfa
3 changed files with 51 additions and 86 deletions

View File

@ -1986,7 +1986,7 @@ int image_builder_create_node(IsoNodeBuilder *builder, IsoImage *image,
} }
/* take a ref to the src, as stream has taken our ref */ /* take a ref to the src, as stream has taken our ref */
iso_file_source_ref(src); iso_file_source_ref(src);
file = calloc(1, sizeof(IsoFile)); file = calloc(1, sizeof(IsoFile));
if (file == NULL) { if (file == NULL) {
free(name); free(name);

View File

@ -632,6 +632,12 @@ int iso_node_is_valid_name(const char *name)
return 1; return 1;
} }
/**
* Check if a given path is valid for the destination of a link.
*
* @return
* 1 if yes, 0 if not
*/
int iso_node_is_valid_link_dest(const char *dest) int iso_node_is_valid_link_dest(const char *dest)
{ {
int ret; int ret;
@ -710,7 +716,7 @@ int iso_dir_insert(IsoDir *dir, IsoNode *node, IsoNode **pos,
return dir->nchildren; return dir->nchildren;
} else { } else {
/* CAN'T HAPPEN */ /* CAN'T HAPPEN */
return ISO_WRONG_ARG_VALUE; return ISO_ASSERT_FAILURE;
} }
} }

View File

@ -46,6 +46,8 @@
*/ */
int iso_tree_add_new_dir(IsoDir *parent, const char *name, IsoDir **dir) int iso_tree_add_new_dir(IsoDir *parent, const char *name, IsoDir **dir)
{ {
int ret;
char *n;
IsoDir *node; IsoDir *node;
IsoNode **pos; IsoNode **pos;
time_t now; time_t now;
@ -56,11 +58,6 @@ int iso_tree_add_new_dir(IsoDir *parent, const char *name, IsoDir **dir)
if (dir) { if (dir) {
*dir = NULL; *dir = NULL;
} }
/* check if the name is valid */
if (!iso_node_is_valid_name(name)) {
return ISO_WRONG_ARG_VALUE;
}
/* find place where to insert and check if it exists */ /* find place where to insert and check if it exists */
if (iso_dir_exists(parent, name, &pos)) { if (iso_dir_exists(parent, name, &pos)) {
@ -68,30 +65,24 @@ int iso_tree_add_new_dir(IsoDir *parent, const char *name, IsoDir **dir)
return ISO_NODE_NAME_NOT_UNIQUE; return ISO_NODE_NAME_NOT_UNIQUE;
} }
node = calloc(1, sizeof(IsoDir)); n = strdup(name);
if (node == NULL) { ret = iso_node_new_dir(n, &node);
return ISO_OUT_OF_MEM; if (ret < 0) {
} free(n);
return ret;
node->node.refcount = 1;
node->node.type = LIBISO_DIR;
node->node.name = strdup(name);
if (node->node.name == NULL) {
free(node);
return ISO_OUT_OF_MEM;
} }
/* permissions from parent */ /* permissions from parent */
node->node.mode = parent->node.mode; iso_node_set_permissions((IsoNode*)node, parent->node.mode);
node->node.uid = parent->node.uid; iso_node_set_uid((IsoNode*)node, parent->node.uid);
node->node.gid = parent->node.gid; iso_node_set_gid((IsoNode*)node, parent->node.gid);
node->node.hidden = parent->node.hidden; iso_node_set_hidden((IsoNode*)node, parent->node.hidden);
/* current time */ /* current time */
now = time(NULL); now = time(NULL);
node->node.atime = now; iso_node_set_atime((IsoNode*)node, now);
node->node.ctime = now; iso_node_set_ctime((IsoNode*)node, now);
node->node.mtime = now; iso_node_set_mtime((IsoNode*)node, now);
if (dir) { if (dir) {
*dir = node; *dir = node;
@ -128,6 +119,8 @@ int iso_tree_add_new_dir(IsoDir *parent, const char *name, IsoDir **dir)
int iso_tree_add_new_symlink(IsoDir *parent, const char *name, int iso_tree_add_new_symlink(IsoDir *parent, const char *name,
const char *dest, IsoSymlink **link) const char *dest, IsoSymlink **link)
{ {
int ret;
char *n, *d;
IsoSymlink *node; IsoSymlink *node;
IsoNode **pos; IsoNode **pos;
time_t now; time_t now;
@ -138,17 +131,6 @@ int iso_tree_add_new_symlink(IsoDir *parent, const char *name,
if (link) { if (link) {
*link = NULL; *link = NULL;
} }
/* check if the name is valid */
if (!iso_node_is_valid_name(name)) {
return ISO_WRONG_ARG_VALUE;
}
/* check if destination is valid */
if (!iso_node_is_valid_link_dest(dest)) {
/* guard against null or empty dest */
return ISO_WRONG_ARG_VALUE;
}
/* find place where to insert */ /* find place where to insert */
if (iso_dir_exists(parent, name, &pos)) { if (iso_dir_exists(parent, name, &pos)) {
@ -156,37 +138,26 @@ int iso_tree_add_new_symlink(IsoDir *parent, const char *name,
return ISO_NODE_NAME_NOT_UNIQUE; return ISO_NODE_NAME_NOT_UNIQUE;
} }
node = calloc(1, sizeof(IsoSymlink)); n = strdup(name);
if (node == NULL) { d = strdup(dest);
return ISO_OUT_OF_MEM; ret = iso_node_new_symlink(n, d, &node);
} if (ret < 0) {
free(n);
node->node.refcount = 1; free(d);
node->node.type = LIBISO_SYMLINK; return ret;
node->node.name = strdup(name);
if (node->node.name == NULL) {
free(node);
return ISO_OUT_OF_MEM;
}
node->dest = strdup(dest);
if (node->dest == NULL) {
free(node->node.name);
free(node);
return ISO_OUT_OF_MEM;
} }
/* permissions from parent */ /* permissions from parent */
node->node.mode = S_IFLNK | 0777; iso_node_set_permissions((IsoNode*)node, 0777);
node->node.uid = parent->node.uid; iso_node_set_uid((IsoNode*)node, parent->node.uid);
node->node.gid = parent->node.gid; iso_node_set_gid((IsoNode*)node, parent->node.gid);
node->node.hidden = parent->node.hidden; iso_node_set_hidden((IsoNode*)node, parent->node.hidden);
/* current time */ /* current time */
now = time(NULL); now = time(NULL);
node->node.atime = now; iso_node_set_atime((IsoNode*)node, now);
node->node.ctime = now; iso_node_set_ctime((IsoNode*)node, now);
node->node.mtime = now; iso_node_set_mtime((IsoNode*)node, now);
if (link) { if (link) {
*link = node; *link = node;
@ -237,6 +208,8 @@ int iso_tree_add_new_symlink(IsoDir *parent, const char *name,
int iso_tree_add_new_special(IsoDir *parent, const char *name, mode_t mode, int iso_tree_add_new_special(IsoDir *parent, const char *name, mode_t mode,
dev_t dev, IsoSpecial **special) dev_t dev, IsoSpecial **special)
{ {
int ret;
char *n;
IsoSpecial *node; IsoSpecial *node;
IsoNode **pos; IsoNode **pos;
time_t now; time_t now;
@ -250,11 +223,6 @@ int iso_tree_add_new_special(IsoDir *parent, const char *name, mode_t mode,
if (special) { if (special) {
*special = NULL; *special = NULL;
} }
/* check if the name is valid */
if (!iso_node_is_valid_name(name)) {
return ISO_WRONG_ARG_VALUE;
}
/* find place where to insert */ /* find place where to insert */
if (iso_dir_exists(parent, name, &pos)) { if (iso_dir_exists(parent, name, &pos)) {
@ -262,32 +230,23 @@ int iso_tree_add_new_special(IsoDir *parent, const char *name, mode_t mode,
return ISO_NODE_NAME_NOT_UNIQUE; return ISO_NODE_NAME_NOT_UNIQUE;
} }
node = calloc(1, sizeof(IsoSpecial)); n = strdup(name);
if (node == NULL) { ret = iso_node_new_special(n, mode, dev, &node);
return ISO_OUT_OF_MEM; if (ret < 0) {
free(n);
return ret;
} }
node->node.refcount = 1;
node->node.type = LIBISO_SPECIAL;
node->node.name = strdup(name);
if (node->node.name == NULL) {
free(node);
return ISO_OUT_OF_MEM;
}
node->node.mode = mode;
node->dev = dev;
/* atts from parent */ /* atts from parent */
node->node.uid = parent->node.uid; iso_node_set_uid((IsoNode*)node, parent->node.uid);
node->node.gid = parent->node.gid; iso_node_set_gid((IsoNode*)node, parent->node.gid);
node->node.hidden = parent->node.hidden; iso_node_set_hidden((IsoNode*)node, parent->node.hidden);
/* current time */ /* current time */
now = time(NULL); now = time(NULL);
node->node.atime = now; iso_node_set_atime((IsoNode*)node, now);
node->node.ctime = now; iso_node_set_ctime((IsoNode*)node, now);
node->node.mtime = now; iso_node_set_mtime((IsoNode*)node, now);
if (special) { if (special) {
*special = node; *special = node;