Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/installer/util/lzma_util.h" | 5 #include "chrome/installer/util/lzma_util.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 | 10 |
| 11 extern "C" { | 11 extern "C" { |
| 12 #include "third_party/lzma_sdk/Archive/7z/7zExtract.h" | 12 #include "third_party/lzma_sdk/C/7z.h" |
| 13 #include "third_party/lzma_sdk/Archive/7z/7zIn.h" | 13 #include "third_party/lzma_sdk/C/7zAlloc.h" |
| 14 #include "third_party/lzma_sdk/7zCrc.h" | 14 #include "third_party/lzma_sdk/C/7zCrc.h" |
| 15 #include "third_party/lzma_sdk/C/7zFile.h" | |
| 16 #include "third_party/lzma_sdk/C/7zVersion.h" | |
| 15 } | 17 } |
| 16 | 18 |
| 17 | 19 |
| 18 namespace { | |
| 19 | |
| 20 typedef struct _CFileInStream { | |
| 21 ISzInStream InStream; | |
| 22 HANDLE File; | |
| 23 } CFileInStream; | |
| 24 | |
| 25 | |
| 26 size_t LzmaReadFile(HANDLE file, void *data, size_t size) { | |
| 27 if (size == 0) | |
| 28 return 0; | |
| 29 | |
| 30 size_t processedSize = 0; | |
| 31 do { | |
| 32 DWORD processedLoc = 0; | |
| 33 BOOL res = ReadFile(file, data, (DWORD) size, &processedLoc, NULL); | |
| 34 data = (void *)((unsigned char *) data + processedLoc); | |
| 35 size -= processedLoc; | |
| 36 processedSize += processedLoc; | |
| 37 if (!res || processedLoc == 0) | |
| 38 break; | |
| 39 } while (size > 0); | |
| 40 | |
| 41 return processedSize; | |
| 42 } | |
| 43 | |
| 44 SZ_RESULT SzFileSeekImp(void *object, CFileSize pos) { | |
| 45 CFileInStream *s = (CFileInStream *) object; | |
| 46 LARGE_INTEGER value; | |
| 47 value.LowPart = (DWORD) pos; | |
| 48 value.HighPart = (LONG) ((UInt64) pos >> 32); | |
| 49 value.LowPart = SetFilePointer(s->File, value.LowPart, &value.HighPart, | |
| 50 FILE_BEGIN); | |
| 51 return ((value.LowPart == 0xFFFFFFFF) && (GetLastError() != NO_ERROR)) ? | |
| 52 SZE_FAIL : SZ_OK; | |
| 53 } | |
| 54 | |
| 55 SZ_RESULT SzFileReadImp(void *object, void **buffer, | |
| 56 size_t maxRequiredSize, size_t *processedSize) { | |
| 57 const int kBufferSize = 1 << 12; | |
| 58 static Byte g_Buffer[kBufferSize]; | |
| 59 if (maxRequiredSize > kBufferSize) | |
| 60 maxRequiredSize = kBufferSize; | |
| 61 | |
| 62 CFileInStream *s = (CFileInStream *) object; | |
| 63 size_t processedSizeLoc; | |
| 64 processedSizeLoc = LzmaReadFile(s->File, g_Buffer, maxRequiredSize); | |
| 65 *buffer = g_Buffer; | |
| 66 if (processedSize != 0) | |
| 67 *processedSize = processedSizeLoc; | |
| 68 return SZ_OK; | |
| 69 } | |
| 70 | |
| 71 } // namespace | |
| 72 | |
| 73 // static | 20 // static |
| 74 int32 LzmaUtil::UnPackArchive(const std::wstring& archive, | 21 int32 LzmaUtil::UnPackArchive(const std::wstring& archive, |
| 75 const std::wstring& output_dir, | 22 const std::wstring& output_dir, |
| 76 std::wstring* output_file) { | 23 std::wstring* output_file) { |
| 77 VLOG(1) << "Opening archive " << archive; | 24 VLOG(1) << "Opening archive " << archive; |
| 78 LzmaUtil lzma_util; | 25 LzmaUtil lzma_util; |
| 79 DWORD ret; | 26 DWORD ret; |
| 80 if ((ret = lzma_util.OpenArchive(archive)) != NO_ERROR) { | 27 if ((ret = lzma_util.OpenArchive(archive)) != NO_ERROR) { |
| 81 LOG(ERROR) << "Unable to open install archive: " << archive | 28 LOG(ERROR) << "Unable to open install archive: " << archive |
| 82 << ", error: " << ret; | 29 << ", error: " << ret; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 | 61 |
| 115 DWORD LzmaUtil::UnPack(const std::wstring& location) { | 62 DWORD LzmaUtil::UnPack(const std::wstring& location) { |
| 116 return UnPack(location, NULL); | 63 return UnPack(location, NULL); |
| 117 } | 64 } |
| 118 | 65 |
| 119 DWORD LzmaUtil::UnPack(const std::wstring& location, | 66 DWORD LzmaUtil::UnPack(const std::wstring& location, |
| 120 std::wstring* output_file) { | 67 std::wstring* output_file) { |
| 121 if (!archive_handle_) | 68 if (!archive_handle_) |
| 122 return ERROR_INVALID_HANDLE; | 69 return ERROR_INVALID_HANDLE; |
| 123 | 70 |
| 124 CFileInStream archiveStream; | 71 CFileInStream archiveStream; |
|
robertshield
2011/03/25 02:34:37
archiveStream -> archive_stream
bradn
2011/03/25 02:53:42
Done.
| |
| 72 CLookToRead lookStream; | |
|
robertshield
2011/03/25 02:34:37
lookStream -> look_stream
bradn
2011/03/25 02:53:42
Done.
bradn
2011/03/25 02:53:42
Done.
| |
| 125 ISzAlloc allocImp; | 73 ISzAlloc allocImp; |
|
robertshield
2011/03/25 02:34:37
alloc_imp
bradn
2011/03/25 02:53:42
Done.
| |
| 126 ISzAlloc allocTempImp; | 74 ISzAlloc allocTempImp; |
|
robertshield
2011/03/25 02:34:37
alloc_temp_imp
bradn
2011/03/25 02:53:42
Done.
| |
| 127 CArchiveDatabaseEx db; | 75 CSzArEx db; |
| 128 DWORD ret = NO_ERROR; | 76 DWORD ret = NO_ERROR; |
| 77 UInt16 *name = NULL; | |
|
robertshield
2011/03/25 02:34:37
nit: UInt16* name
bradn
2011/03/25 02:53:42
Done.
| |
| 78 size_t nameLen = 0; | |
|
robertshield
2011/03/25 02:34:37
name_len
bradn
2011/03/25 02:53:42
Done.
| |
| 129 | 79 |
| 130 archiveStream.File = archive_handle_; | 80 FileInStream_CreateVTable(&archiveStream); |
| 131 archiveStream.InStream.Read = SzFileReadImp; | 81 LookToRead_CreateVTable(&lookStream, False); |
| 132 archiveStream.InStream.Seek = SzFileSeekImp; | 82 lookStream.realStream = &archiveStream.s; |
| 83 LookToRead_Init(&lookStream); | |
| 84 | |
| 133 allocImp.Alloc = SzAlloc; | 85 allocImp.Alloc = SzAlloc; |
| 134 allocImp.Free = SzFree; | 86 allocImp.Free = SzFree; |
| 135 allocTempImp.Alloc = SzAllocTemp; | 87 allocTempImp.Alloc = SzAllocTemp; |
| 136 allocTempImp.Free = SzFreeTemp; | 88 allocTempImp.Free = SzFreeTemp; |
| 137 | 89 |
| 138 CrcGenerateTable(); | 90 CrcGenerateTable(); |
| 139 SzArDbExInit(&db); | 91 SzArEx_Init(&db); |
| 140 if ((ret = SzArchiveOpen(&archiveStream.InStream, &db, | 92 if ((ret = SzArEx_Open(&db, &lookStream.s, |
| 141 &allocImp, &allocTempImp)) != SZ_OK) { | 93 &allocImp, &allocTempImp)) != SZ_OK) { |
| 142 LOG(ERROR) << L"Error returned by SzArchiveOpen: " << ret; | 94 LOG(ERROR) << L"Error returned by SzArEx_Open: " << ret; |
| 143 return ret; | 95 return ret; |
| 144 } | 96 } |
| 145 | 97 |
| 146 Byte *outBuffer = 0; // it must be 0 before first call for each new archive | 98 Byte *outBuffer = 0; // it must be 0 before first call for each new archive |
| 147 UInt32 blockIndex = 0xFFFFFFFF; // can have any value if outBuffer = 0 | 99 UInt32 blockIndex = 0xFFFFFFFF; // can have any value if outBuffer = 0 |
| 148 size_t outBufferSize = 0; // can have any value if outBuffer = 0 | 100 size_t outBufferSize = 0; // can have any value if outBuffer = 0 |
| 149 | 101 |
| 150 for (unsigned int i = 0; i < db.Database.NumFiles; i++) { | 102 for (unsigned int i = 0; i < db.db.NumFiles; i++) { |
| 151 DWORD written; | 103 DWORD written; |
| 152 size_t offset; | 104 size_t offset; |
| 153 size_t outSizeProcessed; | 105 size_t outSizeProcessed; |
| 154 CFileItem *f = db.Database.Files + i; | 106 CSzFileItem *f = db.db.Files + i; |
|
robertshield
2011/03/25 02:34:37
CSzFileItem* f =...
bradn
2011/03/25 02:53:42
Done.
| |
| 155 | 107 |
| 156 if ((ret = SzExtract(&archiveStream.InStream, &db, i, &blockIndex, | 108 if ((ret = SzArEx_Extract(&db, &lookStream.s, i, &blockIndex, |
| 157 &outBuffer, &outBufferSize, &offset, &outSizeProcessed, | 109 &outBuffer, &outBufferSize, &offset, |
| 158 &allocImp, &allocTempImp)) != SZ_OK) { | 110 &outSizeProcessed, &allocImp, |
| 159 LOG(ERROR) << L"Error returned by SzExtract: " << ret; | 111 &allocTempImp)) != SZ_OK) { |
| 112 LOG(ERROR) << L"Error returned by SzArEx_Extract: " << ret; | |
| 160 break; | 113 break; |
| 161 } | 114 } |
| 162 | 115 |
| 163 FilePath file_path = FilePath(location).Append(UTF8ToWide(f->Name)); | 116 size_t len = SzArEx_GetFileNameUtf16(&db, i, NULL); |
| 117 if (len > nameLen) { | |
|
robertshield
2011/03/25 02:34:37
nameLen here can only be 0, and this is also its f
bradn
2011/03/25 02:53:42
Rearranged.
| |
| 118 SzFree(NULL, name); | |
| 119 nameLen = len; | |
| 120 name = (UInt16 *)SzAlloc(NULL, nameLen * sizeof(UInt16)); | |
|
robertshield
2011/03/25 02:34:37
According to style guide, should be reinterpret_ca
bradn
2011/03/25 02:53:42
Done.
| |
| 121 if (!name) { | |
| 122 LOG(ERROR) << L"Can't allocate memory for filename string."; | |
| 123 break; | |
| 124 } | |
| 125 } | |
| 126 SzArEx_GetFileNameUtf16(&db, i, name); | |
| 127 | |
| 128 std::wstring wname; | |
| 129 if (!UTF16ToWide(reinterpret_cast<char16*>(name), len - 1, &wname)) { | |
| 130 LOG(ERROR) << L"Can't convert filename string."; | |
| 131 break; | |
| 132 } | |
| 133 FilePath file_path = FilePath(location).Append(wname); | |
| 164 if (output_file) | 134 if (output_file) |
| 165 *output_file = file_path.value(); | 135 *output_file = file_path.value(); |
| 166 | 136 |
| 167 // If archive entry is directory create it and move on to the next entry. | 137 // If archive entry is directory create it and move on to the next entry. |
| 168 if (f->IsDirectory) { | 138 if (f->IsDir) { |
| 169 CreateDirectory(file_path); | 139 CreateDirectory(file_path); |
| 170 continue; | 140 continue; |
| 171 } | 141 } |
| 172 | 142 |
| 173 CreateDirectory(file_path.DirName()); | 143 CreateDirectory(file_path.DirName()); |
| 174 | 144 |
| 175 HANDLE hFile; | 145 HANDLE hFile; |
| 176 hFile = CreateFile(file_path.value().c_str(), GENERIC_WRITE, 0, NULL, | 146 hFile = CreateFile(file_path.value().c_str(), GENERIC_WRITE, 0, NULL, |
| 177 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | 147 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 178 if (hFile == INVALID_HANDLE_VALUE) { | 148 if (hFile == INVALID_HANDLE_VALUE) { |
| 179 ret = GetLastError(); | 149 ret = GetLastError(); |
| 180 LOG(ERROR) << L"Error returned by CreateFile: " << ret; | 150 LOG(ERROR) << L"Error returned by CreateFile: " << ret; |
| 181 break; | 151 break; |
| 182 } | 152 } |
| 183 | 153 |
| 184 if ((!WriteFile(hFile, outBuffer + offset, (DWORD) outSizeProcessed, | 154 if ((!WriteFile(hFile, outBuffer + offset, (DWORD) outSizeProcessed, |
| 185 &written, NULL)) || | 155 &written, NULL)) || |
| 186 (written != outSizeProcessed)) { | 156 (written != outSizeProcessed)) { |
| 187 ret = GetLastError(); | 157 ret = GetLastError(); |
| 188 CloseHandle(hFile); | 158 CloseHandle(hFile); |
| 189 LOG(ERROR) << L"Error returned by WriteFile: " << ret; | 159 LOG(ERROR) << L"Error returned by WriteFile: " << ret; |
| 190 break; | 160 break; |
| 191 } | 161 } |
| 192 | 162 |
| 193 if (f->IsLastWriteTimeDefined) { | 163 if (f->MTimeDefined) { |
| 194 if (!SetFileTime(hFile, NULL, NULL, | 164 if (!SetFileTime(hFile, NULL, NULL, |
| 195 (const FILETIME *)&(f->LastWriteTime))) { | 165 (const FILETIME *)&(f->MTime))) { |
|
robertshield
2011/03/25 02:34:37
no C-style casts
bradn
2011/03/25 02:53:42
Done.
| |
| 196 ret = GetLastError(); | 166 ret = GetLastError(); |
| 197 CloseHandle(hFile); | 167 CloseHandle(hFile); |
| 198 LOG(ERROR) << L"Error returned by SetFileTime: " << ret; | 168 LOG(ERROR) << L"Error returned by SetFileTime: " << ret; |
| 199 break; | 169 break; |
| 200 } | 170 } |
| 201 } | 171 } |
| 202 if (!CloseHandle(hFile)) { | 172 if (!CloseHandle(hFile)) { |
| 203 ret = GetLastError(); | 173 ret = GetLastError(); |
| 204 LOG(ERROR) << L"Error returned by CloseHandle: " << ret; | 174 LOG(ERROR) << L"Error returned by CloseHandle: " << ret; |
| 205 break; | 175 break; |
| 206 } | 176 } |
| 207 } // for loop | 177 } // for loop |
| 208 | 178 |
| 209 allocImp.Free(outBuffer); | 179 SzFree(NULL, name); |
| 210 SzArDbExFree(&db, allocImp.Free); | 180 SzArEx_Free(&db, &allocImp); |
| 211 return ret; | 181 return ret; |
| 212 } | 182 } |
| 213 | 183 |
| 214 void LzmaUtil::CloseArchive() { | 184 void LzmaUtil::CloseArchive() { |
| 215 if (archive_handle_) { | 185 if (archive_handle_) { |
| 216 CloseHandle(archive_handle_); | 186 CloseHandle(archive_handle_); |
| 217 archive_handle_ = NULL; | 187 archive_handle_ = NULL; |
| 218 } | 188 } |
| 219 } | 189 } |
| 220 | 190 |
| 221 bool LzmaUtil::CreateDirectory(const FilePath& dir) { | 191 bool LzmaUtil::CreateDirectory(const FilePath& dir) { |
| 222 bool ret = true; | 192 bool ret = true; |
| 223 if (directories_created_.find(dir.value()) == directories_created_.end()) { | 193 if (directories_created_.find(dir.value()) == directories_created_.end()) { |
| 224 ret = file_util::CreateDirectory(dir); | 194 ret = file_util::CreateDirectory(dir); |
| 225 if (ret) | 195 if (ret) |
| 226 directories_created_.insert(dir.value()); | 196 directories_created_.insert(dir.value()); |
| 227 } | 197 } |
| 228 return ret; | 198 return ret; |
| 229 } | 199 } |
| OLD | NEW |