memVolume.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. mCreated = mModified;
  47. mFileSystem = fs;
  48. }
  49. ~MemFileData()
  50. {
  51. dFree(mBuffer);
  52. }
  53. bool getAttributes(FileNode::Attributes* attr)
  54. {
  55. attr->name = mPath;
  56. attr->flags = FileNode::File;
  57. attr->size = mFileSize;
  58. attr->mtime = mModified;
  59. attr->atime = mLastAccess;
  60. attr->ctime = mCreated;
  61. return true;
  62. }
  63. FileNodeRef resolve(const Path& path)
  64. {
  65. // Is it me?
  66. String sThisPath(mPath);
  67. String sTargetPath(path);
  68. if (sThisPath == sTargetPath)
  69. return new MemFile(mFileSystem, this);
  70. // Nope
  71. return NULL;
  72. }
  73. Path mPath;
  74. void* mBuffer;
  75. U32 mBufferSize; // This is the size of the memory buffer >= mFileSize
  76. U32 mFileSize; // This is the size of the "file" <= mBufferSize
  77. Time mModified; // Last modified
  78. Time mCreated; // When Created
  79. Time mLastAccess; // Last access
  80. MemFileSystem* mFileSystem;
  81. };
  82. struct MemDirectoryData
  83. {
  84. Path mPath;
  85. MemFileSystem* mFileSystem;
  86. Vector<MemFileData*> mFiles;
  87. Vector<MemDirectoryData*> mDirectories;
  88. MemDirectoryData(MemFileSystem* fs, const Path& path)
  89. {
  90. mFileSystem = fs;
  91. mPath = path;
  92. }
  93. ~MemDirectoryData()
  94. {
  95. for (U32 i = 0; i < mFiles.size(); i++)
  96. {
  97. delete mFiles[i];
  98. }
  99. for (U32 i = 0; i < mDirectories.size(); i++)
  100. {
  101. delete mDirectories[i];
  102. }
  103. }
  104. bool getAttributes(FileNode::Attributes* attr)
  105. {
  106. attr->name = mPath;
  107. attr->flags = FileNode::Directory;
  108. return true;
  109. }
  110. FileNodeRef resolve(const Path& path)
  111. {
  112. // Is it me?
  113. String sThisPath(mPath);
  114. String sTargetPath(path);
  115. if (sThisPath == sTargetPath)
  116. return new MemDirectory(mFileSystem, this);
  117. // Is it one of my children?
  118. if (sTargetPath.find(sThisPath) == 0)
  119. {
  120. FileNodeRef result;
  121. for (U32 i = 0; i < mDirectories.size() && result.isNull(); i++)
  122. result = mDirectories[i]->resolve(path);
  123. for (U32 i = 0; i < mFiles.size() && result.isNull(); i++)
  124. result = mFiles[i]->resolve(path);
  125. return result;
  126. }
  127. // Nope
  128. return NULL;
  129. }
  130. };
  131. //-----------------------------------------------------------------------------
  132. MemFileSystem::MemFileSystem(String volume)
  133. {
  134. mVolume = volume;
  135. mRootDir = new MemDirectoryData(this, volume);
  136. }
  137. MemFileSystem::~MemFileSystem()
  138. {
  139. delete mRootDir;
  140. }
  141. FileNodeRef MemFileSystem::resolve(const Path& path)
  142. {
  143. return mRootDir->resolve(path);
  144. }
  145. //
  146. MemDirectory* MemFileSystem::getParentDir(const Path& path, FileNodeRef& parentRef)
  147. {
  148. parentRef = mRootDir->resolve(path.getRoot() + ":" + path.getPath());
  149. if (parentRef.isNull())
  150. return NULL;
  151. MemDirectory* result = dynamic_cast<MemDirectory*>(parentRef.getPointer());
  152. return result;
  153. }
  154. FileNodeRef MemFileSystem::create(const Path& path, FileNode::Mode mode)
  155. {
  156. // Already exists
  157. FileNodeRef result = mRootDir->resolve(path);
  158. if (result.isValid())
  159. return result;
  160. // Doesn't exist, try to get parent node.
  161. FileNodeRef parentRef;
  162. MemDirectory* mDir = getParentDir(path, parentRef);
  163. if (mDir)
  164. {
  165. MemDirectoryData* mdd = mDir->mDirectoryData;
  166. switch (mode)
  167. {
  168. case FileNode::File :
  169. {
  170. MemFileData* mfd = new MemFileData(this, path);
  171. mdd->mFiles.push_back(mfd);
  172. return new MemFile(this, mfd);
  173. }
  174. break;
  175. case FileNode::Directory :
  176. {
  177. MemDirectoryData* mfd = new MemDirectoryData(this, path);
  178. mdd->mDirectories.push_back(mfd);
  179. return new MemDirectory(this, mfd);
  180. }
  181. break;
  182. default:
  183. // anything else we ignore
  184. break;
  185. }
  186. }
  187. return NULL;
  188. }
  189. bool MemFileSystem::remove(const Path& path)
  190. {
  191. FileNodeRef parentRef;
  192. MemDirectory* mDir = getParentDir(path, parentRef);
  193. MemDirectoryData* mdd = mDir->mDirectoryData;
  194. for (U32 i = 0; i < mdd->mDirectories.size(); i++)
  195. {
  196. if (mdd->mDirectories[i]->mPath == path)
  197. {
  198. delete mdd->mDirectories[i];
  199. mdd->mDirectories.erase_fast(i);
  200. return true;
  201. }
  202. }
  203. for (U32 i = 0; i < mdd->mFiles.size(); i++)
  204. {
  205. if (mdd->mFiles[i]->mPath == path)
  206. {
  207. delete mdd->mFiles[i];
  208. mdd->mFiles.erase_fast(i);
  209. return true;
  210. }
  211. }
  212. return false;
  213. }
  214. bool MemFileSystem::rename(const Path& from,const Path& to)
  215. {
  216. // Source must exist
  217. FileNodeRef source = mRootDir->resolve(from);
  218. if (source.isNull())
  219. return false;
  220. // Destination must not exist
  221. FileNodeRef dest = mRootDir->resolve(to);
  222. if (source.isValid())
  223. return false;
  224. // Get source parent
  225. FileNodeRef sourceParentRef;
  226. MemDirectory* sourceDir = getParentDir(from, sourceParentRef);
  227. // Get dest parent
  228. FileNodeRef destRef;
  229. MemDirectory* mDir = getParentDir(to, destRef);
  230. // Now move it/rename it
  231. if (dynamic_cast<MemDirectory*>(source.getPointer()))
  232. {
  233. MemDirectoryData* sourcedd;
  234. MemDirectoryData* d = sourceDir->mDirectoryData;
  235. for (U32 i = 0; i < d->mDirectories.size(); i++)
  236. {
  237. if (d->mDirectories[i]->mPath == from)
  238. {
  239. sourcedd = d->mDirectories[i];
  240. d->mDirectories.erase_fast(i);
  241. sourcedd->mPath = to;
  242. mDir->mDirectoryData->mDirectories.push_back(sourcedd);
  243. return true;
  244. }
  245. }
  246. } else {
  247. MemFileData* sourceFile;
  248. MemDirectoryData* d = sourceDir->mDirectoryData;
  249. for (U32 i = 0; i < d->mFiles.size(); i++)
  250. {
  251. if (d->mFiles[i]->mPath == from)
  252. {
  253. sourceFile = d->mFiles[i];
  254. d->mFiles.erase_fast(i);
  255. sourceFile->mPath = to;
  256. mDir->mDirectoryData->mFiles.push_back(sourceFile);
  257. return true;
  258. }
  259. }
  260. }
  261. return false;
  262. }
  263. Path MemFileSystem::mapTo(const Path& path)
  264. {
  265. String file = mVolume;
  266. file = Path::Join(file, '/', path.getPath());
  267. file = Path::Join(file, '/', path.getFileName());
  268. file = Path::Join(file, '.', path.getExtension());
  269. return file;
  270. }
  271. Path MemFileSystem::mapFrom(const Path& path)
  272. {
  273. const String::SizeType volumePathLen = mVolume.length();
  274. String pathStr = path.getFullPath();
  275. if ( mVolume.compare( pathStr, volumePathLen, String::NoCase ))
  276. return Path();
  277. return pathStr.substr( volumePathLen, pathStr.length() - volumePathLen );
  278. }
  279. //-----------------------------------------------------------------------------
  280. MemFile::MemFile(MemFileSystem* fs, MemFileData* fileData)
  281. {
  282. mFileData = fileData;
  283. mStatus = Closed;
  284. mCurrentPos = U32_MAX;
  285. mFileSystem = fs;
  286. }
  287. MemFile::~MemFile()
  288. {
  289. }
  290. Path MemFile::getName() const
  291. {
  292. return mFileData->mPath;
  293. }
  294. FileNode::NodeStatus MemFile::getStatus() const
  295. {
  296. return mStatus;
  297. }
  298. bool MemFile::getAttributes(Attributes* attr)
  299. {
  300. return mFileData->getAttributes(attr);
  301. }
  302. U32 MemFile::calculateChecksum()
  303. {
  304. return CRC::calculateCRC(mFileData->mBuffer, mFileData->mFileSize);
  305. }
  306. bool MemFile::open(AccessMode mode)
  307. {
  308. mStatus = Open;
  309. mCurrentPos = 0;
  310. switch (mode)
  311. {
  312. case Read :
  313. case ReadWrite :
  314. mCurrentPos = 0;
  315. break;
  316. case Write :
  317. mCurrentPos = 0;
  318. mFileData->mFileSize = 0;
  319. break;
  320. case WriteAppend :
  321. mCurrentPos = mFileData->mFileSize;
  322. break;
  323. }
  324. return true;
  325. }
  326. bool MemFile::close()
  327. {
  328. mStatus = Closed;
  329. return true;
  330. }
  331. U32 MemFile::getPosition()
  332. {
  333. if (mStatus == Open || mStatus == EndOfFile)
  334. return mCurrentPos;
  335. return 0;
  336. }
  337. U32 MemFile::setPosition(U32 delta, SeekMode mode)
  338. {
  339. if (mStatus != Open && mStatus != EndOfFile)
  340. return 0;
  341. switch (mode)
  342. {
  343. case Begin:
  344. mCurrentPos = delta;
  345. break;
  346. case Current:
  347. mCurrentPos += delta;
  348. break;
  349. case End:
  350. mCurrentPos = mFileData->mFileSize - delta;
  351. break;
  352. }
  353. mStatus = Open;
  354. return mCurrentPos;
  355. }
  356. U32 MemFile::read(void* dst, U32 size)
  357. {
  358. if (mStatus != Open && mStatus != EndOfFile)
  359. return 0;
  360. U32 copyAmount = getMin(size, mFileData->mFileSize - mCurrentPos);
  361. dMemcpy(dst, (U8*) mFileData->mBuffer + mCurrentPos, copyAmount);
  362. mCurrentPos += copyAmount;
  363. mFileData->mLastAccess = Time::getCurrentTime();
  364. if (mCurrentPos == mFileData->mFileSize)
  365. mStatus = EndOfFile;
  366. return copyAmount;
  367. }
  368. U32 MemFile::write(const void* src, U32 size)
  369. {
  370. if ((mStatus != Open && mStatus != EndOfFile) || !size)
  371. return 0;
  372. if (mFileData->mFileSize + size > mFileData->mBufferSize)
  373. {
  374. // Keep doubling our buffer size until we're big enough.
  375. while (mFileData->mFileSize + size > mFileData->mBufferSize)
  376. mFileData->mBufferSize *= 2;
  377. mFileData->mBuffer = dRealloc(mFileData->mBuffer, mFileData->mBufferSize);
  378. if (!mFileData->mBuffer)
  379. {
  380. mStatus = FileSystemFull;
  381. return 0;
  382. }
  383. }
  384. dMemcpy((U8*)mFileData->mBuffer + mCurrentPos, src, size);
  385. mCurrentPos += size;
  386. mFileData->mFileSize = getMax(mFileData->mFileSize, mCurrentPos);
  387. mFileData->mLastAccess = Time::getCurrentTime();
  388. mFileData->mModified = mFileData->mLastAccess;
  389. return size;
  390. }
  391. //-----------------------------------------------------------------------------
  392. MemDirectory::MemDirectory(MemFileSystem* fs, MemDirectoryData* dir)
  393. {
  394. mStatus = Closed;
  395. mDirectoryData = dir;
  396. mFileSystem = fs;
  397. }
  398. MemDirectory::~MemDirectory()
  399. {
  400. }
  401. Path MemDirectory::getName() const
  402. {
  403. return mDirectoryData->mPath;
  404. }
  405. bool MemDirectory::open()
  406. {
  407. mSearchIndex = 0;
  408. mStatus = Open;
  409. return true;
  410. }
  411. bool MemDirectory::close()
  412. {
  413. return true;
  414. }
  415. bool MemDirectory::read(Attributes* entry)
  416. {
  417. if (mStatus != Open)
  418. return false;
  419. if (mSearchIndex < mDirectoryData->mDirectories.size())
  420. {
  421. mDirectoryData->mDirectories[mSearchIndex]->getAttributes(entry);
  422. mSearchIndex++;
  423. return true;
  424. }
  425. AssertFatal(mSearchIndex > mDirectoryData->mDirectories.size(), "This should not happen!");
  426. U32 fileIndex = mSearchIndex - mDirectoryData->mDirectories.size();
  427. if (fileIndex < mDirectoryData->mFiles.size())
  428. {
  429. mDirectoryData->mFiles[mSearchIndex]->getAttributes(entry);
  430. mSearchIndex++;
  431. return true;
  432. }
  433. return false;
  434. }
  435. U32 MemDirectory::calculateChecksum()
  436. {
  437. // Return checksum of current entry
  438. return 0;
  439. }
  440. bool MemDirectory::getAttributes(Attributes* attr)
  441. {
  442. return mDirectoryData->getAttributes(attr);
  443. }
  444. FileNode::NodeStatus MemDirectory::getStatus() const
  445. {
  446. return mStatus;
  447. }
  448. } // Namespace Mem
  449. } // Namespace Torque