FsFile.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * Copyright (c) 2011-2022 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) {
  41. return *this;
  42. }
  43. close();
  44. if (from.m_fFile) {
  45. m_fFile = new (m_fileMem) FatFile;
  46. *m_fFile = *from.m_fFile;
  47. } else if (from.m_xFile) {
  48. m_xFile = new (m_fileMem) ExFatFile;
  49. *m_xFile = *from.m_xFile;
  50. }
  51. return *this;
  52. }
  53. //------------------------------------------------------------------------------
  54. bool FsBaseFile::close() {
  55. bool rtn = m_fFile ? m_fFile->close() : m_xFile ? m_xFile->close() : true;
  56. m_fFile = nullptr;
  57. m_xFile = nullptr;
  58. return rtn;
  59. }
  60. //------------------------------------------------------------------------------
  61. bool FsBaseFile::mkdir(FsBaseFile* dir, const char* path, bool pFlag) {
  62. close();
  63. if (dir->m_fFile) {
  64. m_fFile = new (m_fileMem) FatFile;
  65. if (m_fFile->mkdir(dir->m_fFile, path, pFlag)) {
  66. return true;
  67. }
  68. m_fFile = nullptr;
  69. } else if (dir->m_xFile) {
  70. m_xFile = new (m_fileMem) ExFatFile;
  71. if (m_xFile->mkdir(dir->m_xFile, path, pFlag)) {
  72. return true;
  73. }
  74. m_xFile = nullptr;
  75. }
  76. return false;
  77. }
  78. //------------------------------------------------------------------------------
  79. bool FsBaseFile::open(FsVolume* vol, const char* path, oflag_t oflag) {
  80. if (!vol) {
  81. return false;
  82. }
  83. close();
  84. if (vol->m_fVol) {
  85. m_fFile = new (m_fileMem) FatFile;
  86. if (m_fFile && m_fFile->open(vol->m_fVol, path, oflag)) {
  87. return true;
  88. }
  89. m_fFile = nullptr;
  90. } else if (vol->m_xVol) {
  91. m_xFile = new (m_fileMem) ExFatFile;
  92. if (m_xFile && m_xFile->open(vol->m_xVol, path, oflag)) {
  93. return true;
  94. }
  95. m_xFile = nullptr;
  96. }
  97. return false;
  98. }
  99. //------------------------------------------------------------------------------
  100. bool FsBaseFile::open(FsBaseFile* dir, const char* path, oflag_t oflag) {
  101. close();
  102. if (dir->m_fFile) {
  103. m_fFile = new (m_fileMem) FatFile;
  104. if (m_fFile->open(dir->m_fFile, path, oflag)) {
  105. return true;
  106. }
  107. m_fFile = nullptr;
  108. } else if (dir->m_xFile) {
  109. m_xFile = new (m_fileMem) ExFatFile;
  110. if (m_xFile->open(dir->m_xFile, path, oflag)) {
  111. return true;
  112. }
  113. m_xFile = nullptr;
  114. }
  115. return false;
  116. }
  117. //------------------------------------------------------------------------------
  118. bool FsBaseFile::open(FsBaseFile* dir, uint32_t index, oflag_t oflag) {
  119. close();
  120. if (dir->m_fFile) {
  121. m_fFile = new (m_fileMem) FatFile;
  122. if (m_fFile->open(dir->m_fFile, index, oflag)) {
  123. return true;
  124. }
  125. m_fFile = nullptr;
  126. } else if (dir->m_xFile) {
  127. m_xFile = new (m_fileMem) ExFatFile;
  128. if (m_xFile->open(dir->m_xFile, index, oflag)) {
  129. return true;
  130. }
  131. m_xFile = nullptr;
  132. }
  133. return false;
  134. }
  135. //------------------------------------------------------------------------------
  136. bool FsBaseFile::openCwd() {
  137. close();
  138. if (FsVolume::m_cwv && FsVolume::m_cwv->m_fVol) {
  139. m_fFile = new (m_fileMem) FatFile;
  140. if (m_fFile->openCwd()) {
  141. return true;
  142. }
  143. m_fFile = nullptr;
  144. } else if (FsVolume::m_cwv && FsVolume::m_cwv->m_xVol) {
  145. m_xFile = new (m_fileMem) ExFatFile;
  146. if (m_xFile->openCwd()) {
  147. return true;
  148. }
  149. m_xFile = nullptr;
  150. }
  151. return false;
  152. }
  153. //------------------------------------------------------------------------------
  154. bool FsBaseFile::openNext(FsBaseFile* dir, oflag_t oflag) {
  155. close();
  156. if (dir->m_fFile) {
  157. m_fFile = new (m_fileMem) FatFile;
  158. if (m_fFile->openNext(dir->m_fFile, oflag)) {
  159. return true;
  160. }
  161. m_fFile = nullptr;
  162. } else if (dir->m_xFile) {
  163. m_xFile = new (m_fileMem) ExFatFile;
  164. if (m_xFile->openNext(dir->m_xFile, oflag)) {
  165. return true;
  166. }
  167. m_xFile = nullptr;
  168. }
  169. return false;
  170. }
  171. //------------------------------------------------------------------------------
  172. bool FsBaseFile::openRoot(FsVolume* vol) {
  173. if (!vol) {
  174. return false;
  175. }
  176. close();
  177. if (vol->m_fVol) {
  178. m_fFile = new (m_fileMem) FatFile;
  179. if (m_fFile && m_fFile->openRoot(vol->m_fVol)) {
  180. return true;
  181. }
  182. m_fFile = nullptr;
  183. } else if (vol->m_xVol) {
  184. m_xFile = new (m_fileMem) ExFatFile;
  185. if (m_xFile && m_xFile->openRoot(vol->m_xVol)) {
  186. return true;
  187. }
  188. m_xFile = nullptr;
  189. }
  190. return false;
  191. }
  192. //------------------------------------------------------------------------------
  193. bool FsBaseFile::remove() {
  194. if (m_fFile) {
  195. if (m_fFile->remove()) {
  196. m_fFile = nullptr;
  197. return true;
  198. }
  199. } else if (m_xFile) {
  200. if (m_xFile->remove()) {
  201. m_xFile = nullptr;
  202. return true;
  203. }
  204. }
  205. return false;
  206. }
  207. //------------------------------------------------------------------------------
  208. bool FsBaseFile::rmdir() {
  209. if (m_fFile) {
  210. if (m_fFile->rmdir()) {
  211. m_fFile = nullptr;
  212. return true;
  213. }
  214. } else if (m_xFile) {
  215. if (m_xFile->rmdir()) {
  216. m_xFile = nullptr;
  217. return true;
  218. }
  219. }
  220. return false;
  221. }