a329e4d8de066b6588a9797a7d22ea7b61bd5017
[reactos.git] / dll / win32 / cabinet / cabinet_main.c
1 /*
2 * cabinet.dll main
3 *
4 * Copyright 2002 Patrik Stridvall
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "cabinet.h"
22
23 #define NO_SHLWAPI_REG
24 #include <shlwapi.h>
25 #undef NO_SHLWAPI_REG
26
27 /***********************************************************************
28 * DllGetVersion (CABINET.2)
29 *
30 * Retrieves version information of the 'CABINET.DLL'
31 *
32 * PARAMS
33 * pdvi [O] pointer to version information structure.
34 *
35 * RETURNS
36 * Success: S_OK
37 * Failure: E_INVALIDARG
38 *
39 * NOTES
40 * Supposedly returns version from IE6SP1RP1
41 */
42 HRESULT WINAPI DllGetVersion (DLLVERSIONINFO *pdvi)
43 {
44 WARN("hmmm... not right version number \"5.1.1106.1\"?\n");
45
46 if (pdvi->cbSize != sizeof(DLLVERSIONINFO)) return E_INVALIDARG;
47
48 pdvi->dwMajorVersion = 5;
49 pdvi->dwMinorVersion = 1;
50 pdvi->dwBuildNumber = 1106;
51 pdvi->dwPlatformID = 1;
52
53 return S_OK;
54 }
55
56 /* FDI callback functions */
57
58 static void * CDECL mem_alloc(ULONG cb)
59 {
60 return HeapAlloc(GetProcessHeap(), 0, cb);
61 }
62
63 static void CDECL mem_free(void *memory)
64 {
65 HeapFree(GetProcessHeap(), 0, memory);
66 }
67
68 static INT_PTR CDECL fdi_open(char *pszFile, int oflag, int pmode)
69 {
70 HANDLE handle;
71 DWORD dwAccess = 0;
72 DWORD dwShareMode = 0;
73 DWORD dwCreateDisposition;
74
75 switch (oflag & _O_ACCMODE)
76 {
77 case _O_RDONLY:
78 dwAccess = GENERIC_READ;
79 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
80 break;
81 case _O_WRONLY:
82 dwAccess = GENERIC_WRITE;
83 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
84 break;
85 case _O_RDWR:
86 dwAccess = GENERIC_READ | GENERIC_WRITE;
87 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
88 break;
89 }
90
91 if (oflag & _O_CREAT)
92 {
93 dwCreateDisposition = OPEN_ALWAYS;
94 if (oflag & _O_EXCL) dwCreateDisposition = CREATE_NEW;
95 else if (oflag & _O_TRUNC) dwCreateDisposition = CREATE_ALWAYS;
96 }
97 else
98 {
99 dwCreateDisposition = OPEN_EXISTING;
100 if (oflag & _O_TRUNC) dwCreateDisposition = TRUNCATE_EXISTING;
101 }
102
103 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
104 dwCreateDisposition, 0, NULL);
105
106 return (INT_PTR) handle;
107 }
108
109 static UINT CDECL fdi_read(INT_PTR hf, void *pv, UINT cb)
110 {
111 HANDLE handle = (HANDLE) hf;
112 DWORD dwRead;
113
114 if (ReadFile(handle, pv, cb, &dwRead, NULL))
115 return dwRead;
116
117 return 0;
118 }
119
120 static UINT CDECL fdi_write(INT_PTR hf, void *pv, UINT cb)
121 {
122 HANDLE handle = (HANDLE) hf;
123 DWORD dwWritten;
124
125 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
126 return dwWritten;
127
128 return 0;
129 }
130
131 static int CDECL fdi_close(INT_PTR hf)
132 {
133 HANDLE handle = (HANDLE) hf;
134 return CloseHandle(handle) ? 0 : -1;
135 }
136
137 static LONG CDECL fdi_seek(INT_PTR hf, LONG dist, int seektype)
138 {
139 HANDLE handle = (HANDLE) hf;
140 return SetFilePointer(handle, dist, NULL, seektype);
141 }
142
143 static void fill_file_node(struct FILELIST *pNode, LPCSTR szFilename)
144 {
145 pNode->next = NULL;
146 pNode->DoExtract = FALSE;
147
148 pNode->FileName = HeapAlloc(GetProcessHeap(), 0, strlen(szFilename) + 1);
149 lstrcpyA(pNode->FileName, szFilename);
150 }
151
152 static BOOL file_in_list(struct FILELIST *pNode, LPCSTR szFilename,
153 struct FILELIST **pOut)
154 {
155 while (pNode)
156 {
157 if (!lstrcmpiA(pNode->FileName, szFilename))
158 {
159 if (pOut)
160 *pOut = pNode;
161
162 return TRUE;
163 }
164
165 pNode = pNode->next;
166 }
167
168 return FALSE;
169 }
170
171 static INT_PTR CDECL fdi_notify_extract(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
172 {
173 switch (fdint)
174 {
175 case fdintCOPY_FILE:
176 {
177 struct FILELIST *fileList, *node = NULL;
178 SESSION *pDestination = pfdin->pv;
179 LPSTR szFullPath, szDirectory;
180 HANDLE hFile = 0;
181 DWORD dwSize;
182
183 dwSize = lstrlenA(pDestination->Destination) +
184 lstrlenA("\\") + lstrlenA(pfdin->psz1) + 1;
185 szFullPath = HeapAlloc(GetProcessHeap(), 0, dwSize);
186
187 lstrcpyA(szFullPath, pDestination->Destination);
188 lstrcatA(szFullPath, "\\");
189 lstrcatA(szFullPath, pfdin->psz1);
190
191 /* pull out the destination directory string from the full path */
192 dwSize = strrchr(szFullPath, '\\') - szFullPath + 1;
193 szDirectory = HeapAlloc(GetProcessHeap(), 0, dwSize);
194 lstrcpynA(szDirectory, szFullPath, dwSize);
195
196 pDestination->FileSize += pfdin->cb;
197
198 if (pDestination->Operation & EXTRACT_FILLFILELIST)
199 {
200 fileList = HeapAlloc(GetProcessHeap(), 0,
201 sizeof(struct FILELIST));
202
203 fill_file_node(fileList, pfdin->psz1);
204 fileList->DoExtract = TRUE;
205 fileList->next = pDestination->FileList;
206 pDestination->FileList = fileList;
207 lstrcpyA(pDestination->CurrentFile, szFullPath);
208 pDestination->FileCount++;
209 }
210
211 if ((pDestination->Operation & EXTRACT_EXTRACTFILES) ||
212 file_in_list(pDestination->FilterList, pfdin->psz1, NULL))
213 {
214 /* find the file node */
215 file_in_list(pDestination->FileList, pfdin->psz1, &node);
216
217 if (node && !node->DoExtract)
218 {
219 HeapFree(GetProcessHeap(), 0, szFullPath);
220 HeapFree(GetProcessHeap(), 0, szDirectory);
221 return 0;
222 }
223
224 /* create the destination directory if it doesn't exist */
225 if (GetFileAttributesA(szDirectory) == INVALID_FILE_ATTRIBUTES)
226 {
227 char *ptr;
228
229 for(ptr = szDirectory + strlen(pDestination->Destination)+1; *ptr; ptr++) {
230 if(*ptr == '\\') {
231 *ptr = 0;
232 CreateDirectoryA(szDirectory, NULL);
233 *ptr = '\\';
234 }
235 }
236 CreateDirectoryA(szDirectory, NULL);
237 }
238
239 hFile = CreateFileA(szFullPath, GENERIC_READ | GENERIC_WRITE, 0, NULL,
240 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
241
242 if (hFile != INVALID_HANDLE_VALUE && node)
243 node->DoExtract = FALSE;
244 }
245
246 HeapFree(GetProcessHeap(), 0, szFullPath);
247 HeapFree(GetProcessHeap(), 0, szDirectory);
248
249 return (INT_PTR) hFile;
250 }
251
252 case fdintCLOSE_FILE_INFO:
253 {
254 FILETIME ft;
255 FILETIME ftLocal;
256 HANDLE handle = (HANDLE) pfdin->hf;
257
258 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
259 return FALSE;
260
261 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
262 return FALSE;
263
264 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
265 return FALSE;
266
267 CloseHandle(handle);
268 return TRUE;
269 }
270
271 default:
272 return 0;
273 }
274 }
275
276 /***********************************************************************
277 * Extract (CABINET.3)
278 *
279 * Extracts the contents of the cabinet file to the specified
280 * destination.
281 *
282 * PARAMS
283 * dest [I/O] Controls the operation of Extract. See NOTES.
284 * szCabName [I] Filename of the cabinet to extract.
285 *
286 * RETURNS
287 * Success: S_OK.
288 * Failure: E_FAIL.
289 *
290 * NOTES
291 * The following members of the dest struct control the operation
292 * of Extract:
293 * FileSize [O] The size of all files extracted up to CurrentFile.
294 * Error [O] The error in case the extract operation fails.
295 * FileList [I] A linked list of filenames. Extract only extracts
296 * files from the cabinet that are in this list.
297 * FileCount [O] Contains the number of files in FileList on
298 * completion.
299 * Operation [I] See Operation.
300 * Destination [I] The destination directory.
301 * CurrentFile [O] The last file extracted.
302 * FilterList [I] A linked list of files that should not be extracted.
303 *
304 * Operation
305 * If Operation contains EXTRACT_FILLFILELIST, then FileList will be
306 * filled with all the files in the cabinet. If Operation contains
307 * EXTRACT_EXTRACTFILES, then only the files in the FileList will
308 * be extracted from the cabinet. EXTRACT_FILLFILELIST can be called
309 * by itself, but EXTRACT_EXTRACTFILES must have a valid FileList
310 * in order to succeed. If Operation contains both EXTRACT_FILLFILELIST
311 * and EXTRACT_EXTRACTFILES, then all the files in the cabinet
312 * will be extracted.
313 */
314 HRESULT WINAPI Extract(SESSION *dest, LPCSTR szCabName)
315 {
316 HRESULT res = S_OK;
317 HFDI hfdi;
318 char *str, *end, *path = NULL, *name = NULL;
319
320 TRACE("(%p, %s)\n", dest, debugstr_a(szCabName));
321
322 hfdi = FDICreate(mem_alloc,
323 mem_free,
324 fdi_open,
325 fdi_read,
326 fdi_write,
327 fdi_close,
328 fdi_seek,
329 cpuUNKNOWN,
330 &dest->Error);
331
332 if (!hfdi)
333 return E_FAIL;
334
335 if (GetFileAttributesA(dest->Destination) == INVALID_FILE_ATTRIBUTES)
336 {
337 res = S_OK;
338 goto end;
339 }
340
341 /* split the cabinet name into path + name */
342 str = HeapAlloc(GetProcessHeap(), 0, lstrlenA(szCabName)+1);
343 if (!str)
344 {
345 res = E_OUTOFMEMORY;
346 goto end;
347 }
348 lstrcpyA(str, szCabName);
349
350 if ((end = strrchr(str, '\\')))
351 {
352 path = str;
353 end++;
354 name = HeapAlloc( GetProcessHeap(), 0, strlen(end) + 1 );
355 if (!name)
356 {
357 res = E_OUTOFMEMORY;
358 goto end;
359 }
360 strcpy( name, end );
361 *end = 0;
362 }
363 else
364 {
365 name = str;
366 path = NULL;
367 }
368
369 dest->FileSize = 0;
370
371 if (!FDICopy(hfdi, name, path, 0,
372 fdi_notify_extract, NULL, dest))
373 res = HRESULT_FROM_WIN32(GetLastError());
374
375 end:
376 HeapFree(GetProcessHeap(), 0, path);
377 HeapFree(GetProcessHeap(), 0, name);
378 FDIDestroy(hfdi);
379 return res;
380 }