Skip to content

Commit 9fdf5ae

Browse files
committed
Add close file method to filesystems implementation
1 parent 74a82f6 commit 9fdf5ae

File tree

5 files changed

+71
-28
lines changed

5 files changed

+71
-28
lines changed

include/vfspp/IFileSystem.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,8 @@ class IFileSystem
5555
/*
5656
* Close file
5757
*/
58-
virtual void CloseFile(IFilePtr file)
59-
{
60-
if (!file) {
61-
return;
62-
}
63-
file->Close();
64-
}
65-
58+
virtual void CloseFile(IFilePtr file) = 0;
59+
6660
/*
6761
* Create file on writeable filesystem. Return true if file already exists
6862
*/
@@ -129,4 +123,4 @@ class IFileSystem
129123

130124
}; // namespace vfspp
131125

132-
#endif /* IFILESYSTEM_H */
126+
#endif // IFILESYSTEM_H

include/vfspp/MemoryFile.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ class MemoryFile final : public IFile
237237
m_SeekPos = 0;
238238
m_IsReadOnly = true;
239239

240-
std::ios_base::openmode open_mode = static_cast<std::ios_base::openmode>(0x00);
241240
if ((mode & FileMode::Write) == FileMode::Write) {
242241
m_IsReadOnly = false;
243242
}

include/vfspp/MemoryFileSystem.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ class MemoryFileSystem final : public IFileSystem
119119
return OpenFileST(filePath, mode);
120120
}
121121
}
122+
123+
/*
124+
* Close file
125+
*/
126+
virtual void CloseFile(IFilePtr file) override
127+
{
128+
if constexpr (VFSPP_MT_SUPPORT_ENABLED) {
129+
std::lock_guard<std::mutex> lock(m_Mutex);
130+
CloseFileST(file);
131+
} else {
132+
CloseFileST(file);
133+
}
134+
}
122135

123136
/*
124137
* Create file on writeable filesystem. Returns true if file created successfully
@@ -270,6 +283,16 @@ class MemoryFileSystem final : public IFileSystem
270283
return file;
271284
}
272285

286+
inline void CloseFileST(IFilePtr file)
287+
{
288+
if (!file) {
289+
return;
290+
}
291+
292+
file->Close();
293+
m_FileList.erase(file->GetFileInfo().AbsolutePath());
294+
}
295+
273296
inline bool CreateFileST(const FileInfo& filePath)
274297
{
275298
IFilePtr file = OpenFileST(filePath, IFile::FileMode::Write | IFile::FileMode::Truncate);

include/vfspp/NativeFile.hpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,15 @@ class NativeFile final : public IFile
314314
return 0;
315315
}
316316

317-
m_Stream.read(reinterpret_cast<char*>(buffer), size);
318-
return static_cast<uint64_t>(m_Stream.gcount());
317+
318+
uint64_t leftSize = SizeST() - TellST();
319+
uint64_t maxSize = std::min(size, leftSize);
320+
if (maxSize > 0) {
321+
m_Stream.read(reinterpret_cast<char*>(buffer), maxSize);
322+
return maxSize;
323+
}
324+
325+
return 0;
319326
}
320327

321328
inline uint64_t WriteST(const uint8_t* buffer, uint64_t size)
@@ -373,22 +380,22 @@ class NativeFile final : public IFile
373380
uint64_t totalSize = size;
374381
std::vector<uint8_t> buffer(bufferSize);
375382
while (size > 0) {
376-
stream.read(reinterpret_cast<char*>(buffer.data()), std::min(size, static_cast<uint64_t>(buffer.size())));
377-
uint64_t bytesRead = stream.gcount();
378-
if (bytesRead == 0) {
379-
break;
380-
}
381-
382-
if (size < bytesRead) {
383-
bytesRead = size;
384-
}
385-
386-
WriteST(buffer.data(), bytesRead);
387-
388-
size -= bytesRead;
389-
}
390-
391-
return totalSize - size;
383+
stream.read(reinterpret_cast<char*>(buffer.data()), std::min(size, static_cast<uint64_t>(buffer.size())));
384+
uint64_t bytesRead = static_cast<uint64_t>(stream.gcount());
385+
if (bytesRead == 0) {
386+
break;
387+
}
388+
389+
if (size < bytesRead) {
390+
bytesRead = size;
391+
}
392+
393+
WriteST(buffer.data(), bytesRead);
394+
395+
size -= bytesRead;
396+
}
397+
398+
return totalSize - size;
392399
}
393400

394401
private:

include/vfspp/NativeFileSystem.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ class NativeFileSystem final : public IFileSystem
119119
return OpenFileST(filePath, mode);
120120
}
121121
}
122+
123+
/*
124+
* Close file
125+
*/
126+
virtual void CloseFile(IFilePtr file) override
127+
{
128+
if constexpr (VFSPP_MT_SUPPORT_ENABLED) {
129+
std::lock_guard<std::mutex> lock(m_Mutex);
130+
CloseFileST(file);
131+
} else {
132+
CloseFileST(file);
133+
}
134+
}
122135

123136
/*
124137
* Create file on writeable filesystem. Returns true if file created successfully
@@ -290,6 +303,13 @@ class NativeFileSystem final : public IFileSystem
290303

291304
return file;
292305
}
306+
307+
inline void CloseFileST(IFilePtr file)
308+
{
309+
if (file) {
310+
file->Close();
311+
}
312+
}
293313

294314
inline bool CreateFileST(const FileInfo& filePath)
295315
{

0 commit comments

Comments
 (0)