• 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 %include "tensorflow/python/lib/core/strings.i"
17 %include "tensorflow/python/platform/base.i"
18 
19 %{
20 #include "tensorflow/c/tf_status_helper.h"
21 #include "tensorflow/core/framework/types.h"
22 #include "tensorflow/core/lib/core/status.h"
23 #include "tensorflow/core/lib/core/stringpiece.h"
24 #include "tensorflow/core/lib/io/buffered_inputstream.h"
25 #include "tensorflow/core/lib/io/inputstream_interface.h"
26 #include "tensorflow/core/lib/io/random_inputstream.h"
27 #include "tensorflow/core/lib/io/path.h"
28 #include "tensorflow/core/platform/env.h"
29 #include "tensorflow/core/platform/file_statistics.h"
30 #include "tensorflow/core/platform/file_system.h"
31 #include "tensorflow/core/protobuf/meta_graph.pb.h"
32 %}
33 
34 %{
FileExists(const string & filename,TF_Status * out_status)35 inline void FileExists(const string& filename, TF_Status* out_status) {
36   tensorflow::Status status = tensorflow::Env::Default()->FileExists(filename);
37   if (!status.ok()) {
38     Set_TF_Status_from_Status(out_status, status);
39   }
40 }
41 
FileExists(const tensorflow::StringPiece & filename,TF_Status * out_status)42 inline void FileExists(const tensorflow::StringPiece& filename,
43     TF_Status* out_status) {
44   tensorflow::Status status =
45       tensorflow::Env::Default()->FileExists(string(filename));
46   if (!status.ok()) {
47     Set_TF_Status_from_Status(out_status, status);
48   }
49 }
50 
DeleteFile(const string & filename,TF_Status * out_status)51 inline void DeleteFile(const string& filename, TF_Status* out_status) {
52   tensorflow::Status status = tensorflow::Env::Default()->DeleteFile(filename);
53   if (!status.ok()) {
54     Set_TF_Status_from_Status(out_status, status);
55   }
56 }
57 
ReadFileToString(const string & filename,TF_Status * out_status)58 string ReadFileToString(const string& filename, TF_Status* out_status) {
59   string file_content;
60   tensorflow::Status status = ReadFileToString(tensorflow::Env::Default(),
61       filename, &file_content);
62   if (!status.ok()) {
63     Set_TF_Status_from_Status(out_status, status);
64   }
65   return file_content;
66 }
67 
WriteStringToFile(const string & filename,const string & file_content,TF_Status * out_status)68 void WriteStringToFile(const string& filename, const string& file_content,
69                        TF_Status* out_status) {
70   tensorflow::Status status = WriteStringToFile(tensorflow::Env::Default(),
71       filename, file_content);
72   if (!status.ok()) {
73     Set_TF_Status_from_Status(out_status, status);
74   }
75 }
76 
GetChildren(const string & dir,TF_Status * out_status)77 std::vector<string> GetChildren(const string& dir, TF_Status* out_status) {
78   std::vector<string> results;
79   tensorflow::Status status = tensorflow::Env::Default()->GetChildren(
80       dir, &results);
81   if (!status.ok()) {
82     Set_TF_Status_from_Status(out_status, status);
83   }
84   return results;
85 }
86 
GetMatchingFiles(const string & filename,TF_Status * out_status)87 std::vector<string> GetMatchingFiles(const string& filename,
88                                      TF_Status* out_status) {
89   std::vector<string> results;
90   tensorflow::Status status = tensorflow::Env::Default()->GetMatchingPaths(
91       filename, &results);
92   if (!status.ok()) {
93     Set_TF_Status_from_Status(out_status, status);
94   }
95   return results;
96 }
97 
CreateDir(const string & dirname,TF_Status * out_status)98 void CreateDir(const string& dirname, TF_Status* out_status) {
99   tensorflow::Status status = tensorflow::Env::Default()->CreateDir(dirname);
100   if (!status.ok() && status.code() != tensorflow::error::ALREADY_EXISTS) {
101     Set_TF_Status_from_Status(out_status, status);
102   }
103 }
104 
RecursivelyCreateDir(const string & dirname,TF_Status * out_status)105 void RecursivelyCreateDir(const string& dirname, TF_Status* out_status) {
106   tensorflow::Status status = tensorflow::Env::Default()->RecursivelyCreateDir(
107       dirname);
108   if (!status.ok()) {
109     Set_TF_Status_from_Status(out_status, status);
110   }
111 }
112 
CopyFile(const string & src,const string & target,bool overwrite,TF_Status * out_status)113 void CopyFile(const string& src, const string& target, bool overwrite,
114               TF_Status* out_status) {
115   // If overwrite is false and the target file exists then its an error.
116   if (!overwrite && tensorflow::Env::Default()->FileExists(target).ok()) {
117     TF_SetStatus(out_status, TF_ALREADY_EXISTS, "file already exists");
118     return;
119   }
120   tensorflow::Status status =
121       tensorflow::Env::Default()->CopyFile(src, target);
122   if (!status.ok()) {
123     Set_TF_Status_from_Status(out_status, status);
124   }
125 }
126 
RenameFile(const string & src,const string & target,bool overwrite,TF_Status * out_status)127 void RenameFile(const string& src, const string& target, bool overwrite,
128                 TF_Status* out_status) {
129   // If overwrite is false and the target file exists then its an error.
130   if (!overwrite && tensorflow::Env::Default()->FileExists(target).ok()) {
131     TF_SetStatus(out_status, TF_ALREADY_EXISTS, "file already exists");
132     return;
133   }
134   tensorflow::Status status = tensorflow::Env::Default()->RenameFile(src,
135                                                                      target);
136   if (!status.ok()) {
137     Set_TF_Status_from_Status(out_status, status);
138   }
139 }
140 
141 using tensorflow::int64;
142 
DeleteRecursively(const string & dirname,TF_Status * out_status)143 void DeleteRecursively(const string& dirname, TF_Status* out_status) {
144   int64 undeleted_files, undeleted_dirs;
145   tensorflow::Status status = tensorflow::Env::Default()->DeleteRecursively(
146       dirname, &undeleted_files, &undeleted_dirs);
147   if (!status.ok()) {
148     Set_TF_Status_from_Status(out_status, status);
149     return;
150   }
151   if (undeleted_files > 0 || undeleted_dirs > 0) {
152     TF_SetStatus(out_status, TF_PERMISSION_DENIED,
153                  "could not fully delete dir");
154     return;
155   }
156 }
157 
IsDirectory(const string & dirname,TF_Status * out_status)158 bool IsDirectory(const string& dirname, TF_Status* out_status) {
159   tensorflow::Status status = tensorflow::Env::Default()->IsDirectory(dirname);
160   if (status.ok()) {
161     return true;
162   }
163   // FAILED_PRECONDITION Status response means path exists but isn't a dir.
164   if (status.code() != tensorflow::error::FAILED_PRECONDITION) {
165     Set_TF_Status_from_Status(out_status, status);
166   }
167   return false;
168 }
169 
170 using tensorflow::FileStatistics;
171 
Stat(const string & filename,FileStatistics * stats,TF_Status * out_status)172 void Stat(const string& filename, FileStatistics* stats,
173           TF_Status* out_status) {
174   tensorflow::Status status = tensorflow::Env::Default()->Stat(filename,
175                                                                stats);
176   if (!status.ok()) {
177     Set_TF_Status_from_Status(out_status, status);
178   }
179 }
180 
CreateBufferedInputStream(const string & filename,size_t buffer_size,TF_Status * out_status)181 tensorflow::io::BufferedInputStream* CreateBufferedInputStream(
182     const string& filename, size_t buffer_size, TF_Status* out_status) {
183   std::unique_ptr<tensorflow::RandomAccessFile> file;
184   tensorflow::Status status =
185       tensorflow::Env::Default()->NewRandomAccessFile(filename, &file);
186   if (!status.ok()) {
187     Set_TF_Status_from_Status(out_status, status);
188     return nullptr;
189   }
190   std::unique_ptr<tensorflow::io::RandomAccessInputStream> input_stream(
191       new tensorflow::io::RandomAccessInputStream(
192           file.release(), true /* owns_file */));
193   std::unique_ptr<tensorflow::io::BufferedInputStream> buffered_input_stream(
194       new tensorflow::io::BufferedInputStream(
195           input_stream.release(), buffer_size, true /* owns_input_stream */));
196   return buffered_input_stream.release();
197 }
198 
CreateWritableFile(const string & filename,const string & mode,TF_Status * out_status)199 tensorflow::WritableFile* CreateWritableFile(
200     const string& filename, const string& mode, TF_Status* out_status) {
201   std::unique_ptr<tensorflow::WritableFile> file;
202   tensorflow::Status status;
203   if (mode.find("a") != std::string::npos) {
204     status = tensorflow::Env::Default()->NewAppendableFile(filename, &file);
205   } else {
206     status = tensorflow::Env::Default()->NewWritableFile(filename, &file);
207   }
208   if (!status.ok()) {
209     Set_TF_Status_from_Status(out_status, status);
210     return nullptr;
211   }
212   return file.release();
213 }
214 
AppendToFile(const string & file_content,tensorflow::WritableFile * file,TF_Status * out_status)215 void AppendToFile(const string& file_content, tensorflow::WritableFile* file,
216                   TF_Status* out_status) {
217   tensorflow::Status status = file->Append(file_content);
218   if (!status.ok()) {
219     Set_TF_Status_from_Status(out_status, status);
220   }
221 }
222 
TellFile(tensorflow::WritableFile * file,TF_Status * out_status)223 int64 TellFile(tensorflow::WritableFile* file, TF_Status* out_status) {
224   int64 position = -1;
225   tensorflow::Status status = file->Tell(&position);
226   if (!status.ok()) {
227     Set_TF_Status_from_Status(out_status, status);
228   }
229   return position;
230 }
231 
232 
ReadFromStream(tensorflow::io::BufferedInputStream * stream,size_t bytes,TF_Status * out_status)233 string ReadFromStream(tensorflow::io::BufferedInputStream* stream,
234                       size_t bytes,
235                       TF_Status* out_status) {
236   string result;
237   tensorflow::Status status = stream->ReadNBytes(bytes, &result);
238   if (!status.ok() && status.code() != tensorflow::error::OUT_OF_RANGE) {
239     Set_TF_Status_from_Status(out_status, status);
240     result.clear();
241   }
242   return result;
243 }
244 
245 %}
246 
247 // Ensure that the returned object is destroyed when its wrapper is
248 // garbage collected.
249 %newobject CreateBufferedInputStream;
250 %newobject CreateWritableFile;
251 
252 // Wrap the above functions.
253 inline void FileExists(const string& filename, TF_Status* out_status);
254 inline void DeleteFile(const string& filename, TF_Status* out_status);
255 string ReadFileToString(const string& filename, TF_Status* out_status);
256 void WriteStringToFile(const string& filename, const string& file_content,
257                        TF_Status* out_status);
258 std::vector<string> GetChildren(const string& dir, TF_Status* out_status);
259 std::vector<string> GetMatchingFiles(const string& filename,
260                                      TF_Status* out_status);
261 void CreateDir(const string& dirname, TF_Status* out_status);
262 void RecursivelyCreateDir(const string& dirname, TF_Status* out_status);
263 void CopyFile(const string& oldpath, const string& newpath, bool overwrite,
264               TF_Status* out_status);
265 void RenameFile(const string& oldname, const string& newname, bool overwrite,
266                 TF_Status* out_status);
267 void DeleteRecursively(const string& dirname, TF_Status* out_status);
268 bool IsDirectory(const string& dirname, TF_Status* out_status);
269 void Stat(const string& filename, tensorflow::FileStatistics* stats,
270           TF_Status* out_status);
271 tensorflow::io::BufferedInputStream* CreateBufferedInputStream(
272     const string& filename, size_t buffer_size, TF_Status* out_status);
273 tensorflow::WritableFile* CreateWritableFile(const string& filename,
274                                              const string& mode,
275                                              TF_Status* out_status);
276 void AppendToFile(const string& file_content, tensorflow::WritableFile* file,
277                   TF_Status* out_status);
278 int64 TellFile(tensorflow::WritableFile* file, TF_Status* out_status);
279 string ReadFromStream(tensorflow::io::BufferedInputStream* stream,
280                       size_t bytes,
281                       TF_Status* out_status);
282 
283 %ignore tensorflow::Status::operator=;
284 %include "tensorflow/core/lib/core/status.h"
285 
286 %ignoreall
287 %unignore tensorflow::io::BufferedInputStream;
288 %unignore tensorflow::io::BufferedInputStream::~BufferedInputStream;
289 %unignore tensorflow::io::BufferedInputStream::ReadLineAsString;
290 %unignore tensorflow::io::BufferedInputStream::Seek;
291 %unignore tensorflow::io::BufferedInputStream::Tell;
292 %unignore tensorflow::WritableFile;
293 %unignore tensorflow::WritableFile::Close;
294 %unignore tensorflow::WritableFile::Flush;
295 %unignore tensorflow::WritableFile::~WritableFile;
296 %include "tensorflow/core/platform/file_system.h"
297 %include "tensorflow/core/lib/io/inputstream_interface.h"
298 %include "tensorflow/core/lib/io/buffered_inputstream.h"
299 %unignoreall
300 
301 %include "tensorflow/c/tf_status_helper.h"
302 
303 %ignore tensorflow::io::internal::JoinPathImpl;
304 %include "tensorflow/core/lib/io/path.h"
305 
306 %include "tensorflow/core/platform/file_statistics.h"
307