meta_file_system.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. // Copyright (c) 2012- PPSSPP Project.
  2. // This program is free software: you can redistribute it and/or modify
  3. // it under the terms of the GNU General Public License as published by
  4. // the Free Software Foundation, version 2.0 or later versions.
  5. // This program is distributed in the hope that it will be useful,
  6. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. // GNU General Public License 2.0 for more details.
  9. // A copy of the GPL 2.0 should have been included with the program.
  10. // If not, see http://www.gnu.org/licenses/
  11. // Official git repository and contact information can be found at
  12. // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
  13. #include <set>
  14. #include <algorithm>
  15. #include "common/string_util.h"
  16. #include "core/file_sys/meta_file_system.h"
  17. static bool ApplyPathStringToComponentsVector(std::vector<std::string> &vector, const std::string &pathString)
  18. {
  19. size_t len = pathString.length();
  20. size_t start = 0;
  21. while (start < len)
  22. {
  23. size_t i = pathString.find('/', start);
  24. if (i == std::string::npos)
  25. i = len;
  26. if (i > start)
  27. {
  28. std::string component = pathString.substr(start, i - start);
  29. if (component != ".")
  30. {
  31. if (component == "..")
  32. {
  33. if (vector.size() != 0)
  34. {
  35. vector.pop_back();
  36. }
  37. else
  38. {
  39. // The PSP silently ignores attempts to .. to parent of root directory
  40. WARN_LOG(FILESYS, "RealPath: ignoring .. beyond root - root directory is its own parent: \"%s\"", pathString.c_str());
  41. }
  42. }
  43. else
  44. {
  45. vector.push_back(component);
  46. }
  47. }
  48. }
  49. start = i + 1;
  50. }
  51. return true;
  52. }
  53. /*
  54. * Changes relative paths to absolute, removes ".", "..", and trailing "/"
  55. * "drive:./blah" is absolute (ignore the dot) and "/blah" is relative (because it's missing "drive:")
  56. * babel (and possibly other games) use "/directoryThatDoesNotExist/../directoryThatExists/filename"
  57. */
  58. static bool RealPath(const std::string &currentDirectory, const std::string &inPath, std::string &outPath)
  59. {
  60. size_t inLen = inPath.length();
  61. if (inLen == 0)
  62. {
  63. WARN_LOG(FILESYS, "RealPath: inPath is empty");
  64. outPath = currentDirectory;
  65. return true;
  66. }
  67. size_t inColon = inPath.find(':');
  68. if (inColon + 1 == inLen)
  69. {
  70. // There's nothing after the colon, e.g. umd0: - this is perfectly valid.
  71. outPath = inPath;
  72. return true;
  73. }
  74. bool relative = (inColon == std::string::npos);
  75. std::string prefix, inAfterColon;
  76. std::vector<std::string> cmpnts; // path components
  77. size_t outPathCapacityGuess = inPath.length();
  78. if (relative)
  79. {
  80. size_t curDirLen = currentDirectory.length();
  81. if (curDirLen == 0)
  82. {
  83. ERROR_LOG(FILESYS, "RealPath: inPath \"%s\" is relative, but current directory is empty", inPath.c_str());
  84. return false;
  85. }
  86. size_t curDirColon = currentDirectory.find(':');
  87. if (curDirColon == std::string::npos)
  88. {
  89. ERROR_LOG(FILESYS, "RealPath: inPath \"%s\" is relative, but current directory \"%s\" has no prefix", inPath.c_str(), currentDirectory.c_str());
  90. return false;
  91. }
  92. if (curDirColon + 1 == curDirLen)
  93. {
  94. ERROR_LOG(FILESYS, "RealPath: inPath \"%s\" is relative, but current directory \"%s\" is all prefix and no path. Using \"/\" as path for current directory.", inPath.c_str(), currentDirectory.c_str());
  95. }
  96. else
  97. {
  98. const std::string curDirAfter = currentDirectory.substr(curDirColon + 1);
  99. if (! ApplyPathStringToComponentsVector(cmpnts, curDirAfter) )
  100. {
  101. ERROR_LOG(FILESYS,"RealPath: currentDirectory is not a valid path: \"%s\"", currentDirectory.c_str());
  102. return false;
  103. }
  104. outPathCapacityGuess += curDirLen;
  105. }
  106. prefix = currentDirectory.substr(0, curDirColon + 1);
  107. inAfterColon = inPath;
  108. }
  109. else
  110. {
  111. prefix = inPath.substr(0, inColon + 1);
  112. inAfterColon = inPath.substr(inColon + 1);
  113. }
  114. // Special case: "disc0:" is different from "disc0:/", so keep track of the single slash.
  115. if (inAfterColon == "/")
  116. {
  117. outPath = prefix + inAfterColon;
  118. return true;
  119. }
  120. if (! ApplyPathStringToComponentsVector(cmpnts, inAfterColon) )
  121. {
  122. WARN_LOG(FILESYS, "RealPath: inPath is not a valid path: \"%s\"", inPath.c_str());
  123. return false;
  124. }
  125. outPath.clear();
  126. outPath.reserve(outPathCapacityGuess);
  127. outPath.append(prefix);
  128. size_t numCmpnts = cmpnts.size();
  129. for (size_t i = 0; i < numCmpnts; i++)
  130. {
  131. outPath.append(1, '/');
  132. outPath.append(cmpnts[i]);
  133. }
  134. return true;
  135. }
  136. IFileSystem *MetaFileSystem::GetHandleOwner(u32 handle)
  137. {
  138. std::lock_guard<std::recursive_mutex> guard(lock);
  139. for (size_t i = 0; i < fileSystems.size(); i++)
  140. {
  141. if (fileSystems[i].system->OwnsHandle(handle))
  142. return fileSystems[i].system; //got it!
  143. }
  144. //none found?
  145. return 0;
  146. }
  147. bool MetaFileSystem::MapFilePath(const std::string &_inpath, std::string &outpath, MountPoint **system)
  148. {
  149. std::lock_guard<std::recursive_mutex> guard(lock);
  150. std::string realpath;
  151. // Special handling: host0:command.txt (as seen in Super Monkey Ball Adventures, for example)
  152. // appears to mean the current directory on the UMD. Let's just assume the current directory.
  153. std::string inpath = _inpath;
  154. if (strncasecmp(inpath.c_str(), "host0:", strlen("host0:")) == 0) {
  155. INFO_LOG(FILESYS, "Host0 path detected, stripping: %s", inpath.c_str());
  156. inpath = inpath.substr(strlen("host0:"));
  157. }
  158. const std::string *currentDirectory = &startingDirectory;
  159. _assert_msg_(FILESYS, false, "must implement equiv of __KernelGetCurThread");
  160. int currentThread = 0;//__KernelGetCurThread();
  161. currentDir_t::iterator it = currentDir.find(currentThread);
  162. if (it == currentDir.end())
  163. {
  164. //Attempt to emulate SCE_KERNEL_ERROR_NOCWD / 8002032C: may break things requiring fixes elsewhere
  165. if (inpath.find(':') == std::string::npos /* means path is relative */)
  166. {
  167. lastOpenError = -1;//SCE_KERNEL_ERROR_NOCWD;
  168. WARN_LOG(FILESYS, "Path is relative, but current directory not set for thread %i. returning 8002032C(SCE_KERNEL_ERROR_NOCWD) instead.", currentThread);
  169. }
  170. }
  171. else
  172. {
  173. currentDirectory = &(it->second);
  174. }
  175. if ( RealPath(*currentDirectory, inpath, realpath) )
  176. {
  177. for (size_t i = 0; i < fileSystems.size(); i++)
  178. {
  179. size_t prefLen = fileSystems[i].prefix.size();
  180. if (strncasecmp(fileSystems[i].prefix.c_str(), realpath.c_str(), prefLen) == 0)
  181. {
  182. outpath = realpath.substr(prefLen);
  183. *system = &(fileSystems[i]);
  184. INFO_LOG(FILESYS, "MapFilePath: mapped \"%s\" to prefix: \"%s\", path: \"%s\"", inpath.c_str(), fileSystems[i].prefix.c_str(), outpath.c_str());
  185. return true;
  186. }
  187. }
  188. }
  189. DEBUG_LOG(FILESYS, "MapFilePath: failed mapping \"%s\", returning false", inpath.c_str());
  190. return false;
  191. }
  192. void MetaFileSystem::Mount(std::string prefix, IFileSystem *system)
  193. {
  194. std::lock_guard<std::recursive_mutex> guard(lock);
  195. MountPoint x;
  196. x.prefix = prefix;
  197. x.system = system;
  198. fileSystems.push_back(x);
  199. }
  200. void MetaFileSystem::Unmount(std::string prefix, IFileSystem *system)
  201. {
  202. std::lock_guard<std::recursive_mutex> guard(lock);
  203. MountPoint x;
  204. x.prefix = prefix;
  205. x.system = system;
  206. fileSystems.erase(std::remove(fileSystems.begin(), fileSystems.end(), x), fileSystems.end());
  207. }
  208. void MetaFileSystem::Shutdown()
  209. {
  210. std::lock_guard<std::recursive_mutex> guard(lock);
  211. current = 6;
  212. // Ownership is a bit convoluted. Let's just delete everything once.
  213. std::set<IFileSystem *> toDelete;
  214. for (size_t i = 0; i < fileSystems.size(); i++) {
  215. toDelete.insert(fileSystems[i].system);
  216. }
  217. for (auto iter = toDelete.begin(); iter != toDelete.end(); ++iter)
  218. {
  219. delete *iter;
  220. }
  221. fileSystems.clear();
  222. currentDir.clear();
  223. startingDirectory = "";
  224. }
  225. u32 MetaFileSystem::OpenWithError(int &error, std::string filename, FileAccess access, const char *devicename)
  226. {
  227. std::lock_guard<std::recursive_mutex> guard(lock);
  228. u32 h = OpenFile(filename, access, devicename);
  229. error = lastOpenError;
  230. return h;
  231. }
  232. u32 MetaFileSystem::OpenFile(std::string filename, FileAccess access, const char *devicename)
  233. {
  234. std::lock_guard<std::recursive_mutex> guard(lock);
  235. lastOpenError = 0;
  236. std::string of;
  237. MountPoint *mount;
  238. if (MapFilePath(filename, of, &mount))
  239. {
  240. return mount->system->OpenFile(of, access, mount->prefix.c_str());
  241. }
  242. else
  243. {
  244. return 0;
  245. }
  246. }
  247. FileInfo MetaFileSystem::GetFileInfo(std::string filename)
  248. {
  249. std::lock_guard<std::recursive_mutex> guard(lock);
  250. std::string of;
  251. IFileSystem *system;
  252. if (MapFilePath(filename, of, &system))
  253. {
  254. return system->GetFileInfo(of);
  255. }
  256. else
  257. {
  258. FileInfo bogus; // TODO
  259. return bogus;
  260. }
  261. }
  262. bool MetaFileSystem::GetHostPath(const std::string &inpath, std::string &outpath)
  263. {
  264. std::lock_guard<std::recursive_mutex> guard(lock);
  265. std::string of;
  266. IFileSystem *system;
  267. if (MapFilePath(inpath, of, &system)) {
  268. return system->GetHostPath(of, outpath);
  269. } else {
  270. return false;
  271. }
  272. }
  273. std::vector<FileInfo> MetaFileSystem::GetDirListing(std::string path)
  274. {
  275. std::lock_guard<std::recursive_mutex> guard(lock);
  276. std::string of;
  277. IFileSystem *system;
  278. if (MapFilePath(path, of, &system))
  279. {
  280. return system->GetDirListing(of);
  281. }
  282. else
  283. {
  284. std::vector<FileInfo> empty;
  285. return empty;
  286. }
  287. }
  288. void MetaFileSystem::ThreadEnded(int threadID)
  289. {
  290. std::lock_guard<std::recursive_mutex> guard(lock);
  291. currentDir.erase(threadID);
  292. }
  293. int MetaFileSystem::ChDir(const std::string &dir)
  294. {
  295. std::lock_guard<std::recursive_mutex> guard(lock);
  296. // Retain the old path and fail if the arg is 1023 bytes or longer.
  297. if (dir.size() >= 1023)
  298. return -1;//SCE_KERNEL_ERROR_NAMETOOLONG;
  299. _assert_msg_(FILESYS, false, "must implement equiv of __KernelGetCurThread");
  300. int curThread = 0; //__KernelGetCurThread();
  301. std::string of;
  302. MountPoint *mountPoint;
  303. if (MapFilePath(dir, of, &mountPoint))
  304. {
  305. currentDir[curThread] = mountPoint->prefix + of;
  306. return 0;
  307. }
  308. else
  309. {
  310. for (size_t i = 0; i < fileSystems.size(); i++)
  311. {
  312. const std::string &prefix = fileSystems[i].prefix;
  313. if (strncasecmp(prefix.c_str(), dir.c_str(), prefix.size()) == 0)
  314. {
  315. // The PSP is completely happy with invalid current dirs as long as they have a valid device.
  316. WARN_LOG(FILESYS, "ChDir failed to map path \"%s\", saving as current directory anyway", dir.c_str());
  317. currentDir[curThread] = dir;
  318. return 0;
  319. }
  320. }
  321. WARN_LOG(FILESYS, "ChDir failed to map device for \"%s\", failing", dir.c_str());
  322. return -1;//SCE_KERNEL_ERROR_NODEV;
  323. }
  324. }
  325. bool MetaFileSystem::MkDir(const std::string &dirname)
  326. {
  327. std::lock_guard<std::recursive_mutex> guard(lock);
  328. std::string of;
  329. IFileSystem *system;
  330. if (MapFilePath(dirname, of, &system))
  331. {
  332. return system->MkDir(of);
  333. }
  334. else
  335. {
  336. return false;
  337. }
  338. }
  339. bool MetaFileSystem::RmDir(const std::string &dirname)
  340. {
  341. std::lock_guard<std::recursive_mutex> guard(lock);
  342. std::string of;
  343. IFileSystem *system;
  344. if (MapFilePath(dirname, of, &system))
  345. {
  346. return system->RmDir(of);
  347. }
  348. else
  349. {
  350. return false;
  351. }
  352. }
  353. int MetaFileSystem::RenameFile(const std::string &from, const std::string &to)
  354. {
  355. std::lock_guard<std::recursive_mutex> guard(lock);
  356. std::string of;
  357. std::string rf;
  358. IFileSystem *osystem;
  359. IFileSystem *rsystem = NULL;
  360. if (MapFilePath(from, of, &osystem))
  361. {
  362. // If it's a relative path, it seems to always use from's filesystem.
  363. if (to.find(":/") != to.npos)
  364. {
  365. if (!MapFilePath(to, rf, &rsystem))
  366. return -1;
  367. }
  368. else
  369. {
  370. rf = to;
  371. rsystem = osystem;
  372. }
  373. if (osystem != rsystem)
  374. return -1;//SCE_KERNEL_ERROR_XDEV;
  375. return osystem->RenameFile(of, rf);
  376. }
  377. else
  378. {
  379. return -1;
  380. }
  381. }
  382. bool MetaFileSystem::RemoveFile(const std::string &filename)
  383. {
  384. std::lock_guard<std::recursive_mutex> guard(lock);
  385. std::string of;
  386. IFileSystem *system;
  387. if (MapFilePath(filename, of, &system))
  388. {
  389. return system->RemoveFile(of);
  390. }
  391. else
  392. {
  393. return false;
  394. }
  395. }
  396. void MetaFileSystem::CloseFile(u32 handle)
  397. {
  398. std::lock_guard<std::recursive_mutex> guard(lock);
  399. IFileSystem *sys = GetHandleOwner(handle);
  400. if (sys)
  401. sys->CloseFile(handle);
  402. }
  403. size_t MetaFileSystem::ReadFile(u32 handle, u8 *pointer, s64 size)
  404. {
  405. std::lock_guard<std::recursive_mutex> guard(lock);
  406. IFileSystem *sys = GetHandleOwner(handle);
  407. if (sys)
  408. return sys->ReadFile(handle,pointer,size);
  409. else
  410. return 0;
  411. }
  412. size_t MetaFileSystem::WriteFile(u32 handle, const u8 *pointer, s64 size)
  413. {
  414. std::lock_guard<std::recursive_mutex> guard(lock);
  415. IFileSystem *sys = GetHandleOwner(handle);
  416. if (sys)
  417. return sys->WriteFile(handle,pointer,size);
  418. else
  419. return 0;
  420. }
  421. size_t MetaFileSystem::SeekFile(u32 handle, s32 position, FileMove type)
  422. {
  423. std::lock_guard<std::recursive_mutex> guard(lock);
  424. IFileSystem *sys = GetHandleOwner(handle);
  425. if (sys)
  426. return sys->SeekFile(handle,position,type);
  427. else
  428. return 0;
  429. }
  430. void MetaFileSystem::DoState(PointerWrap &p)
  431. {
  432. std::lock_guard<std::recursive_mutex> guard(lock);
  433. auto s = p.Section("MetaFileSystem", 1);
  434. if (!s)
  435. return;
  436. p.Do(current);
  437. // Save/load per-thread current directory map
  438. p.Do(currentDir);
  439. u32 n = (u32) fileSystems.size();
  440. p.Do(n);
  441. if (n != (u32) fileSystems.size())
  442. {
  443. p.SetError(p.ERROR_FAILURE);
  444. ERROR_LOG(FILESYS, "Savestate failure: number of filesystems doesn't match.");
  445. return;
  446. }
  447. for (u32 i = 0; i < n; ++i)
  448. fileSystems[i].system->DoState(p);
  449. }