VirtualFileSystem.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. #ifndef GUL_VIRTUAL_FILESYSTEM_H
  2. #define GUL_VIRTUAL_FILESYSTEM_H
  3. #include <variant>
  4. #include <filesystem>
  5. #include <map>
  6. #include <iostream>
  7. #include <fstream>
  8. #include <vector>
  9. #include <set>
  10. #include <any>
  11. namespace gul
  12. {
  13. #define USE_UNION
  14. /**
  15. * @brief is_base_of
  16. * @param base
  17. * @param full
  18. * @return
  19. *
  20. * Returns true if base exists at the start of full. For strings, this would be equivelant too
  21. * full.starts_with(base)
  22. */
  23. inline bool is_base_of(std::filesystem::path const & base, std::filesystem::path const & full)
  24. {
  25. auto relPath2 = full.lexically_relative(base);
  26. if(!relPath2.empty())
  27. {
  28. if(*relPath2.begin() == "..")
  29. {
  30. return false;
  31. }
  32. }
  33. return true;
  34. }
  35. struct File
  36. {
  37. std::vector<uint8_t> data;
  38. };
  39. struct Directory
  40. {
  41. std::vector<std::shared_ptr<void>> descriptors;
  42. };
  43. struct Custom
  44. {
  45. std::string type;
  46. std::any value;
  47. };
  48. struct Mount
  49. {
  50. using host_path_type = std::filesystem::path;
  51. #ifdef USE_UNION
  52. std::vector<host_path_type> hosts;
  53. Mount() = default;
  54. Mount(host_path_type const &r)
  55. {
  56. hosts.push_back(r);
  57. }
  58. Mount(std::vector<host_path_type> const &r) : hosts(r)
  59. {
  60. }
  61. template<typename callable_t>
  62. void for_each(host_path_type stem, callable_t && CC) const
  63. {
  64. std::set<host_path_type> _set;
  65. for(auto it = hosts.rbegin(); it!=hosts.rend(); ++it)
  66. {
  67. auto & host = *it;
  68. if(!std::filesystem::exists(host / stem))
  69. continue;
  70. for(auto & A : std::filesystem::directory_iterator(host / stem))
  71. {
  72. auto stem2 = A.path().lexically_relative(host/stem);
  73. if(_set.insert(stem2).second) // need a set so that multiple items
  74. { // dont get duplicated
  75. CC(stem2);
  76. }
  77. }
  78. }
  79. }
  80. bool is_directory(std::filesystem::path const & stem) const
  81. {
  82. for(auto it = hosts.rbegin(); it!=hosts.rend(); ++it)
  83. {
  84. if(std::filesystem::exists( *it / stem))
  85. {
  86. return std::filesystem::is_directory(*it/stem);
  87. }
  88. }
  89. return false;
  90. }
  91. bool is_file(std::filesystem::path const & stem) const
  92. {
  93. for(auto it = hosts.rbegin(); it!=hosts.rend(); ++it)
  94. {
  95. if(std::filesystem::exists( *it / stem))
  96. {
  97. return std::filesystem::is_regular_file(*it/stem);
  98. }
  99. }
  100. return false;
  101. }
  102. bool exists(std::filesystem::path const & stem) const
  103. {
  104. for(auto it = hosts.rbegin(); it!=hosts.rend(); ++it)
  105. {
  106. if(std::filesystem::exists( *it / stem))
  107. {
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113. bool file_size(std::filesystem::path const & stem) const
  114. {
  115. for(auto it = hosts.rbegin(); it!=hosts.rend(); ++it)
  116. {
  117. if(std::filesystem::exists( *it / stem))
  118. {
  119. return std::filesystem::file_size(*it/stem);
  120. }
  121. }
  122. return false;
  123. }
  124. host_path_type host_path(std::filesystem::path const & stem) const
  125. {
  126. for(auto it = hosts.rbegin(); it!=hosts.rend(); ++it)
  127. {
  128. if(std::filesystem::exists( *it / stem))
  129. {
  130. return *it/stem;
  131. }
  132. }
  133. if(hosts.size())
  134. {
  135. return hosts[0] / stem;
  136. }
  137. return {};
  138. }
  139. #else
  140. host_path_type host;
  141. bool is_directory(std::filesystem::path const & stem) const
  142. {
  143. return std::filesystem::is_directory( host / stem);
  144. }
  145. bool is_file(std::filesystem::path const & stem) const
  146. {
  147. return std::filesystem::is_regular_file( host / stem);
  148. }
  149. bool exists(std::filesystem::path const & stem) const
  150. {
  151. return std::filesystem::exists( host / stem);
  152. }
  153. auto file_size(std::filesystem::path const & stem) const
  154. {
  155. return std::filesystem::file_size(host / stem);
  156. }
  157. auto host_path(std::filesystem::path const & stem) const
  158. {
  159. return host / stem;
  160. }
  161. #endif
  162. };
  163. using FileDescriptor = std::variant<File, Directory, Mount, Custom>;
  164. struct VFS
  165. {
  166. using vfs_path_type = std::filesystem::path;
  167. using host_path_type = std::filesystem::path;
  168. VFS()
  169. {
  170. desc["/"] = Directory{};
  171. }
  172. void mkdir(vfs_path_type const pf)
  173. {
  174. if(is_directory(pf.parent_path()) )
  175. {
  176. desc[pf] = Directory{};
  177. }
  178. else
  179. {
  180. mkdir(pf.parent_path());
  181. mkdir(pf);
  182. }
  183. }
  184. void mount(vfs_path_type const & pf, host_path_type const & host)
  185. {
  186. desc[pf] = Mount(host);
  187. }
  188. void mount(vfs_path_type const & pf, std::vector<host_path_type> const & host)
  189. {
  190. desc[pf] = Mount(host);
  191. }
  192. void unmount(vfs_path_type const & pf)
  193. {
  194. desc.erase(pf);
  195. }
  196. void list(vfs_path_type const & p) const
  197. {
  198. for_each(p, [](auto v)
  199. {
  200. std::cout << v << std::endl;
  201. });
  202. }
  203. template<typename callable_t>
  204. bool open(vfs_path_type const & p, callable_t && C) const
  205. {
  206. auto hp = host_path(p);
  207. if(std::filesystem::exists(hp))
  208. {
  209. std::ifstream in(hp);
  210. C(in);
  211. return true;
  212. }
  213. return false;
  214. }
  215. template<typename callable_t>
  216. void for_each_file(vfs_path_type const & p, callable_t && C) const
  217. {
  218. return for_each(p,C);
  219. }
  220. template<typename callable_t>
  221. void for_each_file_recursive(vfs_path_type const & root, callable_t && C) const
  222. {
  223. return for_each_file(root, [root,&C, this](auto const & _p)
  224. {
  225. if(is_directory(root / _p))
  226. {
  227. for_each_file_recursive(root / _p, C);
  228. }
  229. else
  230. {
  231. C(root / _p);
  232. }
  233. });
  234. }
  235. template<typename callable_t>
  236. void for_each(vfs_path_type const & p, callable_t && CC) const
  237. {
  238. auto [mnt, stem] = splitMount(p);
  239. if(mnt.empty())
  240. {
  241. // The path, p, does not contain a mount point
  242. // check if p exists in the descriptors
  243. auto it = desc.find(p);
  244. if(it != desc.end())
  245. {
  246. ++it;
  247. while(it != desc.end())
  248. {
  249. if(is_base_of(p, it->first))
  250. {
  251. auto removeBase = it->first.lexically_relative(p);
  252. if(1==std::distance(removeBase.begin(), removeBase.end()))
  253. {
  254. CC(removeBase);
  255. }
  256. }
  257. ++it;
  258. }
  259. }
  260. return;
  261. }
  262. auto & M = desc.at(mnt);
  263. if(std::holds_alternative<Mount>(M))
  264. {
  265. #ifdef USE_UNION
  266. std::get<Mount>(M).for_each(stem, CC);
  267. #else
  268. auto & host = std::get<Mount>(M).host;
  269. for(auto & A : std::filesystem::directory_iterator(host / stem))
  270. {
  271. CC(A.path().lexically_relative(host / stem));
  272. }
  273. #endif
  274. }
  275. }
  276. /**
  277. * @brief exists
  278. * @param pf
  279. * @return
  280. *
  281. * Returns true if the file exists in the VFS.
  282. */
  283. bool exists(vfs_path_type const & pf) const
  284. {
  285. assert(pf.is_absolute());
  286. auto it = desc.find(pf);
  287. // exists in the Virtual files
  288. if(it != desc.end())
  289. {
  290. return true;
  291. }
  292. auto [mnt, stem] = splitMount(pf);
  293. if(!mnt.empty())
  294. {
  295. auto & M = std::get<Mount>(desc.at(mnt));
  296. return M.exists(stem);
  297. }
  298. return false;
  299. }
  300. bool is_mount(vfs_path_type const & pf) const
  301. {
  302. auto it = desc.find(pf);
  303. if(it == desc.end())
  304. return false;
  305. return std::holds_alternative<Mount>(it->second);
  306. }
  307. /**
  308. * @brief splitMount
  309. * @param pf
  310. * @return
  311. *
  312. * Given a abs path in the VFS. return two components [mount,stem]
  313. * where mount is a path to an active mount point and stem is the
  314. * path within the mount
  315. */
  316. std::pair<vfs_path_type, vfs_path_type> splitMount(vfs_path_type const & pf) const
  317. {
  318. vfs_path_type left = pf.relative_path();
  319. vfs_path_type right;
  320. while(!left.empty())
  321. {
  322. // if 'left' is a mount point to the host
  323. // then check if right exists the filesystem
  324. if(is_mount(vfs_path_type("/") / left))
  325. {
  326. return {vfs_path_type("/") / left, right};
  327. }
  328. // remove the filename from 'left' and
  329. // append it to the front of 'right'
  330. // left = path/to/some
  331. // right = file.txt
  332. right = right.empty() ? left.filename() : (left.filename() / right);
  333. left = left.parent_path();
  334. }
  335. return {{},pf};
  336. }
  337. bool is_directory(vfs_path_type const & pf) const
  338. {
  339. assert(pf.is_absolute());
  340. auto it = desc.find(pf);
  341. // exists in the Virtual files
  342. if(it != desc.end())
  343. {
  344. return std::holds_alternative<Directory>(it->second)
  345. || std::holds_alternative<Mount>(it->second);
  346. }
  347. auto [mnt, stem] = splitMount(pf);
  348. if(!mnt.empty())
  349. {
  350. auto & M = std::get<Mount>(desc.at(mnt));
  351. return M.is_directory(stem);
  352. }
  353. return false;
  354. }
  355. bool is_file(vfs_path_type const & pf) const
  356. {
  357. assert(pf.is_absolute());
  358. auto it = desc.find(pf);
  359. // exists in the Virtual files
  360. if(it != desc.end())
  361. {
  362. return std::holds_alternative<File>(it->second);
  363. }
  364. auto [mnt, stem] = splitMount(pf);
  365. if(!mnt.empty())
  366. {
  367. auto & M = std::get<Mount>(desc.at(mnt));
  368. return M.is_file(stem);
  369. }
  370. return false;
  371. }
  372. uintmax_t file_size(vfs_path_type const & pf) const
  373. {
  374. assert(pf.is_absolute());
  375. auto it = desc.find(pf);
  376. // exists in the Virtual files
  377. if(it != desc.end())
  378. {
  379. if(std::holds_alternative<File>(it->second))
  380. {
  381. return std::get<File>(it->second).data.size();
  382. }
  383. }
  384. auto [mnt, stem] = splitMount(pf);
  385. if(!mnt.empty())
  386. {
  387. auto & M = std::get<Mount>(desc.at(mnt));
  388. return M.file_size(stem);
  389. }
  390. return 0;
  391. }
  392. void print()
  393. {
  394. for(auto &[a,b] : desc)
  395. {
  396. std::cout << a << std::endl;
  397. }
  398. }
  399. host_path_type host_path(vfs_path_type const pf) const
  400. {
  401. assert(pf.is_absolute());
  402. auto it = desc.find(pf);
  403. // exists in the Virtual files
  404. if(it != desc.end())
  405. {
  406. return {};
  407. //if(std::holds_alternative<File>(it->second))
  408. //{
  409. // auto & F = std::get<File>(it->second);
  410. // std::istringstream() F.data
  411. //}
  412. }
  413. auto [mnt, stem] = splitMount(pf);
  414. if(!mnt.empty())
  415. {
  416. auto & M = std::get<Mount>(desc.at(mnt));
  417. return M.host_path(stem);
  418. }
  419. return {};
  420. }
  421. std::map<vfs_path_type, FileDescriptor> desc;
  422. };
  423. }
  424. #endif