mp_flipper_file_helper.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <mp_flipper_runtime.h>
  2. #include <mp_flipper_halport.h>
  3. #include <furi.h>
  4. #include <storage/storage.h>
  5. #include <py/mperrno.h>
  6. #include "mp_flipper_context.h"
  7. mp_flipper_import_stat_t mp_flipper_try_resolve_filesystem_path(FuriString* path) {
  8. mp_flipper_context_t* ctx = mp_flipper_context;
  9. const char* path_str = furi_string_get_cstr(path);
  10. FuriString* _path = furi_string_alloc_printf("%s", path_str);
  11. mp_flipper_import_stat_t stat = MP_FLIPPER_IMPORT_STAT_FILE;
  12. FS_Error error;
  13. FileInfo info;
  14. do {
  15. // make path absolute
  16. if(!furi_string_start_with_str(_path, "/")) {
  17. furi_string_printf(_path, "%s/%s", mp_flipper_root_module_path, path_str);
  18. }
  19. // check if file or folder exists
  20. error = storage_common_stat(ctx->storage, furi_string_get_cstr(_path), &info);
  21. if(error == FSE_OK) {
  22. break;
  23. }
  24. // check for existing python file
  25. furi_string_cat_str(_path, ".py");
  26. error = storage_common_stat(ctx->storage, furi_string_get_cstr(_path), &info);
  27. if(error == FSE_OK) {
  28. break;
  29. }
  30. } while(false);
  31. // file or folder missing
  32. if(error == FSE_NOT_EXIST) {
  33. stat = MP_FLIPPER_IMPORT_STAT_NO_EXIST;
  34. }
  35. // abort on error
  36. else if(error != FSE_OK) {
  37. mp_flipper_raise_os_error_with_filename(MP_ENOENT, furi_string_get_cstr(path));
  38. }
  39. // path points to directory
  40. else if((info.flags & FSF_DIRECTORY) == FSF_DIRECTORY) {
  41. stat = MP_FLIPPER_IMPORT_STAT_DIR;
  42. }
  43. furi_string_move(path, _path);
  44. return stat;
  45. }