mp_flipper_file_helper.c 1.6 KB

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