Skip to content

Commit 6f6c6ef

Browse files
jpnurmiclaude
andcommitted
feat(path): add sentry__path_get_dir_size helper
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9b1c1be commit 6f6c6ef

4 files changed

Lines changed: 50 additions & 0 deletions

File tree

src/path/sentry_path.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ sentry__path_remove_all(const sentry_path_t *path)
7979
return sentry__path_remove(path);
8080
}
8181

82+
size_t
83+
sentry__path_get_dir_size(const sentry_path_t *path)
84+
{
85+
size_t total = 0;
86+
sentry_pathiter_t *iter = sentry__path_iter_directory(path);
87+
const sentry_path_t *entry;
88+
while (iter && (entry = sentry__pathiter_next(iter)) != NULL) {
89+
total += sentry__path_get_size(entry);
90+
}
91+
sentry__pathiter_free(iter);
92+
return total;
93+
}
94+
8295
sentry_filelock_t *
8396
sentry__filelock_new(sentry_path_t *path)
8497
{

src/sentry_path.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ int sentry__path_touch(const sentry_path_t *path);
183183
*/
184184
size_t sentry__path_get_size(const sentry_path_t *path);
185185

186+
/**
187+
* This will return the total size of all files in the directory at `path`,
188+
* or 0 if the directory does not exist or is empty.
189+
*/
190+
size_t sentry__path_get_dir_size(const sentry_path_t *path);
191+
186192
/**
187193
* This will return the last modification time of the file at `path`, or 0 on
188194
* failure.

tests/unit/test_path.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,36 @@ SENTRY_TEST(path_rename)
340340
sentry__path_free(src);
341341
}
342342

343+
SENTRY_TEST(path_dir_size)
344+
{
345+
sentry_path_t *dir
346+
= sentry__path_from_str(SENTRY_TEST_PATH_PREFIX ".dir-size");
347+
TEST_ASSERT(!!dir);
348+
349+
sentry__path_remove_all(dir);
350+
351+
TEST_CHECK(sentry__path_get_dir_size(dir) == 0);
352+
353+
sentry__path_create_dir_all(dir);
354+
355+
sentry_path_t *file_a = sentry__path_join_str(dir, "a.txt");
356+
TEST_ASSERT(!!file_a);
357+
sentry__path_write_buffer(file_a, "hello", 5);
358+
359+
sentry_path_t *file_b = sentry__path_join_str(dir, "b.txt");
360+
TEST_ASSERT(!!file_b);
361+
sentry__path_write_buffer(file_b, "world!", 6);
362+
363+
TEST_CHECK(sentry__path_get_dir_size(dir) == 11);
364+
365+
TEST_CHECK(sentry__path_get_dir_size(file_a) == 0);
366+
367+
sentry__path_remove_all(dir);
368+
sentry__path_free(file_b);
369+
sentry__path_free(file_a);
370+
sentry__path_free(dir);
371+
}
372+
343373
SENTRY_TEST(path_copy)
344374
{
345375
#if defined(SENTRY_PLATFORM_NX)

tests/unit/tests.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ XX(page_allocator)
157157
XX(path_basics)
158158
XX(path_copy)
159159
XX(path_current_exe)
160+
XX(path_dir_size)
160161
XX(path_directory)
161162
XX(path_from_str_n_wo_null_termination)
162163
XX(path_from_str_null)

0 commit comments

Comments
 (0)