FileSystem.h 7.2 KB

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