Skip to content

Commit ca1a63d

Browse files
committed
Fix concurrent file system operation
1 parent 1482f8a commit ca1a63d

File tree

3 files changed

+34
-19
lines changed

3 files changed

+34
-19
lines changed

packages/lw_file_system/lib/src/api/file_system_base.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:flutter/foundation.dart';
66
import 'package:lw_file_system/lw_file_system.dart';
77
import 'package:path/path.dart' as p;
88
import 'package:rxdart/rxdart.dart';
9+
import 'package:synchronized/synchronized.dart';
910

1011
import 'file_system_dav.dart';
1112
import 'file_system_io.dart';
@@ -31,11 +32,15 @@ abstract class GeneralFileSystem {
3132

3233
Future<void> reset();
3334

35+
final _initializeLock = Lock();
36+
3437
Future<void> initialize({bool force = false}) async {
35-
if (force) await reset();
36-
if (force || !await isInitialized()) {
37-
await runInitialize();
38-
}
38+
return _initializeLock.synchronized(() async {
39+
if (force) await reset();
40+
if (force || !await isInitialized()) {
41+
await runInitialize();
42+
}
43+
});
3944
}
4045

4146
@protected

packages/lw_file_system/lib/src/api/file_system_io.dart

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import 'dart:io';
44
import 'package:flutter/foundation.dart';
55
import 'package:lw_file_system/lw_file_system.dart';
66
import 'package:path/path.dart' as p;
7+
import 'package:synchronized/synchronized.dart';
78

89
class IODirectoryFileSystem extends DirectoryFileSystem {
910
@override
1011
final LocalStorage? storage;
1112
final bool useIsolates;
1213

14+
final _lock = Lock();
15+
1316
IODirectoryFileSystem({
1417
this.storage,
1518
required super.config,
@@ -62,21 +65,25 @@ class IODirectoryFileSystem extends DirectoryFileSystem {
6265
@override
6366
Future<RawFileSystemDirectory> createDirectory(String path) async {
6467
path = normalizePath(path);
65-
final directory = Directory(await getAbsolutePath(path));
66-
if (!await directory.exists()) {
67-
await directory.create(recursive: true);
68-
}
69-
return RawFileSystemDirectory(
70-
AssetLocation(path: path, remote: remoteName),
71-
assets: [],
72-
);
68+
return _lock.synchronized(() async {
69+
final directory = Directory(await getAbsolutePath(path));
70+
if (!await directory.exists()) {
71+
await directory.create(recursive: true);
72+
}
73+
return RawFileSystemDirectory(
74+
AssetLocation(path: path, remote: remoteName),
75+
assets: [],
76+
);
77+
});
7378
}
7479

7580
@override
7681
Future<void> deleteAsset(String path) async {
7782
path = normalizePath(path);
78-
// This removes all types of entities
79-
await Directory(await getAbsolutePath(path)).delete(recursive: true);
83+
return _lock.synchronized(() async {
84+
// This removes all types of entities
85+
await Directory(await getAbsolutePath(path)).delete(recursive: true);
86+
});
8087
}
8188

8289
@override
@@ -87,11 +94,13 @@ class IODirectoryFileSystem extends DirectoryFileSystem {
8794
}) async {
8895
path = normalizePath(path);
8996
path = await getAbsolutePath(path);
90-
if (useIsolates) {
91-
await compute(_updateFile, (path, data));
92-
} else {
93-
await _updateFile((path, data));
94-
}
97+
return _lock.synchronized(() async {
98+
if (useIsolates) {
99+
await compute(_updateFile, (path, data));
100+
} else {
101+
await _updateFile((path, data));
102+
}
103+
});
95104
}
96105

97106
Future<void> _updateFile((String, Uint8List) e) async {

packages/lw_file_system/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dependencies:
2626
path: packages/lw_file_system_api
2727
ref: ddd0761c3ed5a48108bddd0448df76d77eeb9da0
2828
async: ^2.12.0
29+
synchronized: ^3.4.0
2930
# path: ^1.8.0
3031

3132
dev_dependencies:

0 commit comments

Comments
 (0)