DiskMountPoint.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #include "DiskMountPoint.h"
  24. #include "Assert.h"
  25. #include "DiskFile.h"
  26. #include "StringUtils.h"
  27. #include "Allocator.h"
  28. namespace crown
  29. {
  30. //-----------------------------------------------------------------------------
  31. DiskMountPoint::DiskMountPoint() : MountPoint(DISK_TYPE)
  32. {
  33. }
  34. //-----------------------------------------------------------------------------
  35. File* DiskMountPoint::open(const char* relative_path, FileOpenMode mode)
  36. {
  37. CE_ASSERT(exists(relative_path), "File does not exist: %s", relative_path);
  38. CE_ASSERT(is_file(relative_path), "File is not a regular file: %s", relative_path);
  39. return CE_NEW(default_allocator(), DiskFile)(mode, os_path(relative_path));
  40. }
  41. //-----------------------------------------------------------------------------
  42. void DiskMountPoint::close(File* file)
  43. {
  44. CE_DELETE(default_allocator(), file);
  45. }
  46. void DiskMountPoint::set_root_path(const char* root_path)
  47. {
  48. CE_ASSERT(root_path != NULL, "Root path must be != NULL");
  49. CE_ASSERT(os::is_absolute_path(root_path), "Root path must be absolute");
  50. string::strncpy(m_root_path, root_path, MAX_PATH_LENGTH);
  51. }
  52. //-----------------------------------------------------------------------------
  53. const char* DiskMountPoint::root_path() const
  54. {
  55. return m_root_path;
  56. }
  57. //-----------------------------------------------------------------------------
  58. bool DiskMountPoint::exists(const char* relative_path)
  59. {
  60. MountPointEntry info;
  61. return get_info(relative_path, info);
  62. }
  63. //-----------------------------------------------------------------------------
  64. bool DiskMountPoint::get_info(const char* relative_path, MountPointEntry& info)
  65. {
  66. // Entering OS-DEPENDENT-PATH-MODE
  67. // (i.e. os_path is of the form: C:\foo\relative_path or /foo/relative_path)
  68. const char* os_path = build_os_path(m_root_path, relative_path);
  69. Log::d("path : %s", os_path);
  70. string::strncpy(info.os_path, os_path, MAX_PATH_LENGTH);
  71. string::strncpy(info.relative_path, relative_path, MAX_PATH_LENGTH);
  72. if (os::is_reg(os_path))
  73. {
  74. info.type = MountPointEntry::FILE;
  75. return true;
  76. }
  77. else if (os::is_dir(os_path))
  78. {
  79. info.type = MountPointEntry::DIRECTORY;
  80. return true;
  81. }
  82. info.type = MountPointEntry::UNKNOWN;
  83. return false;
  84. }
  85. //-----------------------------------------------------------------------------
  86. bool DiskMountPoint::is_file(const char* relative_path)
  87. {
  88. MountPointEntry info;
  89. if (get_info(relative_path, info))
  90. {
  91. return info.type == MountPointEntry::FILE;
  92. }
  93. return false;
  94. }
  95. //-----------------------------------------------------------------------------
  96. bool DiskMountPoint::is_dir(const char* relative_path)
  97. {
  98. MountPointEntry info;
  99. if (get_info(relative_path, info))
  100. {
  101. return info.type == MountPointEntry::DIRECTORY;
  102. }
  103. return false;
  104. }
  105. //-----------------------------------------------------------------------------
  106. bool DiskMountPoint::create_file(const char* relative_path)
  107. {
  108. const char* os_path = build_os_path(m_root_path, relative_path);
  109. return os::mknod(os_path);
  110. }
  111. //-----------------------------------------------------------------------------
  112. bool DiskMountPoint::create_dir(const char* relative_path)
  113. {
  114. const char* os_path = build_os_path(m_root_path, relative_path);
  115. return os::mkdir(os_path);
  116. }
  117. //-----------------------------------------------------------------------------
  118. bool DiskMountPoint::delete_file(const char* relative_path)
  119. {
  120. const char* os_path = build_os_path(m_root_path, relative_path);
  121. return os::unlink(os_path);
  122. }
  123. //-----------------------------------------------------------------------------
  124. bool DiskMountPoint::delete_dir(const char* relative_path)
  125. {
  126. const char* os_path = build_os_path(m_root_path, relative_path);
  127. return os::rmdir(os_path);
  128. }
  129. //-----------------------------------------------------------------------------
  130. const char* DiskMountPoint::os_path(const char* relative_path)
  131. {
  132. static char os_path[MAX_PATH_LENGTH];
  133. MountPointEntry entry;
  134. get_info(relative_path, entry);
  135. string::strncpy(os_path, entry.os_path, MAX_PATH_LENGTH);
  136. return os_path;
  137. }
  138. //-----------------------------------------------------------------------------
  139. const char* DiskMountPoint::build_os_path(const char* base_path, const char* relative_path)
  140. {
  141. static char os_path[MAX_PATH_LENGTH];
  142. string::strncpy(os_path, base_path, MAX_PATH_LENGTH);
  143. size_t base_path_len = string::strlen(base_path);
  144. os_path[base_path_len] = PATH_SEPARATOR;
  145. os_path[base_path_len + 1] = '\0';
  146. string::strcat(os_path, relative_path);
  147. // FIXME FIXME FIXME Replace Crown-specific path separator with OS-specific one
  148. for (size_t j = 0; j < string::strlen(os_path); j++)
  149. {
  150. if (os_path[j] == '/')
  151. {
  152. os_path[j] = PATH_SEPARATOR;
  153. }
  154. }
  155. return os_path;
  156. }
  157. } // namespace crown