@@ -37,9 +37,20 @@ constexpr const char * CACHE_ATTRS_DIR = "attrs";
3737// Removes a file or empty directory.
3838// In case of an error, the error_count increases by one.
3939// Returns number of removed items (0 or 1).
40- std::size_t remove (const std::filesystem::path & path, std::size_t & error_count, Logger & log) noexcept {
40+ std::size_t remove (
41+ const std::filesystem::path & path, std::size_t & error_count, std::size_t & bytes_count, Logger & log) noexcept {
4142 try {
43+ std::error_code ec;
44+ auto size = std::filesystem::file_size (path, ec);
45+ if (ec.value () == 21 ) {
46+ // `path` is a directory. Allocated space is FS depended (usually 4096 bytes). Do not risk it
47+ size = 0 ;
48+ } else if (ec) {
49+ // Usually error #2 (No such file or directory)
50+ size = 0 ;
51+ }
4252 if (std::filesystem::remove (path)) {
53+ bytes_count += size;
4354 return 1 ;
4455 }
4556 } catch (const std::filesystem::filesystem_error & ex) {
@@ -62,10 +73,12 @@ inline std::filesystem::path get_attribute_filepath(
6273RepoCache::RemoveStatistics RepoCache::Impl::cache_remove_attributes (const std::filesystem::path & path, Logger & log) {
6374 auto status = remove_recursive (path / CACHE_ATTRS_DIR, log);
6475 log.debug (
65- " Attributes removal from repository cache in path \" {}\" complete. Removed {} files, {} directories. {} errors" ,
76+ " Attributes removal from repository cache in path \" {}\" complete. "
77+ " Removed {} files, {} directories (total of {} bytes). {} errors" ,
6678 path.native (),
6779 status.get_files_removed (),
6880 status.get_dirs_removed (),
81+ status.get_bytes_removed (), // Print actual bytes (instead of MiB, GiB, ...) for debug
6982 status.get_errors ());
7083 return status;
7184}
@@ -77,19 +90,20 @@ RepoCache::RemoveStatistics RepoCache::Impl::remove_recursive(const std::filesys
7790 for (const auto & dir_entry : std::filesystem::directory_iterator (dir_path, ec)) {
7891 if (dir_entry.is_directory () && !dir_entry.is_symlink ()) {
7992 status += remove_recursive (dir_entry, log);
80- status.p_impl ->dirs_removed += remove (dir_entry, status.p_impl ->errors , log);
93+ status.p_impl ->dirs_removed += remove (dir_entry, status.p_impl ->errors , status. p_impl -> bytes_removed , log);
8194 } else {
82- status.p_impl ->files_removed += remove (dir_entry, status.p_impl ->errors , log);
95+ status.p_impl ->files_removed += remove (dir_entry, status.p_impl ->errors , status. p_impl -> bytes_removed , log);
8396 }
8497 }
85- status.p_impl ->dirs_removed += remove (dir_path, status.p_impl ->errors , log);
98+ status.p_impl ->dirs_removed += remove (dir_path, status.p_impl ->errors , status. p_impl -> bytes_removed , log);
8699 return status;
87100}
88101
89102
90103RepoCacheRemoveStatistics & RepoCacheRemoveStatistics::operator +=(const RepoCacheRemoveStatistics & rhs) noexcept {
91104 p_impl->files_removed += rhs.p_impl ->files_removed ;
92105 p_impl->dirs_removed += rhs.p_impl ->dirs_removed ;
106+ p_impl->bytes_removed += rhs.p_impl ->bytes_removed ;
93107 p_impl->errors += rhs.p_impl ->errors ;
94108 return *this ;
95109}
@@ -109,6 +123,9 @@ std::size_t RepoCacheRemoveStatistics::get_files_removed() {
109123std::size_t RepoCacheRemoveStatistics::get_dirs_removed () {
110124 return p_impl->dirs_removed ;
111125}
126+ std::size_t RepoCacheRemoveStatistics::get_bytes_removed () {
127+ return p_impl->bytes_removed ;
128+ }
112129std::size_t RepoCacheRemoveStatistics::get_errors () {
113130 return p_impl->errors ;
114131}
@@ -133,13 +150,17 @@ RepoCache::RemoveStatistics RepoCache::remove_metadata() {
133150 auto & log = *p_impl->base ->get_logger ();
134151 auto status = p_impl->remove_recursive (p_impl->cache_dir / CACHE_METADATA_DIR, log);
135152
136- status.p_impl ->files_removed += remove (p_impl->cache_dir / CACHE_MIRRORLIST_FILE, status.p_impl ->errors , log);
137- status.p_impl ->files_removed += remove (p_impl->cache_dir / CACHE_METALINK_FILE, status.p_impl ->errors , log);
153+ status.p_impl ->files_removed +=
154+ remove (p_impl->cache_dir / CACHE_MIRRORLIST_FILE, status.p_impl ->errors , status.p_impl ->bytes_removed , log);
155+ status.p_impl ->files_removed +=
156+ remove (p_impl->cache_dir / CACHE_METALINK_FILE, status.p_impl ->errors , status.p_impl ->bytes_removed , log);
138157 log.debug (
139- " Metadata removal from repository cache in path \" {}\" complete. Removed {} files, {} directories. {} errors" ,
158+ " Metadata removal from repository cache in path \" {}\" complete. "
159+ " Removed {} files, {} directories (total of {} bytes). {} errors" ,
140160 p_impl->cache_dir .native (),
141161 status.get_files_removed (),
142162 status.get_dirs_removed (),
163+ status.get_bytes_removed (),
143164 status.get_errors ());
144165 return status;
145166}
@@ -149,10 +170,12 @@ RepoCache::RemoveStatistics RepoCache::remove_packages() {
149170 auto & log = *p_impl->base ->get_logger ();
150171 auto status = p_impl->remove_recursive (p_impl->cache_dir / CACHE_PACKAGES_DIR, log);
151172 log.debug (
152- " Packages removal from repository cache in path \" {}\" complete. Removed {} files, {} directories. {} errors" ,
173+ " Packages removal from repository cache in path \" {}\" complete. "
174+ " Removed {} files, {} directories (total of {} bytes). {} errors" ,
153175 p_impl->cache_dir .native (),
154176 status.get_files_removed (),
155177 status.get_dirs_removed (),
178+ status.get_bytes_removed (),
156179 status.get_errors ());
157180 return status;
158181}
@@ -162,10 +185,12 @@ RepoCache::RemoveStatistics RepoCache::remove_solv_files() {
162185 auto & log = *p_impl->base ->get_logger ();
163186 auto status = p_impl->remove_recursive (p_impl->cache_dir / CACHE_SOLV_FILES_DIR, log);
164187 log.debug (
165- " Solv files removal from repository cache in path \" {}\" complete. Removed {} files, {} directories. {} errors" ,
188+ " Solv files removal from repository cache in path \" {}\" complete. "
189+ " Removed {} files, {} directories (total of {} bytes). {} errors" ,
166190 p_impl->cache_dir .native (),
167191 status.get_files_removed (),
168192 status.get_dirs_removed (),
193+ status.get_bytes_removed (),
169194 status.get_errors ());
170195 return status;
171196}
0 commit comments