File: file_adapter.cc

package info (click to toggle)
pytorch-cuda 2.6.0%2Bdfsg-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 161,620 kB
  • sloc: python: 1,278,832; cpp: 900,322; ansic: 82,710; asm: 7,754; java: 3,363; sh: 2,811; javascript: 2,443; makefile: 597; ruby: 195; xml: 84; objc: 68
file content (82 lines) | stat: -rw-r--r-- 2,364 bytes parent folder | download | duplicates (3)
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
#include "caffe2/serialize/file_adapter.h"
#include <c10/util/Exception.h>
#include <cerrno>
#include <cstdio>
#include <string>
#include "caffe2/core/common.h"

namespace caffe2 {
namespace serialize {

FileAdapter::RAIIFile::RAIIFile(const std::string& file_name) {
  fp_ = fopen(file_name.c_str(), "rb");
  if (fp_ == nullptr) {
    auto old_errno = errno;
#if defined(_WIN32) && (defined(__MINGW32__) || defined(_MSC_VER))
    char buf[1024];
    buf[0] = '\0';
    char* error_msg = buf;
    strerror_s(buf, sizeof(buf), old_errno);
#else
    auto error_msg =
        std::system_category().default_error_condition(old_errno).message();
#endif
    TORCH_CHECK(
        false,
        "open file failed because of errno ",
        old_errno,
        " on fopen: ",
        error_msg,
        ", file path: ",
        file_name);
  }
}

FileAdapter::RAIIFile::~RAIIFile() {
  if (fp_ != nullptr) {
    fclose(fp_);
  }
}

// FileAdapter directly calls C file API.
FileAdapter::FileAdapter(const std::string& file_name) : file_(file_name) {
  const int fseek_ret = fseek(file_.fp_, 0L, SEEK_END);
  TORCH_CHECK(fseek_ret == 0, "fseek returned ", fseek_ret);
#if defined(_MSC_VER)
  const int64_t ftell_ret = _ftelli64(file_.fp_);
#else
  const off_t ftell_ret = ftello(file_.fp_);
#endif
  TORCH_CHECK(ftell_ret != -1L, "ftell returned ", ftell_ret);
  size_ = ftell_ret;
  rewind(file_.fp_);
}

size_t FileAdapter::size() const {
  return size_;
}

size_t FileAdapter::read(uint64_t pos, void* buf, size_t n, const char* what)
    const {
  // Ensure that pos doesn't exceed size_.
  pos = std::min(pos, size_);
  // If pos doesn't exceed size_, then size_ - pos can never be negative (in
  // signed math) or since these are unsigned values, a very large value.
  // Clamp 'n' to the smaller of 'size_ - pos' and 'n' itself. i.e. if the
  // user requested to read beyond the end of the file, we clamp to just the
  // end of the file.
  n = std::min(static_cast<size_t>(size_ - pos), n);
#if defined(_MSC_VER)
  const int fseek_ret = _fseeki64(file_.fp_, pos, SEEK_SET);
#else
  const int fseek_ret = fseeko(file_.fp_, pos, SEEK_SET);
#endif
  TORCH_CHECK(
      fseek_ret == 0, "fseek returned ", fseek_ret, ", context: ", what);
  return fread(buf, 1, n, file_.fp_);
}

FileAdapter::~FileAdapter() = default;

} // namespace serialize
} // namespace caffe2