Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 9872cc7

Browse files
Clarify file sharing flags in FML filesystem APIs on Windows (#38164)
Use shared mode when requesting read access and exclusive mode for write access
1 parent f863b19 commit 9872cc7

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,7 @@ FILE: ../../../flutter/fml/platform/win/command_line_win.cc
10201020
FILE: ../../../flutter/fml/platform/win/errors_win.cc
10211021
FILE: ../../../flutter/fml/platform/win/errors_win.h
10221022
FILE: ../../../flutter/fml/platform/win/file_win.cc
1023+
FILE: ../../../flutter/fml/platform/win/file_win_unittests.cc
10231024
FILE: ../../../flutter/fml/platform/win/mapping_win.cc
10241025
FILE: ../../../flutter/fml/platform/win/message_loop_win.cc
10251026
FILE: ../../../flutter/fml/platform/win/message_loop_win.h

fml/BUILD.gn

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,10 @@ if (enable_unittests) {
351351
}
352352

353353
if (is_win) {
354-
sources += [ "platform/win/wstring_conversion_unittests.cc" ]
354+
sources += [
355+
"platform/win/file_win_unittests.cc",
356+
"platform/win/wstring_conversion_unittests.cc",
357+
]
355358
}
356359

357360
deps = [

fml/platform/win/file_win.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ static DWORD GetDesiredAccessFlags(FilePermission permission) {
8585
static DWORD GetShareFlags(FilePermission permission) {
8686
switch (permission) {
8787
case FilePermission::kRead:
88-
return FILE_SHARE_READ;
88+
return FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
8989
case FilePermission::kWrite:
90-
return FILE_SHARE_WRITE;
90+
return 0;
9191
case FilePermission::kReadWrite:
92-
return FILE_SHARE_READ | FILE_SHARE_WRITE;
92+
return 0;
9393
}
9494
return FILE_SHARE_READ;
9595
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/fml/file.h"
6+
7+
#include <Fileapi.h>
8+
9+
#include "flutter/fml/platform/win/wstring_conversion.h"
10+
#include "gtest/gtest.h"
11+
12+
namespace fml {
13+
namespace testing {
14+
15+
TEST(FileWin, OpenDirectoryShare) {
16+
fml::ScopedTemporaryDirectory temp_dir;
17+
auto temp_path = Utf8ToWideString({temp_dir.path()});
18+
fml::UniqueFD handle(
19+
CreateFile(temp_path.c_str(), GENERIC_WRITE,
20+
FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING,
21+
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS, nullptr));
22+
ASSERT_TRUE(handle.is_valid());
23+
24+
// Check that fml::OpenDirectory(FilePermission::kRead) works with a
25+
// directory that has also been opened for writing.
26+
auto dir = fml::OpenDirectory(temp_dir.path().c_str(), false,
27+
fml::FilePermission::kRead);
28+
ASSERT_TRUE(dir.is_valid());
29+
}
30+
31+
} // namespace testing
32+
} // namespace fml

0 commit comments

Comments
 (0)