file_system_file_util.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_FILE_UTIL_H_
  5. #define STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_FILE_UTIL_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "base/component_export.h"
  9. #include "base/files/file.h"
  10. #include "base/files/file_path.h"
  11. #include "storage/browser/blob/scoped_file.h"
  12. #include "storage/browser/file_system/file_system_operation.h"
  13. namespace base {
  14. class Time;
  15. }
  16. namespace storage {
  17. class FileSystemOperationContext;
  18. class FileSystemURL;
  19. // A file utility interface that provides basic file utility methods for
  20. // FileSystem API.
  21. //
  22. // Layering structure of the FileSystemFileUtil was split out.
  23. // See http://crbug.com/128136 if you need it.
  24. class COMPONENT_EXPORT(STORAGE_BROWSER) FileSystemFileUtil {
  25. public:
  26. using CopyOrMoveOptionSet = FileSystemOperation::CopyOrMoveOptionSet;
  27. // It will be implemented by each subclass such as FileSystemFileEnumerator.
  28. class COMPONENT_EXPORT(STORAGE_BROWSER) AbstractFileEnumerator {
  29. public:
  30. virtual ~AbstractFileEnumerator() = default;
  31. // Returns an empty string if there are no more results.
  32. virtual base::FilePath Next() = 0;
  33. // These methods return metadata for the file most recently returned by
  34. // Next(). If Next() has never been called, or if Next() most recently
  35. // returned an empty string, then return the default values of 0,
  36. // "null time", and false, respectively.
  37. virtual int64_t Size() = 0;
  38. virtual base::Time LastModifiedTime() = 0;
  39. virtual bool IsDirectory() = 0;
  40. };
  41. class COMPONENT_EXPORT(STORAGE_BROWSER) EmptyFileEnumerator
  42. : public AbstractFileEnumerator {
  43. base::FilePath Next() override;
  44. int64_t Size() override;
  45. base::Time LastModifiedTime() override;
  46. bool IsDirectory() override;
  47. };
  48. FileSystemFileUtil(const FileSystemFileUtil&) = delete;
  49. FileSystemFileUtil& operator=(const FileSystemFileUtil&) = delete;
  50. virtual ~FileSystemFileUtil() = default;
  51. // Creates or opens a file with the given flags.
  52. // See header comments for AsyncFileUtil::CreateOrOpen() for more details.
  53. // This is used only by Pepper/NaCl File API.
  54. virtual base::File CreateOrOpen(FileSystemOperationContext* context,
  55. const FileSystemURL& url,
  56. int file_flags) = 0;
  57. // Ensures that the given |url| exist. This creates a empty new file
  58. // at |url| if the |url| does not exist.
  59. // See header comments for AsyncFileUtil::EnsureFileExists() for more details.
  60. virtual base::File::Error EnsureFileExists(
  61. FileSystemOperationContext* context,
  62. const FileSystemURL& url,
  63. bool* created) = 0;
  64. // Creates directory at given url.
  65. // See header comments for AsyncFileUtil::CreateDirectory() for more details.
  66. virtual base::File::Error CreateDirectory(FileSystemOperationContext* context,
  67. const FileSystemURL& url,
  68. bool exclusive,
  69. bool recursive) = 0;
  70. // Retrieves the information about a file.
  71. // See header comments for AsyncFileUtil::GetFileInfo() for more details.
  72. virtual base::File::Error GetFileInfo(FileSystemOperationContext* context,
  73. const FileSystemURL& url,
  74. base::File::Info* file_info,
  75. base::FilePath* platform_path) = 0;
  76. // Returns a pointer to a new instance of AbstractFileEnumerator which is
  77. // implemented for each FileSystemFileUtil subclass. The instance needs to be
  78. // freed by the caller, and its lifetime should not extend past when the
  79. // current call returns to the main FILE message loop.
  80. //
  81. // The supplied context must remain valid at least lifetime of the enumerator
  82. // instance. |this| must remain valid through the lifetime of the created
  83. // enumerator instance.
  84. virtual std::unique_ptr<AbstractFileEnumerator> CreateFileEnumerator(
  85. FileSystemOperationContext* context,
  86. const FileSystemURL& root_url,
  87. bool recursive) = 0;
  88. // Maps |file_system_url| given |context| into |local_file_path|
  89. // which represents physical file location on the host OS.
  90. // This may not always make sense for all subclasses.
  91. virtual base::File::Error GetLocalFilePath(
  92. FileSystemOperationContext* context,
  93. const FileSystemURL& file_system_url,
  94. base::FilePath* local_file_path) = 0;
  95. // Updates the file metadata information.
  96. // See header comments for AsyncFileUtil::Touch() for more details.
  97. virtual base::File::Error Touch(FileSystemOperationContext* context,
  98. const FileSystemURL& url,
  99. const base::Time& last_access_time,
  100. const base::Time& last_modified_time) = 0;
  101. // Truncates a file to the given length.
  102. // See header comments for AsyncFileUtil::Truncate() for more details.
  103. virtual base::File::Error Truncate(FileSystemOperationContext* context,
  104. const FileSystemURL& url,
  105. int64_t length) = 0;
  106. // Copies a single file or moves a single file or directory from |src_url| to
  107. // |dest_url|. Whether moving a directory is supported is
  108. // implementation-defined. The filesystem type of |src_url| and |dest_url|
  109. // MUST be same. For |option|, please see file_system_operation.h
  110. //
  111. // This returns:
  112. // - File::FILE_ERROR_NOT_FOUND if |src_url|
  113. // or the parent directory of |dest_url| does not exist.
  114. // - File::FILE_ERROR_NOT_A_FILE if |src_url| exists but is not a file and the
  115. // operation is copy or the implementation does not support moving
  116. // directories.
  117. // - File::FILE_ERROR_INVALID_OPERATION if |dest_url| exists and
  118. // is not a file.
  119. // - File::FILE_ERROR_FAILED if |dest_url| does not exist and
  120. // its parent path is a file.
  121. //
  122. virtual base::File::Error CopyOrMoveFile(FileSystemOperationContext* context,
  123. const FileSystemURL& src_url,
  124. const FileSystemURL& dest_url,
  125. CopyOrMoveOptionSet options,
  126. bool copy) = 0;
  127. // Copies in a single file from a different filesystem.
  128. // See header comments for AsyncFileUtil::CopyInForeignFile() for
  129. // more details.
  130. virtual base::File::Error CopyInForeignFile(
  131. FileSystemOperationContext* context,
  132. const base::FilePath& src_file_path,
  133. const FileSystemURL& dest_url) = 0;
  134. // Deletes a single file.
  135. // See header comments for AsyncFileUtil::DeleteFile() for more details.
  136. virtual base::File::Error DeleteFile(FileSystemOperationContext* context,
  137. const FileSystemURL& url) = 0;
  138. // Deletes a single empty directory.
  139. // See header comments for AsyncFileUtil::DeleteDirectory() for more details.
  140. virtual base::File::Error DeleteDirectory(FileSystemOperationContext* context,
  141. const FileSystemURL& url) = 0;
  142. // Creates a local snapshot file for a given |url| and returns the
  143. // metadata and platform path of the snapshot file via |callback|.
  144. //
  145. // See header comments for AsyncFileUtil::CreateSnapshotFile() for
  146. // more details.
  147. virtual ScopedFile CreateSnapshotFile(FileSystemOperationContext* context,
  148. const FileSystemURL& url,
  149. base::File::Error* error,
  150. base::File::Info* file_info,
  151. base::FilePath* platform_path) = 0;
  152. protected:
  153. FileSystemFileUtil() = default;
  154. };
  155. } // namespace storage
  156. #endif // STORAGE_BROWSER_FILE_SYSTEM_FILE_SYSTEM_FILE_UTIL_H_