You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.0KB

  1. // Copyright 2011 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // -----------------------------------------------------------------------------
  17. //
  18. // FatFS wrappers.
  19. #include "avrlibx/filesystem/filesystem.h"
  20. #include "avrlibx/system/time.h"
  21. namespace avrlibx {
  22. /* static */
  23. FATFS Filesystem::fs_;
  24. /* static */
  25. FilesystemStatus Filesystem::Init() {
  26. f_mount(0, &fs_);
  27. return disk_initialize(0) ? FS_DISK_ERROR : FS_OK;
  28. }
  29. /* static */
  30. FilesystemStatus Filesystem::Init(uint16_t timeout_ms) {
  31. f_mount(0, &fs_);
  32. for (uint32_t t = milliseconds() + timeout_ms; milliseconds() < t; ) {
  33. if (!disk_initialize(0)) {
  34. return FS_OK;
  35. }
  36. }
  37. return FS_DISK_ERROR;
  38. }
  39. /* static */
  40. FilesystemStatus Filesystem::Unlink(const char* file_name) {
  41. return static_cast<FilesystemStatus>(f_unlink(file_name));
  42. }
  43. /* static */
  44. FilesystemStatus Filesystem::Mkdir(const char* dir_name) {
  45. return static_cast<FilesystemStatus>(f_mkdir(dir_name));
  46. }
  47. /* static */
  48. FilesystemStatus Filesystem::Mkdirs(char* path) {
  49. for (char* p = path + 1; *p; ++p) {
  50. // For each path prefix, attempt to create the path.
  51. if (*p == '/') {
  52. *p = '\0';
  53. FilesystemStatus status = Mkdir(path);
  54. *p = '/';
  55. if (status != FS_OK && status != FS_FILE_EXISTS) {
  56. return status;
  57. }
  58. }
  59. }
  60. return FS_OK;
  61. }
  62. /* static */
  63. FilesystemStatus Filesystem::Chmod(
  64. const char* file_name,
  65. uint8_t value,
  66. uint8_t mask) {
  67. return static_cast<FilesystemStatus>(f_chmod(file_name, value, mask));
  68. }
  69. /* static */
  70. FilesystemStatus Filesystem::Rename(
  71. const char* old_name,
  72. const char* new_name) {
  73. return static_cast<FilesystemStatus>(f_rename(old_name, new_name));
  74. }
  75. /* static */
  76. FilesystemStatus Filesystem::Mkfs() {
  77. return static_cast<FilesystemStatus>(f_mkfs(0, 0, 0));
  78. }
  79. /* static */
  80. uint32_t Filesystem::GetFreeSpace() {
  81. FATFS* p;
  82. DWORD free_clusters = 0;
  83. f_getfree("/", &free_clusters, &p);
  84. return free_clusters * p->csize * 512; // 0 if error.
  85. }
  86. /* static */
  87. FilesystemStatus Filesystem::FileStatus(const char* file_name, FileInfo* info) {
  88. return static_cast<FilesystemStatus>(f_stat(file_name, &info->file_info));
  89. }
  90. /* static */
  91. FilesystemStatus Filesystem::Utime(
  92. const char* file_name,
  93. uint16_t date,
  94. uint16_t time) {
  95. FILINFO f;
  96. f.fdate = date;
  97. f.ftime = time;
  98. return static_cast<FilesystemStatus>(f_utime(file_name, &f));
  99. }
  100. } // namespace avrlibx