Changeset 29901 in vbox for trunk/src/VBox/Runtime/common/checksum
- Timestamp:
- May 31, 2010 12:53:25 PM (15 years ago)
- svn:sync-xref-src-repo-rev:
- 62167
- Location:
- trunk/src/VBox/Runtime/common/checksum
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/checksum/RTSha1Digest.cpp
r29820 r29901 33 33 34 34 #include <iprt/assert.h> 35 #include <iprt/err.h> 36 #include <iprt/stream.h> 35 #include <iprt/mem.h> 37 36 #include <iprt/string.h> 37 #include <iprt/file.h> 38 38 39 39 #include <openssl/sha.h> 40 40 41 42 43 RTR3DECL(int) RTSha1Digest(const char *pszFile, char **ppszDigest) 41 RTR3DECL(int) RTSha1Digest(const char *pszFile, char **ppszDigest, PFNRTSHAPROGRESS pfnProgressCallback, void *pvUser) 44 42 { 45 43 /* Validate input */ 46 44 AssertPtrReturn(pszFile, VERR_INVALID_POINTER); 47 45 AssertPtrReturn(ppszDigest, VERR_INVALID_POINTER); 46 AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER); 48 47 49 48 *ppszDigest = NULL; 49 int rc = VINF_SUCCESS; 50 50 51 51 /* Initialize OpenSSL */ … … 54 54 return VERR_INTERNAL_ERROR; 55 55 56 /** @todo r=bird: Using a stream here doesn't really serve much purpose as 57 * few stream implementations uses a buffer much larger than 4KB. (The 58 * only I'm aware of is libc on OS/2, which uses 8KB.) */ 56 /* Fetch the file size. Only needed if there is a progress callback. */ 57 float multi = 0; 58 if (pfnProgressCallback) 59 { 60 uint64_t cbFile; 61 rc = RTFileQuerySize(pszFile, &cbFile); 62 if (RT_FAILURE(rc)) 63 return rc; 64 multi = 100.0 / cbFile; 65 } 59 66 60 67 /* Open the file to calculate a SHA1 sum of */ 61 PRTSTREAM pStream;62 int rc = RTStrmOpen(pszFile, "rb", &pStream);68 RTFILE file; 69 rc = RTFileOpen(&file, pszFile, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE); 63 70 if (RT_FAILURE(rc)) 64 71 return rc; 65 72 66 73 /* Read that file in blocks */ 67 void *pvBuf[4096]; 74 void *pvBuf = RTMemTmpAlloc(_1M); 75 if (!pvBuf) 76 { 77 RTFileClose(file); 78 rc = VERR_NO_MEMORY; 79 } 68 80 size_t cbRead; 69 do 81 size_t cbReadFull = 0; 82 for (;;) 70 83 { 71 84 cbRead = 0; 72 rc = RT StrmReadEx(pStream, pvBuf, 4096, &cbRead);73 if (RT_FAILURE(rc) )85 rc = RTFileRead(file, pvBuf, _1M, &cbRead); 86 if (RT_FAILURE(rc) || !cbRead) 74 87 break; 75 88 if(!SHA1_Update(&ctx, pvBuf, cbRead)) … … 78 91 break; 79 92 } 80 } while (cbRead > 0); 81 RTStrmClose(pStream); 93 cbReadFull += cbRead; 94 /* Call progress callback if some is defined */ 95 if ( pfnProgressCallback 96 && RT_FAILURE(pfnProgressCallback((unsigned)(cbReadFull * multi), pvUser))) 97 { 98 /* Cancel support */ 99 rc = VERR_CANCELLED; 100 break; 101 } 102 } 103 RTMemTmpFree(pvBuf); 104 RTFileClose(file); 82 105 83 106 if (RT_FAILURE(rc)) … … 85 108 86 109 /* Finally calculate & format the SHA1 sum */ 87 unsigned char auchDig[ 20];110 unsigned char auchDig[RTSHA1_HASH_SIZE]; 88 111 if (!SHA1_Final(auchDig, &ctx)) 89 112 return VERR_INTERNAL_ERROR; -
trunk/src/VBox/Runtime/common/checksum/manifest.cpp
r28800 r29901 55 55 typedef RTMANIFESTFILEENTRY* PRTMANIFESTFILEENTRY; 56 56 57 57 /** 58 * Internal structure used for the progress callback 59 */ 60 typedef struct RTMANIFESTCALLBACKDATA 61 { 62 PFNRTMANIFESTPROGRESS pfnProgressCallback; 63 void *pvUser; 64 uint32_t cMaxFiles; 65 uint32_t cCurrentFile; 66 } RTMANIFESTCALLBACKDATA; 67 typedef RTMANIFESTCALLBACKDATA* PRTMANIFESTCALLBACKDATA; 68 69 int rtSHAProgressCallback(unsigned uPercent, void *pvUser) 70 { 71 PRTMANIFESTCALLBACKDATA pData = (PRTMANIFESTCALLBACKDATA)pvUser; 72 return pData->pfnProgressCallback((unsigned)((uPercent + (float)pData->cCurrentFile * 100.0) / (float)pData->cMaxFiles), pData->pvUser); 73 } 58 74 59 75 RTR3DECL(int) RTManifestVerify(const char *pszManifestFile, PRTMANIFESTTEST paTests, size_t cTests, size_t *piFailed) … … 211 227 212 228 213 RTR3DECL(int) RTManifestVerifyFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles, size_t *piFailed )229 RTR3DECL(int) RTManifestVerifyFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles, size_t *piFailed, PFNRTMANIFESTPROGRESS pfnProgressCallback, void *pvUser) 214 230 { 215 231 /* Validate input */ 216 232 AssertPtrReturn(pszManifestFile, VERR_INVALID_POINTER); 217 233 AssertPtrReturn(papszFiles, VERR_INVALID_POINTER); 234 AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER); 235 236 int rc = VINF_SUCCESS; 218 237 219 238 /* Create our compare list */ … … 222 241 return VERR_NO_MEMORY; 223 242 243 RTMANIFESTCALLBACKDATA callback = { pfnProgressCallback, pvUser, cFiles, 0 }; 224 244 /* Fill our compare list */ 225 int rc = VINF_SUCCESS;226 245 for (size_t i = 0; i < cFiles; ++i) 227 246 { 228 247 char *pszDigest; 229 rc = RTSha1Digest(papszFiles[i], &pszDigest); 248 if (pfnProgressCallback) 249 { 250 callback.cCurrentFile = i; 251 rc = RTSha1Digest(papszFiles[i], &pszDigest, rtSHAProgressCallback, &callback); 252 } 253 else 254 rc = RTSha1Digest(papszFiles[i], &pszDigest, NULL, NULL); 230 255 if (RT_FAILURE(rc)) 231 256 break; … … 250 275 251 276 252 RTR3DECL(int) RTManifestWriteFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles )277 RTR3DECL(int) RTManifestWriteFiles(const char *pszManifestFile, const char * const *papszFiles, size_t cFiles, PFNRTMANIFESTPROGRESS pfnProgressCallback, void *pvUser) 253 278 { 254 279 /* Validate input */ 255 280 AssertPtrReturn(pszManifestFile, VERR_INVALID_POINTER); 256 281 AssertPtrReturn(papszFiles, VERR_INVALID_POINTER); 282 AssertPtrNullReturn(pfnProgressCallback, VERR_INVALID_PARAMETER); 257 283 258 284 /* Open a file to stream in */ … … 262 288 return rc; 263 289 290 RTMANIFESTCALLBACKDATA callback = { pfnProgressCallback, pvUser, cFiles, 0 }; 264 291 for (size_t i = 0; i < cFiles; ++i) 265 292 { 266 293 /* Calculate the SHA1 digest of every file */ 267 294 char *pszDigest; 268 rc = RTSha1Digest(papszFiles[i], &pszDigest); 295 if (pfnProgressCallback) 296 { 297 callback.cCurrentFile = i; 298 rc = RTSha1Digest(papszFiles[i], &pszDigest, rtSHAProgressCallback, &callback); 299 } 300 else 301 rc = RTSha1Digest(papszFiles[i], &pszDigest, NULL, NULL); 269 302 if (RT_FAILURE(rc)) 270 303 break;
Note:
See TracChangeset
for help on using the changeset viewer.