FileSystem.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. // ATOMIC BEGIN
  36. /// Helper class to expose resource iteration to script
  37. class FileNameIterator : public RefCounted
  38. {
  39. ATOMIC_REFCOUNTED(FileNameIterator);
  40. public:
  41. FileNameIterator();
  42. const String& GetCurrent();
  43. bool MoveNext();
  44. void Reset();
  45. Vector<String> filenames_;
  46. private:
  47. unsigned index_;
  48. };
  49. // ATOMIC END
  50. /// Subsystem for file and directory operations and access control.
  51. class ATOMIC_API FileSystem : public Object
  52. {
  53. ATOMIC_OBJECT(FileSystem, Object);
  54. public:
  55. /// Construct.
  56. FileSystem(Context* context);
  57. /// Destruct.
  58. ~FileSystem();
  59. /// Set the current working directory.
  60. bool SetCurrentDir(const String& pathName);
  61. /// Create a directory.
  62. bool CreateDir(const String& pathName);
  63. /// Set whether to execute engine console commands as OS-specific system command.
  64. void SetExecuteConsoleCommands(bool enable);
  65. /// Run a program using the command interpreter, block until it exits and return the exit code. Will fail if any allowed paths are defined.
  66. int SystemCommand(const String& commandLine, bool redirectStdOutToLog = false);
  67. /// Run a specific program, block until it exits and return the exit code. Will fail if any allowed paths are defined.
  68. int SystemRun(const String& fileName, const Vector<String>& arguments);
  69. /// 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.
  70. unsigned SystemCommandAsync(const String& commandLine);
  71. /// 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.
  72. unsigned SystemRunAsync(const String& fileName, const Vector<String>& arguments);
  73. /// Open a file in an external program, with mode such as "edit" optionally specified. Will fail if any allowed paths are defined.
  74. bool SystemOpen(const String& fileName, const String& mode = String::EMPTY);
  75. /// Copy a file. Return true if successful.
  76. bool Copy(const String& srcFileName, const String& destFileName);
  77. /// Rename a file. Return true if successful.
  78. bool Rename(const String& srcFileName, const String& destFileName);
  79. /// Delete a file. Return true if successful.
  80. bool Delete(const String& fileName);
  81. /// 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.
  82. void RegisterPath(const String& pathName);
  83. /// Set a file's last modified time as seconds since 1.1.1970. Return true on success.
  84. bool SetLastModifiedTime(const String& fileName, unsigned newTime);
  85. /// Return the absolute current working directory.
  86. String GetCurrentDir() const;
  87. /// Return whether is executing engine console commands as OS-specific system command.
  88. bool GetExecuteConsoleCommands() const { return executeConsoleCommands_; }
  89. /// Return whether paths have been registered.
  90. bool HasRegisteredPaths() const { return allowedPaths_.Size() > 0; }
  91. /// Check if a path is allowed to be accessed. If no paths are registered, all are allowed.
  92. bool CheckAccess(const String& pathName) const;
  93. /// Returns the file's last modified time as seconds since 1.1.1970, or 0 if can not be accessed.
  94. unsigned GetLastModifiedTime(const String& fileName) const;
  95. /// Check if a file exists.
  96. bool FileExists(const String& fileName) const;
  97. /// Check if a directory exists.
  98. bool DirExists(const String& pathName) const;
  99. /// Scan a directory for specified files.
  100. void ScanDir(Vector<String>& result, const String& pathName, const String& filter, unsigned flags, bool recursive) const;
  101. /// Return the program's directory.
  102. String GetProgramDir() const;
  103. /// Return the user documents directory.
  104. String GetUserDocumentsDir() const;
  105. /// Return the application preferences directory.
  106. String GetAppPreferencesDir(const String& org, const String& app) const;
  107. // ATOMIC BEGIN
  108. /// Scan specified files, returning them as an iterator
  109. SharedPtr<FileNameIterator> ScanDir(const String& pathName, const String& filter, unsigned flags, bool recursive) const;
  110. /// Check if a file or directory exists at the specified path
  111. bool Exists(const String& pathName) const { return FileExists(pathName) || DirExists(pathName); }
  112. bool CopyDir(const String& directoryIn, const String& directoryOut);
  113. bool CreateDirs(const String& root, const String& subdirectory);
  114. bool CreateDirsRecursive(const String& directoryIn);
  115. bool RemoveDir(const String& directoryIn, bool recursive);
  116. // ATOMIC END
  117. private:
  118. /// Scan directory, called internally.
  119. void ScanDirInternal
  120. (Vector<String>& result, String path, const String& startPath, const String& filter, unsigned flags, bool recursive) const;
  121. /// Handle begin frame event to check for completed async executions.
  122. void HandleBeginFrame(StringHash eventType, VariantMap& eventData);
  123. /// Handle a console command event.
  124. void HandleConsoleCommand(StringHash eventType, VariantMap& eventData);
  125. /// Allowed directories.
  126. HashSet<String> allowedPaths_;
  127. /// Async execution queue.
  128. List<AsyncExecRequest*> asyncExecQueue_;
  129. /// Next async execution ID.
  130. unsigned nextAsyncExecID_;
  131. /// Flag for executing engine console commands as OS-specific system command. Default to true.
  132. bool executeConsoleCommands_;
  133. };
  134. /// Split a full path to path, filename and extension. The extension will be converted to lowercase by default.
  135. ATOMIC_API void
  136. SplitPath(const String& fullPath, String& pathName, String& fileName, String& extension, bool lowercaseExtension = true);
  137. /// Return the path from a full path.
  138. ATOMIC_API String GetPath(const String& fullPath);
  139. /// Return the filename from a full path.
  140. ATOMIC_API String GetFileName(const String& fullPath);
  141. /// Return the extension from a full path, converted to lowercase by default.
  142. ATOMIC_API String GetExtension(const String& fullPath, bool lowercaseExtension = true);
  143. /// 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.
  144. ATOMIC_API String GetFileNameAndExtension(const String& fullPath, bool lowercaseExtension = false);
  145. /// Replace the extension of a file name with another.
  146. ATOMIC_API String ReplaceExtension(const String& fullPath, const String& newExtension);
  147. /// Add a slash at the end of the path if missing and convert to internal format (use slashes.)
  148. ATOMIC_API String AddTrailingSlash(const String& pathName);
  149. /// Remove the slash from the end of a path if exists and convert to internal format (use slashes.)
  150. ATOMIC_API String RemoveTrailingSlash(const String& pathName);
  151. /// Return the parent path, or the path itself if not available.
  152. ATOMIC_API String GetParentPath(const String& pathName);
  153. /// Convert a path to internal format (use slashes.)
  154. ATOMIC_API String GetInternalPath(const String& pathName);
  155. /// Convert a path to the format required by the operating system.
  156. ATOMIC_API String GetNativePath(const String& pathName);
  157. /// Convert a path to the format required by the operating system in wide characters.
  158. ATOMIC_API WString GetWideNativePath(const String& pathName);
  159. /// Return whether a path is absolute.
  160. ATOMIC_API bool IsAbsolutePath(const String& pathName);
  161. // ATOMIC BEGIN
  162. ATOMIC_API bool IsAbsoluteParentPath(const String& absParentPath, const String& fullPath);
  163. ATOMIC_API String GetSanitizedPath(const String& path);
  164. /// Given two absolute directory paths, get the relative path from one to the other
  165. /// Returns false if either path isn't absolute, or if they are unrelated
  166. ATOMIC_API bool GetRelativePath(const String& fromPath, const String& toPath, String& output);
  167. // ATOMIC END
  168. }