Skip to content

Commit

Permalink
Merge pull request google#879 from grebe:portable_fs
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 513955740
  • Loading branch information
copybara-github committed Mar 4, 2023
2 parents d97f48f + 56ec5b1 commit d166136
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
20 changes: 11 additions & 9 deletions xls/common/file/filesystem_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -356,18 +356,17 @@ TEST(FilesystemTest, SetTextProtoFileFailsWhenRequiredFieldIsMissing) {
}

TEST(FilesystemTest, GetCurrentDirectoryReturnsCurrentDirectory) {
absl::StatusOr<TempDirectory> temp_dir = TempDirectory::Create();
XLS_ASSERT_OK(temp_dir);
absl::StatusOr<std::filesystem::path> initial_cwd = GetCurrentDirectory();
XLS_ASSERT_OK(initial_cwd);
ASSERT_EQ(0, chdir(temp_dir->path().c_str()));
XLS_ASSERT_OK_AND_ASSIGN(TempDirectory temp_dir, TempDirectory::Create());
XLS_ASSERT_OK_AND_ASSIGN(std::filesystem::path initial_cwd,
GetCurrentDirectory());
ASSERT_EQ(0, chdir(temp_dir.path().c_str()));

absl::StatusOr<std::filesystem::path> new_cwd = GetCurrentDirectory();
XLS_ASSERT_OK(new_cwd);
EXPECT_EQ(temp_dir->path(), *new_cwd);
XLS_ASSERT_OK_AND_ASSIGN(std::filesystem::path new_cwd,
GetCurrentDirectory());
EXPECT_TRUE(std::filesystem::equivalent(temp_dir.path(), new_cwd));

ASSERT_EQ(0,
chdir(initial_cwd->c_str())); // Change back to the original path.
chdir(initial_cwd.c_str())); // Change back to the original path.
}

TEST(FilesystemTest, GetDirectoryEntriesFailsWhenPathDoesNotExist) {
Expand Down Expand Up @@ -421,6 +420,9 @@ TEST(FilesystemTest, GetDirectoryEntriesGivesRelativePathsWhenPathIsRelative) {
}

TEST(FilesystemTest, GetRealPath) {
#ifndef __linux__
GTEST_SKIP() << "Skipping as procfs only available on linux";
#endif /* __linux__ */
XLS_ASSERT_OK_AND_ASSIGN(std::filesystem::path link_path,
GetRealPath("/proc/self/exe"));
XLS_ASSERT_OK_AND_ASSIGN(std::filesystem::path real_path,
Expand Down
29 changes: 27 additions & 2 deletions xls/common/file/get_runfile_path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,37 @@ using ::bazel::tools::cpp::runfiles::Runfiles;
static absl::Mutex mutex(absl::kConstInit);
static Runfiles* runfiles;

#ifdef __APPLE__
#include <mach-o/dyld.h>
#include <sys/syslimits.h>
#endif /* __APPLE__ */

absl::StatusOr<std::filesystem::path> GetSelfExecutablePath() {
#if __linux__
return GetRealPath("/proc/self/exe");
#elif __APPLE__
char path[PATH_MAX+1];
uint32_t size = PATH_MAX;
if (_NSGetExecutablePath(path, &size) == 0) {
return std::filesystem::path(path);
}
return absl::InvalidArgumentError("Self path could not fit into buffer");
#else
#error "Unknown platform"
#endif
}

absl::StatusOr<Runfiles*> GetRunfiles(
const std::string& argv0 = "/proc/self/exe") {
std::optional<std::string_view> argv0 = std::nullopt) {
absl::MutexLock lock(&mutex);
if (runfiles == nullptr) {
// Need to dereference the path, in case it's a link (as with the default).
XLS_ASSIGN_OR_RETURN(auto path, GetRealPath(argv0));
std::filesystem::path path;
if (argv0.has_value()) {
XLS_ASSIGN_OR_RETURN(path, GetRealPath(std::string(argv0.value())));
} else {
XLS_ASSIGN_OR_RETURN(path, GetSelfExecutablePath());
}

std::string error;
runfiles = Runfiles::Create(path.string(), &error);
Expand Down

0 comments on commit d166136

Please sign in to comment.