Commit bad5603d authored by grt's avatar grt Committed by Commit bot

Add File::Duplicate to duplicate a file handle.

BUG=462584
R=rvargas@chromium.org

Review URL: https://codereview.chromium.org/1017243002

Cr-Commit-Position: refs/heads/master@{#321389}
parent ad6b6cbf
......@@ -288,6 +288,13 @@ class BASE_EXPORT File {
// Unlock a file previously locked.
Error Unlock();
// Returns a new object referencing this file for use within the current
// process. Handling of FLAG_DELETE_ON_CLOSE varies by OS. On POSIX, the File
// object that was created or initialized with this flag will have unlinked
// the underlying file when it was created or opened. On Windows, the
// underlying file is deleted when the last handle to it is closed.
File Duplicate();
bool async() const { return async_; }
#if defined(OS_WIN)
......
......@@ -463,6 +463,20 @@ File::Error File::Unlock() {
return CallFctnlFlock(file_.get(), false);
}
File File::Duplicate() {
if (!IsValid())
return File();
PlatformFile other_fd = dup(GetPlatformFile());
if (other_fd == -1)
return File(OSErrorToFileError(errno));
File other(other_fd);
if (async())
other.async_ = true;
return other.Pass();
}
// Static.
File::Error File::OSErrorToFileError(int saved_errno) {
switch (saved_errno) {
......
......@@ -443,6 +443,49 @@ TEST(FileTest, Seek) {
EXPECT_EQ(kOffset, file.Seek(base::File::FROM_END, -kOffset));
}
TEST(FileTest, Duplicate) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
FilePath file_path = temp_dir.path().AppendASCII("file");
File file(file_path,(base::File::FLAG_CREATE |
base::File::FLAG_READ |
base::File::FLAG_WRITE));
ASSERT_TRUE(file.IsValid());
File file2(file.Duplicate());
ASSERT_TRUE(file2.IsValid());
// Write through one handle, close it, read through the other.
static const char kData[] = "now is a good time.";
static const int kDataLen = sizeof(kData) - 1;
ASSERT_EQ(0, file.Seek(base::File::FROM_CURRENT, 0));
ASSERT_EQ(0, file2.Seek(base::File::FROM_CURRENT, 0));
ASSERT_EQ(kDataLen, file.WriteAtCurrentPos(kData, kDataLen));
ASSERT_EQ(kDataLen, file.Seek(base::File::FROM_CURRENT, 0));
ASSERT_EQ(kDataLen, file2.Seek(base::File::FROM_CURRENT, 0));
file.Close();
char buf[kDataLen];
ASSERT_EQ(kDataLen, file2.Read(0, &buf[0], kDataLen));
ASSERT_EQ(std::string(kData, kDataLen), std::string(&buf[0], kDataLen));
}
TEST(FileTest, DuplicateDeleteOnClose) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
FilePath file_path = temp_dir.path().AppendASCII("file");
File file(file_path,(base::File::FLAG_CREATE |
base::File::FLAG_READ |
base::File::FLAG_WRITE |
base::File::FLAG_DELETE_ON_CLOSE));
ASSERT_TRUE(file.IsValid());
File file2(file.Duplicate());
ASSERT_TRUE(file2.IsValid());
file.Close();
file2.Close();
ASSERT_FALSE(base::PathExists(file_path));
}
#if defined(OS_WIN)
TEST(FileTest, GetInfoForDirectory) {
base::ScopedTempDir temp_dir;
......
......@@ -309,6 +309,28 @@ File::Error File::Unlock() {
return FILE_OK;
}
File File::Duplicate() {
if (!IsValid())
return File();
HANDLE other_handle = nullptr;
if (!::DuplicateHandle(GetCurrentProcess(), // hSourceProcessHandle
GetPlatformFile(),
GetCurrentProcess(), // hTargetProcessHandle
&other_handle,
0, // dwDesiredAccess ignored due to SAME_ACCESS
FALSE, // !bInheritHandle
DUPLICATE_SAME_ACCESS)) {
return File(OSErrorToFileError(GetLastError()));
}
File other(other_handle);
if (async())
other.async_ = true;
return other.Pass();
}
// Static.
File::Error File::OSErrorToFileError(DWORD last_error) {
switch (last_error) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment