Treelite
filesystem.cc
Go to the documentation of this file.
1 
8 #include <treelite/filesystem.h>
9 #include <dmlc/logging.h>
10 #include <fstream>
11 
12 #ifdef _WIN32
13 #include <windows.h>
14 #include <Shlwapi.h>
15 #pragma comment(lib, "Shlwapi.lib")
16 #else
17 #include <unistd.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <libgen.h>
23 #include <cstring>
24 #endif
25 
26 namespace {
27 
28 inline void HandleSystemError(const std::string& msg) {
29 #ifdef _WIN32
30  LPVOID msg_buf;
31  DWORD dw = GetLastError();
32  FormatMessage(
33  FORMAT_MESSAGE_ALLOCATE_BUFFER |
34  FORMAT_MESSAGE_FROM_SYSTEM |
35  FORMAT_MESSAGE_IGNORE_INSERTS,
36  NULL,
37  dw,
38  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
39  (LPTSTR)&msg_buf,
40  0, NULL);
41  const std::string msg_err(static_cast<const char*>(msg_buf));
42  LocalFree(msg_buf);
43 #else
44  const std::string msg_err(strerror(errno));
45 #endif
46  LOG(FATAL) << msg << "\nReason: " << msg_err;
47 }
48 
49 } // anonymous namespace
50 
51 namespace treelite {
52 namespace filesystem {
53 
54 void CreateDirectoryIfNotExist(const char* dirpath) {
55 #ifdef _WIN32
56  DWORD ftyp = GetFileAttributesA(dirpath);
57  if (ftyp == INVALID_FILE_ATTRIBUTES) {
58  // directory doesn't seem to exist; attempt to create one
59  if (CreateDirectoryA(dirpath, NULL) == 0) {
60  // failed to create a new directory
61  HandleSystemError(std::string("CreateDirectoryIfNotExist: "
62  "failed to create new directory ") + dirpath);
63  }
64  } else {
65  if (!(ftyp & FILE_ATTRIBUTE_DIRECTORY)) {
66  LOG(FATAL) << "CreateDirectoryIfNotExist: "
67  << dirpath << " is a file, not a directory";
68  }
69  }
70 #else
71  struct stat sb;
72  if (stat(dirpath, &sb) != 0) {
73  // directory doesn't seem to exist; attempt to create one
74  if (mkdir(dirpath, S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
75  // failed to create a new directory
76  HandleSystemError(std::string("CreateDirectoryIfNotExist: "
77  "failed to create new directory ") + dirpath);
78  }
79  } else {
80  if (!S_ISDIR(sb.st_mode)) {
81  LOG(FATAL) << "CreateDirectoryIfNotExist: "
82  << dirpath << " is a file, not a directory";
83  }
84  }
85 #endif
86 }
87 
88 void WriteToFile(const std::string& filename, const std::string& content) {
89  std::ofstream of(filename);
90  of << content;
91 }
92 
93 void WriteToFile(const std::string& filename, const std::vector<char>& content) {
94  std::ofstream of(filename, std::ios::out | std::ios::binary);
95  of.write(content.data(), content.size());
96 }
97 
98 } // namespace filesystem
99 } // namespace treelite
void CreateDirectoryIfNotExist(const char *dirpath)
Create a directory with a given name if one doesn&#39;t exist already.
Definition: filesystem.cc:54
Cross-platform wrapper for common filesystem functions.
void WriteToFile(const std::string &filename, const std::string &content)
Write a sequence of strings to a text file, with newline character ( ) inserted between strings...
Definition: filesystem.cc:88