Blame fstree.c

Packit 2ad57b
/*
Packit 2ad57b
 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
Packit 2ad57b
 *
Packit 2ad57b
 *
Packit 2ad57b
 * This program is free software; you can redistribute it and/or
Packit 2ad57b
 * modify it under the terms of the GNU General Public License as
Packit 2ad57b
 * published by the Free Software Foundation; either version 2 of the
Packit 2ad57b
 * License, or (at your option) any later version.
Packit 2ad57b
 *
Packit 2ad57b
 *  This program is distributed in the hope that it will be useful,
Packit 2ad57b
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 2ad57b
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 2ad57b
 *  General Public License for more details.
Packit 2ad57b
 *
Packit 2ad57b
 *  You should have received a copy of the GNU General Public License
Packit 2ad57b
 *  along with this program; if not, write to the Free Software
Packit 2ad57b
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
Packit 2ad57b
 *                                                                   USA
Packit 2ad57b
 */
Packit 2ad57b
Packit 2ad57b
#include "dtc.h"
Packit 2ad57b
Packit 2ad57b
#include <dirent.h>
Packit 2ad57b
#include <sys/stat.h>
Packit 2ad57b
Packit 2ad57b
static struct node *read_fstree(const char *dirname)
Packit 2ad57b
{
Packit 2ad57b
	DIR *d;
Packit 2ad57b
	struct dirent *de;
Packit 2ad57b
	struct stat st;
Packit 2ad57b
	struct node *tree;
Packit 2ad57b
Packit 2ad57b
	d = opendir(dirname);
Packit 2ad57b
	if (!d)
Packit 2ad57b
		die("Couldn't opendir() \"%s\": %s\n", dirname, strerror(errno));
Packit 2ad57b
Packit 2ad57b
	tree = build_node(NULL, NULL);
Packit 2ad57b
Packit 2ad57b
	while ((de = readdir(d)) != NULL) {
Packit 2ad57b
		char *tmpname;
Packit 2ad57b
Packit 2ad57b
		if (streq(de->d_name, ".")
Packit 2ad57b
		    || streq(de->d_name, ".."))
Packit 2ad57b
			continue;
Packit 2ad57b
Packit 2ad57b
		tmpname = join_path(dirname, de->d_name);
Packit 2ad57b
Packit 2ad57b
		if (lstat(tmpname, &st) < 0)
Packit 2ad57b
			die("stat(%s): %s\n", tmpname, strerror(errno));
Packit 2ad57b
Packit 2ad57b
		if (S_ISREG(st.st_mode)) {
Packit 2ad57b
			struct property *prop;
Packit 2ad57b
			FILE *pfile;
Packit 2ad57b
Packit 2ad57b
			pfile = fopen(tmpname, "rb");
Packit 2ad57b
			if (! pfile) {
Packit 2ad57b
				fprintf(stderr,
Packit 2ad57b
					"WARNING: Cannot open %s: %s\n",
Packit 2ad57b
					tmpname, strerror(errno));
Packit 2ad57b
			} else {
Packit 2ad57b
				prop = build_property(xstrdup(de->d_name),
Packit 2ad57b
						      data_copy_file(pfile,
Packit 2ad57b
								     st.st_size));
Packit 2ad57b
				add_property(tree, prop);
Packit 2ad57b
				fclose(pfile);
Packit 2ad57b
			}
Packit 2ad57b
		} else if (S_ISDIR(st.st_mode)) {
Packit 2ad57b
			struct node *newchild;
Packit 2ad57b
Packit 2ad57b
			newchild = read_fstree(tmpname);
Packit 2ad57b
			newchild = name_node(newchild, xstrdup(de->d_name));
Packit 2ad57b
			add_child(tree, newchild);
Packit 2ad57b
		}
Packit 2ad57b
Packit 2ad57b
		free(tmpname);
Packit 2ad57b
	}
Packit 2ad57b
Packit 2ad57b
	closedir(d);
Packit 2ad57b
	return tree;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
struct dt_info *dt_from_fs(const char *dirname)
Packit 2ad57b
{
Packit 2ad57b
	struct node *tree;
Packit 2ad57b
Packit 2ad57b
	tree = read_fstree(dirname);
Packit 2ad57b
	tree = name_node(tree, "");
Packit 2ad57b
Packit 2ad57b
	return build_dt_info(DTSF_V1, NULL, tree, guess_boot_cpuid(tree));
Packit 2ad57b
}