memVolume.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "core/memVolume.h"
  24. #include "core/crc.h"
  25. #include "core/frameAllocator.h"
  26. #include "core/util/str.h"
  27. #include "core/strings/stringFunctions.h"
  28. #include "platform/platformVolume.h"
  29. namespace Torque
  30. {
  31. namespace Mem
  32. {
  33. // Multiple MemFile's can reference the same path, so this is here to contain
  34. // the actual data at a Path.
  35. struct MemFileData
  36. {
  37. MemFileData(MemFileSystem* fs, const Path& path)
  38. {
  39. mPath = path;
  40. mBufferSize = 1024;
  41. mFileSize = 0;
  42. mBuffer = dMalloc(mBufferSize);
  43. dMemset(mBuffer, 0, mBufferSize);
  44. mModified = Time::getCurrentTime();
  45. mLastAccess = mModified;
  46. mFileSystem = fs;
  47. }
  48. ~MemFileData()
  49. {
  50. dFree(mBuffer);
  51. }
  52. bool getAttributes(FileNode::Attributes* attr)
  53. {
  54. attr->name = mPath;
  55. attr->flags = FileNode::File;
  56. attr->size = mFileSize;
  57. attr->mtime = mModified;
  58. attr->atime = mLastAccess;
  59. return true;
  60. }
  61. FileNodeRef resolve(const Path& path)
  62. {
  63. // Is it me?
  64. String sThisPath(mPath);
  65. String sTargetPath(path);
  66. if (sThisPath == sTargetPath)
  67. return new MemFile(mFileSystem, this);
  68. // Nope
  69. return NULL;
  70. }
  71. Path mPath;
  72. void* mBuffer;
  73. U32 mBufferSize; // This is the size of the memory buffer >= mFileSize
  74. U32 mFileSize; // This is the size of the "file" <= mBufferSize
  75. Time mModified; // Last modified
  76. Time mLastAccess; // Last access
  77. MemFileSystem* mFileSystem;
  78. };
  79. struct MemDirectoryData
  80. {
  81. Path mPath;
  82. MemFileSystem* mFileSystem;
  83. Vector<MemFileData*> mFiles;
  84. Vector<MemDirectoryData*> mDirectories;
  85. MemDirectoryData(MemFileSystem* fs, const Path& path)
  86. {
  87. mFileSystem = fs;
  88. mPath = path;
  89. }
  90. ~MemDirectoryData()
  91. {
  92. for (U32 i = 0; i < mFiles.size(); i++)
  93. {
  94. delete mFiles[i];
  95. }
  96. for (U32 i = 0; i < mDirectories.size(); i++)
  97. {
  98. delete mDirectories[i];
  99. }
  100. }
  101. bool getAttributes(FileNode::Attributes* attr)
  102. {
  103. attr->name = mPath;
  104. attr->flags = FileNode::Directory;
  105. return true;
  106. }
  107. FileNodeRef resolve(const Path& path)
  108. {
  109. // Is it me?
  110. String sThisPath(mPath);
  111. String sTargetPath(path);
  112. if (sThisPath == sTargetPath)
  113. return new MemDirectory(mFileSystem, this);
  114. // Is it one of my children?
  115. if (sTargetPath.find(sThisPath) == 0)
  116. {
  117. FileNodeRef result;
  118. for (U32 i = 0; i < mDirectories.size() && result.isNull(); i++)
  119. result = mDirectories[i]->resolve(path);
  120. for (U32 i = 0; i < mFiles.size() && result.isNull(); i++)
  121. result = mFiles[i]->resolve(path);
  122. return result;
  123. }
  124. // Nope
  125. return NULL;
  126. }
  127. };
  128. //-----------------------------------------------------------------------------
  129. MemFileSystem::MemFileSystem(String volume)
  130. {
  131. mVolume = volume;
  132. mRootDir = new MemDirectoryData(this, volume);
  133. }
  134. MemFileSystem::~MemFileSystem()
  135. {
  136. delete mRootDir;
  137. }
  138. FileNodeRef MemFileSystem::resolve(const Path& path)
  139. {
  140. return mRootDir->resolve(path);
  141. }
  142. //
  143. MemDirectory* MemFileSystem::getParentDir(const Path& path, FileNodeRef& parentRef)
  144. {
  145. parentRef = mRootDir->resolve(path.getRoot() + ":" + path.getPath());
  146. if (parentRef.isNull())
  147. return NULL;
  148. MemDirectory* result = dynamic_cast<MemDirectory*>(parentRef.getPointer());
  149. return result;
  150. }
  151. FileNodeRef MemFileSystem::create(const Path& path, FileNode::Mode mode)
  152. {
  153. // Already exists
  154. FileNodeRef result = mRootDir->resolve(path);
  155. if (result.isValid())
  156. return result;
  157. // Doesn't exist, try to get parent node.
  158. FileNodeRef parentRef;
  159. MemDirectory* mDir = getParentDir(path, parentRef);
  160. if (mDir)
  161. {
  162. MemDirectoryData* mdd = mDir->mDirectoryData;
  163. switch (mode)
  164. {
  165. case FileNode::File :
  166. {
  167. MemFileData* mfd = new MemFileData(this, path);
  168. mdd->mFiles.push_back(mfd);
  169. return new MemFile(this, mfd);
  170. }
  171. break;
  172. case FileNode::Directory :
  173. {
  174. MemDirectoryData* mfd = new MemDirectoryData(this, path);
  175. mdd->mDirectories.push_back(mfd);
  176. return new MemDirectory(this, mfd);
  177. }
  178. break;
  179. default:
  180. // anything else we ignore
  181. break;
  182. }
  183. }
  184. return NULL;
  185. }
  186. bool MemFileSystem::remove(const Path& path)
  187. {
  188. FileNodeRef parentRef;
  189. MemDirectory* mDir = getParentDir(path, parentRef);
  190. MemDirectoryData* mdd = mDir->mDirectoryData;
  191. for (U32 i = 0; i < mdd->mDirectories.size(); i++)
  192. {
  193. if (mdd->mDirectories[i]->mPath == path)
  194. {
  195. delete mdd->mDirectories[i];
  196. mdd->mDirectories.erase_fast(i);
  197. return true;
  198. }
  199. }
  200. for (U32 i = 0; i < mdd->mFiles.size(); i++)
  201. {
  202. if (mdd->mFiles[i]->mPath == path)
  203. {
  204. delete mdd->mFiles[i];
  205. mdd->mFiles.erase_fast(i);
  206. return true;
  207. }
  208. }
  209. return false;
  210. }
  211. bool MemFileSystem::rename(const Path& from,const Path& to)
  212. {
  213. // Source must exist
  214. FileNodeRef source = mRootDir->resolve(from);
  215. if (source.isNull())
  216. return false;
  217. // Destination must not exist
  218. FileNodeRef dest = mRootDir->resolve(to);
  219. if (source.isValid())
  220. return false;
  221. // Get source parent
  222. FileNodeRef sourceParentRef;
  223. MemDirectory* sourceDir = getParentDir(from, sourceParentRef);
  224. // Get dest parent
  225. FileNodeRef destRef;
  226. MemDirectory* mDir = getParentDir(to, destRef);
  227. // Now move it/rename it
  228. if (dynamic_cast<MemDirectory*>(source.getPointer()))
  229. {
  230. MemDirectoryData* sourcedd;
  231. MemDirectoryData* d = sourceDir->mDirectoryData;
  232. for (U32 i = 0; i < d->mDirectories.size(); i++)
  233. {
  234. if (d->mDirectories[i]->mPath == from)
  235. {
  236. sourcedd = d->mDirectories[i];
  237. d->mDirectories.erase_fast(i);
  238. sourcedd->mPath = to;
  239. mDir->mDirectoryData->mDirectories.push_back(sourcedd);
  240. return true;
  241. }
  242. }
  243. } else {
  244. MemFileData* sourceFile;
  245. MemDirectoryData* d = sourceDir->mDirectoryData;
  246. for (U32 i = 0; i < d->mFiles.size(); i++)
  247. {
  248. if (d->mFiles[i]->mPath == from)
  249. {
  250. sourceFile = d->mFiles[i];
  251. d->mFiles.erase_fast(i);
  252. sourceFile->mPath = to;
  253. mDir->mDirectoryData->mFiles.push_back(sourceFile);
  254. return true;
  255. }
  256. }
  257. }
  258. return false;
  259. }
  260. Path MemFileSystem::mapTo(const Path& path)
  261. {
  262. String file = mVolume;
  263. file = Path::Join(file, '/', path.getPath());
  264. file = Path::Join(file, '/', path.getFileName());
  265. file = Path::Join(file, '.', path.getExtension());
  266. return file;
  267. }
  268. Path MemFileSystem::mapFrom(const Path& path)
  269. {
  270. const String::SizeType volumePathLen = mVolume.length();
  271. String pathStr = path.getFullPath();
  272. if ( mVolume.compare( pathStr, volumePathLen, String::NoCase ))
  273. return Path();
  274. return pathStr.substr( volumePathLen, pathStr.length() - volumePathLen );
  275. }
  276. //-----------------------------------------------------------------------------
  277. MemFile::MemFile(MemFileSystem* fs, MemFileData* fileData)
  278. {
  279. mFileData = fileData;
  280. mStatus = Closed;
  281. mCurrentPos = U32_MAX;
  282. mFileSystem = fs;
  283. }
  284. MemFile::~MemFile()
  285. {
  286. }
  287. Path MemFile::getName() const
  288. {
  289. return mFileData->mPath;
  290. }
  291. FileNode::NodeStatus MemFile::getStatus() const
  292. {
  293. return mStatus;
  294. }
  295. bool MemFile::getAttributes(Attributes* attr)
  296. {
  297. return mFileData->getAttributes(attr);
  298. }
  299. U32 MemFile::calculateChecksum()
  300. {
  301. return CRC::calculateCRC(mFileData->mBuffer, mFileData->mFileSize);
  302. }
  303. bool MemFile::open(AccessMode mode)
  304. {
  305. mStatus = Open;
  306. mCurrentPos = 0;
  307. switch (mode)
  308. {
  309. case Read :
  310. case ReadWrite :
  311. mCurrentPos = 0;
  312. break;
  313. case Write :
  314. mCurrentPos = 0;
  315. mFileData->mFileSize = 0;
  316. break;
  317. case WriteAppend :
  318. mCurrentPos = mFileData->mFileSize;
  319. break;
  320. }
  321. return true;
  322. }
  323. bool MemFile::close()
  324. {
  325. mStatus = Closed;
  326. return true;
  327. }
  328. U32 MemFile::getPosition()
  329. {
  330. if (mStatus == Open || mStatus == EndOfFile)
  331. return mCurrentPos;
  332. return 0;
  333. }
  334. U32 MemFile::setPosition(U32 delta, SeekMode mode)
  335. {
  336. if (mStatus != Open && mStatus != EndOfFile)
  337. return 0;
  338. switch (mode)
  339. {
  340. case Begin:
  341. mCurrentPos = delta;
  342. break;
  343. case Current:
  344. mCurrentPos += delta;
  345. break;
  346. case End:
  347. mCurrentPos = mFileData->mFileSize - delta;
  348. break;
  349. }
  350. mStatus = Open;
  351. return mCurrentPos;
  352. }
  353. U32 MemFile::read(void* dst, U32 size)
  354. {
  355. if (mStatus != Open && mStatus != EndOfFile)
  356. return 0;
  357. U32 copyAmount = getMin(size, mFileData->mFileSize - mCurrentPos);
  358. dMemcpy(dst, (U8*) mFileData->mBuffer + mCurrentPos, copyAmount);
  359. mCurrentPos += copyAmount;
  360. mFileData->mLastAccess = Time::getCurrentTime();
  361. if (mCurrentPos == mFileData->mFileSize)
  362. mStatus = EndOfFile;
  363. return copyAmount;
  364. }
  365. U32 MemFile::write(const void* src, U32 size)
  366. {
  367. if ((mStatus != Open && mStatus != EndOfFile) || !size)
  368. return 0;
  369. if (mFileData->mFileSize + size > mFileData->mBufferSize)
  370. {
  371. // Keep doubling our buffer size until we're big enough.
  372. while (mFileData->mFileSize + size > mFileData->mBufferSize)
  373. mFileData->mBufferSize *= 2;
  374. mFileData->mBuffer = dRealloc(mFileData->mBuffer, mFileData->mBufferSize);
  375. if (!mFileData->mBuffer)
  376. {
  377. mStatus = FileSystemFull;
  378. return 0;
  379. }
  380. }
  381. dMemcpy((U8*)mFileData->mBuffer + mCurrentPos, src, size);
  382. mCurrentPos += size;
  383. mFileData->mFileSize = getMax(mFileData->mFileSize, mCurrentPos);
  384. mFileData->mLastAccess = Time::getCurrentTime();
  385. mFileData->mModified = mFileData->mLastAccess;
  386. return size;
  387. }
  388. //-----------------------------------------------------------------------------
  389. MemDirectory::MemDirectory(MemFileSystem* fs, MemDirectoryData* dir)
  390. {
  391. mStatus = Closed;
  392. mDirectoryData = dir;
  393. mFileSystem = fs;
  394. }
  395. MemDirectory::~MemDirectory()
  396. {
  397. }
  398. Path MemDirectory::getName() const
  399. {
  400. return mDirectoryData->mPath;
  401. }
  402. bool MemDirectory::open()
  403. {
  404. mSearchIndex = 0;
  405. mStatus = Open;
  406. return true;
  407. }
  408. bool MemDirectory::close()
  409. {
  410. return true;
  411. }
  412. bool MemDirectory::read(Attributes* entry)
  413. {
  414. if (mStatus != Open)
  415. return false;
  416. if (mSearchIndex < mDirectoryData->mDirectories.size())
  417. {
  418. mDirectoryData->mDirectories[mSearchIndex]->getAttributes(entry);
  419. mSearchIndex++;
  420. return true;
  421. }
  422. AssertFatal(mSearchIndex > mDirectoryData->mDirectories.size(), "This should not happen!");
  423. U32 fileIndex = mSearchIndex - mDirectoryData->mDirectories.size();
  424. if (fileIndex < mDirectoryData->mFiles.size())
  425. {
  426. mDirectoryData->mFiles[mSearchIndex]->getAttributes(entry);
  427. mSearchIndex++;
  428. return true;
  429. }
  430. return false;
  431. }
  432. U32 MemDirectory::calculateChecksum()
  433. {
  434. // Return checksum of current entry
  435. return 0;
  436. }
  437. bool MemDirectory::getAttributes(Attributes* attr)
  438. {
  439. return mDirectoryData->getAttributes(attr);
  440. }
  441. FileNode::NodeStatus MemDirectory::getStatus() const
  442. {
  443. return mStatus;
  444. }
  445. } // Namespace Mem
  446. } // Namespace Torque