1 #include "caffe2/serialize/file_adapter.h"
2 #include <c10/util/Exception.h>
3 #include <cerrno>
4 #include <cstdio>
5 #include <string>
6 #include "caffe2/core/common.h"
7
8 namespace caffe2 {
9 namespace serialize {
10
RAIIFile(const std::string & file_name)11 FileAdapter::RAIIFile::RAIIFile(const std::string& file_name) {
12 fp_ = fopen(file_name.c_str(), "rb");
13 if (fp_ == nullptr) {
14 auto old_errno = errno;
15 #if defined(_WIN32) && (defined(__MINGW32__) || defined(_MSC_VER))
16 char buf[1024];
17 buf[0] = '\0';
18 char* error_msg = buf;
19 strerror_s(buf, sizeof(buf), old_errno);
20 #else
21 auto error_msg =
22 std::system_category().default_error_condition(old_errno).message();
23 #endif
24 AT_ERROR(
25 "open file failed because of errno ",
26 old_errno,
27 " on fopen: ",
28 error_msg,
29 ", file path: ",
30 file_name);
31 }
32 }
33
~RAIIFile()34 FileAdapter::RAIIFile::~RAIIFile() {
35 if (fp_ != nullptr) {
36 fclose(fp_);
37 }
38 }
39
40 // FileAdapter directly calls C file API.
FileAdapter(const std::string & file_name)41 FileAdapter::FileAdapter(const std::string& file_name) : file_(file_name) {
42 const int fseek_ret = fseek(file_.fp_, 0L, SEEK_END);
43 TORCH_CHECK(fseek_ret == 0, "fseek returned ", fseek_ret);
44 #if defined(_MSC_VER)
45 const int64_t ftell_ret = _ftelli64(file_.fp_);
46 #else
47 const off_t ftell_ret = ftello(file_.fp_);
48 #endif
49 TORCH_CHECK(ftell_ret != -1L, "ftell returned ", ftell_ret);
50 size_ = ftell_ret;
51 rewind(file_.fp_);
52 }
53
size() const54 size_t FileAdapter::size() const {
55 return size_;
56 }
57
read(uint64_t pos,void * buf,size_t n,const char * what) const58 size_t FileAdapter::read(uint64_t pos, void* buf, size_t n, const char* what)
59 const {
60 // Ensure that pos doesn't exceed size_.
61 pos = std::min(pos, size_);
62 // If pos doesn't exceed size_, then size_ - pos can never be negative (in
63 // signed math) or since these are unsigned values, a very large value.
64 // Clamp 'n' to the smaller of 'size_ - pos' and 'n' itself. i.e. if the
65 // user requested to read beyond the end of the file, we clamp to just the
66 // end of the file.
67 n = std::min(static_cast<size_t>(size_ - pos), n);
68 #if defined(_MSC_VER)
69 const int fseek_ret = _fseeki64(file_.fp_, pos, SEEK_SET);
70 #else
71 const int fseek_ret = fseeko(file_.fp_, pos, SEEK_SET);
72 #endif
73 TORCH_CHECK(
74 fseek_ret == 0, "fseek returned ", fseek_ret, ", context: ", what);
75 return fread(buf, 1, n, file_.fp_);
76 }
77
78 FileAdapter::~FileAdapter() = default;
79
80 } // namespace serialize
81 } // namespace caffe2
82