mp_flipper_file_helper.c 1.6 KB

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