aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/lib/io/file_io.i
blob: c0c4e035fc3d6a50334acb9228c13c702ef426c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

%include "tensorflow/python/lib/core/strings.i"
%include "tensorflow/python/platform/base.i"

%{
#include "tensorflow/c/tf_status_helper.h"
#include "tensorflow/core/framework/types.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/lib/io/buffered_inputstream.h"
#include "tensorflow/core/lib/io/inputstream_interface.h"
#include "tensorflow/core/lib/io/random_inputstream.h"
#include "tensorflow/core/lib/io/path.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/file_statistics.h"
#include "tensorflow/core/platform/file_system.h"
#include "tensorflow/core/protobuf/meta_graph.pb.h"
%}

%{
inline void FileExists(const string& filename, TF_Status* out_status) {
  tensorflow::Status status = tensorflow::Env::Default()->FileExists(filename);
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
  }
}

inline void FileExists(const tensorflow::StringPiece& filename,
    TF_Status* out_status) {
  tensorflow::Status status =
      tensorflow::Env::Default()->FileExists(filename.ToString());
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
  }
}

inline void DeleteFile(const string& filename, TF_Status* out_status) {
  tensorflow::Status status = tensorflow::Env::Default()->DeleteFile(filename);
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
  }
}

string ReadFileToString(const string& filename, TF_Status* out_status) {
  string file_content;
  tensorflow::Status status = ReadFileToString(tensorflow::Env::Default(),
      filename, &file_content);
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
  }
  return file_content;
}

void WriteStringToFile(const string& filename, const string& file_content,
                       TF_Status* out_status) {
  tensorflow::Status status = WriteStringToFile(tensorflow::Env::Default(),
      filename, file_content);
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
  }
}

std::vector<string> GetChildren(const string& dir, TF_Status* out_status) {
  std::vector<string> results;
  tensorflow::Status status = tensorflow::Env::Default()->GetChildren(
      dir, &results);
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
  }
  return results;
}

std::vector<string> GetMatchingFiles(const string& filename,
                                     TF_Status* out_status) {
  std::vector<string> results;
  tensorflow::Status status = tensorflow::Env::Default()->GetMatchingPaths(
      filename, &results);
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
  }
  return results;
}

void CreateDir(const string& dirname, TF_Status* out_status) {
  tensorflow::Status status = tensorflow::Env::Default()->CreateDir(dirname);
  if (!status.ok() && status.code() != tensorflow::error::ALREADY_EXISTS) {
    Set_TF_Status_from_Status(out_status, status);
  }
}

void RecursivelyCreateDir(const string& dirname, TF_Status* out_status) {
  tensorflow::Status status = tensorflow::Env::Default()->RecursivelyCreateDir(
      dirname);
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
  }
}

void CopyFile(const string& oldpath, const string& newpath, bool overwrite,
              TF_Status* out_status) {
  // If overwrite is false and the newpath file exists then it's an error.
  if (!overwrite && tensorflow::Env::Default()->FileExists(newpath).ok()) {
    TF_SetStatus(out_status, TF_ALREADY_EXISTS, "file already exists");
    return;
  }
  string file_content;
  tensorflow::Status status = ReadFileToString(tensorflow::Env::Default(),
      oldpath, &file_content);
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
    return;
  }
  status = WriteStringToFile(tensorflow::Env::Default(), newpath, file_content);
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
  }
}

void RenameFile(const string& src, const string& target, bool overwrite,
                TF_Status* out_status) {
  // If overwrite is false and the target file exists then its an error.
  if (!overwrite && tensorflow::Env::Default()->FileExists(target).ok()) {
    TF_SetStatus(out_status, TF_ALREADY_EXISTS, "file already exists");
    return;
  }
  tensorflow::Status status = tensorflow::Env::Default()->RenameFile(src,
                                                                     target);
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
  }
}

using tensorflow::int64;

void DeleteRecursively(const string& dirname, TF_Status* out_status) {
  int64 undeleted_files, undeleted_dirs;
  tensorflow::Status status = tensorflow::Env::Default()->DeleteRecursively(
      dirname, &undeleted_files, &undeleted_dirs);
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
    return;
  }
  if (undeleted_files > 0 || undeleted_dirs > 0) {
    TF_SetStatus(out_status, TF_PERMISSION_DENIED,
                 "could not fully delete dir");
    return;
  }
}

bool IsDirectory(const string& dirname, TF_Status* out_status) {
  tensorflow::Status status = tensorflow::Env::Default()->IsDirectory(dirname);
  if (status.ok()) {
    return true;
  }
  // FAILED_PRECONDITION Status response means path exists but isn't a dir.
  if (status.code() != tensorflow::error::FAILED_PRECONDITION) {
    Set_TF_Status_from_Status(out_status, status);
  }
  return false;
}

using tensorflow::FileStatistics;

void Stat(const string& filename, FileStatistics* stats,
          TF_Status* out_status) {
  tensorflow::Status status = tensorflow::Env::Default()->Stat(filename,
                                                               stats);
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
  }
}

tensorflow::io::BufferedInputStream* CreateBufferedInputStream(
    const string& filename, size_t buffer_size, TF_Status* out_status) {
  std::unique_ptr<tensorflow::RandomAccessFile> file;
  tensorflow::Status status =
      tensorflow::Env::Default()->NewRandomAccessFile(filename, &file);
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
    return nullptr;
  }
  std::unique_ptr<tensorflow::io::RandomAccessInputStream> input_stream(
      new tensorflow::io::RandomAccessInputStream(
          file.release(), true /* owns_file */));
  std::unique_ptr<tensorflow::io::BufferedInputStream> buffered_input_stream(
      new tensorflow::io::BufferedInputStream(
          input_stream.release(), buffer_size, true /* owns_input_stream */));
  return buffered_input_stream.release();
}

tensorflow::WritableFile* CreateWritableFile(
    const string& filename, const string& mode, TF_Status* out_status) {
  std::unique_ptr<tensorflow::WritableFile> file;
  tensorflow::Status status;
  if (mode.find("a") != std::string::npos) {
    status = tensorflow::Env::Default()->NewAppendableFile(filename, &file);
  } else {
    status = tensorflow::Env::Default()->NewWritableFile(filename, &file);
  }
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
    return nullptr;
  }
  return file.release();
}

void AppendToFile(const string& file_content, tensorflow::WritableFile* file,
                  TF_Status* out_status) {
  tensorflow::Status status = file->Append(file_content);
  if (!status.ok()) {
    Set_TF_Status_from_Status(out_status, status);
  }
}

string ReadFromStream(tensorflow::io::BufferedInputStream* stream,
                      size_t bytes,
                      TF_Status* out_status) {
  string result;
  tensorflow::Status status = stream->ReadNBytes(bytes, &result);
  if (!status.ok() && status.code() != tensorflow::error::OUT_OF_RANGE) {
    Set_TF_Status_from_Status(out_status, status);
    result.clear();
  }
  return result;
}

%}

// Ensure that the returned object is destroyed when its wrapper is
// garbage collected.
%newobject CreateBufferedInputStream;
%newobject CreateWritableFile;

// Wrap the above functions.
inline void FileExists(const string& filename, TF_Status* out_status);
inline void DeleteFile(const string& filename, TF_Status* out_status);
string ReadFileToString(const string& filename, TF_Status* out_status);
void WriteStringToFile(const string& filename, const string& file_content,
                       TF_Status* out_status);
std::vector<string> GetChildren(const string& dir, TF_Status* out_status);
std::vector<string> GetMatchingFiles(const string& filename,
                                     TF_Status* out_status);
void CreateDir(const string& dirname, TF_Status* out_status);
void RecursivelyCreateDir(const string& dirname, TF_Status* out_status);
void CopyFile(const string& oldpath, const string& newpath, bool overwrite,
              TF_Status* out_status);
void RenameFile(const string& oldname, const string& newname, bool overwrite,
                TF_Status* out_status);
void DeleteRecursively(const string& dirname, TF_Status* out_status);
bool IsDirectory(const string& dirname, TF_Status* out_status);
void Stat(const string& filename, tensorflow::FileStatistics* stats,
          TF_Status* out_status);
tensorflow::io::BufferedInputStream* CreateBufferedInputStream(
    const string& filename, size_t buffer_size, TF_Status* out_status);
tensorflow::WritableFile* CreateWritableFile(const string& filename,
                                             const string& mode,
                                             TF_Status* out_status);
void AppendToFile(const string& file_content, tensorflow::WritableFile* file,
                  TF_Status* out_status);
string ReadFromStream(tensorflow::io::BufferedInputStream* stream,
                      size_t bytes,
                      TF_Status* out_status);

%ignore tensorflow::Status::operator=;
%include "tensorflow/core/lib/core/status.h"

%ignoreall
%unignore tensorflow::io::BufferedInputStream;
%unignore tensorflow::io::BufferedInputStream::~BufferedInputStream;
%unignore tensorflow::io::BufferedInputStream::ReadLineAsString;
%unignore tensorflow::io::BufferedInputStream::Seek;
%unignore tensorflow::io::BufferedInputStream::Tell;
%unignore tensorflow::WritableFile;
%unignore tensorflow::WritableFile::Close;
%unignore tensorflow::WritableFile::Flush;
%unignore tensorflow::WritableFile::~WritableFile;
%include "tensorflow/core/platform/file_system.h"
%include "tensorflow/core/lib/io/inputstream_interface.h"
%include "tensorflow/core/lib/io/buffered_inputstream.h"
%unignoreall

%include "tensorflow/c/tf_status_helper.h"

%ignore tensorflow::io::internal::JoinPathImpl;
%include "tensorflow/core/lib/io/path.h"

%include "tensorflow/core/platform/file_statistics.h"