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

Method DirectRead

Libraries/unrar/file.cpp:409–472  ·  view source on GitHub ↗

Returns -1 in case of error.

Source from the content-addressed store, hash-verified

407
408// Returns -1 in case of error.
409int File::DirectRead(void *Data,size_t Size)
410{
411#ifdef _WIN_ALL
412 const size_t MaxDeviceRead=20000;
413 const size_t MaxLockedRead=32768;
414#endif
415 if (HandleType==FILE_HANDLESTD)
416 {
417#ifdef _WIN_ALL
418// if (Size>MaxDeviceRead)
419// Size=MaxDeviceRead;
420 hFile=GetStdHandle(STD_INPUT_HANDLE);
421#else
422#ifdef FILE_USE_OPEN
423 hFile=STDIN_FILENO;
424#else
425 hFile=stdin;
426#endif
427#endif
428 }
429#ifdef _WIN_ALL
430 // For pipes like 'type file.txt | rar -si arcname' ReadFile may return
431 // data in small ~4KB blocks. It may slightly reduce the compression ratio.
432 DWORD Read;
433 if (!ReadFile(hFile,Data,(DWORD)Size,&Read,NULL))
434 {
435 if (IsDevice() && Size>MaxDeviceRead)
436 return DirectRead(Data,MaxDeviceRead);
437 if (HandleType==FILE_HANDLESTD && GetLastError()==ERROR_BROKEN_PIPE)
438 return 0;
439
440 // We had a bug report about failure to archive 1C database lock file
441 // 1Cv8tmp.1CL, which is a zero length file with a region above 200 KB
442 // permanently locked. If our first read request uses too large buffer
443 // and if we are in -dh mode, so we were able to open the file,
444 // we'll fail with "Read error". So now we use try a smaller buffer size
445 // in case of lock error.
446 if (HandleType==FILE_HANDLENORMAL && Size>MaxLockedRead &&
447 GetLastError()==ERROR_LOCK_VIOLATION)
448 return DirectRead(Data,MaxLockedRead);
449
450 return -1;
451 }
452 return Read;
453#else
454#ifdef FILE_USE_OPEN
455 ssize_t ReadSize=read(hFile,Data,Size);
456 if (ReadSize==-1)
457 return -1;
458 return (int)ReadSize;
459#else
460 if (LastWrite)
461 {
462 fflush(hFile);
463 LastWrite=false;
464 }
465 clearerr(hFile);
466 size_t ReadSize=fread(Data,1,Size,hFile);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected