@@ -68,6 +68,18 @@ File _fileInDir(Directory path, String filename) {
6868 return path.fileSystem.file ('$dirPath $separator $filename ' );
6969}
7070
71+ const int _linuxEagain = 11 ;
72+ const int _posixEacces = 13 ;
73+ const int _macOSEagain = 35 ;
74+ const int _windowsSharingViolation = 32 ;
75+ const int _windowsLockViolation = 33 ;
76+
77+ const _contentionErrorCodes = < String , Set <int >> {
78+ 'linux' : {_linuxEagain, _posixEacces},
79+ 'macos' : {_macOSEagain, _posixEacces},
80+ 'windows' : {_windowsSharingViolation, _windowsLockViolation},
81+ };
82+
7183/// Run [callback] with this Dart process having exclusive access to [file] .
7284///
7385/// Note multiple isolates and isolate groups in the same Dart process share
@@ -86,43 +98,59 @@ Future<T> _runUnderFileLock<T>(
8698}) async {
8799 if (! await file.exists ()) await file.create (recursive: true );
88100 final randomAccessFile = await file.open (mode: .write);
89- var printed = false ;
90- var errorFromCallback = false ;
91- final stopwatch = Stopwatch ()..start ();
92- while (timeout == null || stopwatch.elapsed < timeout) {
93- try {
94- await randomAccessFile.lock (FileLock .exclusive);
101+ try {
102+ var printed = false ;
103+ var errorFromCallback = false ;
104+ final stopwatch = Stopwatch ()..start ();
105+ while (timeout == null || stopwatch.elapsed < timeout) {
95106 try {
96- await randomAccessFile.writeString (
97- 'Last acquired by ${Platform .resolvedExecutable } '
98- '(pid $pid ) running ${Platform .script } on ${DateTime .now ()}.' ,
99- );
107+ await randomAccessFile.lock (FileLock .exclusive);
100108 try {
101- return await callback ();
102- } on FileSystemException {
103- errorFromCallback = true ;
109+ await randomAccessFile.writeString (
110+ 'Last acquired by ${Platform .resolvedExecutable } '
111+ '(pid $pid ) running ${Platform .script } on ${DateTime .now ()}.' ,
112+ );
113+ try {
114+ return await callback ();
115+ } on FileSystemException {
116+ errorFromCallback = true ;
117+ rethrow ;
118+ }
119+ } finally {
120+ await randomAccessFile.unlock ();
121+ }
122+ } on FileSystemException catch (e) {
123+ if (errorFromCallback) {
104124 rethrow ;
105125 }
106- } finally {
107- await randomAccessFile.unlock ();
108- }
109- } on FileSystemException {
110- if (errorFromCallback) {
111- rethrow ;
112- }
113- if (! printed) {
114- logger? .finer (
115- 'Waiting to be able to obtain lock of directory: ${file .path }.' ,
116- );
117- printed = true ;
126+ final errorCode = e.osError? .errorCode;
127+ final contentionErrorCodes =
128+ _contentionErrorCodes[Platform .operatingSystem]! ;
129+ if (errorCode != null && ! contentionErrorCodes.contains (errorCode)) {
130+ final message =
131+ 'Could not acquire the lock to ${file .path }. '
132+ 'Running hooks_runner on a project on a file system where '
133+ 'process locks are not supported is not supported. '
134+ 'Please move your project to a different location.' ;
135+ logger? .severe (message);
136+ throw FileSystemException (message, file.path, e.osError);
137+ }
138+ if (! printed) {
139+ logger? .finer (
140+ 'Waiting to be able to obtain lock of directory: ${file .path }.' ,
141+ );
142+ printed = true ;
143+ }
144+ // Don't busy wait, give the CPU some rest.
145+ // Magic constant taken from flutter_tools for startup lock.
146+ await Future <void >.delayed (const Duration (milliseconds: 50 ));
118147 }
119- // Don't busy wait, give the CPU some rest.
120- // Magic constant taken from flutter_tools for startup lock.
121- await Future <void >.delayed (const Duration (milliseconds: 50 ));
122148 }
123- }
124149
125- final message = 'Could not acquire the lock to ${file .path }.' ;
126- logger? .severe (message);
127- throw TimeoutException (message, timeout);
150+ final message = 'Could not acquire the lock to ${file .path }.' ;
151+ logger? .severe (message);
152+ throw TimeoutException (message, timeout);
153+ } finally {
154+ await randomAccessFile.close ();
155+ }
128156}
0 commit comments