1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements.  See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.  The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License.  You may obtain a copy of the License at
8 //
9 //   http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied.  See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 #pragma once
19 
20 #include <iosfwd>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include "arrow/filesystem/filesystem.h"
26 #include "arrow/util/windows_fixup.h"
27 
28 namespace arrow {
29 namespace fs {
30 namespace internal {
31 
32 struct MockDirInfo {
33   std::string full_path;
34   TimePoint mtime;
35 
36   bool operator==(const MockDirInfo& other) const {
37     return mtime == other.mtime && full_path == other.full_path;
38   }
39 
40   friend ARROW_EXPORT std::ostream& operator<<(std::ostream&, const MockDirInfo&);
41 };
42 
43 struct MockFileInfo {
44   std::string full_path;
45   TimePoint mtime;
46   std::string data;
47 
48   bool operator==(const MockFileInfo& other) const {
49     return mtime == other.mtime && full_path == other.full_path && data == other.data;
50   }
51 
52   friend ARROW_EXPORT std::ostream& operator<<(std::ostream&, const MockFileInfo&);
53 };
54 
55 /// A mock FileSystem implementation that holds its contents in memory.
56 ///
57 /// Useful for validating the FileSystem API, writing conformance suite,
58 /// and bootstrapping FileSystem-based APIs.
59 class ARROW_EXPORT MockFileSystem : public FileSystem {
60  public:
61   explicit MockFileSystem(TimePoint current_time);
62   ~MockFileSystem() override;
63 
type_name()64   std::string type_name() const override { return "mock"; }
65 
66   bool Equals(const FileSystem& other) const override;
67 
68   // XXX It's not very practical to have to explicitly declare inheritance
69   // of default overrides.
70   using FileSystem::GetFileInfo;
71   Result<FileInfo> GetFileInfo(const std::string& path) override;
72   Result<std::vector<FileInfo>> GetFileInfo(const FileSelector& select) override;
73 
74   Status CreateDir(const std::string& path, bool recursive = true) override;
75 
76   Status DeleteDir(const std::string& path) override;
77   Status DeleteDirContents(const std::string& path) override;
78   Status DeleteRootDirContents() override;
79 
80   Status DeleteFile(const std::string& path) override;
81 
82   Status Move(const std::string& src, const std::string& dest) override;
83 
84   Status CopyFile(const std::string& src, const std::string& dest) override;
85 
86   Result<std::shared_ptr<io::InputStream>> OpenInputStream(
87       const std::string& path) override;
88   Result<std::shared_ptr<io::RandomAccessFile>> OpenInputFile(
89       const std::string& path) override;
90   Result<std::shared_ptr<io::OutputStream>> OpenOutputStream(
91       const std::string& path) override;
92   Result<std::shared_ptr<io::OutputStream>> OpenAppendStream(
93       const std::string& path) override;
94 
95   // Contents-dumping helpers to ease testing.
96   // Output is lexicographically-ordered by full path.
97   std::vector<MockDirInfo> AllDirs();
98   std::vector<MockFileInfo> AllFiles();
99 
100   // Create a File with a content from a string.
101   Status CreateFile(const std::string& path, const std::string& content,
102                     bool recursive = true);
103 
104   // Create a MockFileSystem out of (empty) FileInfo. The content of every
105   // file is empty and of size 0. All directories will be created recursively.
106   static Result<std::shared_ptr<FileSystem>> Make(TimePoint current_time,
107                                                   const std::vector<FileInfo>& infos);
108 
109   class Impl;
110 
111  protected:
112   std::unique_ptr<Impl> impl_;
113 };
114 
115 }  // namespace internal
116 }  // namespace fs
117 }  // namespace arrow
118