Skip to content

Commit

Permalink
Wrap rename and renameat
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 committed Jun 15, 2023
1 parent d2eaa6e commit 29cea57
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion native/src/base/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ LOCAL_MODULE := libcompat
LOCAL_SRC_FILES := compat/compat.cpp
# Fix static variables' ctor/dtor when using LTO
# See: https://github.com/android/ndk/issues/1461
LOCAL_EXPORT_LDFLAGS := -static -T src/lto_fix.lds
LOCAL_EXPORT_LDFLAGS := -static -T src/lto_fix.lds -Wl,--wrap=rename -Wl,--wrap=renameat
include $(BUILD_STATIC_LIBRARY)
35 changes: 35 additions & 0 deletions native/src/base/compat/compat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ int ftruncate64(int fd, off64_t length) {
}
#endif

int __real_rename(const char *old_path, const char *new_path);

int __wrap_rename(const char *old_path, const char *new_path) {
return __real_rename(old_path, new_path);
}

int __real_renameat(int old_dir_fd, const char *old_path, int new_dir_fd, const char *new_path);

int __wrap_renameat(int old_dir_fd, const char *old_path, int new_dir_fd, const char *new_path) {
return __real_renameat(old_dir_fd, old_path, new_dir_fd, new_path);
}

[[gnu::weak]]
void android_set_abort_message(const char *) {}

Expand All @@ -112,4 +124,27 @@ extern FILE __sF[];
[[gnu::weak]] FILE* stderr = &__sF[2];

} // extern "C"
#else

#include <linux/fcntl.h>
#include <unistd.h>
#include <syscall.h>

extern "C" {

[[maybe_unused]]
int __wrap_renameat(int old_dir_fd, const char *old_path, int new_dir_fd, const char *new_path) {
long out = syscall(__NR_renameat, old_dir_fd, old_path, new_dir_fd, new_path);
if (out == -1) {
out = syscall(__NR_renameat2, old_dir_fd, old_path, new_dir_fd, new_path, 0);
}
return static_cast<int>(out);
}

[[maybe_unused]]
int __wrap_rename(const char *old_path, const char *new_path) {
return __wrap_renameat(AT_FDCWD, old_path, AT_FDCWD, new_path);
}
}

#endif

0 comments on commit 29cea57

Please sign in to comment.