Filesystem.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "StringUtils.h"
  25. #include "OS.h"
  26. #include "File.h"
  27. #include "HeapAllocator.h"
  28. #include "MountPoint.h"
  29. namespace crown
  30. {
  31. class File;
  32. /// Provides a platform-independent way to access files and directories
  33. /// on the host filesystem.
  34. ///
  35. /// Accessing files:
  36. /// Every file and every directory must be accessed through the Filesystem.
  37. /// Not a single C/C++ std file io call or other similar facilities
  38. /// should be used in any other part of the engine in order to maintain
  39. /// absolute platform independence.
  40. ///
  41. /// Filesystem maintains a root path which acts as base directory for every
  42. /// file operation; access to files outside the root path is not allowed. If
  43. /// you really need it, instantiate another filesystem whith the appropriate
  44. /// root path (e.g.)
  45. ///
  46. /// Filesystem fs("/home/foo"); // fs will use "/home/foo" as root path
  47. ///
  48. /// fs.is_file("bar.txt"); // file "bar.txt" is relative to the root path,
  49. /// // so it refers to "/home/foo/bar.txt"
  50. ///
  51. /// The filesystem will take care of the necessary path conversions.
  52. /// The root path must be an absolute path for the underlying operating system.
  53. /// Examples of valid root paths:
  54. ///
  55. /// 1) "/home/foo"
  56. /// 2) "C:\Users\Phil"
  57. ///
  58. /// The relative paths, used to access files, must follow some strict rules:
  59. ///
  60. /// a) Only unix-like pathnames (i.e. case sensitive and using '/' as separator)
  61. /// are allowed.
  62. /// b) Only relative paths are allowed: the filesystem is responsible for
  63. /// the creation of its absolute platform-specific counterpart.
  64. /// c) Filesystem forbids pathnames containing '.' and '..' to prevent access to
  65. /// files outside the filesystem's root path.
  66. /// d) Platform specific characters like '/', '\\' and ':' are forbidden as well.
  67. /// e) Symlinks, on platforms which support them, are _not_ resolved for the same
  68. /// reason of c)
  69. /// f) Although mixed-case pathnames are allowed, it is generally safer to use
  70. /// only lower-case ones for maximum compatibility.
  71. ///
  72. /// Examples of valid relative paths.
  73. ///
  74. /// 1) data/textures/grass.texture
  75. /// 2) grass.texture
  76. /// 3) foo/bar
  77. class Filesystem
  78. {
  79. public:
  80. Filesystem();
  81. ~Filesystem();
  82. ///
  83. void mount(MountPoint& mp);
  84. ///
  85. void umount(MountPoint& mp);
  86. /// Opens the file @a relative_path with the specified access @a mode
  87. /// contained in @a mount_point
  88. File* open(const char* mount_point, const char* relative_path, FileOpenMode mode);
  89. /// Closes a previously opened file @a stream
  90. void close(File* stream);
  91. bool exists(const char* mount_point, const char* relative_path);
  92. const char* os_path(const char* mount_point, const char* relative_path);
  93. private:
  94. /// Gets __first__ mount point fetchable by @a mount_point
  95. MountPoint* find_mount_point(const char* mount_point);
  96. // Disable copying
  97. Filesystem(const Filesystem&);
  98. Filesystem& operator=(const Filesystem&);
  99. private:
  100. HeapAllocator m_allocator;
  101. char m_root_path[MAX_PATH_LENGTH];
  102. MountPoint* m_mount_point_head;
  103. friend class Device;
  104. };
  105. } // namespace crown