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 archive_stream; |
125 ISzAlloc allocImp; | 72 CLookToRead look_stream; |
126 ISzAlloc allocTempImp; | 73 ISzAlloc alloc_imp; |
127 CArchiveDatabaseEx db; | 74 ISzAlloc alloc_temp_imp; |
75 CSzArEx db; | |
128 DWORD ret = NO_ERROR; | 76 DWORD ret = NO_ERROR; |
129 | 77 |
130 archiveStream.File = archive_handle_; | 78 FileInStream_CreateVTable(&archive_stream); |
131 archiveStream.InStream.Read = SzFileReadImp; | 79 LookToRead_CreateVTable(&look_stream, False); |
132 archiveStream.InStream.Seek = SzFileSeekImp; | 80 look_stream.realStream = &archive_stream.s; |
133 allocImp.Alloc = SzAlloc; | 81 LookToRead_Init(&look_stream); |
134 allocImp.Free = SzFree; | 82 |
135 allocTempImp.Alloc = SzAllocTemp; | 83 alloc_imp.Alloc = SzAlloc; |
136 allocTempImp.Free = SzFreeTemp; | 84 alloc_imp.Free = SzFree; |
85 alloc_temp_imp.Alloc = SzAllocTemp; | |
86 alloc_temp_imp.Free = SzFreeTemp; | |
137 | 87 |
138 CrcGenerateTable(); | 88 CrcGenerateTable(); |
139 SzArDbExInit(&db); | 89 SzArEx_Init(&db); |
140 if ((ret = SzArchiveOpen(&archiveStream.InStream, &db, | 90 if ((ret = SzArEx_Open(&db, &look_stream.s, |
141 &allocImp, &allocTempImp)) != SZ_OK) { | 91 &alloc_imp, &alloc_temp_imp)) != SZ_OK) { |
142 LOG(ERROR) << L"Error returned by SzArchiveOpen: " << ret; | 92 LOG(ERROR) << L"Error returned by SzArEx_Open: " << ret; |
143 return ret; | 93 return ret; |
144 } | 94 } |
145 | 95 |
146 Byte *outBuffer = 0; // it must be 0 before first call for each new archive | 96 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 | 97 UInt32 blockIndex = 0xFFFFFFFF; // can have any value if outBuffer = 0 |
148 size_t outBufferSize = 0; // can have any value if outBuffer = 0 | 98 size_t outBufferSize = 0; // can have any value if outBuffer = 0 |
149 | 99 |
150 for (unsigned int i = 0; i < db.Database.NumFiles; i++) { | 100 for (unsigned int i = 0; i < db.db.NumFiles; i++) { |
151 DWORD written; | 101 DWORD written; |
152 size_t offset; | 102 size_t offset; |
153 size_t outSizeProcessed; | 103 size_t outSizeProcessed; |
154 CFileItem *f = db.Database.Files + i; | 104 CSzFileItem* f = db.db.Files + i; |
155 | 105 |
156 if ((ret = SzExtract(&archiveStream.InStream, &db, i, &blockIndex, | 106 if ((ret = SzArEx_Extract(&db, &look_stream.s, i, &blockIndex, |
157 &outBuffer, &outBufferSize, &offset, &outSizeProcessed, | 107 &outBuffer, &outBufferSize, &offset, |
158 &allocImp, &allocTempImp)) != SZ_OK) { | 108 &outSizeProcessed, &alloc_imp, |
159 LOG(ERROR) << L"Error returned by SzExtract: " << ret; | 109 &alloc_temp_imp)) != SZ_OK) { |
110 LOG(ERROR) << L"Error returned by SzArEx_Extract: " << ret; | |
160 break; | 111 break; |
161 } | 112 } |
162 | 113 |
163 FilePath file_path = FilePath(location).Append(UTF8ToWide(f->Name)); | 114 UInt16* name = NULL; |
115 size_t len = SzArEx_GetFileNameUtf16(&db, i, NULL); | |
116 name = reinterpret_cast<UInt16*>(SzAlloc(NULL, len * sizeof(UInt16))); | |
117 if (!name) { | |
118 LOG(ERROR) << L"Can't allocate memory for filename string."; | |
119 break; | |
120 } | |
121 SzArEx_GetFileNameUtf16(&db, i, name); | |
122 std::wstring wname; | |
123 if (!UTF16ToWide(reinterpret_cast<char16*>(name), len - 1, &wname)) { | |
124 LOG(ERROR) << L"Can't convert filename string."; | |
125 break; | |
126 } | |
127 SzFree(NULL, name); | |
128 | |
129 FilePath file_path = FilePath(location).Append(wname); | |
164 if (output_file) | 130 if (output_file) |
165 *output_file = file_path.value(); | 131 *output_file = file_path.value(); |
166 | 132 |
167 // If archive entry is directory create it and move on to the next entry. | 133 // If archive entry is directory create it and move on to the next entry. |
168 if (f->IsDirectory) { | 134 if (f->IsDir) { |
169 CreateDirectory(file_path); | 135 CreateDirectory(file_path); |
170 continue; | 136 continue; |
171 } | 137 } |
172 | 138 |
173 CreateDirectory(file_path.DirName()); | 139 CreateDirectory(file_path.DirName()); |
174 | 140 |
175 HANDLE hFile; | 141 HANDLE hFile; |
176 hFile = CreateFile(file_path.value().c_str(), GENERIC_WRITE, 0, NULL, | 142 hFile = CreateFile(file_path.value().c_str(), GENERIC_WRITE, 0, NULL, |
177 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | 143 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
178 if (hFile == INVALID_HANDLE_VALUE) { | 144 if (hFile == INVALID_HANDLE_VALUE) { |
179 ret = GetLastError(); | 145 ret = GetLastError(); |
180 LOG(ERROR) << L"Error returned by CreateFile: " << ret; | 146 LOG(ERROR) << L"Error returned by CreateFile: " << ret; |
181 break; | 147 break; |
182 } | 148 } |
183 | 149 |
184 if ((!WriteFile(hFile, outBuffer + offset, (DWORD) outSizeProcessed, | 150 if ((!WriteFile(hFile, outBuffer + offset, (DWORD) outSizeProcessed, |
185 &written, NULL)) || | 151 &written, NULL)) || |
186 (written != outSizeProcessed)) { | 152 (written != outSizeProcessed)) { |
187 ret = GetLastError(); | 153 ret = GetLastError(); |
188 CloseHandle(hFile); | 154 CloseHandle(hFile); |
189 LOG(ERROR) << L"Error returned by WriteFile: " << ret; | 155 LOG(ERROR) << L"Error returned by WriteFile: " << ret; |
190 break; | 156 break; |
191 } | 157 } |
192 | 158 |
193 if (f->IsLastWriteTimeDefined) { | 159 if (f->MTimeDefined) { |
194 if (!SetFileTime(hFile, NULL, NULL, | 160 if (!SetFileTime(hFile, NULL, NULL, |
195 (const FILETIME *)&(f->LastWriteTime))) { | 161 reinterpret_cast<FILETIME *>(&(f->MTime)))) { |
robertshield
2011/03/25 03:23:00
micro-nit: FILETIME*
| |
196 ret = GetLastError(); | 162 ret = GetLastError(); |
197 CloseHandle(hFile); | 163 CloseHandle(hFile); |
198 LOG(ERROR) << L"Error returned by SetFileTime: " << ret; | 164 LOG(ERROR) << L"Error returned by SetFileTime: " << ret; |
199 break; | 165 break; |
200 } | 166 } |
201 } | 167 } |
202 if (!CloseHandle(hFile)) { | 168 if (!CloseHandle(hFile)) { |
203 ret = GetLastError(); | 169 ret = GetLastError(); |
204 LOG(ERROR) << L"Error returned by CloseHandle: " << ret; | 170 LOG(ERROR) << L"Error returned by CloseHandle: " << ret; |
205 break; | 171 break; |
206 } | 172 } |
207 } // for loop | 173 } // for loop |
208 | 174 |
209 allocImp.Free(outBuffer); | 175 SzArEx_Free(&db, &alloc_imp); |
210 SzArDbExFree(&db, allocImp.Free); | |
211 return ret; | 176 return ret; |
212 } | 177 } |
213 | 178 |
214 void LzmaUtil::CloseArchive() { | 179 void LzmaUtil::CloseArchive() { |
215 if (archive_handle_) { | 180 if (archive_handle_) { |
216 CloseHandle(archive_handle_); | 181 CloseHandle(archive_handle_); |
217 archive_handle_ = NULL; | 182 archive_handle_ = NULL; |
218 } | 183 } |
219 } | 184 } |
220 | 185 |
221 bool LzmaUtil::CreateDirectory(const FilePath& dir) { | 186 bool LzmaUtil::CreateDirectory(const FilePath& dir) { |
222 bool ret = true; | 187 bool ret = true; |
223 if (directories_created_.find(dir.value()) == directories_created_.end()) { | 188 if (directories_created_.find(dir.value()) == directories_created_.end()) { |
224 ret = file_util::CreateDirectory(dir); | 189 ret = file_util::CreateDirectory(dir); |
225 if (ret) | 190 if (ret) |
226 directories_created_.insert(dir.value()); | 191 directories_created_.insert(dir.value()); |
227 } | 192 } |
228 return ret; | 193 return ret; |
229 } | 194 } |
OLD | NEW |