debug.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2009 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <debug.h>
  24. #include <string.h>
  25. #include <lib/console.h>
  26. #include <lib/fs.h>
  27. #include <stdlib.h>
  28. #include <platform.h>
  29. #if defined(WITH_LIB_CONSOLE)
  30. #if DEBUGLEVEL > 1
  31. static int cmd_fs(int argc, const cmd_args *argv);
  32. STATIC_COMMAND_START
  33. STATIC_COMMAND("fs", "fs debug commands", &cmd_fs)
  34. STATIC_COMMAND_END(fs);
  35. extern int fs_mount_type(const char *path, const char *device, const char *name);
  36. extern int fs_create_file(const char *path, filecookie *fcookie);
  37. extern int fs_make_dir(const char *path);
  38. extern int fs_write_file(filecookie fcookie, const void *buf, off_t offset, size_t len);
  39. static int cmd_fs(int argc, const cmd_args *argv)
  40. {
  41. int rc = 0;
  42. if (argc < 2) {
  43. notenoughargs:
  44. printf("not enough arguments:\n");
  45. usage:
  46. printf("%s mount <path> <device> [<type>]\n", argv[0].str);
  47. printf("%s unmount <path>\n", argv[0].str);
  48. printf("%s create <path>\n", argv[0].str);
  49. printf("%s mkdir <path>\n", argv[0].str);
  50. printf("%s read <path> [<offset>] [<len>]\n", argv[0].str);
  51. printf("%s write <path> <string> [<offset>]\n", argv[0].str);
  52. printf("%s stat <file>\n", argv[0].str);
  53. return -1;
  54. }
  55. if (!strcmp(argv[1].str, "mount")) {
  56. int err;
  57. if (argc < 4)
  58. goto notenoughargs;
  59. if (argc < 5)
  60. err = fs_mount(argv[2].str, argv[3].str);
  61. else
  62. err = fs_mount_type(argv[2].str, argv[3].str, argv[4].str);
  63. if (err < 0) {
  64. printf("error %d mounting device\n", err);
  65. return err;
  66. }
  67. } else if (!strcmp(argv[1].str, "unmount")) {
  68. int err;
  69. if (argc < 3)
  70. goto notenoughargs;
  71. err = fs_unmount(argv[2].str);
  72. if (err < 0) {
  73. printf("error %d unmounting device\n", err);
  74. return err;
  75. }
  76. } else if (!strcmp(argv[1].str, "create")) {
  77. int err;
  78. filecookie cookie;
  79. if (argc < 3)
  80. goto notenoughargs;
  81. err = fs_create_file(argv[2].str, &cookie);
  82. if (err < 0) {
  83. printf("error %d creating file\n", err);
  84. return err;
  85. }
  86. fs_close_file(cookie);
  87. } else if (!strcmp(argv[1].str, "mkdir")) {
  88. int err;
  89. if (argc < 3)
  90. goto notenoughargs;
  91. err = fs_make_dir(argv[2].str);
  92. if (err < 0) {
  93. printf("error %d making directory\n", err);
  94. return err;
  95. }
  96. } else if (!strcmp(argv[1].str, "read")) {
  97. int err;
  98. char *buf;
  99. off_t off;
  100. size_t len;
  101. filecookie cookie;
  102. struct file_stat stat;
  103. if (argc < 3)
  104. goto notenoughargs;
  105. err = fs_open_file(argv[2].str, &cookie);
  106. if (err < 0) {
  107. printf("error %d opening file\n", err);
  108. return err;
  109. }
  110. err = fs_stat_file(cookie, &stat);
  111. if (err < 0) {
  112. printf("error %d stat'ing file\n", err);
  113. fs_close_file(cookie);
  114. return err;
  115. }
  116. if (argc < 4)
  117. off = 0;
  118. else
  119. off = argv[3].u;
  120. if (argc < 5)
  121. len = stat.size - off;
  122. else
  123. len = argv[4].u;
  124. buf = malloc(len + 1);
  125. err = fs_read_file(cookie, buf, off, len);
  126. if (err < 0) {
  127. printf("error %d reading file\n", err);
  128. free(buf);
  129. fs_close_file(cookie);
  130. return err;
  131. }
  132. buf[len] = '\0';
  133. printf("%s\n", buf);
  134. free(buf);
  135. fs_close_file(cookie);
  136. } else if (!strcmp(argv[1].str, "write")) {
  137. int err;
  138. off_t off;
  139. filecookie cookie;
  140. struct file_stat stat;
  141. if (argc < 3)
  142. goto notenoughargs;
  143. err = fs_open_file(argv[2].str, &cookie);
  144. if (err < 0) {
  145. printf("error %d opening file\n", err);
  146. return err;
  147. }
  148. err = fs_stat_file(cookie, &stat);
  149. if (err < 0) {
  150. printf("error %d stat'ing file\n", err);
  151. fs_close_file(cookie);
  152. return err;
  153. }
  154. if (argc < 5)
  155. off = stat.size;
  156. else
  157. off = argv[4].u;
  158. err = fs_write_file(cookie, argv[3].str, off, strlen(argv[3].str));
  159. if (err < 0) {
  160. printf("error %d writing file\n", err);
  161. fs_close_file(cookie);
  162. return err;
  163. }
  164. fs_close_file(cookie);
  165. } else if (!strcmp(argv[1].str, "stat")) {
  166. int err;
  167. struct file_stat stat;
  168. filecookie cookie;
  169. if (argc < 3)
  170. goto notenoughargs;
  171. err = fs_open_file(argv[2].str, &cookie);
  172. if (err < 0) {
  173. printf("error %d opening file\n", err);
  174. return err;
  175. }
  176. err = fs_stat_file(cookie, &stat);
  177. if (err < 0) {
  178. printf("error %d statting file\n", err);
  179. fs_close_file(cookie);
  180. return err;
  181. }
  182. printf("stat successful:\n");
  183. printf("\tis_dir: %d\n", stat.is_dir ? 1 : 0);
  184. printf("\tsize: %lld\n", stat.size);
  185. fs_close_file(cookie);
  186. } else {
  187. printf("unrecognized subcommand\n");
  188. goto usage;
  189. }
  190. return rc;
  191. }
  192. #endif
  193. #endif