Bug 1440207 - Part 2: Add a cross-platform DuplicateFileHandle method to MFBT, r=glandium

This is convenient in cross-platform code which needs to work with
`UniqueFileHandle` objects. `dup` is not supported by wasi, so the method isn't
available there.

Differential Revision: https://phabricator.services.mozilla.com/D221370
This commit is contained in:
Nika Layzell 2024-10-01 22:21:47 +00:00
Родитель 806a93150c
Коммит 6027e4657c
2 изменённых файлов: 29 добавлений и 0 удалений

Просмотреть файл

@ -32,4 +32,25 @@ void FileHandleDeleter::operator()(FileHandleHelper aHelper) {
}
} // namespace detail
#ifndef __wasm__
UniqueFileHandle DuplicateFileHandle(detail::FileHandleType aFile) {
# ifdef XP_WIN
if (aFile != INVALID_HANDLE_VALUE && aFile != NULL) {
HANDLE handle;
HANDLE currentProcess = ::GetCurrentProcess();
if (::DuplicateHandle(currentProcess, aFile, currentProcess, &handle, 0,
false, DUPLICATE_SAME_ACCESS)) {
return UniqueFileHandle{handle};
}
}
# else
if (aFile != -1) {
return UniqueFileHandle{dup(aFile)};
}
# endif
return nullptr;
}
#endif
} // namespace mozilla

Просмотреть файл

@ -220,6 +220,14 @@ using UniqueFreePtr = UniquePtr<T, detail::FreePolicy<T>>;
using UniqueFileHandle =
UniquePtr<detail::FileHandleType, detail::FileHandleDeleter>;
#ifndef __wasm__
// WASI does not have `dup`
MFBT_API UniqueFileHandle DuplicateFileHandle(detail::FileHandleType aFile);
inline UniqueFileHandle DuplicateFileHandle(const UniqueFileHandle& aFile) {
return DuplicateFileHandle(aFile.get());
}
#endif
#if defined(XP_DARWIN) && !defined(RUST_BINDGEN)
// A RAII class for a Mach port that names a send right.
using UniqueMachSendRight =