FileSystem.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // Copyright (c) 2008-2020 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 Urho3D
  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 URHO3D_API FileSystem : public Object
  37. {
  38. URHO3D_OBJECT(FileSystem, Object);
  39. public:
  40. /// Construct.
  41. explicit FileSystem(Context* context);
  42. /// Destruct.
  43. ~FileSystem() override;
  44. /// Set the current working directory.
  45. /// @property
  46. bool SetCurrentDir(const String& pathName);
  47. /// Create a directory.
  48. bool CreateDir(const String& pathName);
  49. /// Set whether to execute engine console commands as OS-specific system command.
  50. /// @property
  51. void SetExecuteConsoleCommands(bool enable);
  52. /// Run a program using the command interpreter, block until it exits and return the exit code. Will fail if any allowed paths are defined.
  53. int SystemCommand(const String& commandLine, bool redirectStdOutToLog = false);
  54. /// Run a specific program, block until it exits and return the exit code. Will fail if any allowed paths are defined.
  55. int SystemRun(const String& fileName, const Vector<String>& arguments);
  56. /// 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.
  57. unsigned SystemCommandAsync(const String& commandLine);
  58. /// 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.
  59. unsigned SystemRunAsync(const String& fileName, const Vector<String>& arguments);
  60. /// Open a file in an external program, with mode such as "edit" optionally specified. Will fail if any allowed paths are defined.
  61. bool SystemOpen(const String& fileName, const String& mode = String::EMPTY);
  62. /// Copy a file. Return true if successful.
  63. bool Copy(const String& srcFileName, const String& destFileName);
  64. /// Rename a file. Return true if successful.
  65. bool Rename(const String& srcFileName, const String& destFileName);
  66. /// Delete a file. Return true if successful.
  67. bool Delete(const String& fileName);
  68. /// 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.
  69. void RegisterPath(const String& pathName);
  70. /// Set a file's last modified time as seconds since 1.1.1970. Return true on success.
  71. bool SetLastModifiedTime(const String& fileName, unsigned newTime);
  72. /// Return the absolute current working directory.
  73. /// @property
  74. String GetCurrentDir() const;
  75. /// Return whether is executing engine console commands as OS-specific system command.
  76. /// @property
  77. bool GetExecuteConsoleCommands() const { return executeConsoleCommands_; }
  78. /// Return whether paths have been registered.
  79. bool HasRegisteredPaths() const { return allowedPaths_.Size() > 0; }
  80. /// Check if a path is allowed to be accessed. If no paths are registered, all are allowed.
  81. bool CheckAccess(const String& pathName) const;
  82. /// Returns the file's last modified time as seconds since 1.1.1970, or 0 if can not be accessed.
  83. unsigned GetLastModifiedTime(const String& fileName) const;
  84. /// Check if a file exists.
  85. bool FileExists(const String& fileName) const;
  86. /// Check if a directory exists.
  87. bool DirExists(const String& pathName) const;
  88. /// Scan a directory for specified files.
  89. void ScanDir(Vector<String>& result, const String& pathName, const String& filter, unsigned flags, bool recursive) const;
  90. /// Return the program's directory.
  91. /// @property
  92. String GetProgramDir() const;
  93. /// Return the user documents directory.
  94. /// @property
  95. String GetUserDocumentsDir() const;
  96. /// Return the application preferences directory.
  97. String GetAppPreferencesDir(const String& org, const String& app) const;
  98. /// Return path of temporary directory. Path always ends with a forward slash.
  99. /// @property
  100. String GetTemporaryDir() const;
  101. private:
  102. /// Scan directory, called internally.
  103. void ScanDirInternal
  104. (Vector<String>& result, String path, const String& startPath, const String& filter, unsigned flags, bool recursive) const;
  105. /// Handle begin frame event to check for completed async executions.
  106. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  107. /// Handle a console command event.
  108. void HandleConsoleCommand(StringHash eventType, VariantMap& eventData);
  109. /// Allowed directories.
  110. HashSet<String> allowedPaths_;
  111. /// Async execution queue.
  112. List<AsyncExecRequest*> asyncExecQueue_;
  113. /// Next async execution ID.
  114. unsigned nextAsyncExecID_{1};
  115. /// Flag for executing engine console commands as OS-specific system command. Default to true.
  116. bool executeConsoleCommands_{};
  117. };
  118. /// Split a full path to path, filename and extension. The extension will be converted to lowercase by default.
  119. URHO3D_API void
  120. SplitPath(const String& fullPath, String& pathName, String& fileName, String& extension, bool lowercaseExtension = true);
  121. /// Return the path from a full path.
  122. URHO3D_API String GetPath(const String& fullPath);
  123. /// Return the filename from a full path.
  124. URHO3D_API String GetFileName(const String& fullPath);
  125. /// Return the extension from a full path, converted to lowercase by default.
  126. URHO3D_API String GetExtension(const String& fullPath, bool lowercaseExtension = true);
  127. /// 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.
  128. URHO3D_API String GetFileNameAndExtension(const String& fileName, bool lowercaseExtension = false);
  129. /// Replace the extension of a file name with another.
  130. URHO3D_API String ReplaceExtension(const String& fullPath, const String& newExtension);
  131. /// Add a slash at the end of the path if missing and convert to internal format (use slashes).
  132. URHO3D_API String AddTrailingSlash(const String& pathName);
  133. /// Remove the slash from the end of a path if exists and convert to internal format (use slashes).
  134. URHO3D_API String RemoveTrailingSlash(const String& pathName);
  135. /// Return the parent path, or the path itself if not available.
  136. URHO3D_API String GetParentPath(const String& path);
  137. /// Convert a path to internal format (use slashes).
  138. URHO3D_API String GetInternalPath(const String& pathName);
  139. /// Convert a path to the format required by the operating system.
  140. URHO3D_API String GetNativePath(const String& pathName);
  141. /// Convert a path to the format required by the operating system in wide characters.
  142. URHO3D_API WString GetWideNativePath(const String& pathName);
  143. /// Return whether a path is absolute.
  144. URHO3D_API bool IsAbsolutePath(const String& pathName);
  145. }