Changeset 57407 in vbox for trunk/src/VBox/Additions
- Timestamp:
- Aug 18, 2015 9:52:53 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxGuestLib/VBoxGuestR3LibGuestProp.cpp
r57358 r57407 35 35 #include <iprt/string.h> 36 36 #ifndef VBOX_VBGLR3_XSERVER 37 # include <iprt/ cpp/mem.h>37 # include <iprt/mem.h> 38 38 #endif 39 39 #include <iprt/assert.h> … … 61 61 #ifdef VBOX_VBGLR3_XSERVER 62 62 63 # undef RTS TrEnd63 # undef RTStrEnd 64 64 # define RTStrEnd xf86RTStrEnd 65 65 … … 580 580 /** 581 581 * Start enumerating guest properties which match a given pattern. 582 * 582 583 * This function creates a handle which can be used to continue enumerating. 583 584 * … … 600 601 * on success. This must be freed with 601 602 * VbglR3GuestPropEnumFree when it is no longer needed. 602 * @param ppszNameWhere to store the next property name. This will be603 * set to NULL if there are no more properties to604 * enumerate. This pointer should not be freed. Optional.605 * @param ppszValueWhere to store the next property value. This will be606 * set to NULL if there are no more properties to607 * enumerate. This pointer should not be freed. Optional.608 * @param pu64TimestampWhere to store the next property timestamp. This609 * will be set to zero if there are no more properties610 * to enumerate. Optional.611 * @param ppszFlagsWhere to store the next property flags. This will be612 * set to NULL if there are no more properties to613 * enumerate. This pointer should not be freed. Optional.603 * @param ppszName Where to store the next property name. This will be 604 * set to NULL if there are no more properties to 605 * enumerate. This pointer should not be freed. Optional. 606 * @param ppszValue Where to store the next property value. This will be 607 * set to NULL if there are no more properties to 608 * enumerate. This pointer should not be freed. Optional. 609 * @param pu64Timestamp Where to store the next property timestamp. This 610 * will be set to zero if there are no more properties 611 * to enumerate. Optional. 612 * @param ppszFlags Where to store the next property flags. This will be 613 * set to NULL if there are no more properties to 614 * enumerate. This pointer should not be freed. Optional. 614 615 * 615 616 * @remarks While all output parameters are optional, you need at least one to … … 626 627 { 627 628 /* Create the handle. */ 628 RTCMemAutoPtr<VBGLR3GUESTPROPENUM, VbglR3GuestPropEnumFree> Handle; 629 Handle = (PVBGLR3GUESTPROPENUM)RTMemAllocZ(sizeof(VBGLR3GUESTPROPENUM)); 630 if (!Handle) 629 PVBGLR3GUESTPROPENUM pHandle = (PVBGLR3GUESTPROPENUM)RTMemAllocZ(sizeof(VBGLR3GUESTPROPENUM)); 630 if (RT_LIKELY(pHandle)) 631 {/* likely */} 632 else 631 633 return VERR_NO_MEMORY; 632 634 633 635 /* Get the length of the pattern string, including the final terminator. */ 634 size_t c chPatterns = 1;636 size_t cbPatterns = 1; 635 637 for (uint32_t i = 0; i < cPatterns; ++i) 636 cchPatterns += strlen(papszPatterns[i]) + 1; 637 638 /* Pack the pattern array */ 639 RTCMemAutoPtr<char> Patterns; 640 Patterns = (char *)RTMemAlloc(cchPatterns); 638 cbPatterns += strlen(papszPatterns[i]) + 1; 639 640 /* Pack the pattern array. */ 641 char *pszzPatterns = (char *)RTMemAlloc(cbPatterns); 641 642 size_t off = 0; 642 643 for (uint32_t i = 0; i < cPatterns; ++i) 643 644 { 644 645 size_t cb = strlen(papszPatterns[i]) + 1; 645 memcpy(& Patterns[off], papszPatterns[i], cb);646 memcpy(&pszzPatterns[off], papszPatterns[i], cb); 646 647 off += cb; 647 648 } 648 Patterns[off] = '\0'; 649 650 /* Randomly chosen initial size for the buffer to hold the enumeration 651 * information. */ 652 uint32_t cchBuf = 4096; 653 RTCMemAutoPtr<char> Buf; 649 pszzPatterns[off] = '\0'; 654 650 655 651 /* In reading the guest property data we are racing against the host 656 652 * adding more of it, so loop a few times and retry on overflow. */ 657 int rc = VINF_SUCCESS; 653 uint32_t cbBuf = 4096; /* picked out of thin air */ 654 char *pchBuf = NULL; 655 int rc = VINF_SUCCESS; 658 656 for (int i = 0; i < 10; ++i) 659 657 { 660 if (!Buf.realloc(cchBuf)) 658 void *pvNew = RTMemRealloc(pchBuf, cbBuf); 659 if (pvNew) 660 pchBuf = (char *)pvNew; 661 else 661 662 { 662 663 rc = VERR_NO_MEMORY; 663 664 break; 664 665 } 665 rc = VbglR3GuestPropEnumRaw(u32ClientId, Patterns.get(), 666 Buf.get(), cchBuf, &cchBuf); 666 rc = VbglR3GuestPropEnumRaw(u32ClientId, pszzPatterns, pchBuf, cbBuf, &cbBuf); 667 667 if (rc != VERR_BUFFER_OVERFLOW) 668 668 break; 669 cchBuf += 4096; /* Just to increase our chances */ 670 } 669 cbBuf += 4096; /* Just to increase our chances */ 670 } 671 RTMemFree(pszzPatterns); 671 672 if (RT_SUCCESS(rc)) 672 673 { 673 674 /* 674 * Transfer ownership of the buffer to the handle structure and 675 * call VbglR3GuestPropEnumNext to retrieve the first entry. 675 * Complete the handle and call VbglR3GuestPropEnumNext to retrieve the first entry. 676 676 */ 677 Handle->pchNext = Handle->pchBuf = Buf.release(); 678 Handle->pchBufEnd = Handle->pchBuf + cchBuf; 677 pHandle->pchNext = pchBuf; 678 pHandle->pchBuf = pchBuf; 679 pHandle->pchBufEnd = pchBuf + cbBuf; 679 680 680 681 const char *pszNameTmp; 681 682 if (!ppszName) 682 683 ppszName = &pszNameTmp; 683 rc = VbglR3GuestPropEnumNext(Handle.get(), ppszName, ppszValue, 684 pu64Timestamp, ppszFlags); 684 rc = VbglR3GuestPropEnumNext(pHandle, ppszName, ppszValue, pu64Timestamp, ppszFlags); 685 685 if (RT_SUCCESS(rc)) 686 *ppHandle = Handle.release(); 686 { 687 *ppHandle = pHandle; 688 return rc; 689 } 687 690 } 688 691 else if (rc == VERR_BUFFER_OVERFLOW) 689 692 rc = VERR_TOO_MUCH_DATA; 693 RTMemFree(pchBuf); 694 RTMemFree(pHandle); 690 695 return rc; 691 696 }
Note:
See TracChangeset
for help on using the changeset viewer.