FileSystem.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // Copyright (c) 2008-2014 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 "HashSet.h"
  24. #include "List.h"
  25. #include "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. OBJECT(FileSystem);
  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. If it does not contain the Urho3D default CoreData and Data directories, and the current working directory does, return the working directory instead.
  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. private:
  93. /// Scan directory, called internally.
  94. void ScanDirInternal(Vector<String>& result, String path, const String& startPath, const String& filter, unsigned flags, bool recursive) const;
  95. /// Handle begin frame event to check for completed async executions.
  96. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  97. /// Handle a console command event.
  98. void HandleConsoleCommand(StringHash eventType, VariantMap& eventData);
  99. /// Allowed directories.
  100. HashSet<String> allowedPaths_;
  101. /// Cached program directory.
  102. mutable String programDir_;
  103. /// Async execution queue.
  104. List<AsyncExecRequest*> asyncExecQueue_;
  105. /// Next async execution ID.
  106. unsigned nextAsyncExecID_;
  107. /// Flag for executing engine console commands as OS-specific system command. Default to true.
  108. bool executeConsoleCommands_;
  109. };
  110. /// Split a full path to path, filename and extension. The extension will be converted to lowercase by default.
  111. URHO3D_API void SplitPath(const String& fullPath, String& pathName, String& fileName, String& extension, bool lowercaseExtension = true);
  112. /// Return the path from a full path.
  113. URHO3D_API String GetPath(const String& fullPath);
  114. /// Return the filename from a full path.
  115. URHO3D_API String GetFileName(const String& fullPath);
  116. /// Return the extension from a full path, converted to lowercase by default.
  117. URHO3D_API String GetExtension(const String& fullPath, bool lowercaseExtension = true);
  118. /// 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.
  119. URHO3D_API String GetFileNameAndExtension(const String& fullPath, bool lowercaseExtension = false);
  120. /// Replace the extension of a file name with another.
  121. URHO3D_API String ReplaceExtension(const String& fullPath, const String& newExtension);
  122. /// Add a slash at the end of the path if missing and convert to internal format (use slashes.)
  123. URHO3D_API String AddTrailingSlash(const String& pathName);
  124. /// Remove the slash from the end of a path if exists and convert to internal format (use slashes.)
  125. URHO3D_API String RemoveTrailingSlash(const String& pathName);
  126. /// Return the parent path, or the path itself if not available.
  127. URHO3D_API String GetParentPath(const String& pathName);
  128. /// Convert a path to internal format (use slashes.)
  129. URHO3D_API String GetInternalPath(const String& pathName);
  130. /// Convert a path to the format required by the operating system.
  131. URHO3D_API String GetNativePath(const String& pathName);
  132. /// Convert a path to the format required by the operating system in wide characters.
  133. URHO3D_API WString GetWideNativePath(const String& pathName);
  134. /// Return whether a path is absolute.
  135. URHO3D_API bool IsAbsolutePath(const String& pathName);
  136. }