From 751de8211033b40c66d88140137eef76e6b20cfa Mon Sep 17 00:00:00 2001 From: Ritt Konstantin Date: Thu, 7 Jan 2010 19:28:53 +0100 Subject: Avoid repeatedly calling LookupAccountSid in QFSFileEngine::owner MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Try to retrieve user/group name in a single call to LookupAccountSid, by using pre-allocated buffers. Only call a second time if the buffer size is insufficient. 64 bytes should be enough in common case. Previously, we would always call LookupAccountSid twice, even if ERROR_NONE_MAPPED occured first time. Merge-request: 815 Reviewed-by: João Abecasis --- src/corelib/io/qfsfileengine_win.cpp | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/corelib/io/qfsfileengine_win.cpp b/src/corelib/io/qfsfileengine_win.cpp index c3ea08a..bec0075 100644 --- a/src/corelib/io/qfsfileengine_win.cpp +++ b/src/corelib/io/qfsfileengine_win.cpp @@ -1731,20 +1731,31 @@ QString QFSFileEngine::owner(FileOwner own) const own == OwnerGroup ? GROUP_SECURITY_INFORMATION : OWNER_SECURITY_INFORMATION, own == OwnerUser ? &pOwner : 0, own == OwnerGroup ? &pOwner : 0, 0, 0, &pSD) == ERROR_SUCCESS) { - DWORD lowner = 0, ldomain = 0; + DWORD lowner = 64; + DWORD ldomain = 64; + QVarLengthArray owner(lowner); + QVarLengthArray domain(ldomain); SID_NAME_USE use = SidTypeUnknown; // First call, to determine size of the strings (with '\0'). - ptrLookupAccountSidW(NULL, pOwner, NULL, &lowner, NULL, &ldomain, (SID_NAME_USE*)&use); - wchar_t *owner = new wchar_t[lowner]; - wchar_t *domain = new wchar_t[ldomain]; - // Second call, size is without '\0' - if (ptrLookupAccountSidW(NULL, pOwner, (LPWSTR)owner, &lowner, - (LPWSTR)domain, &ldomain, (SID_NAME_USE*)&use)) { - name = QString::fromUtf16((ushort*)owner); + if (!ptrLookupAccountSidW(NULL, pOwner, (LPWSTR)owner.data(), &lowner, + (LPWSTR)domain.data(), &ldomain, (SID_NAME_USE*)&use)) { + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { + if (lowner > (DWORD)owner.size()) + owner.resize(lowner); + if (ldomain > (DWORD)domain.size()) + domain.resize(ldomain); + // Second call, try on resized buf-s + if (!ptrLookupAccountSidW(NULL, pOwner, (LPWSTR)owner.data(), &lowner, + (LPWSTR)domain.data(), &ldomain, (SID_NAME_USE*)&use)) { + lowner = 0; + } + } else { + lowner = 0; + } } + if (lowner != 0) + name = QString::fromWCharArray(owner.data()); LocalFree(pSD); - delete [] owner; - delete [] domain; } } } -- cgit v0.12