aboutsummaryrefslogtreecommitdiff
path: root/zencore/iobuffer.cpp
diff options
context:
space:
mode:
authorMartin Ridgers <[email protected]>2021-09-16 13:54:04 +0200
committerMartin Ridgers <[email protected]>2021-09-16 14:20:52 +0200
commit306de7d9a2c470613cb7697f2b0beff961fff8b5 (patch)
tree7ec6c37e8ec4c66ab14fcfa13f6c4d1e116ad57e /zencore/iobuffer.cpp
parentImplementation of IoBufferExtendedCore() using mmap() (diff)
downloadzen-306de7d9a2c470613cb7697f2b0beff961fff8b5.tar.xz
zen-306de7d9a2c470613cb7697f2b0beff961fff8b5.zip
MakeFromFile() on POSIX
Diffstat (limited to 'zencore/iobuffer.cpp')
-rw-r--r--zencore/iobuffer.cpp66
1 files changed, 43 insertions, 23 deletions
diff --git a/zencore/iobuffer.cpp b/zencore/iobuffer.cpp
index 2473e0530..16b530955 100644
--- a/zencore/iobuffer.cpp
+++ b/zencore/iobuffer.cpp
@@ -15,6 +15,7 @@
#if ZEN_PLATFORM_WINDOWS
# include <atlfile.h>
#else
+# include <sys/stat.h>
# include <sys/mman.h>
#endif
@@ -352,42 +353,61 @@ IoBufferBuilder::MakeFromFileHandle(void* FileHandle, uint64_t Offset, uint64_t
IoBuffer
IoBufferBuilder::MakeFromFile(const path_char_t* FileName, uint64_t Offset, uint64_t Size)
{
+ uint64_t FileSize;
+
+#if ZEN_PLATFORM_WINDOWS
CAtlFile DataFile;
HRESULT hRes = DataFile.Create(FileName, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING);
- if (SUCCEEDED(hRes))
+ if (FAILED(hRes))
{
- ULONGLONG FileSize;
- DataFile.GetSize(FileSize);
+ return {};
+ }
- // TODO: should validate that offset is in range
+ DataFile.GetSize((ULONGLONG&)FileSize);
+#else
+ int Fd = open(FileName, O_RDONLY);
+ if (Fd < 0)
+ {
+ return {};
+ }
+
+ static_assert(sizeof(decltype(stat::st_size)) == sizeof(uint64_t), "fstat() doesn't support large files");
+ struct stat Stat;
+ fstat(Fd, &Stat);
+ FileSize = Stat.st_size;
+#endif // ZEN_PLATFORM_WINDOWS
+
+ // TODO: should validate that offset is in range
- if (Size == ~0ull)
+ if (Size == ~0ull)
+ {
+ Size = FileSize - Offset;
+ }
+ else
+ {
+ // Clamp size
+ if ((Offset + Size) > FileSize)
{
Size = FileSize - Offset;
}
- else
- {
- // Clamp size
- if ((Offset + Size) > FileSize)
- {
- Size = FileSize - Offset;
- }
- }
+ }
- if (Size)
- {
- return IoBuffer(IoBuffer::File, DataFile.Detach(), Offset, Size);
- }
- else
- {
- // For an empty file, we may as well just return an empty memory IoBuffer
- return IoBuffer(IoBuffer::Wrap, "", 0);
- }
+ if (Size)
+ {
+#if ZEN_PLATFORM_WINDOWS
+ void* Fd = DataFile.Detach();
+#endif
+ return IoBuffer(IoBuffer::File, (void*)uintptr_t(Fd), Offset, Size);
}
- return {};
+#if !ZEN_PLATFORM_WINDOWS
+ close(Fd);
+#endif
+
+ // For an empty file, we may as well just return an empty memory IoBuffer
+ return IoBuffer(IoBuffer::Wrap, "", 0);
}
IoBuffer