Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix top directory in file browser #1393

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Fix matched polygon region approximation crash ([#1383](https://github.com/CARTAvis/carta-backend/issues/1383)).
* Fix save image/export regions bug which could cause directory overwrite or deletion ([#1377](https://github.com/CARTAvis/carta-backend/issues/1377)).
* Fix bug in cache slicer transformation which affects some images with rotated axes ([#1389](https://github.com/CARTAvis/carta-backend/pull/1389)).
* Fix bug accessing top (root) folder of file browser ([#2354](https://github.com/CARTAvis/carta-frontend/issues/2354)).

### Changed
* Move the loader cache to separate files ([#1021](https://github.com/CARTAvis/carta-backend/issues/1021)).
Expand Down
29 changes: 16 additions & 13 deletions src/Util/Casacore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,38 +99,41 @@ bool IsSubdirectory(string folder, string top_folder) {
}

casacore::String GetResolvedFilename(const string& root_dir, const string& directory, const string& file, string& message) {
// Given directory (relative to root folder) and file, return resolved path and filename
// (absolute pathname with symlinks resolved)
// Given directory (relative to root directory) and file, return resolved file path.
// Check if file path exists and is readable.
casacore::String resolved_filename;
casacore::Path path(root_dir);
path.append(directory);
casacore::File cc_directory(path);
casacore::File cc_file(path);

if (!cc_directory.exists()) {
// Check directory
if (!cc_file.exists()) {
message = "Directory " + directory + " does not exist.";
} else if (!cc_directory.isReadable()) {
} else if (!cc_file.isReadable()) {
message = "Directory " + directory + " is not readable.";
} else {
// Check file
path.append(file);
casacore::File cc_file(path);

cc_file = casacore::File(path);
if (!cc_file.exists()) {
message = "File " + file + " does not exist.";
} else if (!cc_file.isReadable()) {
message = "File " + file + " is not readable.";
} else {
try {
resolved_filename = cc_file.path().resolvedName();
resolved_filename = path.resolvedName();
} catch (const casacore::AipsError& err) {
try {
resolved_filename = cc_file.path().absoluteName();
} catch (const casacore::AipsError& err) {
// return empty string
message = "Cannot resolve file path.";
// resolvedName() calls absoluteName(), which returns an empty path due to parsing bug to remove dots (., ..) from path,
// resulting in AipsError. Workaround just sets expanded name used for exists() and isReadable().
if (path.absoluteName().empty()) {
resolved_filename = path.expandedName();
} else {
message = err.getMesg();
}
}
}
}

return resolved_filename;
}

Expand Down
21 changes: 21 additions & 0 deletions test/TestFileList.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ class FileListTest : public ::testing::Test {
}
}
}

void TestFileListSubdirectories(const std::string& top_level_folder, const std::string& starting_folder,
const CARTA::FileListRequest& request, bool expected_success = true) {
std::shared_ptr<FileListHandler> file_list_handler = std::make_shared<FileListHandler>(top_level_folder, starting_folder);
CARTA::FileListResponse response;
FileListHandler::ResultMsg result_msg;
file_list_handler->OnFileListRequest(request, response, result_msg);

EXPECT_EQ(response.success(), expected_success);
if (!response.success()) {
return;
}
// Expect no image files, non-zero subdirectories
EXPECT_EQ(response.files_size(), 0);
EXPECT_GT(response.subdirectories_size(), 0);
}
};

TEST_F(FileListTest, SetTopLevelFolder) {
Expand All @@ -64,6 +80,11 @@ TEST_F(FileListTest, SetTopLevelFolder) {

auto request4 = Message::FileListRequest(".");
TestFileList(abs_path, "", request4);

// Request file list for default top folder "/"
// 0 image files, > 0 subdirectories
auto request5 = Message::FileListRequest("");
TestFileListSubdirectories("/", "", request5);
}

TEST_F(FileListTest, SetStartingFolder) {
Expand Down
Loading