Changeset 33235 in vbox
- Timestamp:
- Oct 19, 2010 3:14:06 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Devices/Storage/testcase/vbox-img.cpp
r33227 r33235 254 254 typedef struct FILEIOSTATE 255 255 { 256 RTFILE file; 256 257 /** Offset in the file. */ 257 258 uint64_t off; … … 273 274 AssertPtrNullReturn(pfnCompleted, VERR_INVALID_PARAMETER); 274 275 AssertReturn((fOpen & RTFILE_O_ACCESS_MASK) == RTFILE_O_READ, VERR_INVALID_PARAMETER); 275 276 RTFILE file; 277 int rc = RTFileFromNative(&file, RTFILE_NATIVE_STDIN); 278 if (RT_FAILURE(rc)) 279 return rc; 280 281 /* No need to clear the buffer, the data will be read from disk. */ 276 282 PFILEIOSTATE pFS = (PFILEIOSTATE)RTMemAlloc(sizeof(FILEIOSTATE)); 277 283 if (!pFS) 278 284 return VERR_NO_MEMORY; 279 285 286 pFS->file = file; 280 287 pFS->off = 0; 281 288 pFS->offBuffer = UINT64_MAX; … … 290 297 NOREF(pvUser); 291 298 AssertPtrReturn(pStorage, VERR_INVALID_POINTER); 292 293 299 PFILEIOSTATE pFS = (PFILEIOSTATE)pStorage; 294 300 … … 369 375 do 370 376 { 371 rc = RTFileRead( 0, pTmp, cbTmp, &cbRead);377 rc = RTFileRead(pFS->file, pTmp, cbTmp, &cbRead); 372 378 if (RT_FAILURE(rc)) 373 379 return rc; … … 401 407 do 402 408 { 403 rc = RTFileRead( 0, pTmp, cbTmp, &cbRead);409 rc = RTFileRead(pFS->file, pTmp, cbTmp, &cbRead); 404 410 if (RT_FAILURE(rc)) 405 411 return rc; … … 460 466 AssertPtrNullReturn(pfnCompleted, VERR_INVALID_PARAMETER); 461 467 AssertReturn((fOpen & RTFILE_O_ACCESS_MASK) == RTFILE_O_WRITE, VERR_INVALID_PARAMETER); 462 463 PFILEIOSTATE pFS = (PFILEIOSTATE)RTMemAlloc(sizeof(FILEIOSTATE)); 468 RTFILE file; 469 int rc = RTFileFromNative(&file, RTFILE_NATIVE_STDOUT); 470 if (RT_FAILURE(rc)) 471 return rc; 472 473 /* Must clear buffer, so that skipped over data is initialized properly. */ 474 PFILEIOSTATE pFS = (PFILEIOSTATE)RTMemAllocZ(sizeof(FILEIOSTATE)); 464 475 if (!pFS) 465 476 return VERR_NO_MEMORY; 466 477 478 pFS->file = file; 467 479 pFS->off = 0; 468 pFS->offBuffer = UINT64_MAX;469 pFS->cbBuffer = 0;480 pFS->offBuffer = 0; 481 pFS->cbBuffer = sizeof(FILEIOSTATE); 470 482 471 483 *ppStorage = pFS; … … 477 489 NOREF(pvUser); 478 490 AssertPtrReturn(pStorage, VERR_INVALID_POINTER); 479 480 491 PFILEIOSTATE pFS = (PFILEIOSTATE)pStorage; 492 int rc = VINF_SUCCESS; 493 494 /* Flush any remaining buffer contents. */ 495 if (pFS->cbBuffer) 496 rc = RTFileWrite(pFS->file, &pFS->abBuffer[0], pFS->cbBuffer, NULL); 481 497 482 498 RTMemFree(pFS); 483 499 484 return VINF_SUCCESS;500 return rc; 485 501 } 486 502 … … 554 570 { 555 571 NOREF(pvUser); 556 NOREF(pStorage); 557 NOREF(uOffset); 558 NOREF(cbBuffer); 559 NOREF(pcbWritten); 572 AssertPtrReturn(pStorage, VERR_INVALID_POINTER); 560 573 AssertPtrReturn(pvBuffer, VERR_INVALID_POINTER); 561 AssertFailedReturn(VERR_NOT_SUPPORTED); 574 PFILEIOSTATE pFS = (PFILEIOSTATE)pStorage; 575 AssertReturn(uOffset >= pFS->off, VERR_INVALID_PARAMETER); 576 int rc; 577 578 /* Write the data to the buffer, flushing as required. */ 579 size_t cbTotalWritten = 0; 580 do 581 { 582 /* Flush the buffer if we need a new one. */ 583 while (uOffset > pFS->offBuffer + sizeof(pFS->abBuffer) - 1) 584 { 585 rc = RTFileWrite(pFS->file, &pFS->abBuffer[0], 586 sizeof(pFS->abBuffer), NULL); 587 RT_ZERO(pFS->abBuffer); 588 pFS->offBuffer += sizeof(pFS->abBuffer); 589 pFS->cbBuffer = 0; 590 } 591 592 uint32_t cbThisWrite = RT_MIN(cbBuffer, 593 sizeof(pFS->abBuffer) - uOffset % sizeof(pFS->abBuffer)); 594 memcpy(&pFS->abBuffer[uOffset % sizeof(pFS->abBuffer)], pvBuffer, 595 cbThisWrite); 596 uOffset += cbThisWrite; 597 pvBuffer = (uint8_t *)pvBuffer + cbThisWrite; 598 cbBuffer -= cbThisWrite; 599 cbTotalWritten += cbThisWrite; 600 } while (cbBuffer > 0); 601 602 if (pcbWritten) 603 *pcbWritten = cbTotalWritten; 604 605 pFS->cbBuffer = uOffset % sizeof(pFS->abBuffer); 606 if (!pFS->cbBuffer) 607 pFS->cbBuffer = sizeof(pFS->abBuffer); 608 pFS->off = uOffset; 609 610 return VINF_SUCCESS; 562 611 } 563 612
Note:
See TracChangeset
for help on using the changeset viewer.