Allow hash of current directory
This commit is contained in:
parent
189a7c3ef1
commit
198ddd7782
@ -22,6 +22,15 @@ void dropshell::autocomplete(const std::vector<std::string> &args)
|
|||||||
|
|
||||||
// std::cout<<" cmd = ["<<cmd<<"]"<<std::endl;
|
// std::cout<<" cmd = ["<<cmd<<"]"<<std::endl;
|
||||||
|
|
||||||
|
if (cmd=="hash")
|
||||||
|
{ // output files and folders in the current directory, one per line.
|
||||||
|
std::filesystem::directory_iterator dir_iter(std::filesystem::current_path());
|
||||||
|
for (const auto& entry : dir_iter)
|
||||||
|
std::cout << entry.path().filename().string() << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string noargcmds[] = {"templates","autocomplete_list_servers","autocomplete_list_services","autocomplete_list_commands"};
|
std::string noargcmds[] = {"templates","autocomplete_list_servers","autocomplete_list_services","autocomplete_list_commands"};
|
||||||
if (std::find(std::begin(noargcmds), std::end(noargcmds), cmd) != std::end(noargcmds))
|
if (std::find(std::begin(noargcmds), std::end(noargcmds), cmd) != std::end(noargcmds))
|
||||||
return;
|
return;
|
||||||
|
@ -92,11 +92,27 @@ uint64_t hash_directory_recursive(const std::string &path) {
|
|||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint64_t hash_path(const std::string &path) {
|
||||||
|
if (!std::filesystem::exists(path)) {
|
||||||
|
std::cerr << "Path does not exist: " << path << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (std::filesystem::is_directory(path)) {
|
||||||
|
return hash_directory_recursive(path);
|
||||||
|
} else if (std::filesystem::is_regular_file(path)) {
|
||||||
|
return hash_file(path);
|
||||||
|
} else {
|
||||||
|
std::cerr << "Path is neither a file nor a directory: " << path << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void hash_demo(const std::string & path)
|
void hash_demo(const std::string & path)
|
||||||
{
|
{
|
||||||
std::cout << "Hashing directory: " << path << std::endl;
|
std::cout << "Hashing path: " << path << std::endl;
|
||||||
auto start = std::chrono::high_resolution_clock::now();
|
auto start = std::chrono::high_resolution_clock::now();
|
||||||
XXH64_hash_t hash = hash_directory_recursive(path);
|
XXH64_hash_t hash = hash_path(path);
|
||||||
auto end = std::chrono::high_resolution_clock::now();
|
auto end = std::chrono::high_resolution_clock::now();
|
||||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||||
std::cout << "Hash: " << hash << " (took " << duration.count() << "ms)" << std::endl;
|
std::cout << "Hash: " << hash << " (took " << duration.count() << "ms)" << std::endl;
|
||||||
@ -107,7 +123,7 @@ int hash_demo_raw(const std::string & path)
|
|||||||
if (!std::filesystem::exists(path)) {
|
if (!std::filesystem::exists(path)) {
|
||||||
std::cout << 0 <<std::endl; return 1;
|
std::cout << 0 <<std::endl; return 1;
|
||||||
}
|
}
|
||||||
XXH64_hash_t hash = hash_directory_recursive(path);
|
XXH64_hash_t hash = hash_path(path);
|
||||||
std::cout << hash << std::endl;
|
std::cout << hash << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,8 @@ namespace dropshell {
|
|||||||
|
|
||||||
uint64_t hash_directory_recursive(const std::string &path);
|
uint64_t hash_directory_recursive(const std::string &path);
|
||||||
|
|
||||||
|
uint64_t hash_path(const std::string &path);
|
||||||
|
|
||||||
void hash_demo(const std::string & path);
|
void hash_demo(const std::string & path);
|
||||||
|
|
||||||
int hash_demo_raw(const std::string & path);
|
int hash_demo_raw(const std::string & path);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user