Skip to content

Commit bc81b2c

Browse files
rohan100jaintensorflower-gardener
authored andcommitted
Adding support for Stat in the FileIO API.
Change: 129043456
1 parent 41724a6 commit bc81b2c

7 files changed

Lines changed: 73 additions & 10 deletions

File tree

tensorflow/core/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,7 @@ filegroup(
10101010
"platform/default/protobuf.h",
10111011
"platform/default/thread_annotations.h",
10121012
"platform/env.h",
1013+
"platform/file_statistics.h",
10131014
"platform/file_system.h",
10141015
"platform/fingerprint.h",
10151016
"platform/host_info.h",
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 THIRD_PARTY_TENSORFLOW_CORE_PLATFORM_FILE_STATISTICS_H_
17+
#define THIRD_PARTY_TENSORFLOW_CORE_PLATFORM_FILE_STATISTICS_H_
18+
19+
#include "tensorflow/core/platform/types.h"
20+
21+
namespace tensorflow {
22+
23+
struct FileStatistics {
24+
// The length of the file or -1 if finding file length is not supported.
25+
int64 length = -1;
26+
// The last modified time in nanoseconds.
27+
int64 mtime_nsec = 0;
28+
// This is the mode_t from stat.h containing file type and permission
29+
// information.
30+
mode_t mode = 0;
31+
32+
FileStatistics() {}
33+
~FileStatistics() {}
34+
};
35+
36+
} // namespace tensorflow
37+
38+
#endif // THIRD_PARTY_TENSORFLOW_CORE_PLATFORM_FILE_STATISTICS_H_

tensorflow/core/platform/file_system.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ limitations under the License.
2424
#include "tensorflow/core/lib/core/errors.h"
2525
#include "tensorflow/core/lib/core/status.h"
2626
#include "tensorflow/core/lib/core/stringpiece.h"
27+
#include "tensorflow/core/platform/file_statistics.h"
2728
#include "tensorflow/core/platform/macros.h"
2829
#include "tensorflow/core/platform/protobuf.h"
2930
#include "tensorflow/core/platform/types.h"
@@ -34,16 +35,6 @@ class RandomAccessFile;
3435
class ReadOnlyMemoryRegion;
3536
class WritableFile;
3637

37-
struct FileStatistics {
38-
// The length of the file or -1 if finding file length is not supported.
39-
int64 length;
40-
// The last modified time in nanoseconds.
41-
int64 mtime_nsec;
42-
// This field contains more than just the permissions bits. More information
43-
// can be found on the man page for stat(2).
44-
mode_t mode;
45-
};
46-
4738
/// A generic interface for accessing a file system.
4839
class FileSystem {
4940
public:

tensorflow/python/lib/io/file_io.i

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ limitations under the License.
2424
#include "tensorflow/core/lib/io/path.h"
2525
#include "tensorflow/core/lib/io/match.h"
2626
#include "tensorflow/core/platform/env.h"
27+
#include "tensorflow/core/platform/file_statistics.h"
2728
#include "tensorflow/core/protobuf/meta_graph.pb.h"
2829
%}
2930

@@ -143,6 +144,17 @@ bool IsDirectory(const string& dirname, TF_Status* out_status) {
143144
}
144145
return false;
145146
}
147+
148+
using tensorflow::FileStatistics;
149+
150+
void Stat(const string& filename, FileStatistics* stats,
151+
TF_Status* out_status) {
152+
tensorflow::Status status = tensorflow::Env::Default()->Stat(filename,
153+
stats);
154+
if (!status.ok()) {
155+
Set_TF_Status_from_Status(out_status, status);
156+
}
157+
}
146158
%}
147159

148160
// Wrap the above functions.
@@ -160,5 +172,8 @@ void RenameFile(const string& oldname, const string& newname, bool overwrite,
160172
TF_Status* out_status);
161173
void DeleteRecursively(const string& dirname, TF_Status* out_status);
162174
bool IsDirectory(const string& dirname, TF_Status* out_status);
175+
void Stat(const string& filename, tensorflow::FileStatistics* stats,
176+
TF_Status* out_status);
163177

164178
%include "tensorflow/core/lib/io/path.h"
179+
%include "tensorflow/core/platform/file_statistics.h"

tensorflow/python/lib/io/file_io.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,10 @@ def walk(top, in_order=True):
151151

152152
if not in_order:
153153
yield here
154+
155+
156+
def stat(filename):
157+
file_statistics = pywrap_tensorflow.FileStatistics()
158+
with errors.raise_exception_on_not_ok_status() as status:
159+
pywrap_tensorflow.Stat(compat.as_bytes(filename), file_statistics, status)
160+
return file_statistics

tensorflow/python/lib/io/file_io_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,5 +247,14 @@ def testWalkFailure(self):
247247
self.assertItemsEqual(all_subdirs, [])
248248
self.assertItemsEqual(all_files, [])
249249

250+
def testStat(self):
251+
file_path = os.path.join(self._base_dir, "temp_file")
252+
file_io.write_string_to_file(file_path, "testing")
253+
file_statistics = file_io.stat(file_path)
254+
os_statistics = os.stat(file_path)
255+
self.assertEquals(7, file_statistics.length)
256+
self.assertEqual(
257+
int(os_statistics.st_mtime), int(file_statistics.mtime_nsec / 1e9))
258+
250259
if __name__ == "__main__":
251260
tf.test.main()

tensorflow/python/platform/base.i

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ std::vector<type>* OUTPUT (std::vector<type> temp),
146146
%enddef
147147

148148
_LIST_OUTPUT_TYPEMAP(string, _SwigBytes_FromString);
149+
_LIST_OUTPUT_TYPEMAP(long long, PyLong_FromLongLong);
149150
_LIST_OUTPUT_TYPEMAP(unsigned long long, PyLong_FromUnsignedLongLong);
150151

151152
%typemap(in) uint64 {
@@ -178,6 +179,7 @@ _LIST_OUTPUT_TYPEMAP(unsigned long long, PyLong_FromUnsignedLongLong);
178179
%enddef
179180

180181
_COPY_TYPEMAPS(unsigned long long, uint64);
182+
_COPY_TYPEMAPS(long long, int64);
181183

182184
// SWIG macros for explicit API declaration.
183185
// Usage:

0 commit comments

Comments
 (0)