diff options
| author | 2021-11-10 00:54:35 -0500 | |
|---|---|---|
| committer | 2021-11-10 00:54:35 -0500 | |
| commit | 58fb7a0cee13d84170aac52f3f89d91888e1afe3 (patch) | |
| tree | 1d6312ba15f9ece5a8031e5280dfb8b38be8dfa3 /gulrak-filesystem/examples/du.cpp | |
| parent | 28db84b46139c9bb2bbcac8c6cc56e71d1e35629 (diff) | |
| parent | beb08eb751fa8e1f72042f263316ab5e5ddb596d (diff) | |
Update upstream source from tag 'upstream/2.4.0+dfsg'
Update to upstream version '2.4.0+dfsg'
with Debian dir 4b6668cc08c7b0e56b1e1f7b4e89ecdb316182a0
Diffstat (limited to 'gulrak-filesystem/examples/du.cpp')
| -rw-r--r-- | gulrak-filesystem/examples/du.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/gulrak-filesystem/examples/du.cpp b/gulrak-filesystem/examples/du.cpp new file mode 100644 index 0000000..929b809 --- /dev/null +++ b/gulrak-filesystem/examples/du.cpp @@ -0,0 +1,61 @@ +#include <iostream> +#include <iomanip> +#include <chrono> + +#if defined(__cplusplus) && __cplusplus >= 201703L && defined(__has_include) +#if __has_include(<filesystem>) +#define GHC_USE_STD_FS +#include <filesystem> +namespace fs = std::filesystem; +#endif +#endif +#ifndef GHC_USE_STD_FS +#include <ghc/filesystem.hpp> +namespace fs = ghc::filesystem; +#endif + +int main(int argc, char* argv[]) +{ +#ifdef GHC_FILESYSTEM_VERSION + fs::u8arguments u8guard(argc, argv); + if(!u8guard.valid()) { + std::cerr << "Invalid character encoding, UTF-8 based encoding needed." << std::endl; + std::exit(EXIT_FAILURE); + } +#endif + if(argc > 2) { + std::cerr << "USAGE: du <path>" << std::endl; + exit(1); + } + fs::path dir{"."}; + if(argc == 2) { + dir = fs::u8path(argv[1]); + } + + uint64_t totalSize = 0; + int totalDirs = 0; + int totalFiles = 0; + int maxDepth = 0; + + try { + auto rdi = fs::recursive_directory_iterator(dir); + for(auto de : rdi) { + if(rdi.depth() > maxDepth) { + maxDepth = rdi.depth(); + } + if(de.is_regular_file()) { + totalSize += de.file_size(); + ++totalFiles; + } + else if(de.is_directory()) { + ++totalDirs; + } + } + } + catch(fs::filesystem_error fe) { + std::cerr << "Error: " << fe.what() << std::endl; + exit(1); + } + std::cout << totalSize << " bytes in " << totalFiles << " files and " << totalDirs << " directories, maximum depth: " << maxDepth << std::endl; + return 0; +} |
