/* vim: set noet ts=8 sts=8 sw=8 : */

/**
 * \file tree.h
 *
 * Declare the structure of a libisofs filesystem tree. The files in this
 * tree can come from either the local filesystem or from another .iso image
 * (for multisession).
 *
 * This tree preserves as much information as it can about the files; names
 * are stored in wchar_t and we preserve POSIX attributes. This tree does
 * *not* include information that is necessary for writing out, for example,
 * an ISO level 1 tree. That information will go in a different tree because
 * the structure is sufficiently different.
 */

#ifndef LIBISO_TREE_H
#define LIBISO_TREE_H

#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#include <wchar.h>

#include "libisofs.h"

//enum file_location {
//	LIBISO_FILESYS,
//	LIBISO_PREVSESSION,
//	LIBISO_NONE		/**< for files/dirs that were added with
//				  * iso_tree_add_new_XXX. */
//};

/**
 * This tells us where to read the data from a file. Either we read from the
 * local filesystem or we just point to the block on a previous session.
 */
//struct iso_file_location
//{
//	enum file_location type;
//  /*	union {*/
//		char *path;	/* in the current locale */
//		uint32_t block;
//  /*	};*/
//};

enum iso_tree_node_type {
	LIBISO_NODE_DIR,
	LIBISO_NODE_FILE,
	LIBISO_NODE_SYMLINK,
	LIBISO_NODE_BOOTCATALOG
};

/**
 * A node in the filesystem tree.
 */
struct iso_tree_node
{
	struct iso_tree_node_dir *parent;
	char *name;
	struct stat attrib;	/**< The POSIX attributes of this node as
				  * documented in "man 2 stat". */
				  
	int hide_flags; /**< If the node is to be hidden in RR/ISO or
	                 *   Joilet tree */
	enum iso_tree_node_type type;
};

/**
 * A node in the filesystem tree that represents a regular file
 */
struct iso_tree_node_file
{
	struct iso_tree_node node;
	
	char *path; /**< the path of the file on local filesystem */
	int sort_weight; /**< It sorts the order in which the file data is
	                  *   written to the CD image. Higher weighting files
	                  *   are written at the beginning of image */
	
	/* when read from an existing ISO image, we need to store the
	 * block where file contents are written, and not the path.
	 * Maybe instead of a char *path we will need to go back to
	 * struct iso_file_location loc;
	 */
	/* struct iso_file_location loc; */
				/**< Only used for regular files and symbolic
				 * links (ie. files for which we might have to
				 * copy data). */
};

/**
 * A node in the filesystem tree that represents a symbolic link
 */
struct iso_tree_node_symlink
{
	struct iso_tree_node node;
	
	char *dest; /**< Destination of the link */
};

/**
 * A directory on the filesystem tree
 */
struct iso_tree_node_dir
{
	struct iso_tree_node node;

	size_t nchildren;	/**< The number of children of this
				  * directory (if this is a directory). */
	struct iso_tree_node **children;
};

/**
 * Recursively free a directory.
 *
 * \param root The root of the directory heirarchy to free.
 *
 * \pre \p root is non-NULL.
 */
void iso_tree_free(struct iso_tree_node *root);

/**
 * Adds a child to a directory
 */
void iso_tree_add_child(struct iso_tree_node_dir *parent,
	             struct iso_tree_node *child);

/**
 * A function that prints verbose information about a directory.
 *
 * \param dir The directory about which to print information.
 * \param data Unspecified function-dependent data.
 * \param spaces The number of spaces to prepend to the output.
 *
 * \see iso_tree_print_verbose
 */
typedef void (*print_dir_callback) (const struct iso_tree_node *dir,
				    void *data,
				    int spaces);
/**
 * A function that prints verbose information about a file.
 *
 * \param dir The file about which to print information.
 * \param data Unspecified function-dependent data.
 * \param spaces The number of spaces to prepend to the output.
 *
 * \see iso_tree_print_verbose
 */
typedef void (*print_file_callback) (const struct iso_tree_node *file,
				     void *data,
				     int spaces);

/**
 * Recursively print a directory heirarchy. For each node in the directory
 * heirarchy, call a callback function to print information more verbosely.
 *
 * \param root The root of the directory heirarchy to print.
 * \param dir The callback function to call for each directory in the tree.
 * \param file The callback function to call for each file in the tree.
 * \param callback_data The data to pass to the callback functions.
 * \param spaces The number of spaces to prepend to the output.
 *
 * \pre \p root is not NULL.
 * \pre Neither of the callback functions modifies the directory heirarchy.
 */
void iso_tree_print_verbose(const struct iso_tree_node *root,
			    print_dir_callback dir,
			    print_file_callback file,
			    void *callback_data,
			    int spaces);

#define ISO_ISDIR(n) (n->type == LIBISO_NODE_DIR)
#define ISO_ISREG(n) (n->type == LIBISO_NODE_FILE)
#define ISO_ISLNK(n) (n->type == LIBISO_NODE_SYMLINK)

#endif /* LIBISO_TREE_H */
