moduos_file.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2017 Damien P. George
  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 <stdint.h>
  27. #include <string.h>
  28. #include "py/runtime.h"
  29. #include "py/objstr.h"
  30. #include "py/mperrno.h"
  31. #include "moduos_file.h"
  32. #if MICROPY_MODUOS_FILE
  33. mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  34. return mp_const_none;
  35. }
  36. MP_DEFINE_CONST_FUN_OBJ_KW(mp_vfs_mount_obj, 2, mp_vfs_mount);
  37. mp_obj_t mp_vfs_umount(mp_obj_t mnt_in) {
  38. return mp_const_none;
  39. }
  40. MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_umount_obj, mp_vfs_umount);
  41. mp_obj_t mp_vfs_chdir(mp_obj_t path_in) {
  42. char *changepath = mp_obj_str_get_str(path_in);
  43. if (chdir(changepath) != 0) {
  44. rt_kprintf("No such directory: %s\n", changepath);
  45. }
  46. return mp_const_none;
  47. }
  48. MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_chdir_obj, mp_vfs_chdir);
  49. mp_obj_t mp_vfs_getcwd(void) {
  50. char buf[MICROPY_ALLOC_PATH_MAX + 1];
  51. getcwd(buf, sizeof(buf));
  52. return mp_obj_new_str(buf, strlen(buf), false);
  53. }
  54. MP_DEFINE_CONST_FUN_OBJ_0(mp_vfs_getcwd_obj, mp_vfs_getcwd);
  55. mp_obj_t mp_vfs_listdir(size_t n_args, const mp_obj_t *args) {
  56. extern void ls(const char *pathname);
  57. if (n_args == 0) {
  58. #ifdef DFS_USING_WORKDIR
  59. extern char working_directory[];
  60. ls(working_directory);
  61. #else
  62. ls("/");
  63. #endif
  64. } else {
  65. ls(mp_obj_str_get_str(args[0]));
  66. }
  67. return mp_const_none;
  68. }
  69. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_vfs_listdir_obj, 0, 1, mp_vfs_listdir);
  70. mp_obj_t mp_vfs_mkdir(mp_obj_t path_in) {
  71. char *createpath = mp_obj_str_get_str(path_in);
  72. int res = mkdir(createpath, 0);
  73. if (res != 0) {
  74. mp_raise_OSError(MP_EEXIST);
  75. }
  76. return mp_const_none;
  77. }
  78. MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_mkdir_obj, mp_vfs_mkdir);
  79. mp_obj_t mp_vfs_remove(uint n_args, const mp_obj_t *arg) {
  80. int index;
  81. if (n_args == 0) {
  82. rt_kprintf("Usage: rm FILE...\n");
  83. rt_kprintf("Remove (unlink) the FILE(s).\n");
  84. return mp_const_none;
  85. }
  86. for (index = 0; index < n_args; index++) {
  87. //rt_kprintf("Remove %s.\n", mp_obj_str_get_str(arg[index]));
  88. unlink(mp_obj_str_get_str(arg[index]));
  89. }
  90. // TODO
  91. return mp_const_none;
  92. }
  93. MP_DEFINE_CONST_FUN_OBJ_VAR(mp_vfs_remove_obj, 0, mp_vfs_remove);
  94. mp_obj_t mp_vfs_rename(mp_obj_t old_path_in, mp_obj_t new_path_in) {
  95. return mp_const_none;
  96. }
  97. MP_DEFINE_CONST_FUN_OBJ_2(mp_vfs_rename_obj, mp_vfs_rename);
  98. mp_obj_t mp_vfs_rmdir(uint n_args, const mp_obj_t *arg) {
  99. int index;
  100. if (n_args == 0) {
  101. rt_kprintf("Usage: rm FILE...\n");
  102. rt_kprintf("Remove (unlink) the FILE(s).\n");
  103. return mp_const_none;
  104. }
  105. for (index = 0; index < n_args; index++) {
  106. rt_kprintf("Remove %s.\n", mp_obj_str_get_str(arg[index]));
  107. unlink(mp_obj_str_get_str(arg[index]));
  108. }
  109. // TODO
  110. return mp_const_none;
  111. }
  112. MP_DEFINE_CONST_FUN_OBJ_VAR(mp_vfs_rmdir_obj, 0, mp_vfs_rmdir);
  113. mp_obj_t mp_vfs_stat(mp_obj_t path_in) {
  114. struct stat buf;
  115. char *createpath = mp_obj_str_get_str(path_in);
  116. int res = stat(createpath, &buf);
  117. if (res != 0) {
  118. mp_raise_OSError(MP_EPERM);
  119. }
  120. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL));
  121. t->items[0] = MP_OBJ_NEW_SMALL_INT(buf.st_mode); // st_mode
  122. t->items[1] = MP_OBJ_NEW_SMALL_INT(buf.st_ino); // st_ino
  123. t->items[2] = MP_OBJ_NEW_SMALL_INT(buf.st_dev); // st_dev
  124. t->items[3] = MP_OBJ_NEW_SMALL_INT(buf.st_nlink); // st_nlink
  125. t->items[4] = MP_OBJ_NEW_SMALL_INT(buf.st_uid); // st_uid
  126. t->items[5] = MP_OBJ_NEW_SMALL_INT(buf.st_gid); // st_gid
  127. t->items[6] = mp_obj_new_int_from_uint(buf.st_size); // st_size
  128. t->items[7] = MP_OBJ_NEW_SMALL_INT(buf.st_atime); // st_atime
  129. t->items[8] = MP_OBJ_NEW_SMALL_INT(buf.st_mtime); // st_mtime
  130. t->items[9] = MP_OBJ_NEW_SMALL_INT(buf.st_ctime); // st_ctime
  131. return MP_OBJ_FROM_PTR(t);
  132. }
  133. MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_stat_obj, mp_vfs_stat);
  134. #endif // MICROPY_VFS