moduos_file.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017 SummerGift <zhangyuan@rt-thread.com>
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "py/mpconfig.h"
  27. #if MICROPY_PY_MODUOS_FILE
  28. #include <stdint.h>
  29. #include <string.h>
  30. #include <dfs_posix.h>
  31. #include "py/runtime.h"
  32. #include "py/objstr.h"
  33. #include "py/mperrno.h"
  34. #include "moduos_file.h"
  35. mp_obj_t mp_posix_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  36. return mp_const_none;
  37. }
  38. MP_DEFINE_CONST_FUN_OBJ_KW(mp_posix_mount_obj, 2, mp_posix_mount);
  39. mp_obj_t mp_posix_umount(mp_obj_t mnt_in) {
  40. return mp_const_none;
  41. }
  42. MP_DEFINE_CONST_FUN_OBJ_1(mp_posix_umount_obj, mp_posix_umount);
  43. mp_obj_t mp_posix_chdir(mp_obj_t path_in) {
  44. const char *changepath = mp_obj_str_get_str(path_in);
  45. if (chdir(changepath) != 0) {
  46. rt_kprintf("No such directory: %s\n", changepath);
  47. }
  48. return mp_const_none;
  49. }
  50. MP_DEFINE_CONST_FUN_OBJ_1(mp_posix_chdir_obj, mp_posix_chdir);
  51. mp_obj_t mp_posix_getcwd(void) {
  52. char buf[MICROPY_ALLOC_PATH_MAX + 1];
  53. getcwd(buf, sizeof(buf));
  54. return mp_obj_new_str(buf, strlen(buf));
  55. }
  56. MP_DEFINE_CONST_FUN_OBJ_0(mp_posix_getcwd_obj, mp_posix_getcwd);
  57. #include <dfs_file.h>
  58. static struct dfs_fd fd;
  59. static struct dirent dirent;
  60. mp_obj_t mp_posix_listdir(size_t n_args, const mp_obj_t *args) {
  61. mp_obj_t dir_list = mp_obj_new_list(0, NULL);
  62. struct stat stat;
  63. int length;
  64. char *fullpath, *path;
  65. const char *pathname;
  66. if (n_args == 0) {
  67. #ifdef DFS_USING_WORKDIR
  68. extern char working_directory[];
  69. pathname = working_directory;
  70. #else
  71. pathname = "/";
  72. #endif
  73. } else {
  74. pathname = mp_obj_str_get_str(args[0]);
  75. }
  76. fullpath = NULL;
  77. if (pathname == NULL)
  78. {
  79. #ifdef DFS_USING_WORKDIR
  80. extern char working_directory[];
  81. /* open current working directory */
  82. path = rt_strdup(working_directory);
  83. #else
  84. path = rt_strdup("/");
  85. #endif
  86. if (path == NULL)
  87. mp_raise_OSError(MP_ENOMEM); /* out of memory */
  88. }
  89. else
  90. {
  91. path = (char *)pathname;
  92. }
  93. /* list directory */
  94. if (dfs_file_open(&fd, path, O_DIRECTORY) == 0)
  95. {
  96. do {
  97. memset(&dirent, 0, sizeof(struct dirent));
  98. length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent));
  99. if (length > 0) {
  100. memset(&stat, 0, sizeof(struct stat));
  101. /* build full path for each file */
  102. fullpath = dfs_normalize_path(path, dirent.d_name);
  103. if (fullpath == NULL)
  104. break;
  105. if (dfs_file_stat(fullpath, &stat) == 0) {
  106. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
  107. t->items[0] = mp_obj_new_str(dirent.d_name, strlen(dirent.d_name));
  108. t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR);
  109. t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // no inode number
  110. mp_obj_t next = MP_OBJ_FROM_PTR(t);
  111. mp_obj_t *items;
  112. mp_obj_get_array_fixed_n(next, 3, &items);
  113. mp_obj_list_append(dir_list, items[0]);
  114. } else {
  115. rt_kprintf("BAD file: %s\n", dirent.d_name);
  116. }
  117. rt_free(fullpath);
  118. }
  119. } while (length > 0);
  120. dfs_file_close(&fd);
  121. }
  122. else
  123. {
  124. rt_kprintf("No such directory\n");
  125. }
  126. if (pathname == NULL)
  127. rt_free(path);
  128. return dir_list;
  129. }
  130. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_posix_listdir_obj, 0, 1, mp_posix_listdir);
  131. mp_obj_t mp_posix_mkdir(mp_obj_t path_in) {
  132. const char *createpath = mp_obj_str_get_str(path_in);
  133. int res = mkdir(createpath, 0);
  134. if (res != 0) {
  135. mp_raise_OSError(MP_EEXIST);
  136. }
  137. return mp_const_none;
  138. }
  139. MP_DEFINE_CONST_FUN_OBJ_1(mp_posix_mkdir_obj, mp_posix_mkdir);
  140. mp_obj_t mp_posix_remove(uint n_args, const mp_obj_t *arg) {
  141. int index;
  142. if (n_args == 0) {
  143. rt_kprintf("Usage: rm FILE...\n");
  144. rt_kprintf("Remove (unlink) the FILE(s).\n");
  145. return mp_const_none;
  146. }
  147. for (index = 0; index < n_args; index++) {
  148. //rt_kprintf("Remove %s.\n", mp_obj_str_get_str(arg[index]));
  149. unlink(mp_obj_str_get_str(arg[index]));
  150. }
  151. // TODO recursive deletion
  152. return mp_const_none;
  153. }
  154. MP_DEFINE_CONST_FUN_OBJ_VAR(mp_posix_remove_obj, 0, mp_posix_remove);
  155. mp_obj_t mp_posix_rename(mp_obj_t old_path_in, mp_obj_t new_path_in) {
  156. const char *old_path = mp_obj_str_get_str(old_path_in);
  157. const char *new_path = mp_obj_str_get_str(new_path_in);
  158. int res = rename(old_path, new_path);
  159. if (res != 0) {
  160. mp_raise_OSError(MP_EPERM);
  161. }
  162. return mp_const_none;
  163. }
  164. MP_DEFINE_CONST_FUN_OBJ_2(mp_posix_rename_obj, mp_posix_rename);
  165. mp_obj_t mp_posix_rmdir(uint n_args, const mp_obj_t *arg) {
  166. int index;
  167. if (n_args == 0) {
  168. rt_kprintf("Usage: rm FILE...\n");
  169. rt_kprintf("Remove (unlink) the FILE(s).\n");
  170. return mp_const_none;
  171. }
  172. for (index = 0; index < n_args; index++) {
  173. //rt_kprintf("Remove %s.\n", mp_obj_str_get_str(arg[index]));
  174. rmdir(mp_obj_str_get_str(arg[index]));
  175. }
  176. // TODO recursive deletion
  177. return mp_const_none;
  178. }
  179. MP_DEFINE_CONST_FUN_OBJ_VAR(mp_posix_rmdir_obj, 0, mp_posix_rmdir);
  180. mp_obj_t mp_posix_stat(mp_obj_t path_in) {
  181. struct stat buf;
  182. const char *createpath = mp_obj_str_get_str(path_in);
  183. int res = stat(createpath, &buf);
  184. if (res != 0) {
  185. mp_raise_OSError(MP_EPERM);
  186. }
  187. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
  188. t->items[0] = MP_OBJ_NEW_SMALL_INT(buf.st_mode); // st_mode
  189. t->items[1] = MP_OBJ_NEW_SMALL_INT(buf.st_ino); // st_ino
  190. t->items[2] = MP_OBJ_NEW_SMALL_INT(buf.st_dev); // st_dev
  191. t->items[3] = MP_OBJ_NEW_SMALL_INT(buf.st_nlink); // st_nlink
  192. t->items[4] = MP_OBJ_NEW_SMALL_INT(buf.st_uid); // st_uid
  193. t->items[5] = MP_OBJ_NEW_SMALL_INT(buf.st_gid); // st_gid
  194. t->items[6] = mp_obj_new_int_from_uint(buf.st_size); // st_size
  195. t->items[7] = MP_OBJ_NEW_SMALL_INT(buf.st_atime); // st_atime
  196. t->items[8] = MP_OBJ_NEW_SMALL_INT(buf.st_mtime); // st_mtime
  197. t->items[9] = MP_OBJ_NEW_SMALL_INT(buf.st_ctime); // st_ctime
  198. return MP_OBJ_FROM_PTR(t);
  199. }
  200. MP_DEFINE_CONST_FUN_OBJ_1(mp_posix_stat_obj, mp_posix_stat);
  201. mp_import_stat_t mp_posix_import_stat(const char *path) {
  202. struct stat stat;
  203. if (dfs_file_stat(path, &stat) == 0) {
  204. if (S_ISDIR(stat.st_mode)) {
  205. return MP_IMPORT_STAT_DIR;
  206. } else {
  207. return MP_IMPORT_STAT_FILE;
  208. }
  209. } else {
  210. return MP_IMPORT_STAT_NO_EXIST;
  211. }
  212. }
  213. #endif //MICROPY_MODUOS_FILE