symbian-qemu-0.9.1-12/dtc-trunk/fstree.c
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     1 /*
       
     2  * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
       
     3  *
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU General Public License as
       
     7  * published by the Free Software Foundation; either version 2 of the
       
     8  * License, or (at your option) any later version.
       
     9  *
       
    10  *  This program is distributed in the hope that it will be useful,
       
    11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  *  General Public License for more details.
       
    14  *
       
    15  *  You should have received a copy of the GNU General Public License
       
    16  *  along with this program; if not, write to the Free Software
       
    17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
       
    18  *                                                                   USA
       
    19  */
       
    20 
       
    21 #include "dtc.h"
       
    22 
       
    23 #include <dirent.h>
       
    24 #include <sys/stat.h>
       
    25 
       
    26 #ifdef _WIN32
       
    27 #define lstat stat
       
    28 #endif
       
    29 
       
    30 static struct node *read_fstree(const char *dirname)
       
    31 {
       
    32 	DIR *d;
       
    33 	struct dirent *de;
       
    34 	struct stat st;
       
    35 	struct node *tree;
       
    36 
       
    37 	d = opendir(dirname);
       
    38 	if (!d)
       
    39 		die("Couldn't opendir() \"%s\": %s\n", dirname, strerror(errno));
       
    40 
       
    41 	tree = build_node(NULL, NULL);
       
    42 
       
    43 	while ((de = readdir(d)) != NULL) {
       
    44 		char *tmpnam;
       
    45 
       
    46 		if (streq(de->d_name, ".")
       
    47 		    || streq(de->d_name, ".."))
       
    48 			continue;
       
    49 
       
    50 		tmpnam = join_path(dirname, de->d_name);
       
    51 
       
    52 		if (lstat(tmpnam, &st) < 0)
       
    53 			die("stat(%s): %s\n", tmpnam, strerror(errno));
       
    54 
       
    55 		if (S_ISREG(st.st_mode)) {
       
    56 			struct property *prop;
       
    57 			FILE *pfile;
       
    58 
       
    59 			pfile = fopen(tmpnam, "r");
       
    60 			if (! pfile) {
       
    61 				fprintf(stderr,
       
    62 					"WARNING: Cannot open %s: %s\n",
       
    63 					tmpnam, strerror(errno));
       
    64 			} else {
       
    65 				prop = build_property(strdup(de->d_name),
       
    66 						      data_copy_file(pfile,
       
    67 								     st.st_size),
       
    68 						      NULL);
       
    69 				add_property(tree, prop);
       
    70 				fclose(pfile);
       
    71 			}
       
    72 		} else if (S_ISDIR(st.st_mode)) {
       
    73 			struct node *newchild;
       
    74 
       
    75 			newchild = read_fstree(tmpnam);
       
    76 			newchild = name_node(newchild, strdup(de->d_name),
       
    77 					     NULL);
       
    78 			add_child(tree, newchild);
       
    79 		}
       
    80 
       
    81 		free(tmpnam);
       
    82 	}
       
    83 
       
    84 	return tree;
       
    85 }
       
    86 
       
    87 struct boot_info *dt_from_fs(const char *dirname)
       
    88 {
       
    89 	struct node *tree;
       
    90 
       
    91 	tree = read_fstree(dirname);
       
    92 	tree = name_node(tree, "", NULL);
       
    93 
       
    94 	return build_boot_info(NULL, tree, 0);
       
    95 }
       
    96