Skip to content

Commit

Permalink
backport of #946
Browse files Browse the repository at this point in the history
  • Loading branch information
veggiesaurus committed Nov 25, 2021
1 parent ab7b047 commit 5b0d2e7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/Frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,8 @@ bool Frame::ExportCASAImage(casacore::ImageInterface<casacore::Float>& image, fs
bool success(false);

// Remove the old image file if it has a same file name
if (fs::exists(output_filename)) {
std::error_code error_code;
if (fs::exists(output_filename, error_code)) {
fs::remove_all(output_filename);
}

Expand Down
23 changes: 16 additions & 7 deletions src/SessionManager/ProgramSettings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ ProgramSettings::ProgramSettings(int argc, char** argv) {
const fs::path system_settings_path = "/etc/carta/backend.json";

json settings;
if (fs::exists(system_settings_path) && !no_system_config) {
std::error_code error_code;

if (!no_system_config && fs::exists(system_settings_path, error_code)) {
settings = JSONSettingsFromFile(system_settings_path.string());
system_settings_json_exists = true;
debug_msgs.push_back(fmt::format("Reading system settings from {}.", system_settings_path.string()));
}

if (fs::exists(user_settings_path) && !no_user_config) {
if (!no_user_config && fs::exists(user_settings_path, error_code)) {
auto user_settings = JSONSettingsFromFile(user_settings_path.string());
user_settings_json_exists = true;
debug_msgs.push_back(fmt::format("Reading user settings from {}.", user_settings_path.string()));
Expand All @@ -67,6 +69,12 @@ ProgramSettings::ProgramSettings(int argc, char** argv) {
json ProgramSettings::JSONSettingsFromFile(const std::string& json_file_path) {
std::ifstream ifs(json_file_path, std::ifstream::in);
json j;

if (!ifs.good()) {
warning_msgs.push_back(fmt::format("Error reading config file {}.", json_file_path));
return j;
}

try {
j = json::parse(ifs, nullptr, true, true);
} catch (json::exception& err) {
Expand Down Expand Up @@ -275,8 +283,8 @@ end.
no_browser = result["no_browser"].as<bool>();
read_only_mode = result["read_only_mode"].as<bool>();

no_user_config = result.count("no_user_config") ? true : false;
no_system_config = result.count("no_system_config") ? true : false;
no_user_config = result.count("no_user_config") != 0;
no_system_config = result.count("no_system_config") != 0;

applyOptionalArgument(top_level_folder, "root", result);
// Override deprecated "root" argument
Expand All @@ -301,8 +309,9 @@ end.

for (const auto& arg : positional_arguments) {
fs::path p(arg);
if (fs::exists(p)) {
if (fs::is_directory(p)) {
std::error_code error_code;
if (fs::exists(p, error_code)) {
if (fs::is_directory(p, error_code)) {
auto image_type = casacore::ImageOpener::imageType(p.string());
if (image_type == casacore::ImageOpener::AIPSPP || image_type == casacore::ImageOpener::MIRIAD ||
image_type == casacore::ImageOpener::IMAGECONCAT || image_type == casacore::ImageOpener::IMAGEEXPR ||
Expand All @@ -314,7 +323,7 @@ end.
file_paths.clear();
break;
}
} else if (!fs::is_regular_file(p)) {
} else if (!fs::is_regular_file(p, error_code)) {
// Ignore invalid files
file_paths.clear();
break;
Expand Down
31 changes: 20 additions & 11 deletions src/SimpleFrontendServer/SimpleFrontendServer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,19 @@ void SimpleFrontendServer::HandleStaticRequest(Res* res, Req* req) {
bool gzip_compressed = false;
auto gzip_path = path;
gzip_path += ".gz";
if (accepts_gzip && fs::exists(gzip_path) && fs::is_regular_file(gzip_path)) {
std::error_code error_code;
if (accepts_gzip && fs::exists(gzip_path, error_code) && fs::is_regular_file(gzip_path, error_code)) {
gzip_compressed = true;
path = gzip_path;
}

if (fs::exists(path) && fs::is_regular_file(path)) {
if (fs::exists(path, error_code) && fs::is_regular_file(path, error_code)) {
// Check file size
ifstream file(path.string(), ios::binary | ios::ate);
if (!file.good()) {
res->writeStatus(HTTP_404);
return;
}
streamsize size = file.tellg();
file.seekg(0, ios::beg);

Expand Down Expand Up @@ -111,13 +116,15 @@ void SimpleFrontendServer::HandleStaticRequest(Res* res, Req* req) {
}

bool SimpleFrontendServer::IsValidFrontendFolder(fs::path folder) {
std::error_code error_code;

// Check that the folder exists
if (!fs::exists(folder) || !fs::is_directory(folder)) {
if (!fs::exists(folder, error_code) || !fs::is_directory(folder, error_code)) {
return false;
}
// Check that index.html exists
folder /= "index.html";
if (!fs::exists(folder) || !fs::is_regular_file(folder)) {
if (!fs::exists(folder, error_code) || !fs::is_regular_file(folder, error_code)) {
return false;
}
// Check that index.html can be read
Expand All @@ -137,15 +144,16 @@ void SimpleFrontendServer::AddNoCacheHeaders(Res* res) {

json SimpleFrontendServer::GetExistingPreferences() {
auto preferences_path = _config_folder / "preferences.json";
if (!fs::exists(preferences_path)) {
return {{"version", 1}};
}

try {
std::error_code error_code;
if (!fs::exists(preferences_path, error_code)) {
return {{"version", 1}};
}
ifstream file(preferences_path.string());
string json_string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return json::parse(json_string);
} catch (exception) {
} catch (json::exception e) {
spdlog::warn(e.what());
return {};
}
}
Expand Down Expand Up @@ -357,13 +365,14 @@ void SimpleFrontendServer::HandleClearLayout(Res* res, Req* req) {
nlohmann::json SimpleFrontendServer::GetExistingLayouts() {
auto layout_folder = _config_folder / "layouts";
json layouts = json::object();
if (fs::exists(layout_folder)) {
std::error_code error_code;
if (fs::exists(layout_folder, error_code)) {
for (auto& p : fs::directory_iterator(layout_folder)) {
try {
string filename = p.path().filename().string();
regex layout_regex(R"(^(.+)\.json$)");
smatch sm;
if (fs::is_regular_file(p) && regex_search(filename, sm, layout_regex) && sm.size() == 2) {
if (fs::is_regular_file(p, error_code) && regex_search(filename, sm, layout_regex) && sm.size() == 2) {
string layout_name = sm[1];
ifstream file(p.path().string());
string json_string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
Expand Down
11 changes: 8 additions & 3 deletions src/Table/Table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ using namespace std;

Table::Table(const string& filename, bool header_only) : _valid(false), _filename(filename), _num_rows(0), _available_rows(0) {
fs::path file_path(filename);

if (!fs::exists(file_path)) {
_parse_error_message = "File does not exist!";
std::error_code error_code;
if (!fs::exists(file_path, error_code)) {
_parse_error_message = "File does not exist or cannot be read!";
return;
}

Expand All @@ -49,6 +49,11 @@ Table::Table(const string& filename, bool header_only) : _valid(false), _filenam
string Table::GetHeader(const string& filename) {
ifstream in(filename);
string header_string;

if (!in.good()) {
return header_string;
}

// Measure entire file size to ensure we don't read past EOF
in.seekg(0, ios_base::end);
size_t header_size = min(size_t(in.tellg()), size_t(MAX_HEADER_SIZE));
Expand Down
20 changes: 15 additions & 5 deletions src/Table/TableController.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ void TableController::OnOpenFileRequest(const CARTA::OpenCatalogFile& open_file_

open_file_response.set_file_id(file_id);
auto file_path = GetPath(open_file_request.directory(), open_file_request.name());
if (!fs::exists(file_path) || !fs::is_regular_file(file_path)) {
std::error_code error_code;

if (!fs::exists(file_path, error_code) || !fs::is_regular_file(file_path, error_code)) {
open_file_response.set_message(fmt::format("Cannot find path {}", file_path.string()));
open_file_response.set_success(false);
return;
Expand Down Expand Up @@ -186,8 +188,9 @@ void TableController::OnFileListRequest(
const CARTA::CatalogListRequest& file_list_request, CARTA::CatalogListResponse& file_list_response) {
fs::path root_path(_top_level_folder);
fs::path file_path = GetPath(file_list_request.directory());
std::error_code error_code;

if (!fs::exists(file_path) || !fs::is_directory(file_path)) {
if (!fs::exists(file_path, error_code) || !fs::is_directory(file_path, error_code)) {
file_list_response.set_success(false);
file_list_response.set_message("Incorrect file path");
return;
Expand Down Expand Up @@ -221,7 +224,13 @@ void TableController::OnFileListRequest(
break;
}

if (fs::is_directory(entry)) {
// Skip files that can't be read
std::error_code error_code;
if (!fs::exists(entry, error_code)) {
continue;
}

if (fs::is_directory(entry, error_code)) {
try {
// Try to construct a directory iterator. If it fails, the directory is inaccessible
auto test_directory_iterator = fs::directory_iterator(entry);
Expand All @@ -230,7 +239,7 @@ void TableController::OnFileListRequest(
// Skip inaccessible folders
continue;
}
} else if (fs::is_regular_file(entry) && fs::exists(entry)) {
} else if (fs::is_regular_file(entry, error_code)) {
uint32_t file_magic_number = GetMagicNumber(entry.path().string());
CARTA::CatalogFileType file_type;
if (file_magic_number == XML_MAGIC_NUMBER) {
Expand Down Expand Up @@ -274,8 +283,9 @@ void TableController::OnFileListRequest(
void TableController::OnFileInfoRequest(
const CARTA::CatalogFileInfoRequest& file_info_request, CARTA::CatalogFileInfoResponse& file_info_response) {
fs::path file_path = GetPath(file_info_request.directory(), file_info_request.name());
std::error_code error_code;

if (!fs::exists(file_path) || !fs::is_regular_file(file_path)) {
if (!fs::exists(file_path, error_code) || !fs::is_regular_file(file_path, error_code)) {
file_info_response.set_success(false);
file_info_response.set_message("Incorrect file path");
return;
Expand Down

0 comments on commit 5b0d2e7

Please sign in to comment.