Skip to content

Commit 8012a9e

Browse files
committed
feat: add sentry__path_get_mtime() (#1317)
#1317
1 parent 70f38a2 commit 8012a9e

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

src/path/sentry_path_unix.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,17 @@ sentry__path_get_size(const sentry_path_t *path)
267267
}
268268
}
269269

270+
time_t
271+
sentry__path_get_mtime(const sentry_path_t *path)
272+
{
273+
struct stat buf;
274+
if (stat(path->path, &buf) == 0) {
275+
return (time_t)buf.st_mtime;
276+
} else {
277+
return 0;
278+
}
279+
}
280+
270281
sentry_path_t *
271282
sentry__path_append_str(const sentry_path_t *base, const char *suffix)
272283
{

src/path/sentry_path_windows.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,17 @@ sentry__path_get_size(const sentry_path_t *path)
329329
}
330330
}
331331

332+
time_t
333+
sentry__path_get_mtime(const sentry_path_t *path)
334+
{
335+
struct _stat buf;
336+
if (_wstat(path->path, &buf) == 0) {
337+
return (time_t)buf.st_mtime;
338+
} else {
339+
return 0;
340+
}
341+
}
342+
332343
sentry_path_t *
333344
sentry__path_append_str(const sentry_path_t *base, const char *suffix)
334345
{

src/sentry_path.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "sentry_boot.h"
55

66
#include <stdio.h>
7+
#include <time.h>
78

89
#ifdef SENTRY_PLATFORM_WINDOWS
910
typedef wchar_t sentry_pathchar_t;
@@ -152,6 +153,12 @@ int sentry__path_touch(const sentry_path_t *path);
152153
*/
153154
size_t sentry__path_get_size(const sentry_path_t *path);
154155

156+
/**
157+
* This will return the last modification time of the file at `path`, or 0 on
158+
* failure.
159+
*/
160+
time_t sentry__path_get_mtime(const sentry_path_t *path);
161+
155162
/**
156163
* This will read all the content of `path` into a newly allocated buffer, and
157164
* write its size into `size_out`.

tests/unit/test_path.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
#include "sentry_string.h"
33
#include "sentry_testsupport.h"
44

5+
#ifdef SENTRY_PLATFORM_WINDOWS
6+
# include <windows.h>
7+
# define sleep_s(SECONDS) Sleep(SECONDS * 1000)
8+
#else
9+
# include <unistd.h>
10+
# define sleep_s(SECONDS) sleep(SECONDS)
11+
#endif
12+
513
SENTRY_TEST(recursive_paths)
614
{
715
sentry_path_t *base = sentry__path_from_str(SENTRY_TEST_PATH_PREFIX ".foo");
@@ -240,3 +248,30 @@ SENTRY_TEST(path_directory)
240248
sentry__path_free(path_1);
241249
sentry__path_free(path_2);
242250
}
251+
252+
SENTRY_TEST(path_mtime)
253+
{
254+
sentry_path_t *path
255+
= sentry__path_from_str(SENTRY_TEST_PATH_PREFIX "foo.txt");
256+
TEST_ASSERT(!!path);
257+
258+
TEST_CHECK(sentry__path_remove(path) == 0);
259+
TEST_CHECK(!sentry__path_is_file(path));
260+
TEST_CHECK(sentry__path_get_mtime(path) <= 0);
261+
262+
sentry__path_touch(path);
263+
TEST_CHECK(sentry__path_is_file(path));
264+
265+
int before = sentry__path_get_mtime(path);
266+
TEST_CHECK(before > 0);
267+
268+
sleep_s(1);
269+
270+
sentry__path_write_buffer(path, "after", 5);
271+
int after = sentry__path_get_mtime(path);
272+
TEST_CHECK(after > 0);
273+
TEST_CHECK(before < after);
274+
275+
sentry__path_remove(path);
276+
sentry__path_free(path);
277+
}

tests/unit/tests.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ XX(path_from_str_n_wo_null_termination)
9292
XX(path_from_str_null)
9393
XX(path_joining_unix)
9494
XX(path_joining_windows)
95+
XX(path_mtime)
9596
XX(path_relative_filename)
9697
XX(procmaps_parser)
9798
XX(propagation_context_init)

0 commit comments

Comments
 (0)