1204431Sraj/* 2204431Sraj * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005. 3204431Sraj * 4204431Sraj * 5204431Sraj * This program is free software; you can redistribute it and/or 6204431Sraj * modify it under the terms of the GNU General Public License as 7204431Sraj * published by the Free Software Foundation; either version 2 of the 8204431Sraj * License, or (at your option) any later version. 9204431Sraj * 10204431Sraj * This program is distributed in the hope that it will be useful, 11204431Sraj * but WITHOUT ANY WARRANTY; without even the implied warranty of 12204431Sraj * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13204431Sraj * General Public License for more details. 14204431Sraj * 15204431Sraj * You should have received a copy of the GNU General Public License 16204431Sraj * along with this program; if not, write to the Free Software 17204431Sraj * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 18204431Sraj * USA 19204431Sraj */ 20204431Sraj 21204431Sraj#include "dtc.h" 22204431Sraj 23204431Sraj#include <dirent.h> 24204431Sraj#include <sys/stat.h> 25204431Sraj 26204431Srajstatic struct node *read_fstree(const char *dirname) 27204431Sraj{ 28204431Sraj DIR *d; 29204431Sraj struct dirent *de; 30204431Sraj struct stat st; 31204431Sraj struct node *tree; 32204431Sraj 33204431Sraj d = opendir(dirname); 34204431Sraj if (!d) 35204431Sraj die("Couldn't opendir() \"%s\": %s\n", dirname, strerror(errno)); 36204431Sraj 37204431Sraj tree = build_node(NULL, NULL); 38204431Sraj 39204431Sraj while ((de = readdir(d)) != NULL) { 40318102Sgonzo char *tmpname; 41204431Sraj 42204431Sraj if (streq(de->d_name, ".") 43204431Sraj || streq(de->d_name, "..")) 44204431Sraj continue; 45204431Sraj 46318102Sgonzo tmpname = join_path(dirname, de->d_name); 47204431Sraj 48318102Sgonzo if (lstat(tmpname, &st) < 0) 49318102Sgonzo die("stat(%s): %s\n", tmpname, strerror(errno)); 50204431Sraj 51204431Sraj if (S_ISREG(st.st_mode)) { 52204431Sraj struct property *prop; 53204431Sraj FILE *pfile; 54204431Sraj 55318102Sgonzo pfile = fopen(tmpname, "rb"); 56204431Sraj if (! pfile) { 57204431Sraj fprintf(stderr, 58204431Sraj "WARNING: Cannot open %s: %s\n", 59318102Sgonzo tmpname, strerror(errno)); 60204431Sraj } else { 61204433Sraj prop = build_property(xstrdup(de->d_name), 62204431Sraj data_copy_file(pfile, 63238742Simp st.st_size)); 64204431Sraj add_property(tree, prop); 65204431Sraj fclose(pfile); 66204431Sraj } 67204431Sraj } else if (S_ISDIR(st.st_mode)) { 68204431Sraj struct node *newchild; 69204431Sraj 70318102Sgonzo newchild = read_fstree(tmpname); 71238742Simp newchild = name_node(newchild, xstrdup(de->d_name)); 72204431Sraj add_child(tree, newchild); 73204431Sraj } 74204431Sraj 75318102Sgonzo free(tmpname); 76204431Sraj } 77204431Sraj 78238742Simp closedir(d); 79204431Sraj return tree; 80204431Sraj} 81204431Sraj 82318102Sgonzostruct dt_info *dt_from_fs(const char *dirname) 83204431Sraj{ 84204431Sraj struct node *tree; 85204431Sraj 86204431Sraj tree = read_fstree(dirname); 87238742Simp tree = name_node(tree, ""); 88204431Sraj 89318102Sgonzo return build_dt_info(DTSF_V1, NULL, tree, guess_boot_cpuid(tree)); 90204431Sraj} 91