FsFile.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * Copyright (c) 2011-2021 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. } else if (vol->m_xVol) {
  94. m_xFile = new (m_fileMem) ExFatFile;
  95. if (m_xFile && m_xFile->open(vol->m_xVol, path, oflag)) {
  96. return true;
  97. }
  98. m_xFile = nullptr;
  99. }
  100. return false;
  101. }
  102. //------------------------------------------------------------------------------
  103. bool FsBaseFile::open(FsBaseFile* dir, const char* path, oflag_t oflag) {
  104. close();
  105. if (dir->m_fFile) {
  106. m_fFile = new (m_fileMem) FatFile;
  107. if (m_fFile->open(dir->m_fFile, path, oflag)) {
  108. return true;
  109. }
  110. m_fFile = nullptr;
  111. } else if (dir->m_xFile) {
  112. m_xFile = new (m_fileMem) ExFatFile;
  113. if (m_xFile->open(dir->m_xFile, path, oflag)) {
  114. return true;
  115. }
  116. m_xFile = nullptr;
  117. }
  118. return false;
  119. }
  120. //------------------------------------------------------------------------------
  121. bool FsBaseFile::open(FsBaseFile* dir, uint32_t index, oflag_t oflag) {
  122. close();
  123. if (dir->m_fFile) {
  124. m_fFile = new (m_fileMem) FatFile;
  125. if (m_fFile->open(dir->m_fFile, index, oflag)) {
  126. return true;
  127. }
  128. m_fFile = nullptr;
  129. } else if (dir->m_xFile) {
  130. m_xFile = new (m_fileMem) ExFatFile;
  131. if (m_xFile->open(dir->m_xFile, index, oflag)) {
  132. return true;
  133. }
  134. m_xFile = nullptr;
  135. }
  136. return false;
  137. }
  138. //------------------------------------------------------------------------------
  139. bool FsBaseFile::openNext(FsBaseFile* dir, oflag_t oflag) {
  140. close();
  141. if (dir->m_fFile) {
  142. m_fFile = new (m_fileMem) FatFile;
  143. if (m_fFile->openNext(dir->m_fFile, oflag)) {
  144. return true;
  145. }
  146. m_fFile = nullptr;
  147. } else if (dir->m_xFile) {
  148. m_xFile = new (m_fileMem) ExFatFile;
  149. if (m_xFile->openNext(dir->m_xFile, oflag)) {
  150. return true;
  151. }
  152. m_xFile = nullptr;
  153. }
  154. return false;
  155. }
  156. //------------------------------------------------------------------------------
  157. bool FsBaseFile::openRoot(FsVolume* vol) {
  158. if (!vol) {
  159. return false;
  160. }
  161. close();
  162. if (vol->m_fVol) {
  163. m_fFile = new (m_fileMem) FatFile;
  164. if (m_fFile && m_fFile->openRoot(vol->m_fVol)) {
  165. return true;
  166. }
  167. m_fFile = nullptr;
  168. } else if (vol->m_xVol) {
  169. m_xFile = new (m_fileMem) ExFatFile;
  170. if (m_xFile && m_xFile->openRoot(vol->m_xVol)) {
  171. return true;
  172. }
  173. m_xFile = nullptr;
  174. }
  175. return false;
  176. }
  177. //------------------------------------------------------------------------------
  178. bool FsBaseFile::remove() {
  179. if (m_fFile) {
  180. if (m_fFile->remove()) {
  181. m_fFile = nullptr;
  182. return true;
  183. }
  184. } else if (m_xFile) {
  185. if (m_xFile->remove()) {
  186. m_xFile = nullptr;
  187. return true;
  188. }
  189. }
  190. return false;
  191. }
  192. //------------------------------------------------------------------------------
  193. bool FsBaseFile::rmdir() {
  194. if (m_fFile) {
  195. if (m_fFile->rmdir()) {
  196. m_fFile = nullptr;
  197. return true;
  198. }
  199. } else if (m_xFile) {
  200. if (m_xFile->rmdir()) {
  201. m_xFile = nullptr;
  202. return true;
  203. }
  204. }
  205. return false;
  206. }