@@ -28,88 +28,139 @@ namespace firebase {
28
28
namespace firestore {
29
29
namespace util {
30
30
31
- // High-level routines for the manipulating the filesystem. All filesystems
32
- // are required to implement these routines.
33
-
34
31
/* *
35
- * Answers the question "is this path a directory? The path is not required to
36
- * have a trailing slash.
37
- *
38
- * Typical return codes include:
39
- * * Ok - The path exists and is a directory.
40
- * * FailedPrecondition - Some component of the path is not a directory. This
41
- * does not necessarily imply that the path exists and is a file.
42
- * * NotFound - The path does not exist
43
- * * PermissionDenied - Insufficient permissions to access the path.
32
+ * A high-level interface describing filesystem operations.
44
33
*/
45
- Status IsDirectory (const Path& path);
34
+ class Filesystem {
35
+ public:
36
+ Filesystem (const Filesystem&) = delete ;
37
+ Filesystem& operator =(const Filesystem&) = delete ;
46
38
47
- /* *
48
- * Recursively creates all the directories in the path name if they don't
49
- * exist.
50
- *
51
- * @return Ok if the directory was created or already existed.
52
- */
53
- Status RecursivelyCreateDir (const Path& path);
39
+ /* *
40
+ * Returns a singleton default filesystem implementation for the current
41
+ * operating system.
42
+ */
43
+ static Filesystem* Default ();
54
44
55
- /* *
56
- * Recursively deletes the contents of the given pathname. If the pathname is
57
- * a file, deletes just that file. The the pathname is a directory, deletes
58
- * everything within the directory.
59
- *
60
- * @return Ok if the directory was deleted or did not exist.
61
- */
62
- Status RecursivelyDelete (const Path& path);
45
+ /* *
46
+ * Returns a system-defined best directory in which to create application
47
+ * data. Values vary wildly across platforms. They include:
48
+ *
49
+ * * iOS: $container/Documents/$app_name
50
+ * * Linux: $HOME/.local/share/$app_name
51
+ * * macOS: $HOME/.$app_name
52
+ * * Other UNIX: $HOME/.$app_name
53
+ * * tvOS: $HOME/Library/Caches/$app_name
54
+ * * Windows: %USERPROFILE%/AppData/Local
55
+ *
56
+ * Note: the returned path is just where the system thinks the application
57
+ * data should be stored, but AppDataDir does not actually guarantee that this
58
+ * path exists.
59
+ *
60
+ * @param app_name The name of the application.
61
+ */
62
+ virtual StatusOr<Path> AppDataDir (absl::string_view app_name);
63
63
64
- /* *
65
- * Marks the given directory as excluded from platform-specific backup schemes
66
- * like iCloud backup.
67
- */
68
- Status ExcludeFromBackups (const Path& dir);
64
+ /* *
65
+ * Returns system-defined best directory in which to create temporary files.
66
+ * Typical return values are like `/tmp` on UNIX systems. Clients should
67
+ * create randomly named directories or files within this location to avoid
68
+ * collisions. Absent any changes that might affect the underlying calls, the
69
+ * value returned from TempDir will be stable over time.
70
+ *
71
+ * Note: the returned path is just where the system thinks temporary files
72
+ * should be stored, but TempDir does not actually guarantee that this path
73
+ * exists.
74
+ */
75
+ virtual Path TempDir ();
69
76
70
- /* *
71
- * Returns a system-defined best directory in which to create application data.
72
- * Values vary wildly across platforms. They include:
73
- *
74
- * * iOS: $container/Documents/$app_name
75
- * * Linux: $HOME/.local/share/$app_name
76
- * * macOS: $HOME/.$app_name
77
- * * Other UNIX: $HOME/.$app_name
78
- * * tvOS: $HOME/Library/Caches/$app_name
79
- * * Windows: %USERPROFILE%/AppData/Local
80
- *
81
- * Note: the returned path is just where the system thinks the application data
82
- * should be stored, but AppDataDir does not actually guarantee that this path
83
- * exists.
84
- *
85
- * @param app_name The name of the application.
86
- */
87
- StatusOr<Path> AppDataDir (absl::string_view app_name);
77
+ /* *
78
+ * Answers the question "is this path a directory? The path is not required to
79
+ * have a trailing slash.
80
+ *
81
+ * Typical return codes include:
82
+ * * Ok - The path exists and is a directory.
83
+ * * FailedPrecondition - Some component of the path is not a directory.
84
+ * This does not necessarily imply that the path exists and is a file.
85
+ * * NotFound - The path does not exist
86
+ * * PermissionDenied - Insufficient permissions to access the path.
87
+ */
88
+ virtual Status IsDirectory (const Path& path);
88
89
89
- /* *
90
- * Returns system-defined best directory in which to create temporary files.
91
- * Typical return values are like `/tmp` on UNIX systems. Clients should create
92
- * randomly named directories or files within this location to avoid collisions.
93
- * Absent any changes that might affect the underlying calls, the value returned
94
- * from TempDir will be stable over time.
95
- *
96
- * Note: the returned path is just where the system thinks temporary files
97
- * should be stored, but TempDir does not actually guarantee that this path
98
- * exists.
99
- */
100
- Path TempDir ();
90
+ /* *
91
+ * On success, returns the size in bytes of the file specified by
92
+ * `path`.
93
+ */
94
+ virtual StatusOr<int64_t > FileSize (const Path& path);
101
95
102
- /* *
103
- * On success, returns the size in bytes of the file specified by
104
- * `path`.
105
- */
106
- StatusOr<int64_t > FileSize (const Path& path);
96
+ /* *
97
+ * Recursively creates all the directories in the path name if they don't
98
+ * exist.
99
+ *
100
+ * @return Ok if the directory was created or already existed.
101
+ */
102
+ virtual Status RecursivelyCreateDir (const Path& path);
107
103
108
- /* *
109
- * On success, opens the file at the given `path` and returns its contents as
110
- * a string.
111
- */
112
- StatusOr<std::string> ReadFile (const Path& path);
104
+ /* *
105
+ * Recursively deletes the contents of the given pathname. If the pathname is
106
+ * a file, deletes just that file. The the pathname is a directory, deletes
107
+ * everything within the directory.
108
+ *
109
+ * @return Ok if the directory was deleted or did not exist.
110
+ */
111
+ virtual Status RecursivelyRemove (const Path& path);
112
+
113
+ /* *
114
+ * Creates the given directory. The immediate parent directory must already
115
+ * exist and not already be a file.
116
+ *
117
+ * @return Ok if the directory was created or already existed. On some systems
118
+ * this may also return Ok if a regular file exists at the given path.
119
+ */
120
+ virtual Status CreateDir (const Path& path);
121
+
122
+ /* *
123
+ * Deletes the given directory if it exists.
124
+ *
125
+ * @return Ok if the directory was deleted or did not exist. Returns a
126
+ * system-defined error if the path is not a directory or the directory is
127
+ * non-empty.
128
+ */
129
+ virtual Status RemoveDir (const Path& path);
130
+
131
+ /* *
132
+ * Deletes the given file if it exists.
133
+ *
134
+ * @return Ok if the file was deleted or did not exist. Returns a
135
+ * system-defined error if the path exists but is not a regular file.
136
+ */
137
+ virtual Status RemoveFile (const Path& path);
138
+
139
+ /* *
140
+ * Recursively deletes the contents of the given pathname that is known to be
141
+ * a directory.
142
+ *
143
+ * @return Ok if the directory was deleted or did not exist. Returns a
144
+ * system-defined error if the path exists but is not a directory.
145
+ *
146
+ */
147
+ virtual Status RecursivelyRemoveDir (const Path& path);
148
+
149
+ /* *
150
+ * Marks the given directory as excluded from platform-specific backup schemes
151
+ * like iCloud backup.
152
+ */
153
+ virtual Status ExcludeFromBackups (const Path& dir);
154
+
155
+ /* *
156
+ * On success, opens the file at the given `path` and returns its contents as
157
+ * a string.
158
+ */
159
+ virtual StatusOr<std::string> ReadFile (const Path& path);
160
+
161
+ protected:
162
+ Filesystem () = default ;
163
+ };
113
164
114
165
/* *
115
166
* Implements an iterator over the contents of a directory. Initializes to the
0 commit comments