1 /* 2 * Copyright 2023 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef FCP_TENSORFLOW_FILE_DESCRIPTOR_FILESYSTEM_H_ 18 #define FCP_TENSORFLOW_FILE_DESCRIPTOR_FILESYSTEM_H_ 19 20 #include <memory> 21 #include <vector> 22 23 #include "tensorflow/core/platform/file_system.h" 24 25 namespace tensorflow { 26 namespace fcp { 27 28 // Filesystem for file descriptors that reads URIs in the form "fd:///<int>", 29 // where <int> is a valid file descriptor number. This filesystem can be useful 30 // to support situations in which a file descriptor is opened in Java and passed 31 // to the JNI layer. 32 // 33 // CAVEATS: 34 // * This filesystem is non-hierarchical and read-only; many functions simply 35 // return tensorflow::error::code::UNIMPLEMENTED. 36 // * To read a file descriptor, this filesystem makes a dup and closes the dup 37 // when it is done with it. The code that originally created the URI is 38 // responsible for closing the original file descriptor. 39 class FileDescriptorFileSystem : public tensorflow::FileSystem { 40 public: 41 FileDescriptorFileSystem() = default; 42 ~FileDescriptorFileSystem() override = default; 43 44 tensorflow::Status NewRandomAccessFile( 45 const std::string& filename, 46 std::unique_ptr<RandomAccessFile>* result) override; 47 48 // Clears *results and stores pattern if pattern is a literal match of a valid 49 // file descriptor. As such, this does not support the full pattern matching 50 // specification as described by FileSystem::GetMatchingPaths. 51 tensorflow::Status GetMatchingPaths( 52 const std::string& pattern, std::vector<std::string>* results) override; 53 54 tensorflow::Status Stat(const std::string& fname, 55 tensorflow::FileStatistics* stats) override; 56 57 tensorflow::Status GetFileSize(const std::string& fname, 58 uint64* size) override; 59 60 // Not necessary to read TF checkpoints; these always return UNIMPLEMENTED 61 tensorflow::Status NewReadOnlyMemoryRegionFromFile( 62 const std::string& filename, 63 std::unique_ptr<ReadOnlyMemoryRegion>* result) override; 64 65 tensorflow::Status FileExists(const std::string& fname) override; 66 67 // The fd filesystem is non-hierarchical; this always returns UNIMPLEMENTED 68 tensorflow::Status GetChildren(const std::string& dir, 69 std::vector<string>* r) override; 70 71 // The fd filesystem is read-only; these always return UNIMPLEMENTED 72 tensorflow::Status NewWritableFile( 73 const std::string& fname, std::unique_ptr<WritableFile>* result) override; 74 tensorflow::Status NewAppendableFile( 75 const std::string& fname, std::unique_ptr<WritableFile>* result) override; 76 tensorflow::Status DeleteFile(const std::string& f) override; 77 tensorflow::Status CreateDir(const std::string& d) override; 78 tensorflow::Status DeleteDir(const std::string& d) override; 79 tensorflow::Status RenameFile(const std::string& s, 80 const std::string& t) override; 81 tensorflow::Status CanCreateTempFile(const std::string& fname, 82 bool* can_create_temp_file) override; 83 }; 84 85 } // namespace fcp 86 } // namespace tensorflow 87 88 #endif // FCP_TENSORFLOW_FILE_DESCRIPTOR_FILESYSTEM_H_ 89