• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_CONTRIB_ANDROID_ASSET_MANAGER_FILESYSTEM_H_
17 #define TENSORFLOW_CONTRIB_ANDROID_ASSET_MANAGER_FILESYSTEM_H_
18 
19 #include <android/asset_manager.h>
20 #include <android/asset_manager_jni.h>
21 
22 #include "tensorflow/core/platform/file_system.h"
23 
24 namespace tensorflow {
25 
26 // FileSystem that uses Android's AAssetManager. Once initialized with a given
27 // AAssetManager, files in the given AAssetManager can be accessed through the
28 // prefix given when registered with the TensorFlow Env.
29 // Note that because APK assets are immutable, any operation that tries to
30 // modify the FileSystem will return tensorflow::error::code::UNIMPLEMENTED.
31 class AssetManagerFileSystem : public FileSystem {
32  public:
33   // Initialize an AssetManagerFileSystem. Note that this does not register the
34   // file system with TensorFlow.
35   // asset_manager - Non-null Android AAssetManager that backs this file
36   //   system. The asset manager is not owned by this file system, and must
37   //   outlive this class.
38   // prefix - Common prefix to strip from all file URIs before passing them to
39   //   the asset_manager. This is required because TensorFlow gives the entire
40   //   file URI (file:///my_dir/my_file.txt) and AssetManager only knows paths
41   //   relative to its base directory.
42   AssetManagerFileSystem(AAssetManager* asset_manager, const string& prefix);
43   ~AssetManagerFileSystem() override = default;
44 
45   Status FileExists(const string& fname) override;
46   Status NewRandomAccessFile(
47       const string& filename,
48       std::unique_ptr<RandomAccessFile>* result) override;
49   Status NewReadOnlyMemoryRegionFromFile(
50       const string& filename,
51       std::unique_ptr<ReadOnlyMemoryRegion>* result) override;
52 
53   Status GetFileSize(const string& f, uint64* s) override;
54   // Currently just returns size.
55   Status Stat(const string& fname, FileStatistics* stat) override;
56   Status GetChildren(const string& dir, std::vector<string>* r) override;
57 
58   // All these functions return Unimplemented error. Asset storage is
59   // read only.
60   Status NewWritableFile(const string& fname,
61                          std::unique_ptr<WritableFile>* result) override;
62   Status NewAppendableFile(const string& fname,
63                            std::unique_ptr<WritableFile>* result) override;
64   Status DeleteFile(const string& f) override;
65   Status CreateDir(const string& d) override;
66   Status DeleteDir(const string& d) override;
67   Status RenameFile(const string& s, const string& t) override;
68 
69   Status GetMatchingPaths(const string& pattern,
70                           std::vector<string>* results) override;
71 
72  private:
73   string RemoveAssetPrefix(const string& name);
74 
75   // Return a string path that can be passed into AAssetManager functions.
76   // For example, 'my_prefix://some/dir/' would return 'some/dir'.
77   string NormalizeDirectoryPath(const string& fname);
78   bool DirectoryExists(const std::string& fname);
79 
80   AAssetManager* asset_manager_;
81   string prefix_;
82 };
83 
84 }  // namespace tensorflow
85 #endif  // TENSORFLOW_CONTRIB_ANDROID_ASSET_MANAGER_FILESYSTEM_H_
86