Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/path/sentry_path_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,14 @@ sentry__path_remove(const sentry_path_t *path)
return 1;
}

int
sentry__path_rename(const sentry_path_t *src, const sentry_path_t *dst)
{
int status;
EINTR_RETRY(rename(src->path, dst->path), &status);
return status == 0 ? 0 : 1;
}

int
sentry__path_create_dir_all(const sentry_path_t *path)
{
Expand Down
12 changes: 12 additions & 0 deletions src/path/sentry_path_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,18 @@ sentry__path_remove(const sentry_path_t *path)
return removal_success ? 0 : !is_last_error_path_not_found();
}

int
sentry__path_rename(const sentry_path_t *src, const sentry_path_t *dst)
{
wchar_t *src_w = src->path_w;
wchar_t *dst_w = dst->path_w;
if (!src_w || !dst_w) {
return 1;
}
// MOVEFILE_REPLACE_EXISTING allows overwriting the destination if it exists
return MoveFileExW(src_w, dst_w, MOVEFILE_REPLACE_EXISTING) ? 0 : 1;
}

int
sentry__path_create_dir_all(const sentry_path_t *path)
{
Expand Down
7 changes: 7 additions & 0 deletions src/sentry_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ int sentry__path_remove(const sentry_path_t *path);
*/
int sentry__path_remove_all(const sentry_path_t *path);

/**
* Rename/move the file or directory from `src` to `dst`.
* This will overwrite `dst` if it already exists.
* Returns 0 on success.
*/
int sentry__path_rename(const sentry_path_t *src, const sentry_path_t *dst);

/**
* This will create the directory referred to by `path`, and any non-existing
* parent directory.
Expand Down
57 changes: 57 additions & 0 deletions tests/unit/test_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,60 @@ SENTRY_TEST(path_mtime)
sentry__path_remove(path);
sentry__path_free(path);
}

SENTRY_TEST(path_rename)
{
sentry_path_t *src
= sentry__path_from_str(SENTRY_TEST_PATH_PREFIX ".rename-src");
TEST_ASSERT(!!src);
sentry_path_t *dst
= sentry__path_from_str(SENTRY_TEST_PATH_PREFIX ".rename-dst");
TEST_ASSERT(!!dst);

// cleanup
sentry__path_remove_all(src);
sentry__path_remove_all(dst);

// rename file
sentry__path_touch(src);
TEST_CHECK(sentry__path_is_file(src));
TEST_CHECK(sentry__path_rename(src, dst) == 0);
TEST_CHECK(!sentry__path_is_file(src));
TEST_CHECK(sentry__path_is_file(dst));
sentry__path_remove(dst);

// rename with content preserved
sentry__path_write_buffer(src, "hello", 5);
TEST_CHECK(sentry__path_rename(src, dst) == 0);
size_t len = 0;
char *buf = sentry__path_read_to_buffer(dst, &len);
TEST_CHECK(len == 5);
TEST_CHECK(memcmp(buf, "hello", 5) == 0);
sentry_free(buf);
sentry__path_remove(dst);

// overwrite existing dst
sentry__path_write_buffer(src, "src-data", 8);
sentry__path_write_buffer(dst, "dst-data", 8);
TEST_CHECK(sentry__path_rename(src, dst) == 0);
TEST_CHECK(!sentry__path_is_file(src));
buf = sentry__path_read_to_buffer(dst, &len);
TEST_CHECK(len == 8);
TEST_CHECK(memcmp(buf, "src-data", 8) == 0);
sentry_free(buf);
sentry__path_remove(dst);

// rename nonexistent src
TEST_CHECK(sentry__path_rename(src, dst) != 0);

// rename directory
sentry__path_create_dir_all(src);
TEST_CHECK(sentry__path_is_dir(src));
TEST_CHECK(sentry__path_rename(src, dst) == 0);
TEST_CHECK(!sentry__path_is_dir(src));
TEST_CHECK(sentry__path_is_dir(dst));
sentry__path_remove_all(dst);

sentry__path_free(dst);
sentry__path_free(src);
}
1 change: 1 addition & 0 deletions tests/unit/tests.inc
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ XX(path_joining_unix)
XX(path_joining_windows)
XX(path_mtime)
XX(path_relative_filename)
XX(path_rename)
XX(process_invalid)
XX(process_spawn)
XX(procmaps_parser)
Expand Down
Loading