off_t BackupView::DirectorySize(BPath* path, bool recurse) { off_t bytes = 0; if (!path) { printf("Error: Invalid path passed!\n"); return bytes; } //printf("%s: %s\n", __func__, path->Path()); BDirectory dir(path->Path()); int32 entries = dir.CountEntries(); //printf("%s: items: %" B_PRId32 "\n", __func__, entries); dir.Rewind(); for (int32 i = 0; i < entries; i++) { BEntry entry; dir.GetNextEntry(&entry); struct stat st; entry.GetStat(&st); if (S_ISDIR(st.st_mode) && recurse == true) { BPath nextPath; entry.GetPath(&nextPath); bytes += DirectorySize(&nextPath); } else { bytes += st.st_size; } } return bytes; }
void BackupView::RefreshSizes() { off_t size; for (uint32 i = 0; i < LOCATION_COUNT; i++) { BPath directory; find_directory(gLocationMap[i].location, &directory); size = DirectorySize(&directory, gLocationMap[i].recurse); BackupListItem* item = (BackupListItem*)fBackupList->ItemAt(i); if (!item) continue; item->SetBytes(size); } RefreshTotal(); }
util::Error DirectorySize(const RealPath& path, int depth, long long& kBytes, bool ignoreHidden) { kBytes = 0; if (depth < 0) return util::Error::Failure(EINVAL); if (depth == 0) return util::Error::Success(); try { for (auto& entry : util::path::DirContainer(path.ToString())) { if (ignoreHidden && entry[0] == '.') continue; try { auto entryPath = path / entry; util::path::Status status(entryPath.ToString()); if (status.IsDirectory()) { if (!status.IsSymLink()) { long long subKBytes; if (DirectorySize(entryPath, depth - 1, subKBytes, ignoreHidden)) kBytes += subKBytes; } } else if (status.IsRegularFile()) { kBytes += status.Size() / 1024; } } catch (const util::SystemError& e) { } } } catch (const util::SystemError& e) { return util::Error::Failure(e.Errno()); } return util::Error::Success(); }