virtualMountSystem.cpp 10 KB

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