diff --git a/libisofs/libisofs.h b/libisofs/libisofs.h index a48666d..a5bd199 100644 --- a/libisofs/libisofs.h +++ b/libisofs/libisofs.h @@ -2731,6 +2731,35 @@ void iso_tree_set_report_callback(IsoImage *image, int iso_tree_add_node(IsoImage *image, IsoDir *parent, const char *path, IsoNode **node); +/** + * Add a new node to the image tree, from an existing file, and with the + * given name, that must not exist on dir. + * + * @param image + * The image + * @param parent + * The directory in the image tree where the node will be added. + * @param name + * The name that the node will have on image. + * @param path + * The path of the file to add in the filesystem. + * @param node + * place where to store a pointer to the newly added file. No + * extra ref is addded, so you will need to call iso_node_ref() if you + * really need it. You can pass NULL in this parameter if you don't need + * the pointer. + * @return + * number of nodes in parent if success, < 0 otherwise + * Possible errors: + * ISO_NULL_POINTER, if image, parent or path are NULL + * ISO_NODE_NAME_NOT_UNIQUE, a node with same name already exists + * ISO_OUT_OF_MEM + * + * @since 0.6.4 + */ +int iso_tree_add_new_node(IsoImage *image, IsoDir *parent, const char *name, + const char *path, IsoNode **node); + /** * Add the contents of a dir to a given directory of the iso tree. * diff --git a/libisofs/tree.c b/libisofs/tree.c index 5dc360e..420b611 100644 --- a/libisofs/tree.c +++ b/libisofs/tree.c @@ -475,6 +475,59 @@ int iso_tree_add_node(IsoImage *image, IsoDir *parent, const char *path, return result; } +int iso_tree_add_new_node(IsoImage *image, IsoDir *parent, const char *name, + const char *path, IsoNode **node) +{ + int result; + IsoFilesystem *fs; + IsoFileSource *file; + IsoNode *new; + IsoNode **pos; + + if (image == NULL || parent == NULL || name == NULL || path == NULL) { + return ISO_NULL_POINTER; + } + + if (node) { + *node = NULL; + } + + fs = image->fs; + result = fs->get_by_path(fs, path, &file); + if (result < 0) { + return result; + } + + /* find place where to insert */ + result = iso_dir_exists(parent, name, &pos); + if (result) { + /* a node with same name already exists */ + iso_file_source_unref(file); + return ISO_NODE_NAME_NOT_UNIQUE; + } + + result = image->builder->create_node(image->builder, image, file, &new); + if (result < 0) { + return result; + } + + /* free the file */ + iso_file_source_unref(file); + + result = iso_node_set_name(new, name); + if (result < 0) { + iso_node_unref(new); + return result; + } + + if (node) { + *node = new; + } + + /* finally, add node to parent */ + return iso_dir_insert(parent, new, pos, ISO_REPLACE_NEVER); +} + static int check_excludes(IsoImage *image, const char *path) {