From 55b16026c5df166fec3d9370a5108292872c990f Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 26 Jan 2018 16:47:46 +0300 Subject: [PATCH] Optionally ignore EACCES in realpath. GitOrigin-RevId: 8043b753ec748b6ff21accb5efde782047e5cd50 --- td/telegram/Td.cpp | 2 +- td/telegram/files/FileManager.cpp | 2 +- tdnet/td/net/HttpReader.cpp | 2 +- tdutils/td/utils/port/path.cpp | 14 ++++++++++---- tdutils/td/utils/port/path.h | 2 +- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 9f7541e4..47cd367e 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -4374,7 +4374,7 @@ Status Td::fix_parameters(TdParameters ¶meters) { dir += TD_DIR_SLASH; } TRY_STATUS(mkpath(dir, 0750)); - TRY_RESULT(real_dir, realpath(dir)); + TRY_RESULT(real_dir, realpath(dir, true)); if (dir.back() != TD_DIR_SLASH) { dir += TD_DIR_SLASH; } diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index b0c7d06b..790cec14 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -466,7 +466,7 @@ Status FileManager::check_local_location(FullLocalFileLocation &location, int64 if (location.path_.empty()) { return Status::Error("File must have non-empty path"); } - TRY_RESULT(path, realpath(location.path_)); + TRY_RESULT(path, realpath(location.path_, true)); if (bad_paths_.count(path) != 0) { return Status::Error("Sending of internal database files is forbidden"); } diff --git a/tdnet/td/net/HttpReader.cpp b/tdnet/td/net/HttpReader.cpp index dc289eb3..a842a11f 100644 --- a/tdnet/td/net/HttpReader.cpp +++ b/tdnet/td/net/HttpReader.cpp @@ -719,7 +719,7 @@ Status HttpReader::open_temp_file(CSlice desired_file_name) { return Status::Error("Can't find temporary directory"); } - TRY_RESULT(dir, realpath(tmp_dir)); + TRY_RESULT(dir, realpath(tmp_dir, true)); CHECK(!dir.empty()); auto first_try = try_open_temp_file(dir, desired_file_name); diff --git a/tdutils/td/utils/port/path.cpp b/tdutils/td/utils/port/path.cpp index cf89fe67..c9b0ad23 100644 --- a/tdutils/td/utils/port/path.cpp +++ b/tdutils/td/utils/port/path.cpp @@ -81,13 +81,19 @@ Status rename(CSlice from, CSlice to) { return Status::OK(); } -Result realpath(CSlice slice) { +Result realpath(CSlice slice, bool ignore_access_denied) { char full_path[PATH_MAX + 1]; + string res; char *err = skip_eintr([&] { return ::realpath(slice.c_str(), full_path); }); if (err != full_path) { - return OS_ERROR(PSLICE() << "Realpath failed for \"" << slice << '"'); + if (ignore_access_denied && errno == EACCES) { + res = slice.str(); + } else { + return OS_ERROR(PSLICE() << "Realpath failed for \"" << slice << '"'); + } + } else { + res = full_path; } - string res = full_path; if (res.empty()) { return Status::Error("Empty path"); } @@ -225,7 +231,7 @@ Status rename(CSlice from, CSlice to) { return Status::OK(); } -Result realpath(CSlice slice) { +Result realpath(CSlice slice, bool /*ignore_access_denied*/) { wchar_t buf[MAX_PATH + 1]; TRY_RESULT(wslice, to_wstring(slice)); auto status = GetFullPathNameW(wslice.c_str(), MAX_PATH, buf, nullptr); diff --git a/tdutils/td/utils/port/path.h b/tdutils/td/utils/port/path.h index 8f145d78..47b7d3a3 100644 --- a/tdutils/td/utils/port/path.h +++ b/tdutils/td/utils/port/path.h @@ -38,7 +38,7 @@ namespace td { Status mkdir(CSlice dir, int32 mode = 0700) TD_WARN_UNUSED_RESULT; Status mkpath(CSlice path, int32 mode = 0700) TD_WARN_UNUSED_RESULT; Status rename(CSlice from, CSlice to) TD_WARN_UNUSED_RESULT; -Result realpath(CSlice slice) TD_WARN_UNUSED_RESULT; +Result realpath(CSlice slice, bool ignore_access_denied = false) TD_WARN_UNUSED_RESULT; Status chdir(CSlice dir) TD_WARN_UNUSED_RESULT; Status rmdir(CSlice dir) TD_WARN_UNUSED_RESULT; Status unlink(CSlice path) TD_WARN_UNUSED_RESULT;