makefiles: Explicitly create destination dirs when installing symlinks.
[wine/zf.git] / dlls / cabinet / cabinet_main.c
blobf95eca93c52afd2aaa4fdf96e1283cef6843b8b2
1 /*
2 * cabinet.dll main
4 * Copyright 2002 Patrik Stridvall
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.
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.
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
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <string.h>
24 #include <fcntl.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #define NO_SHLWAPI_REG
30 #include "shlwapi.h"
31 #undef NO_SHLWAPI_REG
33 #include "cabinet.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(cabinet);
40 /***********************************************************************
41 * DllGetVersion (CABINET.2)
43 * Retrieves version information of the 'CABINET.DLL'
45 * PARAMS
46 * pdvi [O] pointer to version information structure.
48 * RETURNS
49 * Success: S_OK
50 * Failure: E_INVALIDARG
52 * NOTES
53 * Supposedly returns version from IE6SP1RP1
55 HRESULT WINAPI DllGetVersion (DLLVERSIONINFO *pdvi)
57 WARN("hmmm... not right version number \"5.1.1106.1\"?\n");
59 if (pdvi->cbSize != sizeof(DLLVERSIONINFO)) return E_INVALIDARG;
61 pdvi->dwMajorVersion = 5;
62 pdvi->dwMinorVersion = 1;
63 pdvi->dwBuildNumber = 1106;
64 pdvi->dwPlatformID = 1;
66 return S_OK;
69 /* FDI callback functions */
71 static void * CDECL mem_alloc(ULONG cb)
73 return HeapAlloc(GetProcessHeap(), 0, cb);
76 static void CDECL mem_free(void *memory)
78 HeapFree(GetProcessHeap(), 0, memory);
81 static INT_PTR CDECL fdi_open(char *pszFile, int oflag, int pmode)
83 HANDLE handle;
84 DWORD dwAccess = 0;
85 DWORD dwShareMode = 0;
86 DWORD dwCreateDisposition;
88 switch (oflag & _O_ACCMODE)
90 case _O_RDONLY:
91 dwAccess = GENERIC_READ;
92 dwShareMode = FILE_SHARE_READ | FILE_SHARE_DELETE;
93 break;
94 case _O_WRONLY:
95 dwAccess = GENERIC_WRITE;
96 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
97 break;
98 case _O_RDWR:
99 dwAccess = GENERIC_READ | GENERIC_WRITE;
100 dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
101 break;
104 if (oflag & _O_CREAT)
106 dwCreateDisposition = OPEN_ALWAYS;
107 if (oflag & _O_EXCL) dwCreateDisposition = CREATE_NEW;
108 else if (oflag & _O_TRUNC) dwCreateDisposition = CREATE_ALWAYS;
110 else
112 dwCreateDisposition = OPEN_EXISTING;
113 if (oflag & _O_TRUNC) dwCreateDisposition = TRUNCATE_EXISTING;
116 handle = CreateFileA(pszFile, dwAccess, dwShareMode, NULL,
117 dwCreateDisposition, 0, NULL);
119 return (INT_PTR) handle;
122 static UINT CDECL fdi_read(INT_PTR hf, void *pv, UINT cb)
124 HANDLE handle = (HANDLE) hf;
125 DWORD dwRead;
127 if (ReadFile(handle, pv, cb, &dwRead, NULL))
128 return dwRead;
130 return 0;
133 static UINT CDECL fdi_write(INT_PTR hf, void *pv, UINT cb)
135 HANDLE handle = (HANDLE) hf;
136 DWORD dwWritten;
138 if (WriteFile(handle, pv, cb, &dwWritten, NULL))
139 return dwWritten;
141 return 0;
144 static int CDECL fdi_close(INT_PTR hf)
146 HANDLE handle = (HANDLE) hf;
147 return CloseHandle(handle) ? 0 : -1;
150 static LONG CDECL fdi_seek(INT_PTR hf, LONG dist, int seektype)
152 HANDLE handle = (HANDLE) hf;
153 return SetFilePointer(handle, dist, NULL, seektype);
156 static void fill_file_node(struct FILELIST *pNode, LPCSTR szFilename)
158 pNode->next = NULL;
159 pNode->DoExtract = FALSE;
161 pNode->FileName = HeapAlloc(GetProcessHeap(), 0, strlen(szFilename) + 1);
162 lstrcpyA(pNode->FileName, szFilename);
165 static BOOL file_in_list(struct FILELIST *pNode, LPCSTR szFilename,
166 struct FILELIST **pOut)
168 while (pNode)
170 if (!lstrcmpiA(pNode->FileName, szFilename))
172 if (pOut)
173 *pOut = pNode;
175 return TRUE;
178 pNode = pNode->next;
181 return FALSE;
184 static INT_PTR CDECL fdi_notify_extract(FDINOTIFICATIONTYPE fdint, PFDINOTIFICATION pfdin)
186 switch (fdint)
188 case fdintCOPY_FILE:
190 struct FILELIST *fileList, *node = NULL;
191 SESSION *pDestination = pfdin->pv;
192 LPSTR szFullPath, szDirectory;
193 HANDLE hFile = 0;
194 DWORD dwSize;
196 dwSize = lstrlenA(pDestination->Destination) +
197 lstrlenA("\\") + lstrlenA(pfdin->psz1) + 1;
198 szFullPath = HeapAlloc(GetProcessHeap(), 0, dwSize);
200 lstrcpyA(szFullPath, pDestination->Destination);
201 lstrcatA(szFullPath, "\\");
202 lstrcatA(szFullPath, pfdin->psz1);
204 /* pull out the destination directory string from the full path */
205 dwSize = strrchr(szFullPath, '\\') - szFullPath + 1;
206 szDirectory = HeapAlloc(GetProcessHeap(), 0, dwSize);
207 lstrcpynA(szDirectory, szFullPath, dwSize);
209 pDestination->FileSize += pfdin->cb;
211 if (pDestination->Operation & EXTRACT_FILLFILELIST)
213 fileList = HeapAlloc(GetProcessHeap(), 0,
214 sizeof(struct FILELIST));
216 fill_file_node(fileList, pfdin->psz1);
217 fileList->DoExtract = TRUE;
218 fileList->next = pDestination->FileList;
219 pDestination->FileList = fileList;
220 lstrcpyA(pDestination->CurrentFile, szFullPath);
221 pDestination->FileCount++;
224 if ((pDestination->Operation & EXTRACT_EXTRACTFILES) ||
225 file_in_list(pDestination->FilterList, pfdin->psz1, NULL))
227 /* find the file node */
228 file_in_list(pDestination->FileList, pfdin->psz1, &node);
230 if (node && !node->DoExtract)
232 HeapFree(GetProcessHeap(), 0, szFullPath);
233 HeapFree(GetProcessHeap(), 0, szDirectory);
234 return 0;
237 /* create the destination directory if it doesn't exist */
238 if (GetFileAttributesA(szDirectory) == INVALID_FILE_ATTRIBUTES)
240 char *ptr;
242 for(ptr = szDirectory + strlen(pDestination->Destination)+1; *ptr; ptr++) {
243 if(*ptr == '\\') {
244 *ptr = 0;
245 CreateDirectoryA(szDirectory, NULL);
246 *ptr = '\\';
249 CreateDirectoryA(szDirectory, NULL);
252 hFile = CreateFileA(szFullPath, GENERIC_READ | GENERIC_WRITE, 0, NULL,
253 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
255 if (hFile != INVALID_HANDLE_VALUE && node)
256 node->DoExtract = FALSE;
259 HeapFree(GetProcessHeap(), 0, szFullPath);
260 HeapFree(GetProcessHeap(), 0, szDirectory);
262 return (INT_PTR) hFile;
265 case fdintCLOSE_FILE_INFO:
267 FILETIME ft;
268 FILETIME ftLocal;
269 HANDLE handle = (HANDLE) pfdin->hf;
271 if (!DosDateTimeToFileTime(pfdin->date, pfdin->time, &ft))
272 return FALSE;
274 if (!LocalFileTimeToFileTime(&ft, &ftLocal))
275 return FALSE;
277 if (!SetFileTime(handle, &ftLocal, 0, &ftLocal))
278 return FALSE;
280 CloseHandle(handle);
281 return TRUE;
284 default:
285 return 0;
289 /***********************************************************************
290 * Extract (CABINET.3)
292 * Extracts the contents of the cabinet file to the specified
293 * destination.
295 * PARAMS
296 * dest [I/O] Controls the operation of Extract. See NOTES.
297 * szCabName [I] Filename of the cabinet to extract.
299 * RETURNS
300 * Success: S_OK.
301 * Failure: E_FAIL.
303 * NOTES
304 * The following members of the dest struct control the operation
305 * of Extract:
306 * FileSize [O] The size of all files extracted up to CurrentFile.
307 * Error [O] The error in case the extract operation fails.
308 * FileList [I] A linked list of filenames. Extract only extracts
309 * files from the cabinet that are in this list.
310 * FileCount [O] Contains the number of files in FileList on
311 * completion.
312 * Operation [I] See Operation.
313 * Destination [I] The destination directory.
314 * CurrentFile [O] The last file extracted.
315 * FilterList [I] A linked list of files that should not be extracted.
317 * Operation
318 * If Operation contains EXTRACT_FILLFILELIST, then FileList will be
319 * filled with all the files in the cabinet. If Operation contains
320 * EXTRACT_EXTRACTFILES, then only the files in the FileList will
321 * be extracted from the cabinet. EXTRACT_FILLFILELIST can be called
322 * by itself, but EXTRACT_EXTRACTFILES must have a valid FileList
323 * in order to succeed. If Operation contains both EXTRACT_FILLFILELIST
324 * and EXTRACT_EXTRACTFILES, then all the files in the cabinet
325 * will be extracted.
327 HRESULT WINAPI Extract(SESSION *dest, LPCSTR szCabName)
329 HRESULT res = S_OK;
330 HFDI hfdi;
331 char *str, *end, *path = NULL, *name = NULL;
333 TRACE("(%p, %s)\n", dest, debugstr_a(szCabName));
335 hfdi = FDICreate(mem_alloc,
336 mem_free,
337 fdi_open,
338 fdi_read,
339 fdi_write,
340 fdi_close,
341 fdi_seek,
342 cpuUNKNOWN,
343 &dest->Error);
345 if (!hfdi)
346 return E_FAIL;
348 if (GetFileAttributesA(dest->Destination) == INVALID_FILE_ATTRIBUTES)
350 res = S_OK;
351 goto end;
354 /* split the cabinet name into path + name */
355 str = HeapAlloc(GetProcessHeap(), 0, lstrlenA(szCabName)+1);
356 if (!str)
358 res = E_OUTOFMEMORY;
359 goto end;
361 lstrcpyA(str, szCabName);
363 if ((end = strrchr(str, '\\')))
365 path = str;
366 end++;
367 name = HeapAlloc( GetProcessHeap(), 0, strlen(end) + 1 );
368 if (!name)
370 res = E_OUTOFMEMORY;
371 goto end;
373 strcpy( name, end );
374 *end = 0;
376 else
378 name = str;
379 path = NULL;
382 dest->FileSize = 0;
384 if (!FDICopy(hfdi, name, path, 0,
385 fdi_notify_extract, NULL, dest))
386 res = HRESULT_FROM_WIN32(GetLastError());
388 end:
389 HeapFree(GetProcessHeap(), 0, path);
390 HeapFree(GetProcessHeap(), 0, name);
391 FDIDestroy(hfdi);
392 return res;