14#define stat_struct stat
18#define stat_struct __stat64
30 : fp_(fp), use_stdio_(use_stdio) {}
34 virtual size_t Read(
void *ptr,
size_t size) {
35 return std::fread(ptr, 1, size, fp_);
37 virtual void Write(
const void *ptr,
size_t size) {
38 CHECK(std::fwrite(ptr, 1, size, fp_) == size)
39 <<
"FileStream.Write incomplete";
41 virtual void Seek(
size_t pos) {
43 CHECK(!std::fseek(fp_,
static_cast<long>(pos), SEEK_SET));
45 CHECK(!_fseeki64(fp_, pos, SEEK_SET));
48 virtual size_t Tell(
void) {
50 return std::ftell(fp_);
52 return _ftelli64(fp_);
55 virtual bool AtEnd(
void)
const {
56 return std::feof(fp_) != 0;
58 inline void Close(
void) {
59 if (fp_ != NULL && !use_stdio_) {
60 std::fclose(fp_); fp_ = NULL;
70 struct stat_struct sb;
73 if (stat(path.name.c_str(), &sb) == -1) {
78 if (lstat(path.name.c_str(), &sb) == 0) {
81 LOG(INFO) <<
"LocalFileSystem.GetPathInfo: detected symlink "
82 << path.name <<
" error: " << strerror(errsv);
86 LOG(FATAL) <<
"LocalFileSystem.GetPathInfo: "
87 << path.name <<
" error: " << strerror(errsv);
89 ret.size = sb.st_size;
91 if ((sb.st_mode & S_IFMT) == S_IFDIR) {
92 ret.type = kDirectory;
101 DIR *dir = opendir(path.name.c_str());
104 LOG(FATAL) <<
"LocalFileSystem.ListDirectory " << path.str()
105 <<
" error: " << strerror(errsv);
110 while ((ent = readdir(dir)) != NULL) {
111 if (!strcmp(ent->d_name,
"."))
continue;
112 if (!strcmp(ent->d_name,
".."))
continue;
114 if (pp.name[pp.name.length() - 1] !=
'/') {
117 pp.name += ent->d_name;
123 std::string pattern = path.name +
"/*";
124 HANDLE handle = FindFirstFile(pattern.c_str(), &fd);
125 if (handle == INVALID_HANDLE_VALUE) {
126 int errsv = GetLastError();
127 LOG(FATAL) <<
"LocalFileSystem.ListDirectory " << path.str()
128 <<
" error: " << strerror(errsv);
131 if (strcmp(fd.cFileName,
".") && strcmp(fd.cFileName,
"..")) {
133 char clast = pp.name[pp.name.length() - 1];
134 if (pp.name ==
".") {
135 pp.name = fd.cFileName;
136 }
else if (clast !=
'/' && clast !=
'\\') {
138 pp.name += fd.cFileName;
142 }
while (FindNextFile(handle, &fd));
148 const char*
const mode,
150 bool use_stdio =
false;
153 const int fname_length = MultiByteToWideChar(CP_UTF8, 0, path.name.c_str(), -1,
nullptr, 0);
154 CHECK(fname_length > 0) <<
" LocalFileSystem::Open \"" << path.str()
155 <<
"\": " <<
"Invalid character sequence.";
156 std::wstring fname(fname_length, 0);
157 MultiByteToWideChar(CP_UTF8, 0, path.name.c_str(), -1, &fname[0], fname_length);
159 const int mode_length = MultiByteToWideChar(CP_UTF8, 0, mode, -1,
nullptr, 0);
160 std::wstring wmode(mode_length, 0);
161 MultiByteToWideChar(CP_UTF8, 0, mode, -1, &wmode[0], mode_length);
164#ifndef DMLC_DISABLE_STDIN
165 if (!wcscmp(fname.c_str(), L
"stdin")) {
166 use_stdio =
true; fp = stdin;
168 if (!wcscmp(fname.c_str(), L
"stdout")) {
169 use_stdio =
true; fp = stdout;
172 if (!wcsncmp(fname.c_str(), L
"file://", 7)) { fname = fname.substr(7); }
174 std::wstring flag(wmode.c_str());
175 if (flag == L
"w") flag = L
"wb";
176 if (flag == L
"r") flag = L
"rb";
178 fp = _wfopen(fname.c_str(), flag.c_str());
180 fp = fopen(fname, flag.c_str());
184 const char *fname = path.name.c_str();
186#ifndef DMLC_DISABLE_STDIN
187 if (!strcmp(fname,
"stdin")) {
188 use_stdio =
true; fp = stdin;
190 if (!strcmp(fname,
"stdout")) {
191 use_stdio =
true; fp = stdout;
194 if (!strncmp(fname,
"file://", 7)) fname += 7;
196 std::string flag = mode;
197 if (flag ==
"w") flag =
"wb";
198 if (flag ==
"r") flag =
"rb";
200 fp = fopen64(fname, flag.c_str());
202 fp = fopen(fname, flag.c_str());
209 CHECK(allow_null) <<
" LocalFileSystem::Open \"" << path.str() <<
"\": " << strerror(errno);
214 return Open(path,
"r", allow_null);
interface of i/o stream that support seek
Definition io.h:109
implementation of file i/o stream
Definition local_filesys.cc:27
virtual size_t Tell(void)
tell the position of the stream
Definition local_filesys.cc:48
virtual void Seek(size_t pos)
seek to certain position of the file
Definition local_filesys.cc:41
virtual void Write(const void *ptr, size_t size)
writes data to a stream
Definition local_filesys.cc:37
virtual size_t Read(void *ptr, size_t size)
reads data from a stream
Definition local_filesys.cc:34
virtual void ListDirectory(const URI &path, std::vector< FileInfo > *out_list)
list files in a directory
Definition local_filesys.cc:99
virtual FileInfo GetPathInfo(const URI &path)
get information about a path
Definition local_filesys.cc:69
virtual SeekStream * OpenForRead(const URI &path, bool allow_null)
open a seekable stream for read
Definition local_filesys.cc:213
virtual SeekStream * Open(const URI &path, const char *const flag, bool allow_null)
open a stream, will report error and exit if bad thing happens NOTE: the IStream can continue to work...
Definition local_filesys.cc:147
defines configuration macros
defines logging macros of dmlc allows use of GLOG, fall back to internal implementation when disabled
namespace for dmlc
Definition array_view.h:12