1616#include <stdlib.h>
1717#include <string.h>
1818
19+ static char *
20+ read_metadata (
21+ const sentry_path_t * path , sentry_slice_t * first , sentry_slice_t * second )
22+ {
23+ size_t size = 0 ;
24+ char * contents_buf = sentry__path_read_to_buffer (path , & size );
25+ if (!contents_buf ) {
26+ return NULL ;
27+ }
28+
29+ sentry_slice_t contents
30+ = sentry__slice_trim ((sentry_slice_t ) { contents_buf , size });
31+ if (contents .len == 0 ) {
32+ return contents_buf ;
33+ }
34+
35+ size_t first_len = 0 ;
36+ while (first_len < contents .len
37+ && !isspace ((unsigned char )contents .ptr [first_len ])) {
38+ first_len ++ ;
39+ }
40+ * first = (sentry_slice_t ) { contents .ptr , first_len };
41+ * second = sentry__slice_trim ((sentry_slice_t ) {
42+ contents .ptr + first_len , contents .len - first_len });
43+
44+ return contents_buf ;
45+ }
46+
47+ static int
48+ write_metadata (const sentry_path_t * path , const char * first , size_t first_len ,
49+ const char * second , size_t second_len )
50+ {
51+ bool has_second = second != NULL ;
52+ size_t buf_len = first_len + (has_second ? 1 + second_len + 1 : 0 );
53+ char * buf = sentry_malloc (buf_len );
54+ if (!buf ) {
55+ return 1 ;
56+ }
57+
58+ memcpy (buf , first , first_len );
59+ if (has_second ) {
60+ buf [first_len ] = '\n' ;
61+ memcpy (buf + first_len + 1 , second , second_len );
62+ buf [first_len + 1 + second_len ] = '\n' ;
63+ }
64+
65+ int rv = sentry__path_write_buffer (path , buf , buf_len );
66+ sentry_free (buf );
67+
68+ return rv ;
69+ }
70+
1971sentry_run_t *
2072sentry__run_new (const sentry_path_t * database_path )
2173{
@@ -149,35 +201,24 @@ sentry__run_load_installation_id(sentry_run_t *run,
149201
150202 const size_t uuid_len = 36 ;
151203 char uuid_str [37 ] = { 0 };
152- size_t size = 0 ;
153- char * contents = sentry__path_read_to_buffer (id_path , & size );
154- if (contents && size >= uuid_len ) {
155- // expect: "<uuid>(\s+<public_key>)?"
156- sentry_slice_t tail = { contents + uuid_len , size - uuid_len };
157- sentry_slice_t key = sentry__slice_trim (tail );
158- if ((tail .len == 0 || isspace ((unsigned char )tail .ptr [0 ]))
159- && sentry__slice_eqs (key , public_key )) {
160- memcpy (uuid_str , contents , uuid_len );
161- uuid_str [uuid_len ] = '\0' ;
162- }
204+ sentry_slice_t first = { 0 };
205+ sentry_slice_t second = { 0 };
206+ char * contents = read_metadata (id_path , & first , & second );
207+ // expect: "<uuid>(\s+<public_key>)?"
208+ if (first .len == uuid_len && sentry__slice_eqs (second , public_key )) {
209+ memcpy (uuid_str , first .ptr , uuid_len );
210+ uuid_str [uuid_len ] = '\0' ;
163211 }
164212 sentry_free (contents );
165213
166214 if (uuid_str [0 ] == '\0' ) {
167215 sentry_uuid_t uuid = sentry_uuid_new_v4 ();
168216 sentry_uuid_as_string (& uuid , uuid_str );
169217
170- const size_t buf_len = uuid_len + 1 + key_len + 1 ;
171- char * buf = sentry_malloc (buf_len );
172- if (buf ) {
173- memcpy (buf , uuid_str , uuid_len );
174- buf [uuid_len ] = '\n' ;
175- memcpy (buf + uuid_len + 1 , public_key , key_len );
176- buf [uuid_len + 1 + key_len ] = '\n' ;
177- if (sentry__path_write_buffer (id_path , buf , buf_len ) != 0 ) {
178- SENTRY_WARN ("failed to persist installation ID" );
179- }
180- sentry_free (buf );
218+ int rv
219+ = write_metadata (id_path , uuid_str , uuid_len , public_key , key_len );
220+ if (rv ) {
221+ SENTRY_WARN ("failed to persist installation ID" );
181222 }
182223 }
183224
@@ -974,7 +1015,8 @@ sentry__cleanup_cache(const sentry_options_t *options)
9741015static const char * g_last_crash_filename = "last_crash" ;
9751016
9761017bool
977- sentry__write_crash_marker (const sentry_options_t * options )
1018+ sentry__write_crash_marker (
1019+ const sentry_options_t * options , const sentry_uuid_t * event_id )
9781020{
9791021 char * iso_time = sentry__usec_time_to_iso8601 (sentry__usec_time ());
9801022 if (!iso_time ) {
@@ -989,7 +1031,17 @@ sentry__write_crash_marker(const sentry_options_t *options)
9891031 }
9901032
9911033 size_t iso_time_len = strlen (iso_time );
992- int rv = sentry__path_write_buffer (marker_path , iso_time , iso_time_len );
1034+ char event_id_str [37 ];
1035+ const char * event_id_value = NULL ;
1036+ size_t event_id_len = 0 ;
1037+ if (event_id && !sentry_uuid_is_nil (event_id )) {
1038+ sentry_uuid_as_string (event_id , event_id_str );
1039+ event_id_value = event_id_str ;
1040+ event_id_len = strlen (event_id_str );
1041+ }
1042+
1043+ int rv = write_metadata (
1044+ marker_path , iso_time , iso_time_len , event_id_value , event_id_len );
9931045 sentry_free (iso_time );
9941046 sentry__path_free (marker_path );
9951047
@@ -999,6 +1051,41 @@ sentry__write_crash_marker(const sentry_options_t *options)
9991051 return !rv ;
10001052}
10011053
1054+ int
1055+ sentry__read_crash_marker (
1056+ const sentry_options_t * options , sentry_last_crash_t * crash )
1057+ {
1058+ if (crash ) {
1059+ memset (crash , 0 , sizeof (* crash ));
1060+ }
1061+
1062+ sentry_path_t * marker_path
1063+ = sentry__path_join_str (options -> database_path , g_last_crash_filename );
1064+ if (!marker_path ) {
1065+ return 0 ;
1066+ }
1067+
1068+ sentry_slice_t first = { 0 };
1069+ sentry_slice_t second = { 0 };
1070+ char * contents = read_metadata (marker_path , & first , & second );
1071+ int crashed = contents ? 1 : 0 ;
1072+ if (crash && first .len > 0 ) {
1073+ crash -> timestamp = sentry__iso8601_to_usec (first .ptr , first .len );
1074+ }
1075+
1076+ if (crash && (second .len == 32 || second .len == 36 )) {
1077+ sentry_uuid_t parsed
1078+ = sentry_uuid_from_string_n (second .ptr , second .len );
1079+ if (!sentry_uuid_is_nil (& parsed )) {
1080+ crash -> event_id = parsed ;
1081+ }
1082+ }
1083+
1084+ sentry_free (contents );
1085+ sentry__path_free (marker_path );
1086+ return crashed ;
1087+ }
1088+
10021089bool
10031090sentry__has_crash_marker (const sentry_options_t * options )
10041091{
0 commit comments