Skip to content

Commit 518d4b1

Browse files
committed
fix read/write big files on linux
1 parent ded1582 commit 518d4b1

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

sources/libcore/filesystem/realFiles.cpp

+30-12
Original file line numberDiff line numberDiff line change
@@ -179,53 +179,71 @@ namespace cage
179179
{
180180
CAGE_ASSERT(f);
181181
#ifdef CAGE_SYSTEM_WINDOWS
182-
HANDLE handle = (HANDLE)_get_osfhandle(_fileno(f));
182+
const HANDLE handle = (HANDLE)_get_osfhandle(_fileno(f));
183183
uint64 off = 0;
184184
uint64 s = buffer.size();
185185
while (s > 0)
186186
{
187187
OVERLAPPED o;
188188
detail::memset(&o, 0, sizeof(o));
189-
uint64 p = at + off;
189+
const uint64 p = at + off;
190190
o.Offset = (DWORD)p;
191191
o.OffsetHigh = (DWORD)((uint64)p >> 32);
192-
uint64 k = min(s, (uint64)1024 * 1024 * 1024); // write in chunks of 1 GB
192+
const uint64 k = min(s, (uint64)1024 * 1024 * 1024); // read in chunks of 1 GB
193193
DWORD r = 0;
194194
if (!ReadFile(handle, buffer.data() + off, numeric_cast<DWORD>(k), &r, &o) || r != k)
195195
CAGE_THROW_ERROR(SystemError, "ReadFile", GetLastError());
196196
off += k;
197197
s -= k;
198198
}
199199
#else
200-
if (pread(fileno(f), buffer.data(), buffer.size(), at) != buffer.size())
201-
CAGE_THROW_ERROR(SystemError, "pread", errno);
200+
const int handle = fileno(f);
201+
uint64 off = 0;
202+
uint64 s = buffer.size();
203+
while (s > 0)
204+
{
205+
const uint64 k = min(s, (uint64)1024 * 1024 * 1024); // read in chunks of 1 GB
206+
if (pread(handle, buffer.data() + off, k, at) != k)
207+
CAGE_THROW_ERROR(SystemError, "pread", errno);
208+
off += k;
209+
s -= k;
210+
}
202211
#endif
203212
}
204213

205214
void writeAtImpl(PointerRange<const char> buffer, const uint64 at, FILE *f)
206215
{
207216
CAGE_ASSERT(f);
208217
#ifdef CAGE_SYSTEM_WINDOWS
209-
HANDLE handle = (HANDLE)_get_osfhandle(_fileno(f));
218+
const HANDLE handle = (HANDLE)_get_osfhandle(_fileno(f));
210219
uint64 off = 0;
211220
uint64 s = buffer.size();
212221
while (s > 0)
213222
{
214223
OVERLAPPED o;
215224
detail::memset(&o, 0, sizeof(o));
216-
uint64 p = at + off;
225+
const uint64 p = at + off;
217226
o.Offset = (DWORD)p;
218227
o.OffsetHigh = (DWORD)((uint64)p >> 32);
219-
uint64 k = min(s, (uint64)1024 * 1024 * 1024); // write in chunks of 1 GB
228+
const uint64 k = min(s, (uint64)1024 * 1024 * 1024); // write in chunks of 1 GB
220229
DWORD r = 0;
221230
if (!WriteFile(handle, buffer.data() + off, numeric_cast<DWORD>(k), &r, &o) || r != k)
222231
CAGE_THROW_ERROR(SystemError, "WriteFile", GetLastError());
223232
off += k;
224233
s -= k;
225234
}
226235
#else
227-
if (pwrite(fileno(f), buffer.data(), buffer.size(), at) != buffer.size())
228-
CAGE_THROW_ERROR(SystemError, "pwrite", errno);
236+
const int handle = fileno(f);
237+
uint64 off = 0;
238+
uint64 s = buffer.size();
239+
while (s > 0)
240+
{
241+
const uint64 k = min(s, (uint64)1024 * 1024 * 1024); // write in chunks of 1 GB
242+
if (pwrite(handle, buffer.data() + off, k, at) != k)
243+
CAGE_THROW_ERROR(SystemError, "pwrite", errno);
244+
off += k;
245+
s -= k;
246+
}
229247
#endif
230248
}
231249

@@ -322,9 +340,9 @@ namespace cage
322340
uint64 size() override
323341
{
324342
CAGE_ASSERT(f);
325-
uint64 pos = ftell(f);
343+
const uint64 pos = ftell(f);
326344
fseek(f, 0, 2);
327-
uint64 siz = ftell(f);
345+
const uint64 siz = ftell(f);
328346
fseek(f, pos, 0);
329347
return siz;
330348
}

0 commit comments

Comments
 (0)