Skip to content

Commit

Permalink
Merge branch 'main' into Insert_LumiPS_B_FieldMap
Browse files Browse the repository at this point in the history
  • Loading branch information
dhevang authored Sep 20, 2023
2 parents bb516a3 + 92e7d1c commit 1be7ce2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/pr-backport.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ jobs:
- uses: actions/checkout@v3
- name: Create backport pull requests
uses: korthout/backport-action@v1
with:
github_token: ${{ secrets.BACKPORT_ACTION_TOKEN }}
7 changes: 4 additions & 3 deletions src/FileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ using namespace dd4hep;

void usage(int argc, char** argv)
{
std::cout << "Usage: -plugin <name> -arg [-arg] \n"
" name: factory name FileLoader \n"
std::cerr << "Usage: -plugin <name> -arg [-arg] \n"
" cache:<string> cache location (may be read-only) \n"
" file:<string> file location \n"
" url:<string> url location \n"
Expand All @@ -46,8 +45,10 @@ long load_file(Detector& /* desc */, int argc, char** argv)
url = (argv[i] + 4);
else if (0 == std::strncmp("cmd:", argv[i], 4))
cmd = (argv[i] + 4);
else
else {
std::cerr << "Unexpected argument \"" << argv[i] << "\"" << std::endl;
usage(argc, argv);
}
}
printout(DEBUG, "FileLoader", "arg cache: " + cache);
printout(DEBUG, "FileLoader", "arg file: " + file);
Expand Down
34 changes: 25 additions & 9 deletions src/FileLoaderHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ inline void EnsureFileFromURLExists(std::string url, std::string file, std::stri
return;
}

if (fs::exists(fs::symlink_status(hash_path)) && !fs::exists(fs::status(hash_path))) {
printout(INFO, "FileLoader", "removing broken \"" + hash_path.string() + "\" symlink");
remove(hash_path);
}

// if hash does not exist, we try to retrieve file from cache
if (!fs::exists(hash_path)) {
// recursive loop into cache directories
Expand All @@ -80,27 +85,38 @@ inline void EnsureFileFromURLExists(std::string url, std::string file, std::stri
fs::path cache_path(cache);
printout(INFO, "FileLoader", "cache " + cache_path.string());
if (fs::exists(cache_path)) {
for (auto const& dir_entry : fs::recursive_directory_iterator(cache_path)) {
if (!dir_entry.is_directory())
continue;
fs::path cache_dir_path = cache_path / dir_entry;
auto check_path = [&](const fs::path &cache_dir_path) {
printout(INFO, "FileLoader", "checking " + cache_dir_path.string());
fs::path cache_hash_path = cache_dir_path / hash;
if (fs::exists(cache_hash_path)) {
// symlink hash to cache/.../hash
printout(INFO, "FileLoader",
"file " + file + " with hash " + hash + " found in " + cache_hash_path.string());
fs::path link_target;
if (cache_hash_path.is_absolute()) {
link_target = cache_hash_path;
} else {
link_target = fs::proximate(cache_hash_path, parent_path);
}
try {
fs::create_symlink(cache_hash_path, hash_path);
fs::create_symlink(link_target, hash_path);
success = true;
} catch (const fs::filesystem_error&) {
printout(ERROR, "FileLoader",
"unable to link from " + hash_path.string() + " to " + cache_hash_path.string());
printout(ERROR, "FileLoader", "hint: this may be resolved by removing directory " + parent_path.string());
printout(ERROR, "FileLoader", "hint: or in that directory removing the file or link " + cache_hash_path.string());
"unable to link from " + hash_path.string() + " to " + link_target.string());
std::_Exit(EXIT_FAILURE);
}
break;
return true;
}
return false;
};
if (!check_path(cache_path)) {
for (auto const& dir_entry : fs::recursive_directory_iterator(cache_path)) {
if (!dir_entry.is_directory())
continue;
if (check_path(dir_entry.path())) {
break;
};
}
}
}
Expand Down

0 comments on commit 1be7ce2

Please sign in to comment.