fstree.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include "dtc.h"
  21. #include <dirent.h>
  22. #include <sys/stat.h>
  23. static struct node *read_fstree(const char *dirname)
  24. {
  25. DIR *d;
  26. struct dirent *de;
  27. struct stat st;
  28. struct node *tree;
  29. d = opendir(dirname);
  30. if (!d)
  31. die("Couldn't opendir() \"%s\": %s\n", dirname, strerror(errno));
  32. tree = build_node(NULL, NULL);
  33. while ((de = readdir(d)) != NULL) {
  34. char *tmpname;
  35. if (streq(de->d_name, ".")
  36. || streq(de->d_name, ".."))
  37. continue;
  38. tmpname = join_path(dirname, de->d_name);
  39. if (lstat(tmpname, &st) < 0)
  40. die("stat(%s): %s\n", tmpname, strerror(errno));
  41. if (S_ISREG(st.st_mode)) {
  42. struct property *prop;
  43. FILE *pfile;
  44. pfile = fopen(tmpname, "rb");
  45. if (! pfile) {
  46. fprintf(stderr,
  47. "WARNING: Cannot open %s: %s\n",
  48. tmpname, strerror(errno));
  49. } else {
  50. prop = build_property(xstrdup(de->d_name),
  51. data_copy_file(pfile,
  52. st.st_size));
  53. add_property(tree, prop);
  54. fclose(pfile);
  55. }
  56. } else if (S_ISDIR(st.st_mode)) {
  57. struct node *newchild;
  58. newchild = read_fstree(tmpname);
  59. newchild = name_node(newchild, xstrdup(de->d_name));
  60. add_child(tree, newchild);
  61. }
  62. free(tmpname);
  63. }
  64. closedir(d);
  65. return tree;
  66. }
  67. struct boot_info *dt_from_fs(const char *dirname)
  68. {
  69. struct node *tree;
  70. tree = read_fstree(dirname);
  71. tree = name_node(tree, "");
  72. return build_boot_info(NULL, tree, guess_boot_cpuid(tree));
  73. }