Skip to content

Commit

Permalink
Just use std exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
trivialfis committed Apr 22, 2020
1 parent ade6296 commit 9b32ae0
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 61 deletions.
5 changes: 0 additions & 5 deletions src/cli_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -441,11 +441,6 @@ class CLI {

rabit::Init(argc, argv);
std::string config_path = argv[1];
if (!common::CanReadFile(config_path)) {
std::cerr << "Failed to open config file: \"" << argv[1] << "\"\n"
<< CliHelp();
exit(1);
}

common::ConfigParser cp(config_path);
auto cfg = cp.Parse();
Expand Down
12 changes: 9 additions & 3 deletions src/common/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ class ConfigParser {
std::string LoadConfigFile(const std::string& path) {
std::ifstream fin(path, std::ios_base::in | std::ios_base::binary);
CHECK(fin) << "Failed to open: " << path;
std::string content{std::istreambuf_iterator<char>(fin),
std::istreambuf_iterator<char>()};
return content;
try {
std::string content{std::istreambuf_iterator<char>(fin),
std::istreambuf_iterator<char>()};
return content;
} catch (std::ios_base::failure const &e) {
LOG(FATAL) << "Failed to read: \"" << path << "\"\n"
<< e.what();
}
return "";
}

/*!
Expand Down
49 changes: 5 additions & 44 deletions src/common/io.cc
Original file line number Diff line number Diff line change
@@ -1,48 +1,19 @@
/*!
* Copyright (c) by XGBoost Contributors 2019-2020
* Copyright (c) by XGBoost Contributors 2019
*/
#define XGBOOST_IS_UNIX() \
defined(__unix__) || defined(unix) || defined(__unix) || defined(__APPLE__)

#if XGBOOST_IS_UNIX()
#if defined(__unix__)
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#endif // XGBOOST_IS_UNIX()

#endif // defined(__unix__)
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <utility>

#include "xgboost/logging.h"
#include "io.h"

#ifndef S_ISDIR

#ifdef _MSC_VER
#include <io.h>
// from: https://www.linuxquestions.org/questions/programming-9/porting-to-win32-429334/
#define XGBOOST_S_ISDIR(mode) (((mode) & _S_IFMT) == _S_IFDIR)
#endif // _MSC_VER

#else

#define XGBOOST_S_ISDIR(mode) S_ISDIR((mode))
#endif // S_ISDIR

namespace {
int Access(char const* path, int type) {
#if defined(_MSC_VER)
return _access(path, type);
#else
return access(path, type);
#endif // defined(_MSC_VER)
}
} // anonymous namespace

namespace xgboost {
namespace common {

Expand Down Expand Up @@ -137,9 +108,7 @@ std::string LoadSequentialFile(std::string fname) {
};

std::string buffer;
CHECK(CanReadFile(fname.c_str()))
<< "Failed to read file: " << fname;
#if XGBOOST_IS_UNIX()
#if defined(__unix__)
struct stat fs;
if (stat(fname.c_str(), &fs) != 0) {
OpenErr();
Expand Down Expand Up @@ -168,18 +137,10 @@ std::string LoadSequentialFile(std::string fname) {
buffer.resize(fsize + 1);
fread(&buffer[0], 1, fsize, f);
fclose(f);
#endif // XGBOOST_IS_UNIX()
#endif // defined(__unix__)
buffer.back() = '\0';
return buffer;
}

bool CanReadFile(std::string const& path) {
struct stat path_stat;
stat(path.c_str(), &path_stat);
return Access(path.c_str(), 0) == 0 && !XGBOOST_S_ISDIR(path_stat.st_mode);
}
} // namespace common
} // namespace xgboost

#undef XGBOOST_S_ISDIR
#undef XGBOOST_IS_UNIX
1 change: 0 additions & 1 deletion src/common/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ inline std::string FileExtension(std::string const& fname) {
}
}

bool CanReadFile(std::string const& path);
} // namespace common
} // namespace xgboost
#endif // XGBOOST_COMMON_IO_H_
8 changes: 0 additions & 8 deletions tests/cpp/common/test_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,5 @@ TEST(IO, FixedSizeStream) {
ASSERT_EQ(huge_buffer, out_buffer);
}
}

TEST(IO, CanReadFile) {
// Non-exist file.
ASSERT_FALSE(CanReadFile("foo"));
// Can't read directory
ASSERT_FALSE(CanReadFile("./"));
ASSERT_TRUE(CanReadFile(__FILE__));
}
} // namespace common
} // namespace xgboost

0 comments on commit 9b32ae0

Please sign in to comment.