llvm.org GIT mirror llvm / 0e5ac4b
[Support] Remove getPathFromOpenFD, it was unused Summary: It was added to support clang warnings about includes with case mismatches, but it ended up not being necessary. Reviewers: twoh, rafael Subscribers: hiraditya, llvm-commits Differential Revision: https://reviews.llvm.org/D36328 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@310078 91177308-0d34-0410-b5e6-96231b3b80d8 Reid Kleckner 2 years ago
4 changed file(s) with 0 addition(s) and 179 deletion(s). Raw diff Collapse all Expand all
684684 std::error_code createUniqueDirectory(const Twine &Prefix,
685685 SmallVectorImpl &ResultPath);
686686
687 /// @brief Fetch a path to an open file, as specified by a file descriptor
688 ///
689 /// @param FD File descriptor to a currently open file
690 /// @param ResultPath The buffer into which to write the path
691 std::error_code getPathFromOpenFD(int FD, SmallVectorImpl &ResultPath);
692
693687 enum OpenFlags : unsigned {
694688 F_None = 0,
695689
806806 return std::error_code();
807807 }
808808
809 std::error_code getPathFromOpenFD(int FD, SmallVectorImpl &ResultPath) {
810 if (FD < 0)
811 return make_error_code(errc::bad_file_descriptor);
812
813 #if defined(F_GETPATH)
814 // When F_GETPATH is availble, it is the quickest way to get
815 // the path from a file descriptor.
816 ResultPath.reserve(MAXPATHLEN);
817 if (::fcntl(FD, F_GETPATH, ResultPath.begin()) == -1)
818 return std::error_code(errno, std::generic_category());
819
820 ResultPath.set_size(strlen(ResultPath.begin()));
821 #else
822 // If we have a /proc filesystem mounted, we can quickly establish the
823 // real name of the file with readlink. Otherwise, we don't know how to
824 // get the filename from a file descriptor. Give up.
825 if (!fs::hasProcSelfFD())
826 return make_error_code(errc::function_not_supported);
827
828 ResultPath.reserve(PATH_MAX);
829 char ProcPath[64];
830 snprintf(ProcPath, sizeof(ProcPath), "/proc/self/fd/%d", FD);
831 ssize_t CharCount = ::readlink(ProcPath, ResultPath.begin(), ResultPath.capacity());
832 if (CharCount < 0)
833 return std::error_code(errno, std::generic_category());
834
835 // Was the filename truncated?
836 if (static_cast(CharCount) == ResultPath.capacity()) {
837 // Use lstat to get the size of the filename
838 struct stat sb;
839 if (::lstat(ProcPath, &sb) < 0)
840 return std::error_code(errno, std::generic_category());
841
842 ResultPath.reserve(sb.st_size + 1);
843 CharCount = ::readlink(ProcPath, ResultPath.begin(), ResultPath.capacity());
844 if (CharCount < 0)
845 return std::error_code(errno, std::generic_category());
846
847 // Test for race condition: did the link size change?
848 if (CharCount > sb.st_size)
849 return std::error_code(ENAMETOOLONG, std::generic_category());
850 }
851 ResultPath.set_size(static_cast(CharCount));
852 #endif
853 return std::error_code();
854 }
855
856809 template
857810 static std::error_code remove_directories_impl(const T &Entry,
858811 bool IgnoreErrors) {
958958 return std::error_code();
959959 }
960960
961 std::error_code getPathFromOpenFD(int FD, SmallVectorImpl &ResultPath) {
962 HANDLE FileHandle = reinterpret_cast(::_get_osfhandle(FD));
963 if (FileHandle == INVALID_HANDLE_VALUE)
964 return make_error_code(errc::bad_file_descriptor);
965
966 DWORD CharCount;
967 SmallVector TempPath;
968 do {
969 CharCount = ::GetFinalPathNameByHandleW(FileHandle, TempPath.begin(),
970 TempPath.capacity(),
971 FILE_NAME_NORMALIZED);
972 if (CharCount < TempPath.capacity())
973 break;
974
975 // Reserve sufficient space for the path as well as the null character. Even
976 // though the API does not document that it is required, if we reserve just
977 // CharCount space, the function call will not store the resulting path and
978 // still report success.
979 TempPath.reserve(CharCount + 1);
980 } while (true);
981
982 if (CharCount == 0)
983 return mapWindowsError(::GetLastError());
984
985 TempPath.set_size(CharCount);
986
987 // On earlier Windows releases, the character count includes the terminating
988 // null.
989 if (TempPath.back() == L'\0') {
990 --CharCount;
991 TempPath.pop_back();
992 }
993
994 return windows::UTF16ToUTF8(TempPath.data(), CharCount, ResultPath);
995 }
996
997961 std::error_code remove_directories(const Twine &path, bool IgnoreErrors) {
998962 // Convert to utf-16.
999963 SmallVector Path16;
11441144 EXPECT_EQ(Path, "/foo");
11451145 }
11461146
1147 TEST_F(FileSystemTest, PathFromFD) {
1148 // Create a temp file.
1149 int FileDescriptor;
1150 SmallString<64> TempPath;
1151 ASSERT_NO_ERROR(
1152 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
1153 FileRemover Cleanup(TempPath);
1154
1155 // Make sure it exists.
1156 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1157
1158 // Try to get the path from the file descriptor
1159 SmallString<64> ResultPath;
1160 std::error_code ErrorCode =
1161 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1162
1163 // If we succeeded, check that the paths are the same (modulo case):
1164 if (!ErrorCode) {
1165 // The paths returned by createTemporaryFile and getPathFromOpenFD
1166 // should reference the same file on disk.
1167 fs::UniqueID D1, D2;
1168 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1169 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1170 ASSERT_EQ(D1, D2);
1171 }
1172
1173 ::close(FileDescriptor);
1174 }
1175
1176 TEST_F(FileSystemTest, PathFromFDWin32) {
1177 // Create a temp file.
1178 int FileDescriptor;
1179 SmallString<64> TempPath;
1180 ASSERT_NO_ERROR(
1181 fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
1182 FileRemover Cleanup(TempPath);
1183
1184 // Make sure it exists.
1185 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1186
1187 SmallVector ResultPath;
1188 std::error_code ErrorCode =
1189 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1190
1191 if (!ErrorCode) {
1192 // Now that we know how much space is required for the path, create a path
1193 // buffer with exactly enough space (sans null terminator, which should not
1194 // be present), and call getPathFromOpenFD again to ensure that the API
1195 // properly handles exactly-sized buffers.
1196 SmallVector ExactSizedPath(ResultPath.size());
1197 ErrorCode = fs::getPathFromOpenFD(FileDescriptor, ExactSizedPath);
1198 ResultPath = ExactSizedPath;
1199 }
1200
1201 if (!ErrorCode) {
1202 fs::UniqueID D1, D2;
1203 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1204 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1205 ASSERT_EQ(D1, D2);
1206 }
1207 ::close(FileDescriptor);
1208 }
1209
1210 TEST_F(FileSystemTest, PathFromFDUnicode) {
1211 // Create a temp file.
1212 int FileDescriptor;
1213 SmallString<64> TempPath;
1214
1215 // Test Unicode: "/(pi)r^2.aleth.0"
1216 ASSERT_NO_ERROR(
1217 fs::createTemporaryFile("\xCF\x80r\xC2\xB2",
1218 "\xE2\x84\xB5.0", FileDescriptor, TempPath));
1219 FileRemover Cleanup(TempPath);
1220
1221 // Make sure it exists.
1222 ASSERT_TRUE(sys::fs::exists(Twine(TempPath)));
1223
1224 SmallVector ResultPath;
1225 std::error_code ErrorCode =
1226 fs::getPathFromOpenFD(FileDescriptor, ResultPath);
1227
1228 if (!ErrorCode) {
1229 fs::UniqueID D1, D2;
1230 ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath), D1));
1231 ASSERT_NO_ERROR(fs::getUniqueID(Twine(ResultPath), D2));
1232 ASSERT_EQ(D1, D2);
1233 }
1234 ::close(FileDescriptor);
1235 }
1236
12371147 TEST_F(FileSystemTest, OpenFileForRead) {
12381148 // Create a temp file.
12391149 int FileDescriptor;