Clarify file sharing flags in FML filesystem APIs on Windows (#38164)
Use shared mode when requesting read access and exclusive mode for write access
diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter
index ee77e26..8e517e6 100644
--- a/ci/licenses_golden/licenses_flutter
+++ b/ci/licenses_golden/licenses_flutter
@@ -1020,6 +1020,7 @@
FILE: ../../../flutter/fml/platform/win/errors_win.cc
FILE: ../../../flutter/fml/platform/win/errors_win.h
FILE: ../../../flutter/fml/platform/win/file_win.cc
+FILE: ../../../flutter/fml/platform/win/file_win_unittests.cc
FILE: ../../../flutter/fml/platform/win/mapping_win.cc
FILE: ../../../flutter/fml/platform/win/message_loop_win.cc
FILE: ../../../flutter/fml/platform/win/message_loop_win.h
diff --git a/fml/BUILD.gn b/fml/BUILD.gn
index 0b25ea5..1656b11 100644
--- a/fml/BUILD.gn
+++ b/fml/BUILD.gn
@@ -351,7 +351,10 @@
}
if (is_win) {
- sources += [ "platform/win/wstring_conversion_unittests.cc" ]
+ sources += [
+ "platform/win/file_win_unittests.cc",
+ "platform/win/wstring_conversion_unittests.cc",
+ ]
}
deps = [
diff --git a/fml/platform/win/file_win.cc b/fml/platform/win/file_win.cc
index 5034b21..674a343 100644
--- a/fml/platform/win/file_win.cc
+++ b/fml/platform/win/file_win.cc
@@ -85,11 +85,11 @@
static DWORD GetShareFlags(FilePermission permission) {
switch (permission) {
case FilePermission::kRead:
- return FILE_SHARE_READ;
+ return FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
case FilePermission::kWrite:
- return FILE_SHARE_WRITE;
+ return 0;
case FilePermission::kReadWrite:
- return FILE_SHARE_READ | FILE_SHARE_WRITE;
+ return 0;
}
return FILE_SHARE_READ;
}
diff --git a/fml/platform/win/file_win_unittests.cc b/fml/platform/win/file_win_unittests.cc
new file mode 100644
index 0000000..f4240b9
--- /dev/null
+++ b/fml/platform/win/file_win_unittests.cc
@@ -0,0 +1,32 @@
+// Copyright 2013 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "flutter/fml/file.h"
+
+#include <Fileapi.h>
+
+#include "flutter/fml/platform/win/wstring_conversion.h"
+#include "gtest/gtest.h"
+
+namespace fml {
+namespace testing {
+
+TEST(FileWin, OpenDirectoryShare) {
+ fml::ScopedTemporaryDirectory temp_dir;
+ auto temp_path = Utf8ToWideString({temp_dir.path()});
+ fml::UniqueFD handle(
+ CreateFile(temp_path.c_str(), GENERIC_WRITE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, nullptr));
+ ASSERT_TRUE(handle.is_valid());
+
+ // Check that fml::OpenDirectory(FilePermission::kRead) works with a
+ // directory that has also been opened for writing.
+ auto dir = fml::OpenDirectory(temp_dir.path().c_str(), false,
+ fml::FilePermission::kRead);
+ ASSERT_TRUE(dir.is_valid());
+}
+
+} // namespace testing
+} // namespace fml
\ No newline at end of file