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