* Sync up to trunk head (r64959).
[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_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
241
242 if (hFile == INVALID_HANDLE_VALUE)
243 hFile = 0;
244 else if (node)
245 node->DoExtract = FALSE;
246 }
247
248 HeapFree(GetProcessHeap(), 0, szFullPath);
249 HeapFree(GetProcessHeap(), 0, szDirectory);
250
251 return (INT_PTR) hFile;
252 }
253
254 case fdintCLOSE_FILE_INFO:
255 {
256 FILETIME ft;
257 FILETIME ftLocal;
258 HANDLE handle = (HANDLE) pfdin->hf;
259
260 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
261 return FALSE;
262
263 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
264 return FALSE;
265
266 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
267 return FALSE;
268
269 CloseHandle(handle);
270 return TRUE;
271 }
272
273 default:
274 return 0;
275 }
276 }
277
278 /***********************************************************************
279 * Extract (CABINET.3)
280 *
281 * Extracts the contents of the cabinet file to the specified
282 * destination.
283 *
284 * PARAMS
285 * dest [I/O] Controls the operation of Extract. See NOTES.
286 * szCabName [I] Filename of the cabinet to extract.
287 *
288 * RETURNS
289 * Success: S_OK.
290 * Failure: E_FAIL.
291 *
292 * NOTES
293 * The following members of the dest struct control the operation
294 * of Extract:
295 * FileSize [O] The size of all files extracted up to CurrentFile.
296 * Error [O] The error in case the extract operation fails.
297 * FileList [I] A linked list of filenames. Extract only extracts
298 * files from the cabinet that are in this list.
299 * FileCount [O] Contains the number of files in FileList on
300 * completion.
301 * Operation [I] See Operation.
302 * Destination [I] The destination directory.
303 * CurrentFile [O] The last file extracted.
304 * FilterList [I] A linked list of files that should not be extracted.
305 *
306 * Operation
307 * If Operation contains EXTRACT_FILLFILELIST, then FileList will be
308 * filled with all the files in the cabinet. If Operation contains
309 * EXTRACT_EXTRACTFILES, then only the files in the FileList will
310 * be extracted from the cabinet. EXTRACT_FILLFILELIST can be called
311 * by itself, but EXTRACT_EXTRACTFILES must have a valid FileList
312 * in order to succeed. If Operation contains both EXTRACT_FILLFILELIST
313 * and EXTRACT_EXTRACTFILES, then all the files in the cabinet
314 * will be extracted.
315 */
316 HRESULT WINAPI Extract(SESSION *dest, LPCSTR szCabName)
317 {
318 HRESULT res = S_OK;
319 HFDI hfdi;
320 char *str, *end, *path = NULL, *name = NULL;
321
322 TRACE("(%p, %s)\n", dest, szCabName);
323
324 hfdi = FDICreate(mem_alloc,
325 mem_free,
326 fdi_open,
327 fdi_read,
328 fdi_write,
329 fdi_close,
330 fdi_seek,
331 cpuUNKNOWN,
332 &dest->Error);
333
334 if (!hfdi)
335 return E_FAIL;
336
337 if (GetFileAttributesA(dest->Destination) == INVALID_FILE_ATTRIBUTES)
338 {
339 res = S_OK;
340 goto end;
341 }
342
343 /* split the cabinet name into path + name */
344 str = HeapAlloc(GetProcessHeap(), 0, lstrlenA(szCabName)+1);
345 if (!str)
346 {
347 res = E_OUTOFMEMORY;
348 goto end;
349 }
350 lstrcpyA(str, szCabName);
351
352 if ((end = strrchr(str, '\\')))
353 {
354 path = str;
355 end++;
356 name = HeapAlloc( GetProcessHeap(), 0, strlen(end) + 1 );
357 if (!name)
358 {
359 res = E_OUTOFMEMORY;
360 goto end;
361 }
362 strcpy( name, end );
363 *end = 0;
364 }
365 else
366 {
367 name = str;
368 path = NULL;
369 }
370
371 dest->FileSize = 0;
372
373 if (!FDICopy(hfdi, name, path, 0,
374 fdi_notify_extract, NULL, dest))
375 res = HRESULT_FROM_WIN32(GetLastError());
376
377 end:
378 HeapFree(GetProcessHeap(), 0, path);
379 HeapFree(GetProcessHeap(), 0, name);
380 FDIDestroy(hfdi);
381 return res;
382 }