diff options
author | Colin Walters <walters@verbum.org> | 2023-04-14 14:44:38 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-14 14:44:38 -0400 |
commit | 90f7c7bd9f98ebcdff81e0363a4d0117b876d5c3 (patch) | |
tree | 8089f5e13e9295171fe7ceb0a7bf33f525822e8c /src/libotutil/ot-fs-utils.c | |
parent | e509b24a01a949cab6cda18c39263d6ce88d62fe (diff) | |
parent | 462d413d2e8ef4e8196ac99c142dd165086f7b69 (diff) | |
download | ostree-90f7c7bd9f98ebcdff81e0363a4d0117b876d5c3.tar.gz |
Merge pull request #2848 from jlebon/pr/calculate-and-cleanup-prep
Prep patches for automatic early prune
Diffstat (limited to 'src/libotutil/ot-fs-utils.c')
-rw-r--r-- | src/libotutil/ot-fs-utils.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/libotutil/ot-fs-utils.c b/src/libotutil/ot-fs-utils.c index 64ebc966..935b1bbc 100644 --- a/src/libotutil/ot-fs-utils.c +++ b/src/libotutil/ot-fs-utils.c @@ -245,3 +245,47 @@ ot_parse_file_by_line (const char *path, return TRUE; } + +/* Calculate the size of the files contained in a directory. Symlinks are not + * followed. */ +gboolean +ot_get_dir_size (int dfd, + const char *path, + guint64 *out_size, + GCancellable *cancellable, + GError **error) +{ + g_auto(GLnxDirFdIterator) dfd_iter = { 0, }; + if (!glnx_dirfd_iterator_init_at (dfd, path, FALSE, &dfd_iter, error)) + return FALSE; + + *out_size = 0; + while (TRUE) + { + struct dirent *dent; + if (!glnx_dirfd_iterator_next_dent_ensure_dtype (&dfd_iter, &dent, cancellable, error)) + return FALSE; + + if (dent == NULL) + break; + + if (dent->d_type == DT_REG) + { + struct stat stbuf; + if (!glnx_fstatat (dfd_iter.fd, dent->d_name, &stbuf, AT_SYMLINK_NOFOLLOW, error)) + return FALSE; + + *out_size += stbuf.st_size; + } + else if (dent->d_type == DT_DIR) + { + guint64 subdir_size; + if (!ot_get_dir_size (dfd_iter.fd, dent->d_name, &subdir_size, cancellable, error)) + return FALSE; + + *out_size += subdir_size; + } + } + + return TRUE; +} |