Filesystem.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #pragma once
  23. #include "String.h"
  24. #include "Stream.h"
  25. #include "OS.h"
  26. namespace crown
  27. {
  28. struct FilesystemEntry
  29. {
  30. enum Type
  31. {
  32. DIRECTORY = 0, ///< The entry is a directory
  33. FILE, ///< The entry is a file
  34. MEMORY, ///< The entry is a memory file (i.e. does not exist on the disk)
  35. UNKNOWN ///< The entry type is unknown
  36. };
  37. FilesystemEntry() : type(UNKNOWN) {}
  38. Type type; ///< Type of the entry
  39. char os_path[os::MAX_PATH_LENGTH]; ///< OS-specific path (use only for debug)
  40. char relative_path[os::MAX_PATH_LENGTH]; ///< Relative path of the entry
  41. };
  42. /// Filesystem.
  43. ///
  44. /// Provides a platform-independent way to access files and directories
  45. /// on the host filesystem.
  46. ///
  47. /// Accessing files:
  48. /// Every file and every directory must be accessed through the Filesystem.
  49. /// Not a single C/C++ std file io call or other similar facilities
  50. /// should be used in any other part of the engine in order to maintain
  51. /// absolute platform independence.
  52. ///
  53. /// Filesystem maintains a root path which acts as base directory for every
  54. /// file operation; access to files outside the root path is not allowed. If
  55. /// you really need it, instantiate another filesystem whith the appropriate
  56. /// root path (e.g.)
  57. ///
  58. /// Filesystem fs("/home/foo"); // fs will use "/home/foo" as root path
  59. ///
  60. /// fs.is_file("bar.txt"); // file "bar.txt" is relative to the root path,
  61. /// // so it refers to "/home/foo/bar.txt"
  62. ///
  63. /// The filesystem will take care of the necessary path conversions.
  64. /// The root path can be really anything: platform-specific/absolute/relative/whatever.
  65. /// Examples of valid root paths.
  66. ///
  67. /// 1) "/home/foo"
  68. /// 2) "C:\Users\Phil"
  69. /// 3) "\abc$\/..)?/"
  70. ///
  71. /// The relative paths must follow some strict rules:
  72. ///
  73. /// a) Only unix-like pathnames (i.e. case sensitive and using '/' as separator)
  74. /// are allowed.
  75. /// b) Only relative paths are allowed: the filesystem is responsible for
  76. /// the creation of its absolute platform-specific counterpart.
  77. /// c) Filesystem forbids pathnames containing '.' and '..' to prevent access to
  78. /// files outside the filesystem's root path.
  79. /// d) Platform specific characters like '/', '\\' and ':' are forbidden as well.
  80. /// e) Symlinks, on platforms which support them, are _not_ resolved for the same
  81. /// reason of c)
  82. /// f) Although mixed-case pathnames are allowed, it is generally safer to use
  83. /// only lower-case ones for maximum compatibility.
  84. ///
  85. /// Examples of valid relative paths.
  86. ///
  87. /// data/textures/grass.texture
  88. /// grass.texture
  89. /// foo/bar
  90. class Filesystem
  91. {
  92. public:
  93. /// The @root_path must be absolute.
  94. Filesystem(const char* root_path);
  95. ~Filesystem();
  96. /// Returns the root path of the filesystem
  97. const char* root_path() const;
  98. /// TODO
  99. bool get_info(const char* base_path, const char* relative_path, FilesystemEntry& info);
  100. /// Returns whether the @relative_path exists on disk
  101. bool exists(const char* relative_path);
  102. /// Returns whether @relative_path is a regular file
  103. bool is_file(const char* relative_path);
  104. /// Returns whether @relative_path if a directory
  105. bool is_dir(const char* relative_path);
  106. /// Creates a regular file named @relative_path
  107. bool create_file(const char* relative_path);
  108. /// Creates a directory named @relative_path
  109. bool create_dir(const char* relative_path);
  110. /// Deletes the regular file @relative_path
  111. bool delete_file(const char* relative_path);
  112. /// Deletes the directory @relative_path
  113. bool delete_dir(const char* relative_path);
  114. Stream* open(const char* relative_path, StreamOpenMode mode);
  115. void close(Stream* stream);
  116. private:
  117. const char* build_os_path(const char* basePath, const char* relative_path);
  118. private:
  119. char m_root_path[os::MAX_PATH_LENGTH];
  120. // Disable copying
  121. Filesystem(const Filesystem&);
  122. Filesystem& operator=(const Filesystem&);
  123. friend class Device;
  124. };
  125. } // namespace crown