virtualMountSystem.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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/virtualMountSystem.h"
  24. #include "console/console.h"
  25. #include "core/tAlgorithm.h"
  26. namespace Torque
  27. {
  28. namespace FS
  29. {
  30. bool gVMSVerboseLog = false;
  31. bool VirtualMountSystem::mount(String root, FileSystemRef fs)
  32. {
  33. bool ok = Parent::mount(root,fs);
  34. if (!ok)
  35. return false;
  36. #ifdef TORQUE_LOWER_ZIPCASE
  37. root = String::ToLower(root);
  38. #endif
  39. mRootMap[root].push_back(fs);
  40. // PathFSMap* rootDict = NULL;
  41. // if (!mMountMap.tryGetValue(root, rootDict))
  42. // {
  43. // rootDict = new PathFSMap();
  44. // mMountMap[root] = rootDict;
  45. // }
  46. //
  47. // U32 start = Platform::getRealMilliseconds();
  48. //
  49. // // get the paths from the fs and add them to the rootDict
  50. // Vector<String> paths;
  51. //
  52. // // we'll use the mount system's findByPattern function to build the path list.
  53. // // but, we want to override its default behavior so that it searches only the desired fs.
  54. // _setFindByPatternOverrideFS(fs);
  55. //
  56. // Torque::Path basePath;
  57. // // we use an empty root so that the resulting filenames don't have the root filename in them.
  58. // // we don't want to include the root in the dict has entries. we can omit the root because we have
  59. // // specified an override FS; the search would fail otherwise.
  60. // //basePath.setRoot(root);
  61. // basePath.setRoot("");
  62. // basePath.setPath("/");
  63. // mUseParentFind = true;
  64. // if (findByPattern(basePath, "*.*", true, paths, true) == -1)
  65. // {
  66. // // this is probably a problem
  67. // _log("Unable to get paths from filesystem for virtual mount");
  68. // _setFindByPatternOverrideFS(NULL);
  69. // mUseParentFind = false;
  70. // return false;
  71. // }
  72. //
  73. // _setFindByPatternOverrideFS(NULL);
  74. // mUseParentFind = false;
  75. //
  76. // for (S32 i = 0; i < paths.size(); ++i)
  77. // {
  78. // String path = String::ToLower(paths[i]);
  79. //
  80. // // is it a directory? if so remove dir prefix
  81. // String dirPrefix = "DIR:";
  82. // String::SizeType dIdx = path.find(dirPrefix, 0, String::NoCase);
  83. // if (dIdx == 0)
  84. // path = path.substr(dirPrefix.length());
  85. // // omit leading /
  86. // if (path[(String::SizeType)0] == '/')
  87. // path = path.substr(1);
  88. //
  89. // // warn about duplicate files (not directories)
  90. // // JMQ: disabled this, it false alarms at startup because the mount doc always mounts the
  91. // // root before other processing mounts. still, would be useful, maybe change the mount doc to
  92. // // not mount root?
  93. // if (dIdx != 0 && (*rootDict)[path].size() > 0)
  94. // _log(String::ToString("Duplicate file path detected, first volume containing file will be used: %s", path.c_str()));
  95. //
  96. // (*rootDict)[path].push_back(fs);
  97. // }
  98. //
  99. // if (gVMSVerboseLog)
  100. // _log(String::ToString("Indexed virtual file system in %ums", Platform::getRealMilliseconds() - start));
  101. return true;
  102. }
  103. bool VirtualMountSystem::mount(String root, const Path &path)
  104. {
  105. //AssertFatal(false, "This function not supported in virtual mount system");
  106. return Parent::mount(root, path);
  107. }
  108. FileSystemRef VirtualMountSystem::unmount(String root)
  109. {
  110. FileSystemRef ret = Parent::unmount(root);
  111. mRootMap.erase(root);
  112. // clear all filesystem lists for root.
  113. // PathFSMap* rootDict = NULL;
  114. // root = String::ToLower(root);
  115. // if (!mMountMap.tryGetValue(root, rootDict))
  116. // return ret;
  117. //
  118. // // buh bye
  119. // mMountMap.erase(root);
  120. // delete rootDict;
  121. return ret;
  122. }
  123. bool VirtualMountSystem::unmount(FileSystemRef fs)
  124. {
  125. bool unmounted = Parent::unmount(fs);
  126. if (!unmounted)
  127. return false;
  128. for(PathFSMap::Iterator ritr = mRootMap.begin();ritr != mRootMap.end();++ritr)
  129. {
  130. RootToFSVec &vec = (*ritr).value;
  131. for (S32 i = vec.size() - 1;i >= 0;i--)
  132. {
  133. if (vec[i].getPointer() == fs.getPointer())
  134. vec.erase(i);
  135. }
  136. }
  137. // this is a linear time operation, because we have to search every path in all roots
  138. // to remove references to the fs.
  139. // contant time operation can be achieved be using the unmount(string) version, which unmounts all
  140. // filesystems for a given root and so doesn't need to do any searching.
  141. // U32 start = Platform::getRealMilliseconds();
  142. // for (RootToPathFSMap::Iterator riter = mMountMap.begin();
  143. // riter != mMountMap.end();
  144. // ++riter)
  145. // {
  146. // PathFSMap* rootDict = (*riter).value;
  147. // for (PathFSMap::Iterator piter = rootDict->begin();
  148. // piter != rootDict->end();
  149. // ++piter)
  150. // {
  151. // Vector<FileSystemRef>& plist = (*piter).value;
  152. // for (S32 i = plist.size() - 1;
  153. // i >= 0;
  154. // i--)
  155. // {
  156. // if (plist[i].getPointer() == fs.getPointer())
  157. // plist.erase(i);
  158. // }
  159. // }
  160. // }
  161. //
  162. // if (gVMSVerboseLog)
  163. // _log(String::ToString("Unmounted virtual file system in %ums", Platform::getRealMilliseconds() - start));
  164. return true;
  165. }
  166. S32 VirtualMountSystem::findByPattern( const Path &inBasePath, const String &inFilePattern, bool inRecursive, Vector<String> &outList, bool includeDirs/* =false */, bool multiMatch /* = true */ )
  167. {
  168. if (mUseParentFind)
  169. // use parent version
  170. return Parent::findByPattern(inBasePath, inFilePattern, inRecursive, outList, includeDirs, multiMatch);
  171. // don't want to re-enter this version
  172. mUseParentFind = true;
  173. // kind of cheesy, just call find by pattern on each File system mounted on the root
  174. for (Vector<MountFS>::const_iterator itr = mMountList.begin(); itr != mMountList.end(); itr++)
  175. {
  176. if (itr->root.equal( inBasePath.getRoot(), String::NoCase ) )
  177. {
  178. FileSystemRef fsref = itr->fileSystem;
  179. _setFindByPatternOverrideFS(fsref);
  180. Parent::findByPattern(inBasePath, inFilePattern, inRecursive, outList, includeDirs, multiMatch);
  181. _setFindByPatternOverrideFS(NULL);
  182. }
  183. }
  184. mUseParentFind = false;
  185. return outList.size();
  186. }
  187. bool VirtualMountSystem::createPath(const Path& path)
  188. {
  189. bool ret = Parent::createPath(path);
  190. // if (ret)
  191. // {
  192. // // make sure the filesystem that owns the path has the path elements
  193. // // in its search table (so that we can open the file, if it is new)
  194. // String root = String::ToLower(path.getRoot());
  195. // FileSystemRef fsRef = getFileSystem(path);
  196. //
  197. // PathFSMap* rootDict = mMountMap[root];
  198. // if (rootDict)
  199. // {
  200. // // add all directories in the path
  201. // // add the filename
  202. //
  203. // // Start from the top and work our way down
  204. // Path sub,dir;
  205. // dir.setPath("");
  206. // for (U32 i = 0; i < path.getDirectoryCount(); i++)
  207. // {
  208. // sub.setPath(path.getDirectory(i));
  209. // dir.appendPath(sub);
  210. //
  211. // Vector<FileSystemRef>& fsList = (*rootDict)[String::ToLower(dir.getPath())];
  212. // Vector<FileSystemRef>::iterator iter = ::find(fsList.begin(), fsList.end(), fsRef);
  213. //
  214. // if (iter == fsList.end())
  215. // fsList.push_back(fsRef);
  216. // }
  217. //
  218. // // add full file path
  219. // Vector<FileSystemRef>& fsList = (*rootDict)[String::ToLower(path.getFullPath(false))];
  220. // Vector<FileSystemRef>::iterator iter = ::find(fsList.begin(), fsList.end(), fsRef);
  221. //
  222. // if (iter == fsList.end())
  223. // fsList.push_back(fsRef);
  224. // }
  225. // }
  226. return ret;
  227. }
  228. void VirtualMountSystem::_log(const String& msg)
  229. {
  230. String newMsg = "VirtualMountSystem: " + msg;
  231. Con::warnf("%s", newMsg.c_str());
  232. }
  233. FileSystemRef VirtualMountSystem::_removeMountFromList(String root)
  234. {
  235. return Parent::_removeMountFromList(root);
  236. }
  237. FileSystemRef VirtualMountSystem::_getFileSystemFromList(const Path& fullpath) const
  238. {
  239. String root = fullpath.getRoot();
  240. String path = fullpath.getFullPathWithoutRoot();
  241. // eat leading slash
  242. if (path[(String::SizeType)0] == '/')
  243. path = path.substr(1);
  244. #ifdef TORQUE_LOWER_ZIPCASE
  245. // lowercase the root and path
  246. root = String::ToLower(root);
  247. path = String::ToLower(path);
  248. #endif
  249. // find the dictionary for root
  250. // PathFSMap* rootDict = NULL;
  251. // if (!mMountMap.tryGetValue(root, rootDict))
  252. // return NULL;
  253. //
  254. // // see if we have a FS list for this path
  255. // Vector<FileSystemRef>& fsList = (*rootDict)[path];
  256. RootToFSVec fsList;
  257. if(! mRootMap.tryGetValue(root, fsList))
  258. return NULL;
  259. if (fsList.size() == 0)
  260. {
  261. // no exact match for path, defer to parent
  262. return Parent::_getFileSystemFromList(fullpath);
  263. }
  264. else
  265. {
  266. // find the right file system
  267. if(fsList.size() == 1)
  268. return fsList[0];
  269. // Go in reverse order to pick up the last matching virtual path
  270. for(S32 i = fsList.size()-1; i >= 0 ; --i)
  271. {
  272. FileNodeRef fn = fsList[i]->resolve(path);
  273. if(fn != NULL)
  274. return fsList[i];
  275. }
  276. return fsList[0];
  277. }
  278. }
  279. } //namespace FS
  280. } //namespace Torque