@@ -38,6 +38,7 @@ XFile::~XFile() {
3838X_STATUS XFile::QueryDirectory (X_FILE_DIRECTORY_INFORMATION* out_info,
3939 size_t length, const std::string_view file_name,
4040 bool restart) {
41+ std::lock_guard<std::mutex> lock (file_lock_);
4142 assert_not_null (out_info);
4243
4344 vfs::Entry* entry = nullptr ;
@@ -91,6 +92,15 @@ X_STATUS XFile::QueryDirectory(X_FILE_DIRECTORY_INFORMATION* out_info,
9192X_STATUS XFile::Read (uint32_t buffer_guest_address, uint32_t buffer_length,
9293 uint64_t byte_offset, uint32_t * out_bytes_read,
9394 uint32_t apc_context, bool notify_completion) {
95+ std::lock_guard<std::mutex> lock (file_lock_);
96+ return ReadInternal (buffer_guest_address, buffer_length, byte_offset,
97+ out_bytes_read, apc_context, notify_completion);
98+ }
99+
100+ X_STATUS XFile::ReadInternal (uint32_t buffer_guest_address,
101+ uint32_t buffer_length, uint64_t byte_offset,
102+ uint32_t * out_bytes_read, uint32_t apc_context,
103+ bool notify_completion) {
94104 if (byte_offset == uint64_t (-1 )) {
95105 // Read from current position.
96106 byte_offset = position_;
@@ -184,6 +194,7 @@ X_STATUS XFile::Read(uint32_t buffer_guest_address, uint32_t buffer_length,
184194X_STATUS XFile::ReadScatter (uint32_t segments_guest_address, uint32_t length,
185195 uint64_t byte_offset, uint32_t * out_bytes_read,
186196 uint32_t apc_context) {
197+ std::lock_guard<std::mutex> lock (file_lock_);
187198 X_STATUS result = X_STATUS_SUCCESS;
188199
189200 // segments points to an array of buffer pointers of type
@@ -206,12 +217,13 @@ X_STATUS XFile::ReadScatter(uint32_t segments_guest_address, uint32_t length,
206217 }
207218
208219 uint32_t bytes_read = 0 ;
209- result = Read (read_buffer, read_length,
210- byte_offset ? ((byte_offset != -1 && byte_offset != -2 )
211- ? byte_offset + read_total
212- : byte_offset)
213- : -1 ,
214- &bytes_read, apc_context, false );
220+ result =
221+ ReadInternal (read_buffer, read_length,
222+ byte_offset ? ((byte_offset != -1 && byte_offset != -2 )
223+ ? byte_offset + read_total
224+ : byte_offset)
225+ : -1 ,
226+ &bytes_read, apc_context, false );
215227
216228 if (result != X_STATUS_SUCCESS) {
217229 break ;
@@ -240,6 +252,7 @@ X_STATUS XFile::ReadScatter(uint32_t segments_guest_address, uint32_t length,
240252X_STATUS XFile::Write (uint32_t buffer_guest_address, uint32_t buffer_length,
241253 uint64_t byte_offset, uint32_t * out_bytes_written,
242254 uint32_t apc_context) {
255+ std::lock_guard<std::mutex> lock (file_lock_);
243256 if (byte_offset == uint64_t (-1 )) {
244257 // Write from current position.
245258 byte_offset = position_;
@@ -269,7 +282,10 @@ X_STATUS XFile::Write(uint32_t buffer_guest_address, uint32_t buffer_length,
269282 return result;
270283}
271284
272- X_STATUS XFile::SetLength (size_t length) { return file_->SetLength (length); }
285+ X_STATUS XFile::SetLength (size_t length) {
286+ std::lock_guard<std::mutex> lock (file_lock_);
287+ return file_->SetLength (length);
288+ }
273289X_STATUS XFile::Rename (const std::filesystem::path file_path) {
274290 entry ()->Rename (file_path);
275291 return X_STATUS_SUCCESS;
0 commit comments