MCPcopy Create free account
hub / github.com/abbeycode/UnrarKit / Write

Method Write

Libraries/unrar/file.cpp:294–367  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

292
293
294bool File::Write(const void *Data,size_t Size)
295{
296 if (Size==0)
297 return true;
298 if (HandleType==FILE_HANDLESTD)
299 {
300#ifdef _WIN_ALL
301 hFile=GetStdHandle(STD_OUTPUT_HANDLE);
302#else
303 // Cannot use the standard stdout here, because it already has wide orientation.
304 if (hFile==FILE_BAD_HANDLE)
305 {
306#ifdef FILE_USE_OPEN
307 hFile=dup(STDOUT_FILENO); // Open new stdout stream.
308#else
309 hFile=fdopen(dup(STDOUT_FILENO),"w"); // Open new stdout stream.
310#endif
311 }
312#endif
313 }
314 bool Success;
315 while (1)
316 {
317 Success=false;
318#ifdef _WIN_ALL
319 DWORD Written=0;
320 if (HandleType!=FILE_HANDLENORMAL)
321 {
322 // writing to stdout can fail in old Windows if data block is too large
323 const size_t MaxSize=0x4000;
324 for (size_t I=0;I<Size;I+=MaxSize)
325 {
326 Success=WriteFile(hFile,(byte *)Data+I,(DWORD)Min(Size-I,MaxSize),&Written,NULL)==TRUE;
327 if (!Success)
328 break;
329 }
330 }
331 else
332 Success=WriteFile(hFile,Data,(DWORD)Size,&Written,NULL)==TRUE;
333#else
334#ifdef FILE_USE_OPEN
335 ssize_t Written=write(hFile,Data,Size);
336 Success=Written==Size;
337#else
338 int Written=fwrite(Data,1,Size,hFile);
339 Success=Written==Size && !ferror(hFile);
340#endif
341#endif
342 if (!Success && AllowExceptions && HandleType==FILE_HANDLENORMAL)
343 {
344#if defined(_WIN_ALL) && !defined(SFX_MODULE) && !defined(RARDLL)
345 int ErrCode=GetLastError();
346 int64 FilePos=Tell();
347 uint64 FreeSize=GetFreeDisk(FileName);
348 SetLastError(ErrCode);
349 if (FreeSize>Size && FilePos-Size<=0xffffffff && FilePos+Size>0xffffffff)
350 ErrHandler.WriteErrorFAT(FileName);
351#endif

Callers 5

RestoreMethod · 0.80
CopyMethod · 0.80
RestoreMethod · 0.80
UnpWriteMethod · 0.80
ExtractFileCopyMethod · 0.80

Calls 4

GetFreeDiskFunction · 0.85
WriteErrorFATMethod · 0.80
AskRepeatWriteMethod · 0.80
WriteErrorMethod · 0.80

Tested by

no test coverage detected