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 <memory> 21 #include <string> 22 #include <vector> 23 24 #include "arrow/filesystem/filesystem.h" 25 #include "arrow/python/common.h" 26 #include "arrow/python/visibility.h" 27 #include "arrow/util/macros.h" 28 29 namespace arrow { 30 namespace py { 31 namespace fs { 32 33 class ARROW_PYTHON_EXPORT PyFileSystemVtable { 34 public: 35 std::function<void(PyObject*, std::string* out)> get_type_name; 36 std::function<bool(PyObject*, const arrow::fs::FileSystem& other)> equals; 37 38 std::function<void(PyObject*, const std::string& path, arrow::fs::FileInfo* out)> 39 get_file_info; 40 std::function<void(PyObject*, const std::vector<std::string>& paths, 41 std::vector<arrow::fs::FileInfo>* out)> 42 get_file_info_vector; 43 std::function<void(PyObject*, const arrow::fs::FileSelector&, 44 std::vector<arrow::fs::FileInfo>* out)> 45 get_file_info_selector; 46 47 std::function<void(PyObject*, const std::string& path, bool)> create_dir; 48 std::function<void(PyObject*, const std::string& path)> delete_dir; 49 std::function<void(PyObject*, const std::string& path)> delete_dir_contents; 50 std::function<void(PyObject*)> delete_root_dir_contents; 51 std::function<void(PyObject*, const std::string& path)> delete_file; 52 std::function<void(PyObject*, const std::string& src, const std::string& dest)> move; 53 std::function<void(PyObject*, const std::string& src, const std::string& dest)> 54 copy_file; 55 56 std::function<void(PyObject*, const std::string& path, 57 std::shared_ptr<io::InputStream>* out)> 58 open_input_stream; 59 std::function<void(PyObject*, const std::string& path, 60 std::shared_ptr<io::RandomAccessFile>* out)> 61 open_input_file; 62 std::function<void(PyObject*, const std::string& path, 63 std::shared_ptr<io::OutputStream>* out)> 64 open_output_stream; 65 std::function<void(PyObject*, const std::string& path, 66 std::shared_ptr<io::OutputStream>* out)> 67 open_append_stream; 68 69 std::function<void(PyObject*, const std::string& path, std::string* out)> 70 normalize_path; 71 }; 72 73 class ARROW_PYTHON_EXPORT PyFileSystem : public arrow::fs::FileSystem { 74 public: 75 PyFileSystem(PyObject* handler, PyFileSystemVtable vtable); 76 ~PyFileSystem() override; 77 78 static std::shared_ptr<PyFileSystem> Make(PyObject* handler, PyFileSystemVtable vtable); 79 80 std::string type_name() const override; 81 82 bool Equals(const FileSystem& other) const override; 83 84 Result<arrow::fs::FileInfo> GetFileInfo(const std::string& path) override; 85 Result<std::vector<arrow::fs::FileInfo>> GetFileInfo( 86 const std::vector<std::string>& paths) override; 87 Result<std::vector<arrow::fs::FileInfo>> GetFileInfo( 88 const arrow::fs::FileSelector& select) override; 89 90 Status CreateDir(const std::string& path, bool recursive = true) override; 91 92 Status DeleteDir(const std::string& path) override; 93 Status DeleteDirContents(const std::string& path) override; 94 Status DeleteRootDirContents() override; 95 96 Status DeleteFile(const std::string& path) override; 97 98 Status Move(const std::string& src, const std::string& dest) override; 99 100 Status CopyFile(const std::string& src, const std::string& dest) override; 101 102 Result<std::shared_ptr<io::InputStream>> OpenInputStream( 103 const std::string& path) override; 104 Result<std::shared_ptr<io::RandomAccessFile>> OpenInputFile( 105 const std::string& path) override; 106 Result<std::shared_ptr<io::OutputStream>> OpenOutputStream( 107 const std::string& path) override; 108 Result<std::shared_ptr<io::OutputStream>> OpenAppendStream( 109 const std::string& path) override; 110 111 Result<std::string> NormalizePath(std::string path) override; 112 handler()113 PyObject* handler() const { return handler_.obj(); } 114 115 private: 116 OwnedRefNoGIL handler_; 117 PyFileSystemVtable vtable_; 118 }; 119 120 } // namespace fs 121 } // namespace py 122 } // namespace arrow 123