FileSystem.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/HashSet.h"
  24. #include "../Container/List.h"
  25. #include "../Core/Object.h"
  26. namespace Atomic
  27. {
  28. class AsyncExecRequest;
  29. /// Return files.
  30. static const unsigned SCAN_FILES = 0x1;
  31. /// Return directories.
  32. static const unsigned SCAN_DIRS = 0x2;
  33. /// Return also hidden files.
  34. static const unsigned SCAN_HIDDEN = 0x4;
  35. /// Subsystem for file and directory operations and access control.
  36. class ATOMIC_API FileSystem : public Object
  37. {
  38. ATOMIC_OBJECT(FileSystem, Object);
  39. public:
  40. /// Construct.
  41. FileSystem(Context* context);
  42. /// Destruct.
  43. ~FileSystem();
  44. /// Set the current working directory.
  45. bool SetCurrentDir(const String& pathName);
  46. /// Create a directory.
  47. bool CreateDir(const String& pathName);
  48. /// Set whether to execute engine console commands as OS-specific system command.
  49. void SetExecuteConsoleCommands(bool enable);
  50. /// Run a program using the command interpreter, block until it exits and return the exit code. Will fail if any allowed paths are defined.
  51. int SystemCommand(const String& commandLine, bool redirectStdOutToLog = false);
  52. /// Run a specific program, block until it exits and return the exit code. Will fail if any allowed paths are defined.
  53. int SystemRun(const String& fileName, const Vector<String>& arguments);
  54. /// Run a program using the command interpreter asynchronously. Return a request ID or M_MAX_UNSIGNED if failed. The exit code will be posted together with the request ID in an AsyncExecFinished event. Will fail if any allowed paths are defined.
  55. unsigned SystemCommandAsync(const String& commandLine);
  56. /// Run a specific program asynchronously. Return a request ID or M_MAX_UNSIGNED if failed. The exit code will be posted together with the request ID in an AsyncExecFinished event. Will fail if any allowed paths are defined.
  57. unsigned SystemRunAsync(const String& fileName, const Vector<String>& arguments);
  58. /// Open a file in an external program, with mode such as "edit" optionally specified. Will fail if any allowed paths are defined.
  59. bool SystemOpen(const String& fileName, const String& mode = String::EMPTY);
  60. /// Copy a file. Return true if successful.
  61. bool Copy(const String& srcFileName, const String& destFileName);
  62. /// Rename a file. Return true if successful.
  63. bool Rename(const String& srcFileName, const String& destFileName);
  64. /// Delete a file. Return true if successful.
  65. bool Delete(const String& fileName);
  66. /// Register a path as allowed to access. If no paths are registered, all are allowed. Registering allowed paths is considered securing the Urho3D execution environment: running programs and opening files externally through the system will fail afterward.
  67. void RegisterPath(const String& pathName);
  68. /// Set a file's last modified time as seconds since 1.1.1970. Return true on success.
  69. bool SetLastModifiedTime(const String& fileName, unsigned newTime);
  70. /// Return the absolute current working directory.
  71. String GetCurrentDir() const;
  72. /// Return whether is executing engine console commands as OS-specific system command.
  73. bool GetExecuteConsoleCommands() const { return executeConsoleCommands_; }
  74. /// Return whether paths have been registered.
  75. bool HasRegisteredPaths() const { return allowedPaths_.Size() > 0; }
  76. /// Check if a path is allowed to be accessed. If no paths are registered, all are allowed.
  77. bool CheckAccess(const String& pathName) const;
  78. /// Returns the file's last modified time as seconds since 1.1.1970, or 0 if can not be accessed.
  79. unsigned GetLastModifiedTime(const String& fileName) const;
  80. /// Check if a file exists.
  81. bool FileExists(const String& fileName) const;
  82. /// Check if a directory exists.
  83. bool DirExists(const String& pathName) const;
  84. /// Scan a directory for specified files.
  85. void ScanDir(Vector<String>& result, const String& pathName, const String& filter, unsigned flags, bool recursive) const;
  86. /// Return the program's directory.
  87. String GetProgramDir() const;
  88. /// Return the user documents directory.
  89. String GetUserDocumentsDir() const;
  90. /// Return the application preferences directory.
  91. String GetAppPreferencesDir(const String& org, const String& app) const;
  92. // ATOMIC BEGIN
  93. /// Check if a file or directory exists at the specified path
  94. bool Exists(const String& pathName) const { return FileExists(pathName) || DirExists(pathName); }
  95. bool CopyDir(const String& directoryIn, const String& directoryOut);
  96. bool CreateDirs(const String& root, const String& subdirectory);
  97. bool CreateDirsRecursive(const String& directoryIn);
  98. bool RemoveDir(const String& directoryIn, bool recursive);
  99. // ATOMIC END
  100. private:
  101. /// Scan directory, called internally.
  102. void ScanDirInternal
  103. (Vector<String>& result, String path, const String& startPath, const String& filter, unsigned flags, bool recursive) const;
  104. /// Handle begin frame event to check for completed async executions.
  105. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  106. /// Handle a console command event.
  107. void HandleConsoleCommand(StringHash eventType, VariantMap& eventData);
  108. /// Allowed directories.
  109. HashSet<String> allowedPaths_;
  110. /// Async execution queue.
  111. List<AsyncExecRequest*> asyncExecQueue_;
  112. /// Next async execution ID.
  113. unsigned nextAsyncExecID_;
  114. /// Flag for executing engine console commands as OS-specific system command. Default to true.
  115. bool executeConsoleCommands_;
  116. };
  117. /// Split a full path to path, filename and extension. The extension will be converted to lowercase by default.
  118. ATOMIC_API void
  119. SplitPath(const String& fullPath, String& pathName, String& fileName, String& extension, bool lowercaseExtension = true);
  120. /// Return the path from a full path.
  121. ATOMIC_API String GetPath(const String& fullPath);
  122. /// Return the filename from a full path.
  123. ATOMIC_API String GetFileName(const String& fullPath);
  124. /// Return the extension from a full path, converted to lowercase by default.
  125. ATOMIC_API String GetExtension(const String& fullPath, bool lowercaseExtension = true);
  126. /// Return the filename and extension from a full path. The case of the extension is preserved by default, so that the file can be opened in case-sensitive operating systems.
  127. ATOMIC_API String GetFileNameAndExtension(const String& fullPath, bool lowercaseExtension = false);
  128. /// Replace the extension of a file name with another.
  129. ATOMIC_API String ReplaceExtension(const String& fullPath, const String& newExtension);
  130. /// Add a slash at the end of the path if missing and convert to internal format (use slashes.)
  131. ATOMIC_API String AddTrailingSlash(const String& pathName);
  132. /// Remove the slash from the end of a path if exists and convert to internal format (use slashes.)
  133. ATOMIC_API String RemoveTrailingSlash(const String& pathName);
  134. /// Return the parent path, or the path itself if not available.
  135. ATOMIC_API String GetParentPath(const String& pathName);
  136. /// Convert a path to internal format (use slashes.)
  137. ATOMIC_API String GetInternalPath(const String& pathName);
  138. /// Convert a path to the format required by the operating system.
  139. ATOMIC_API String GetNativePath(const String& pathName);
  140. /// Convert a path to the format required by the operating system in wide characters.
  141. ATOMIC_API WString GetWideNativePath(const String& pathName);
  142. /// Return whether a path is absolute.
  143. ATOMIC_API bool IsAbsolutePath(const String& pathName);
  144. // ATOMIC BEGIN
  145. ATOMIC_API bool IsAbsoluteParentPath(const String& absParentPath, const String& fullPath);
  146. ATOMIC_API String GetSanitizedPath(const String& path);
  147. /// Given two absolute directory paths, get the relative path from one to the other
  148. /// Returns false if either path isn't absolute, or if they are unrelated
  149. ATOMIC_API bool GetRelativePath(const String& fromPath, const String& toPath, String& output);
  150. // ATOMIC END
  151. }