Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

187 lines
5.5KB

  1. /**
  2. * Copyright (c) 2011-2019 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "./FsLib.h"
  26. //------------------------------------------------------------------------------
  27. FsBaseFile::FsBaseFile(const FsBaseFile& from) {
  28. m_fFile = nullptr;
  29. m_xFile = nullptr;
  30. if (from.m_fFile) {
  31. m_fFile = new (m_fileMem) FatFile;
  32. *m_fFile = *from.m_fFile;
  33. } else if (from.m_xFile) {
  34. m_xFile = new (m_fileMem) ExFatFile;
  35. *m_xFile = *from.m_xFile;
  36. }
  37. }
  38. //------------------------------------------------------------------------------
  39. FsBaseFile& FsBaseFile::operator=(const FsBaseFile& from) {
  40. if (this == &from) return *this;
  41. close();
  42. if (from.m_fFile) {
  43. m_fFile = new (m_fileMem) FatFile;
  44. *m_fFile = *from.m_fFile;
  45. } else if (from.m_xFile) {
  46. m_xFile = new (m_fileMem) ExFatFile;
  47. *m_xFile = *from.m_xFile;
  48. }
  49. return *this;
  50. }
  51. //------------------------------------------------------------------------------
  52. bool FsBaseFile::close() {
  53. if (m_fFile && m_fFile->close()) {
  54. m_fFile = nullptr;
  55. return true;
  56. }
  57. if (m_xFile && m_xFile->close()) {
  58. m_xFile = nullptr;
  59. return true;
  60. }
  61. return false;
  62. }
  63. //------------------------------------------------------------------------------
  64. bool FsBaseFile::mkdir(FsBaseFile* dir, const char* path, bool pFlag) {
  65. close();
  66. if (dir->m_fFile) {
  67. m_fFile = new (m_fileMem) FatFile;
  68. if (m_fFile->mkdir(dir->m_fFile, path, pFlag)) {
  69. return true;
  70. }
  71. m_fFile = nullptr;
  72. } else if (dir->m_xFile) {
  73. m_xFile = new (m_fileMem) ExFatFile;
  74. if (m_xFile->mkdir(dir->m_xFile, path, pFlag)) {
  75. return true;
  76. }
  77. m_xFile = nullptr;
  78. }
  79. return false;
  80. }
  81. //------------------------------------------------------------------------------
  82. bool FsBaseFile::open(FsVolume* vol, const char* path, oflag_t oflag) {
  83. if (!vol) {
  84. return false;
  85. }
  86. close();
  87. if (vol->m_fVol) {
  88. m_fFile = new (m_fileMem) FatFile;
  89. if (m_fFile && m_fFile->open(vol->m_fVol, path, oflag)) {
  90. return true;
  91. }
  92. m_fFile = nullptr;
  93. return false;
  94. } else if (vol->m_xVol) {
  95. m_xFile = new (m_fileMem) ExFatFile;
  96. if (m_xFile && m_xFile->open(vol->m_xVol, path, oflag)) {
  97. return true;
  98. }
  99. m_xFile = nullptr;
  100. }
  101. return false;
  102. }
  103. //------------------------------------------------------------------------------
  104. bool FsBaseFile::open(FsBaseFile* dir, const char* path, oflag_t oflag) {
  105. close();
  106. if (dir->m_fFile) {
  107. m_fFile = new (m_fileMem) FatFile;
  108. if (m_fFile->open(dir->m_fFile, path, oflag)) {
  109. return true;
  110. }
  111. m_fFile = nullptr;
  112. } else if (dir->m_xFile) {
  113. m_xFile = new (m_fileMem) ExFatFile;
  114. if (m_xFile->open(dir->m_xFile, path, oflag)) {
  115. return true;
  116. }
  117. m_xFile = nullptr;
  118. }
  119. return false;
  120. }
  121. //------------------------------------------------------------------------------
  122. bool FsBaseFile::open(FsBaseFile* dir, uint32_t index, oflag_t oflag) {
  123. close();
  124. if (dir->m_fFile) {
  125. m_fFile = new (m_fileMem) FatFile;
  126. if (m_fFile->open(dir->m_fFile, index, oflag)) {
  127. return true;
  128. }
  129. m_fFile = nullptr;
  130. } else if (dir->m_xFile) {
  131. m_xFile = new (m_fileMem) ExFatFile;
  132. if (m_xFile->open(dir->m_xFile, index, oflag)) {
  133. return true;
  134. }
  135. m_xFile = nullptr;
  136. }
  137. return false;
  138. }
  139. //------------------------------------------------------------------------------
  140. bool FsBaseFile::openNext(FsBaseFile* dir, oflag_t oflag) {
  141. close();
  142. if (dir->m_fFile) {
  143. m_fFile = new (m_fileMem) FatFile;
  144. if (m_fFile->openNext(dir->m_fFile, oflag)) {
  145. return true;
  146. }
  147. m_fFile = nullptr;
  148. } else if (dir->m_xFile) {
  149. m_xFile = new (m_fileMem) ExFatFile;
  150. if (m_xFile->openNext(dir->m_xFile, oflag)) {
  151. return true;
  152. }
  153. m_xFile = nullptr;
  154. }
  155. return false;
  156. }
  157. //------------------------------------------------------------------------------
  158. bool FsBaseFile::remove() {
  159. if (m_fFile) {
  160. if (m_fFile->remove()) {
  161. m_fFile = nullptr;
  162. return true;
  163. }
  164. } else if (m_xFile) {
  165. if (m_xFile->remove()) {
  166. m_xFile = nullptr;
  167. return true;
  168. }
  169. }
  170. return false;
  171. }
  172. //------------------------------------------------------------------------------
  173. bool FsBaseFile::rmdir() {
  174. if (m_fFile) {
  175. if (m_fFile->rmdir()) {
  176. m_fFile = nullptr;
  177. return true;
  178. }
  179. } else if (m_xFile) {
  180. if (m_xFile->rmdir()) {
  181. m_xFile = nullptr;
  182. return true;
  183. }
  184. }
  185. return false;
  186. }