1 /*
2 * Advpack file functions
3 *
4 * Copyright 2006 James Hawkins
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 <stdarg.h>
22 #include <stdlib.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "winnls.h"
29 #include "winver.h"
30 #include "winternl.h"
31 #include "setupapi.h"
32 #include "advpub.h"
33 #include "fdi.h"
34 #include "wine/debug.h"
35 #include "advpack_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(advpack);
38
39 /* converts an ansi double null-terminated list to a unicode list */
ansi_to_unicode_list(LPCSTR ansi_list)40 static LPWSTR ansi_to_unicode_list(LPCSTR ansi_list)
41 {
42 DWORD len, wlen = 0;
43 LPWSTR list;
44 LPCSTR ptr = ansi_list;
45
46 while (*ptr) ptr += lstrlenA(ptr) + 1;
47 len = ptr + 1 - ansi_list;
48 wlen = MultiByteToWideChar(CP_ACP, 0, ansi_list, len, NULL, 0);
49 list = HeapAlloc(GetProcessHeap(), 0, wlen * sizeof(WCHAR));
50 MultiByteToWideChar(CP_ACP, 0, ansi_list, len, list, wlen);
51 return list;
52 }
53
54 /***********************************************************************
55 * AddDelBackupEntryA (ADVPACK.@)
56 *
57 * See AddDelBackupEntryW.
58 */
AddDelBackupEntryA(LPCSTR lpcszFileList,LPCSTR lpcszBackupDir,LPCSTR lpcszBaseName,DWORD dwFlags)59 HRESULT WINAPI AddDelBackupEntryA(LPCSTR lpcszFileList, LPCSTR lpcszBackupDir,
60 LPCSTR lpcszBaseName, DWORD dwFlags)
61 {
62 UNICODE_STRING backupdir, basename;
63 LPWSTR filelist;
64 LPCWSTR backup;
65 HRESULT res;
66
67 TRACE("(%s, %s, %s, %d)\n", debugstr_a(lpcszFileList),
68 debugstr_a(lpcszBackupDir), debugstr_a(lpcszBaseName), dwFlags);
69
70 if (lpcszFileList)
71 filelist = ansi_to_unicode_list(lpcszFileList);
72 else
73 filelist = NULL;
74
75 RtlCreateUnicodeStringFromAsciiz(&backupdir, lpcszBackupDir);
76 RtlCreateUnicodeStringFromAsciiz(&basename, lpcszBaseName);
77
78 if (lpcszBackupDir)
79 backup = backupdir.Buffer;
80 else
81 backup = NULL;
82
83 res = AddDelBackupEntryW(filelist, backup, basename.Buffer, dwFlags);
84
85 HeapFree(GetProcessHeap(), 0, filelist);
86
87 RtlFreeUnicodeString(&backupdir);
88 RtlFreeUnicodeString(&basename);
89
90 return res;
91 }
92
93 /***********************************************************************
94 * AddDelBackupEntryW (ADVPACK.@)
95 *
96 * Either appends the files in the file list to the backup section of
97 * the specified INI, or deletes the entries from the INI file.
98 *
99 * PARAMS
100 * lpcszFileList [I] NULL-separated list of filenames.
101 * lpcszBackupDir [I] Path of the backup directory.
102 * lpcszBaseName [I] Basename of the INI file.
103 * dwFlags [I] AADBE_ADD_ENTRY adds the entries in the file list
104 * to the INI file, while AADBE_DEL_ENTRY removes
105 * the entries from the INI file.
106 *
107 * RETURNS
108 * S_OK in all cases.
109 *
110 * NOTES
111 * If the INI file does not exist before adding entries to it, the file
112 * will be created.
113 *
114 * If lpcszBackupDir is NULL, the INI file is assumed to exist in
115 * c:\windows or created there if it does not exist.
116 */
AddDelBackupEntryW(LPCWSTR lpcszFileList,LPCWSTR lpcszBackupDir,LPCWSTR lpcszBaseName,DWORD dwFlags)117 HRESULT WINAPI AddDelBackupEntryW(LPCWSTR lpcszFileList, LPCWSTR lpcszBackupDir,
118 LPCWSTR lpcszBaseName, DWORD dwFlags)
119 {
120 WCHAR szIniPath[MAX_PATH];
121 LPCWSTR szString = NULL;
122
123 static const WCHAR szBackupEntry[] = {
124 '-','1',',','0',',','0',',','0',',','0',',','0',',','-','1',0
125 };
126
127 static const WCHAR backslash[] = {'\\',0};
128 static const WCHAR ini[] = {'.','i','n','i',0};
129 static const WCHAR backup[] = {'b','a','c','k','u','p',0};
130
131 TRACE("(%s, %s, %s, %d)\n", debugstr_w(lpcszFileList),
132 debugstr_w(lpcszBackupDir), debugstr_w(lpcszBaseName), dwFlags);
133
134 if (!lpcszFileList || !*lpcszFileList)
135 return S_OK;
136
137 if (lpcszBackupDir)
138 lstrcpyW(szIniPath, lpcszBackupDir);
139 else
140 GetWindowsDirectoryW(szIniPath, MAX_PATH);
141
142 lstrcatW(szIniPath, backslash);
143 lstrcatW(szIniPath, lpcszBaseName);
144 lstrcatW(szIniPath, ini);
145
146 SetFileAttributesW(szIniPath, FILE_ATTRIBUTE_NORMAL);
147
148 if (dwFlags & AADBE_ADD_ENTRY)
149 szString = szBackupEntry;
150 else if (dwFlags & AADBE_DEL_ENTRY)
151 szString = NULL;
152
153 /* add or delete the INI entries */
154 while (*lpcszFileList)
155 {
156 WritePrivateProfileStringW(backup, lpcszFileList, szString, szIniPath);
157 lpcszFileList += lstrlenW(lpcszFileList) + 1;
158 }
159
160 /* hide the INI file */
161 SetFileAttributesW(szIniPath, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN);
162
163 return S_OK;
164 }
165
166 /* FIXME: this is only for the local case, X:\ */
167 #define ROOT_LENGTH 3
168
pQuietQueueCallback(PVOID Context,UINT Notification,UINT_PTR Param1,UINT_PTR Param2)169 static UINT CALLBACK pQuietQueueCallback(PVOID Context, UINT Notification,
170 UINT_PTR Param1, UINT_PTR Param2)
171 {
172 return 1;
173 }
174
pQueueCallback(PVOID Context,UINT Notification,UINT_PTR Param1,UINT_PTR Param2)175 static UINT CALLBACK pQueueCallback(PVOID Context, UINT Notification,
176 UINT_PTR Param1, UINT_PTR Param2)
177 {
178 /* only be verbose for error notifications */
179 if (!Notification ||
180 Notification == SPFILENOTIFY_RENAMEERROR ||
181 Notification == SPFILENOTIFY_DELETEERROR ||
182 Notification == SPFILENOTIFY_COPYERROR)
183 {
184 return SetupDefaultQueueCallbackW(Context, Notification,
185 Param1, Param2);
186 }
187
188 return 1;
189 }
190
191 /***********************************************************************
192 * AdvInstallFileA (ADVPACK.@)
193 *
194 * See AdvInstallFileW.
195 */
AdvInstallFileA(HWND hwnd,LPCSTR lpszSourceDir,LPCSTR lpszSourceFile,LPCSTR lpszDestDir,LPCSTR lpszDestFile,DWORD dwFlags,DWORD dwReserved)196 HRESULT WINAPI AdvInstallFileA(HWND hwnd, LPCSTR lpszSourceDir, LPCSTR lpszSourceFile,
197 LPCSTR lpszDestDir, LPCSTR lpszDestFile,
198 DWORD dwFlags, DWORD dwReserved)
199 {
200 UNICODE_STRING sourcedir, sourcefile;
201 UNICODE_STRING destdir, destfile;
202 HRESULT res;
203
204 TRACE("(%p, %s, %s, %s, %s, %d, %d)\n", hwnd, debugstr_a(lpszSourceDir),
205 debugstr_a(lpszSourceFile), debugstr_a(lpszDestDir),
206 debugstr_a(lpszDestFile), dwFlags, dwReserved);
207
208 if (!lpszSourceDir || !lpszSourceFile || !lpszDestDir)
209 return E_INVALIDARG;
210
211 RtlCreateUnicodeStringFromAsciiz(&sourcedir, lpszSourceDir);
212 RtlCreateUnicodeStringFromAsciiz(&sourcefile, lpszSourceFile);
213 RtlCreateUnicodeStringFromAsciiz(&destdir, lpszDestDir);
214 RtlCreateUnicodeStringFromAsciiz(&destfile, lpszDestFile);
215
216 res = AdvInstallFileW(hwnd, sourcedir.Buffer, sourcefile.Buffer,
217 destdir.Buffer, destfile.Buffer, dwFlags, dwReserved);
218
219 RtlFreeUnicodeString(&sourcedir);
220 RtlFreeUnicodeString(&sourcefile);
221 RtlFreeUnicodeString(&destdir);
222 RtlFreeUnicodeString(&destfile);
223
224 return res;
225 }
226
227 /***********************************************************************
228 * AdvInstallFileW (ADVPACK.@)
229 *
230 * Copies a file from the source to a destination.
231 *
232 * PARAMS
233 * hwnd [I] Handle to the window used for messages.
234 * lpszSourceDir [I] Source directory.
235 * lpszSourceFile [I] Source filename.
236 * lpszDestDir [I] Destination directory.
237 * lpszDestFile [I] Optional destination filename.
238 * dwFlags [I] See advpub.h.
239 * dwReserved [I] Reserved. Must be 0.
240 *
241 * RETURNS
242 * Success: S_OK.
243 * Failure: E_FAIL.
244 *
245 * NOTES
246 * If lpszDestFile is NULL, the destination filename is the same as
247 * lpszSourceFIle.
248 */
AdvInstallFileW(HWND hwnd,LPCWSTR lpszSourceDir,LPCWSTR lpszSourceFile,LPCWSTR lpszDestDir,LPCWSTR lpszDestFile,DWORD dwFlags,DWORD dwReserved)249 HRESULT WINAPI AdvInstallFileW(HWND hwnd, LPCWSTR lpszSourceDir, LPCWSTR lpszSourceFile,
250 LPCWSTR lpszDestDir, LPCWSTR lpszDestFile,
251 DWORD dwFlags, DWORD dwReserved)
252 {
253 PSP_FILE_CALLBACK_W pFileCallback;
254 LPWSTR szDestFilename;
255 LPCWSTR szPath;
256 WCHAR szRootPath[ROOT_LENGTH];
257 DWORD dwLen, dwLastError;
258 HSPFILEQ fileQueue;
259 PVOID pContext;
260
261 TRACE("(%p, %s, %s, %s, %s, %d, %d)\n", hwnd, debugstr_w(lpszSourceDir),
262 debugstr_w(lpszSourceFile), debugstr_w(lpszDestDir),
263 debugstr_w(lpszDestFile), dwFlags, dwReserved);
264
265 if (!lpszSourceDir || !lpszSourceFile || !lpszDestDir)
266 return E_INVALIDARG;
267
268 fileQueue = SetupOpenFileQueue();
269 if (fileQueue == INVALID_HANDLE_VALUE)
270 return HRESULT_FROM_WIN32(GetLastError());
271
272 pContext = NULL;
273 dwLastError = ERROR_SUCCESS;
274
275 lstrcpynW(szRootPath, lpszSourceDir, ROOT_LENGTH);
276 szPath = lpszSourceDir + ROOT_LENGTH;
277
278 /* use lpszSourceFile as destination filename if lpszDestFile is NULL */
279 if (lpszDestFile)
280 {
281 dwLen = lstrlenW(lpszDestFile);
282 szDestFilename = HeapAlloc(GetProcessHeap(), 0, (dwLen+1) * sizeof(WCHAR));
283 lstrcpyW(szDestFilename, lpszDestFile);
284 }
285 else
286 {
287 dwLen = lstrlenW(lpszSourceFile);
288 szDestFilename = HeapAlloc(GetProcessHeap(), 0, (dwLen+1) * sizeof(WCHAR));
289 lstrcpyW(szDestFilename, lpszSourceFile);
290 }
291
292 /* add the file copy operation to the setup queue */
293 if (!SetupQueueCopyW(fileQueue, szRootPath, szPath, lpszSourceFile, NULL,
294 NULL, lpszDestDir, szDestFilename, dwFlags))
295 {
296 dwLastError = GetLastError();
297 goto done;
298 }
299
300 pContext = SetupInitDefaultQueueCallbackEx(hwnd, INVALID_HANDLE_VALUE,
301 0, 0, NULL);
302 if (!pContext)
303 {
304 dwLastError = GetLastError();
305 goto done;
306 }
307
308 /* don't output anything for AIF_QUIET */
309 if (dwFlags & AIF_QUIET)
310 pFileCallback = pQuietQueueCallback;
311 else
312 pFileCallback = pQueueCallback;
313
314 /* perform the file copy */
315 if (!SetupCommitFileQueueW(hwnd, fileQueue, pFileCallback, pContext))
316 {
317 dwLastError = GetLastError();
318 goto done;
319 }
320
321 done:
322 SetupTermDefaultQueueCallback(pContext);
323 SetupCloseFileQueue(fileQueue);
324
325 HeapFree(GetProcessHeap(), 0, szDestFilename);
326
327 return HRESULT_FROM_WIN32(dwLastError);
328 }
329
DELNODE_recurse_dirtree(LPWSTR fname,DWORD flags)330 static HRESULT DELNODE_recurse_dirtree(LPWSTR fname, DWORD flags)
331 {
332 DWORD fattrs = GetFileAttributesW(fname);
333 HRESULT ret = E_FAIL;
334
335 static const WCHAR asterisk[] = {'*',0};
336 static const WCHAR dot[] = {'.',0};
337 static const WCHAR dotdot[] = {'.','.',0};
338
339 if (fattrs & FILE_ATTRIBUTE_DIRECTORY)
340 {
341 HANDLE hFindFile;
342 WIN32_FIND_DATAW w32fd;
343 BOOL done = TRUE;
344 int fname_len = lstrlenW(fname);
345 #ifdef __REACTOS__
346 if (flags & ADN_DEL_IF_EMPTY)
347 goto deleteinitialdirectory;
348 #endif
349
350 /* Generate a path with wildcard suitable for iterating */
351 if (fname_len && fname[fname_len-1] != '\\') fname[fname_len++] = '\\';
352 lstrcpyW(fname + fname_len, asterisk);
353
354 if ((hFindFile = FindFirstFileW(fname, &w32fd)) != INVALID_HANDLE_VALUE)
355 {
356 /* Iterate through the files in the directory */
357 for (done = FALSE; !done; done = !FindNextFileW(hFindFile, &w32fd))
358 {
359 TRACE("%s\n", debugstr_w(w32fd.cFileName));
360 if (lstrcmpW(dot, w32fd.cFileName) != 0 &&
361 lstrcmpW(dotdot, w32fd.cFileName) != 0)
362 {
363 lstrcpyW(fname + fname_len, w32fd.cFileName);
364 if (DELNODE_recurse_dirtree(fname, flags) != S_OK)
365 {
366 break; /* Failure */
367 }
368 }
369 }
370 FindClose(hFindFile);
371 }
372
373 /* We're done with this directory, so restore the old path without wildcard */
374 *(fname + fname_len) = '\0';
375
376 if (done)
377 {
378 #ifdef __REACTOS__
379 deleteinitialdirectory:
380 #endif
381 TRACE("%s: directory\n", debugstr_w(fname));
382 #ifdef __REACTOS__
383 SetFileAttributesW(fname, FILE_ATTRIBUTE_NORMAL);
384 if (RemoveDirectoryW(fname))
385 #else
386 if (SetFileAttributesW(fname, FILE_ATTRIBUTE_NORMAL) && RemoveDirectoryW(fname))
387 #endif
388 {
389 ret = S_OK;
390 }
391 }
392 }
393 else
394 {
395 TRACE("%s: file\n", debugstr_w(fname));
396 #ifdef __REACTOS__
397 SetFileAttributesW(fname, FILE_ATTRIBUTE_NORMAL);
398 if (DeleteFileW(fname))
399 #else
400 if (SetFileAttributesW(fname, FILE_ATTRIBUTE_NORMAL) && DeleteFileW(fname))
401 #endif
402 {
403 ret = S_OK;
404 }
405 }
406
407 return ret;
408 }
409
410 /***********************************************************************
411 * DelNodeA (ADVPACK.@)
412 *
413 * See DelNodeW.
414 */
DelNodeA(LPCSTR pszFileOrDirName,DWORD dwFlags)415 HRESULT WINAPI DelNodeA(LPCSTR pszFileOrDirName, DWORD dwFlags)
416 {
417 UNICODE_STRING fileordirname;
418 HRESULT res;
419
420 TRACE("(%s, %d)\n", debugstr_a(pszFileOrDirName), dwFlags);
421
422 RtlCreateUnicodeStringFromAsciiz(&fileordirname, pszFileOrDirName);
423
424 res = DelNodeW(fileordirname.Buffer, dwFlags);
425
426 RtlFreeUnicodeString(&fileordirname);
427
428 return res;
429 }
430
431 /***********************************************************************
432 * DelNodeW (ADVPACK.@)
433 *
434 * Deletes a file or directory
435 *
436 * PARAMS
437 * pszFileOrDirName [I] Name of file or directory to delete
438 * dwFlags [I] Flags; see include/advpub.h
439 *
440 * RETURNS
441 * Success: S_OK
442 * Failure: E_FAIL
443 *
444 * BUGS
445 * - Ignores flags
446 * - Native version apparently does a lot of checking to make sure
447 * we're not trying to delete a system directory etc.
448 */
DelNodeW(LPCWSTR pszFileOrDirName,DWORD dwFlags)449 HRESULT WINAPI DelNodeW(LPCWSTR pszFileOrDirName, DWORD dwFlags)
450 {
451 WCHAR fname[MAX_PATH];
452 HRESULT ret = E_FAIL;
453
454 TRACE("(%s, %d)\n", debugstr_w(pszFileOrDirName), dwFlags);
455
456 #ifdef __REACTOS__
457 if (dwFlags & ~ADN_DEL_IF_EMPTY)
458 FIXME("Flags %#x ignored!\n", dwFlags & ~ADN_DEL_IF_EMPTY);
459 #else
460 if (dwFlags)
461 FIXME("Flags ignored!\n");
462 #endif
463
464 if (pszFileOrDirName && *pszFileOrDirName)
465 {
466 lstrcpyW(fname, pszFileOrDirName);
467
468 /* TODO: Should check for system directory deletion etc. here */
469
470 ret = DELNODE_recurse_dirtree(fname, dwFlags);
471 }
472
473 return ret;
474 }
475
476 /***********************************************************************
477 * DelNodeRunDLL32A (ADVPACK.@)
478 *
479 * See DelNodeRunDLL32W.
480 */
DelNodeRunDLL32A(HWND hWnd,HINSTANCE hInst,LPSTR cmdline,INT show)481 HRESULT WINAPI DelNodeRunDLL32A(HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show)
482 {
483 UNICODE_STRING params;
484 HRESULT hr;
485
486 TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_a(cmdline), show);
487
488 RtlCreateUnicodeStringFromAsciiz(¶ms, cmdline);
489
490 hr = DelNodeRunDLL32W(hWnd, hInst, params.Buffer, show);
491
492 RtlFreeUnicodeString(¶ms);
493
494 return hr;
495 }
496
497 /***********************************************************************
498 * DelNodeRunDLL32W (ADVPACK.@)
499 *
500 * Deletes a file or directory, WinMain style.
501 *
502 * PARAMS
503 * hWnd [I] Handle to the window used for the display.
504 * hInst [I] Instance of the process.
505 * cmdline [I] Contains parameters in the order FileOrDirName,Flags.
506 * show [I] How the window should be shown.
507 *
508 * RETURNS
509 * Success: S_OK.
510 * Failure: E_FAIL.
511 */
DelNodeRunDLL32W(HWND hWnd,HINSTANCE hInst,LPWSTR cmdline,INT show)512 HRESULT WINAPI DelNodeRunDLL32W(HWND hWnd, HINSTANCE hInst, LPWSTR cmdline, INT show)
513 {
514 LPWSTR szFilename, szFlags;
515 LPWSTR cmdline_copy, cmdline_ptr;
516 DWORD dwFlags = 0;
517 HRESULT res;
518
519 TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_w(cmdline), show);
520
521 cmdline_copy = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmdline) + 1) * sizeof(WCHAR));
522 cmdline_ptr = cmdline_copy;
523 lstrcpyW(cmdline_copy, cmdline);
524
525 /* get the parameters at indexes 0 and 1 respectively */
526 szFilename = get_parameter(&cmdline_ptr, ',', TRUE);
527 szFlags = get_parameter(&cmdline_ptr, ',', TRUE);
528
529 if (szFlags)
530 dwFlags = wcstol(szFlags, NULL, 10);
531
532 res = DelNodeW(szFilename, dwFlags);
533
534 HeapFree(GetProcessHeap(), 0, cmdline_copy);
535
536 return res;
537 }
538
539 /* The following definitions were copied from dlls/cabinet/cabinet.h */
540
541 /* SESSION Operation */
542 #define EXTRACT_FILLFILELIST 0x00000001
543 #define EXTRACT_EXTRACTFILES 0x00000002
544
545 struct FILELIST{
546 LPSTR FileName;
547 struct FILELIST *next;
548 BOOL DoExtract;
549 };
550
551 typedef struct {
552 INT FileSize;
553 ERF Error;
554 struct FILELIST *FileList;
555 INT FileCount;
556 INT Operation;
557 CHAR Destination[MAX_PATH];
558 CHAR CurrentFile[MAX_PATH];
559 CHAR Reserved[MAX_PATH];
560 struct FILELIST *FilterList;
561 } SESSION;
562
563 static HRESULT (WINAPI *pExtract)(SESSION*, LPCSTR);
564
565 /* removes legal characters before and after file list, and
566 * converts the file list to a NULL-separated list
567 */
convert_file_list(LPCSTR FileList,DWORD * dwNumFiles)568 static LPSTR convert_file_list(LPCSTR FileList, DWORD *dwNumFiles)
569 {
570 DWORD dwLen;
571 const char *first = FileList;
572 const char *last = FileList + strlen(FileList) - 1;
573 LPSTR szConvertedList, temp;
574
575 /* any number of these chars before the list is OK */
576 while (first < last && (*first == ' ' || *first == '\t' || *first == ':'))
577 first++;
578
579 /* any number of these chars after the list is OK */
580 while (last > first && (*last == ' ' || *last == '\t' || *last == ':'))
581 last--;
582
583 if (first == last)
584 return NULL;
585
586 dwLen = last - first + 3; /* room for double-null termination */
587 szConvertedList = HeapAlloc(GetProcessHeap(), 0, dwLen);
588 lstrcpynA(szConvertedList, first, dwLen - 1);
589 szConvertedList[dwLen - 1] = '\0';
590
591 /* empty list */
592 if (!szConvertedList[0])
593 {
594 HeapFree(GetProcessHeap(), 0, szConvertedList);
595 return NULL;
596 }
597
598 *dwNumFiles = 1;
599
600 /* convert the colons to double-null termination */
601 temp = szConvertedList;
602 while (*temp)
603 {
604 if (*temp == ':')
605 {
606 *temp = '\0';
607 (*dwNumFiles)++;
608 }
609
610 temp++;
611 }
612
613 return szConvertedList;
614 }
615
free_file_node(struct FILELIST * pNode)616 static void free_file_node(struct FILELIST *pNode)
617 {
618 HeapFree(GetProcessHeap(), 0, pNode->FileName);
619 HeapFree(GetProcessHeap(), 0, pNode);
620 }
621
622 /* determines whether szFile is in the NULL-separated szFileList */
file_in_list(LPCSTR szFile,LPCSTR szFileList)623 static BOOL file_in_list(LPCSTR szFile, LPCSTR szFileList)
624 {
625 DWORD dwLen = lstrlenA(szFile);
626 DWORD dwTestLen;
627
628 while (*szFileList)
629 {
630 dwTestLen = lstrlenA(szFileList);
631
632 if (dwTestLen == dwLen)
633 {
634 if (!lstrcmpiA(szFile, szFileList))
635 return TRUE;
636 }
637
638 szFileList += dwTestLen + 1;
639 }
640
641 return FALSE;
642 }
643
644
645 /* returns the number of files that are in both the linked list and szFileList */
fill_file_list(SESSION * session,LPCSTR szCabName,LPCSTR szFileList)646 static DWORD fill_file_list(SESSION *session, LPCSTR szCabName, LPCSTR szFileList)
647 {
648 DWORD dwNumFound = 0;
649 struct FILELIST *pNode;
650
651 session->Operation |= EXTRACT_FILLFILELIST;
652 if (pExtract(session, szCabName) != S_OK)
653 {
654 session->Operation &= ~EXTRACT_FILLFILELIST;
655 return -1;
656 }
657
658 pNode = session->FileList;
659 while (pNode)
660 {
661 if (!file_in_list(pNode->FileName, szFileList))
662 pNode->DoExtract = FALSE;
663 else
664 dwNumFound++;
665
666 pNode = pNode->next;
667 }
668
669 session->Operation &= ~EXTRACT_FILLFILELIST;
670 return dwNumFound;
671 }
672
free_file_list(SESSION * session)673 static void free_file_list(SESSION* session)
674 {
675 struct FILELIST *next, *curr = session->FileList;
676
677 while (curr)
678 {
679 next = curr->next;
680 free_file_node(curr);
681 curr = next;
682 }
683 }
684
685 /***********************************************************************
686 * ExtractFilesA (ADVPACK.@)
687 *
688 * Extracts the specified files from a cab archive into
689 * a destination directory.
690 *
691 * PARAMS
692 * CabName [I] Filename of the cab archive.
693 * ExpandDir [I] Destination directory for the extracted files.
694 * Flags [I] Reserved.
695 * FileList [I] Optional list of files to extract. See NOTES.
696 * LReserved [I] Reserved. Must be NULL.
697 * Reserved [I] Reserved. Must be 0.
698 *
699 * RETURNS
700 * Success: S_OK.
701 * Failure: E_FAIL.
702 *
703 * NOTES
704 * FileList is a colon-separated list of filenames. If FileList is
705 * non-NULL, only the files in the list will be extracted from the
706 * cab file, otherwise all files will be extracted. Any number of
707 * spaces, tabs, or colons can be before or after the list, but
708 * the list itself must only be separated by colons.
709 */
ExtractFilesA(LPCSTR CabName,LPCSTR ExpandDir,DWORD Flags,LPCSTR FileList,LPVOID LReserved,DWORD Reserved)710 HRESULT WINAPI ExtractFilesA(LPCSTR CabName, LPCSTR ExpandDir, DWORD Flags,
711 LPCSTR FileList, LPVOID LReserved, DWORD Reserved)
712 {
713 SESSION session;
714 HMODULE hCabinet;
715 HRESULT res = S_OK;
716 DWORD dwFileCount = 0;
717 DWORD dwFilesFound = 0;
718 LPSTR szConvertedList = NULL;
719
720 TRACE("(%s, %s, %d, %s, %p, %d)\n", debugstr_a(CabName), debugstr_a(ExpandDir),
721 Flags, debugstr_a(FileList), LReserved, Reserved);
722
723 if (!CabName || !ExpandDir)
724 return E_INVALIDARG;
725
726 if (GetFileAttributesA(ExpandDir) == INVALID_FILE_ATTRIBUTES)
727 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
728
729 hCabinet = LoadLibraryA("cabinet.dll");
730 if (!hCabinet)
731 return E_FAIL;
732
733 ZeroMemory(&session, sizeof(SESSION));
734
735 pExtract = (void *)GetProcAddress(hCabinet, "Extract");
736 if (!pExtract)
737 {
738 res = E_FAIL;
739 goto done;
740 }
741
742 lstrcpyA(session.Destination, ExpandDir);
743
744 if (FileList)
745 {
746 szConvertedList = convert_file_list(FileList, &dwFileCount);
747 if (!szConvertedList)
748 {
749 res = E_FAIL;
750 goto done;
751 }
752
753 dwFilesFound = fill_file_list(&session, CabName, szConvertedList);
754 if (dwFilesFound != dwFileCount)
755 {
756 res = E_FAIL;
757 goto done;
758 }
759 }
760 else
761 session.Operation |= EXTRACT_FILLFILELIST;
762
763 session.Operation |= EXTRACT_EXTRACTFILES;
764 res = pExtract(&session, CabName);
765
766 done:
767 free_file_list(&session);
768 FreeLibrary(hCabinet);
769 HeapFree(GetProcessHeap(), 0, szConvertedList);
770
771 return res;
772 }
773
774 /***********************************************************************
775 * ExtractFilesW (ADVPACK.@)
776 *
777 * Extracts the specified files from a cab archive into
778 * a destination directory.
779 *
780 * PARAMS
781 * CabName [I] Filename of the cab archive.
782 * ExpandDir [I] Destination directory for the extracted files.
783 * Flags [I] Reserved.
784 * FileList [I] Optional list of files to extract. See NOTES.
785 * LReserved [I] Reserved. Must be NULL.
786 * Reserved [I] Reserved. Must be 0.
787 *
788 * RETURNS
789 * Success: S_OK.
790 * Failure: E_FAIL.
791 *
792 * NOTES
793 * FileList is a colon-separated list of filenames. If FileList is
794 * non-NULL, only the files in the list will be extracted from the
795 * cab file, otherwise all files will be extracted. Any number of
796 * spaces, tabs, or colons can be before or after the list, but
797 * the list itself must only be separated by colons.
798 *
799 * BUGS
800 * Unimplemented.
801 */
ExtractFilesW(LPCWSTR CabName,LPCWSTR ExpandDir,DWORD Flags,LPCWSTR FileList,LPVOID LReserved,DWORD Reserved)802 HRESULT WINAPI ExtractFilesW(LPCWSTR CabName, LPCWSTR ExpandDir, DWORD Flags,
803 LPCWSTR FileList, LPVOID LReserved, DWORD Reserved)
804 {
805 char *cab_name = NULL, *expand_dir = NULL, *file_list = NULL;
806 HRESULT hres = S_OK;
807
808 TRACE("(%s, %s, %d, %s, %p, %d)\n", debugstr_w(CabName), debugstr_w(ExpandDir),
809 Flags, debugstr_w(FileList), LReserved, Reserved);
810
811 if(CabName) {
812 cab_name = heap_strdupWtoA(CabName);
813 if(!cab_name)
814 return E_OUTOFMEMORY;
815 }
816
817 if(ExpandDir) {
818 expand_dir = heap_strdupWtoA(ExpandDir);
819 if(!expand_dir)
820 hres = E_OUTOFMEMORY;
821 }
822
823 if(SUCCEEDED(hres) && FileList) {
824 file_list = heap_strdupWtoA(FileList);
825 if(!file_list)
826 hres = E_OUTOFMEMORY;
827 }
828
829 /* cabinet.dll, which does the real job of extracting files, doesn't have UNICODE API,
830 so we need W->A conversion at some point anyway. */
831 if(SUCCEEDED(hres))
832 hres = ExtractFilesA(cab_name, expand_dir, Flags, file_list, LReserved, Reserved);
833
834 heap_free(cab_name);
835 heap_free(expand_dir);
836 heap_free(file_list);
837 return hres;
838 }
839
840 /***********************************************************************
841 * FileSaveMarkNotExistA (ADVPACK.@)
842 *
843 * See FileSaveMarkNotExistW.
844 */
FileSaveMarkNotExistA(LPSTR pszFileList,LPSTR pszDir,LPSTR pszBaseName)845 HRESULT WINAPI FileSaveMarkNotExistA(LPSTR pszFileList, LPSTR pszDir, LPSTR pszBaseName)
846 {
847 TRACE("(%s, %s, %s)\n", debugstr_a(pszFileList),
848 debugstr_a(pszDir), debugstr_a(pszBaseName));
849
850 return AddDelBackupEntryA(pszFileList, pszDir, pszBaseName, AADBE_DEL_ENTRY);
851 }
852
853 /***********************************************************************
854 * FileSaveMarkNotExistW (ADVPACK.@)
855 *
856 * Marks the files in the file list as not existing so they won't be
857 * backed up during a save.
858 *
859 * PARAMS
860 * pszFileList [I] NULL-separated list of filenames.
861 * pszDir [I] Path of the backup directory.
862 * pszBaseName [I] Basename of the INI file.
863 *
864 * RETURNS
865 * Success: S_OK.
866 * Failure: E_FAIL.
867 */
FileSaveMarkNotExistW(LPWSTR pszFileList,LPWSTR pszDir,LPWSTR pszBaseName)868 HRESULT WINAPI FileSaveMarkNotExistW(LPWSTR pszFileList, LPWSTR pszDir, LPWSTR pszBaseName)
869 {
870 TRACE("(%s, %s, %s)\n", debugstr_w(pszFileList),
871 debugstr_w(pszDir), debugstr_w(pszBaseName));
872
873 return AddDelBackupEntryW(pszFileList, pszDir, pszBaseName, AADBE_DEL_ENTRY);
874 }
875
876 /***********************************************************************
877 * FileSaveRestoreA (ADVPACK.@)
878 *
879 * See FileSaveRestoreW.
880 */
FileSaveRestoreA(HWND hDlg,LPSTR pszFileList,LPSTR pszDir,LPSTR pszBaseName,DWORD dwFlags)881 HRESULT WINAPI FileSaveRestoreA(HWND hDlg, LPSTR pszFileList, LPSTR pszDir,
882 LPSTR pszBaseName, DWORD dwFlags)
883 {
884 UNICODE_STRING filelist, dir, basename;
885 HRESULT hr;
886
887 TRACE("(%p, %s, %s, %s, %d)\n", hDlg, debugstr_a(pszFileList),
888 debugstr_a(pszDir), debugstr_a(pszBaseName), dwFlags);
889
890 RtlCreateUnicodeStringFromAsciiz(&filelist, pszFileList);
891 RtlCreateUnicodeStringFromAsciiz(&dir, pszDir);
892 RtlCreateUnicodeStringFromAsciiz(&basename, pszBaseName);
893
894 hr = FileSaveRestoreW(hDlg, filelist.Buffer, dir.Buffer,
895 basename.Buffer, dwFlags);
896
897 RtlFreeUnicodeString(&filelist);
898 RtlFreeUnicodeString(&dir);
899 RtlFreeUnicodeString(&basename);
900
901 return hr;
902 }
903
904 /***********************************************************************
905 * FileSaveRestoreW (ADVPACK.@)
906 *
907 * Saves or restores the files in the specified file list.
908 *
909 * PARAMS
910 * hDlg [I] Handle to the dialog used for the display.
911 * pszFileList [I] NULL-separated list of filenames.
912 * pszDir [I] Path of the backup directory.
913 * pszBaseName [I] Basename of the backup files.
914 * dwFlags [I] See advpub.h.
915 *
916 * RETURNS
917 * Success: S_OK.
918 * Failure: E_FAIL.
919 *
920 * NOTES
921 * If pszFileList is NULL on restore, all files will be restored.
922 *
923 * BUGS
924 * Unimplemented.
925 */
FileSaveRestoreW(HWND hDlg,LPWSTR pszFileList,LPWSTR pszDir,LPWSTR pszBaseName,DWORD dwFlags)926 HRESULT WINAPI FileSaveRestoreW(HWND hDlg, LPWSTR pszFileList, LPWSTR pszDir,
927 LPWSTR pszBaseName, DWORD dwFlags)
928 {
929 FIXME("(%p, %s, %s, %s, %d) stub\n", hDlg, debugstr_w(pszFileList),
930 debugstr_w(pszDir), debugstr_w(pszBaseName), dwFlags);
931
932 return E_FAIL;
933 }
934
935 /***********************************************************************
936 * FileSaveRestoreOnINFA (ADVPACK.@)
937 *
938 * See FileSaveRestoreOnINFW.
939 */
FileSaveRestoreOnINFA(HWND hWnd,LPCSTR pszTitle,LPCSTR pszINF,LPCSTR pszSection,LPCSTR pszBackupDir,LPCSTR pszBaseBackupFile,DWORD dwFlags)940 HRESULT WINAPI FileSaveRestoreOnINFA(HWND hWnd, LPCSTR pszTitle, LPCSTR pszINF,
941 LPCSTR pszSection, LPCSTR pszBackupDir,
942 LPCSTR pszBaseBackupFile, DWORD dwFlags)
943 {
944 UNICODE_STRING title, inf, section;
945 UNICODE_STRING backupdir, backupfile;
946 HRESULT hr;
947
948 TRACE("(%p, %s, %s, %s, %s, %s, %d)\n", hWnd, debugstr_a(pszTitle),
949 debugstr_a(pszINF), debugstr_a(pszSection), debugstr_a(pszBackupDir),
950 debugstr_a(pszBaseBackupFile), dwFlags);
951
952 RtlCreateUnicodeStringFromAsciiz(&title, pszTitle);
953 RtlCreateUnicodeStringFromAsciiz(&inf, pszINF);
954 RtlCreateUnicodeStringFromAsciiz(§ion, pszSection);
955 RtlCreateUnicodeStringFromAsciiz(&backupdir, pszBackupDir);
956 RtlCreateUnicodeStringFromAsciiz(&backupfile, pszBaseBackupFile);
957
958 hr = FileSaveRestoreOnINFW(hWnd, title.Buffer, inf.Buffer, section.Buffer,
959 backupdir.Buffer, backupfile.Buffer, dwFlags);
960
961 RtlFreeUnicodeString(&title);
962 RtlFreeUnicodeString(&inf);
963 RtlFreeUnicodeString(§ion);
964 RtlFreeUnicodeString(&backupdir);
965 RtlFreeUnicodeString(&backupfile);
966
967 return hr;
968 }
969
970 /***********************************************************************
971 * FileSaveRestoreOnINFW (ADVPACK.@)
972 *
973 *
974 * PARAMS
975 * hWnd [I] Handle to the window used for the display.
976 * pszTitle [I] Title of the window.
977 * pszINF [I] Fully-qualified INF filename.
978 * pszSection [I] GenInstall INF section name.
979 * pszBackupDir [I] Directory to store the backup file.
980 * pszBaseBackupFile [I] Basename of the backup files.
981 * dwFlags [I] See advpub.h
982 *
983 * RETURNS
984 * Success: S_OK.
985 * Failure: E_FAIL.
986 *
987 * NOTES
988 * If pszSection is NULL, the default section will be used.
989 *
990 * BUGS
991 * Unimplemented.
992 */
FileSaveRestoreOnINFW(HWND hWnd,LPCWSTR pszTitle,LPCWSTR pszINF,LPCWSTR pszSection,LPCWSTR pszBackupDir,LPCWSTR pszBaseBackupFile,DWORD dwFlags)993 HRESULT WINAPI FileSaveRestoreOnINFW(HWND hWnd, LPCWSTR pszTitle, LPCWSTR pszINF,
994 LPCWSTR pszSection, LPCWSTR pszBackupDir,
995 LPCWSTR pszBaseBackupFile, DWORD dwFlags)
996 {
997 FIXME("(%p, %s, %s, %s, %s, %s, %d): stub\n", hWnd, debugstr_w(pszTitle),
998 debugstr_w(pszINF), debugstr_w(pszSection), debugstr_w(pszBackupDir),
999 debugstr_w(pszBaseBackupFile), dwFlags);
1000
1001 return E_FAIL;
1002 }
1003
1004 /***********************************************************************
1005 * GetVersionFromFileA (ADVPACK.@)
1006 *
1007 * See GetVersionFromFileExW.
1008 */
GetVersionFromFileA(LPCSTR Filename,LPDWORD MajorVer,LPDWORD MinorVer,BOOL Version)1009 HRESULT WINAPI GetVersionFromFileA(LPCSTR Filename, LPDWORD MajorVer,
1010 LPDWORD MinorVer, BOOL Version )
1011 {
1012 TRACE("(%s, %p, %p, %d)\n", debugstr_a(Filename), MajorVer, MinorVer, Version);
1013 return GetVersionFromFileExA(Filename, MajorVer, MinorVer, Version);
1014 }
1015
1016 /***********************************************************************
1017 * GetVersionFromFileW (ADVPACK.@)
1018 *
1019 * See GetVersionFromFileExW.
1020 */
GetVersionFromFileW(LPCWSTR Filename,LPDWORD MajorVer,LPDWORD MinorVer,BOOL Version)1021 HRESULT WINAPI GetVersionFromFileW(LPCWSTR Filename, LPDWORD MajorVer,
1022 LPDWORD MinorVer, BOOL Version )
1023 {
1024 TRACE("(%s, %p, %p, %d)\n", debugstr_w(Filename), MajorVer, MinorVer, Version);
1025 return GetVersionFromFileExW(Filename, MajorVer, MinorVer, Version);
1026 }
1027
1028 /* data for GetVersionFromFileEx */
1029 typedef struct tagLANGANDCODEPAGE
1030 {
1031 WORD wLanguage;
1032 WORD wCodePage;
1033 } LANGANDCODEPAGE;
1034
1035 /***********************************************************************
1036 * GetVersionFromFileExA (ADVPACK.@)
1037 *
1038 * See GetVersionFromFileExW.
1039 */
GetVersionFromFileExA(LPCSTR lpszFilename,LPDWORD pdwMSVer,LPDWORD pdwLSVer,BOOL bVersion)1040 HRESULT WINAPI GetVersionFromFileExA(LPCSTR lpszFilename, LPDWORD pdwMSVer,
1041 LPDWORD pdwLSVer, BOOL bVersion )
1042 {
1043 UNICODE_STRING filename;
1044 HRESULT res;
1045
1046 TRACE("(%s, %p, %p, %d)\n", debugstr_a(lpszFilename),
1047 pdwMSVer, pdwLSVer, bVersion);
1048
1049 RtlCreateUnicodeStringFromAsciiz(&filename, lpszFilename);
1050
1051 res = GetVersionFromFileExW(filename.Buffer, pdwMSVer, pdwLSVer, bVersion);
1052
1053 RtlFreeUnicodeString(&filename);
1054
1055 return res;
1056 }
1057
1058 /***********************************************************************
1059 * GetVersionFromFileExW (ADVPACK.@)
1060 *
1061 * Gets the files version or language information.
1062 *
1063 * PARAMS
1064 * lpszFilename [I] The file to get the info from.
1065 * pdwMSVer [O] Major version.
1066 * pdwLSVer [O] Minor version.
1067 * bVersion [I] Whether to retrieve version or language info.
1068 *
1069 * RETURNS
1070 * Always returns S_OK.
1071 *
1072 * NOTES
1073 * If bVersion is TRUE, version information is retrieved, else
1074 * pdwMSVer gets the language ID and pdwLSVer gets the codepage ID.
1075 */
GetVersionFromFileExW(LPCWSTR lpszFilename,LPDWORD pdwMSVer,LPDWORD pdwLSVer,BOOL bVersion)1076 HRESULT WINAPI GetVersionFromFileExW(LPCWSTR lpszFilename, LPDWORD pdwMSVer,
1077 LPDWORD pdwLSVer, BOOL bVersion )
1078 {
1079 VS_FIXEDFILEINFO *pFixedVersionInfo;
1080 LANGANDCODEPAGE *pLangAndCodePage;
1081 DWORD dwHandle, dwInfoSize;
1082 WCHAR szWinDir[MAX_PATH];
1083 WCHAR szFile[MAX_PATH];
1084 LPVOID pVersionInfo = NULL;
1085 BOOL bFileCopied = FALSE;
1086 UINT uValueLen;
1087
1088 static const WCHAR backslash[] = {'\\',0};
1089 static const WCHAR translation[] = {
1090 '\\','V','a','r','F','i','l','e','I','n','f','o',
1091 '\\','T','r','a','n','s','l','a','t','i','o','n',0
1092 };
1093
1094 TRACE("(%s, %p, %p, %d)\n", debugstr_w(lpszFilename),
1095 pdwMSVer, pdwLSVer, bVersion);
1096
1097 *pdwLSVer = 0;
1098 *pdwMSVer = 0;
1099
1100 lstrcpynW(szFile, lpszFilename, MAX_PATH);
1101
1102 dwInfoSize = GetFileVersionInfoSizeW(szFile, &dwHandle);
1103 if (!dwInfoSize)
1104 {
1105 /* check that the file exists */
1106 if (GetFileAttributesW(szFile) == INVALID_FILE_ATTRIBUTES)
1107 return S_OK;
1108
1109 /* file exists, but won't be found by GetFileVersionInfoSize,
1110 * so copy it to the temp dir where it will be found.
1111 */
1112 GetWindowsDirectoryW(szWinDir, MAX_PATH);
1113 GetTempFileNameW(szWinDir, NULL, 0, szFile);
1114 CopyFileW(lpszFilename, szFile, FALSE);
1115 bFileCopied = TRUE;
1116
1117 dwInfoSize = GetFileVersionInfoSizeW(szFile, &dwHandle);
1118 if (!dwInfoSize)
1119 goto done;
1120 }
1121
1122 pVersionInfo = HeapAlloc(GetProcessHeap(), 0, dwInfoSize);
1123 if (!pVersionInfo)
1124 goto done;
1125
1126 if (!GetFileVersionInfoW(szFile, dwHandle, dwInfoSize, pVersionInfo))
1127 goto done;
1128
1129 if (bVersion)
1130 {
1131 if (!VerQueryValueW(pVersionInfo, backslash,
1132 (LPVOID *)&pFixedVersionInfo, &uValueLen))
1133 goto done;
1134
1135 if (!uValueLen)
1136 goto done;
1137
1138 *pdwMSVer = pFixedVersionInfo->dwFileVersionMS;
1139 *pdwLSVer = pFixedVersionInfo->dwFileVersionLS;
1140 }
1141 else
1142 {
1143 if (!VerQueryValueW(pVersionInfo, translation,
1144 (LPVOID *)&pLangAndCodePage, &uValueLen))
1145 goto done;
1146
1147 if (!uValueLen)
1148 goto done;
1149
1150 *pdwMSVer = pLangAndCodePage->wLanguage;
1151 *pdwLSVer = pLangAndCodePage->wCodePage;
1152 }
1153
1154 done:
1155 HeapFree(GetProcessHeap(), 0, pVersionInfo);
1156
1157 if (bFileCopied)
1158 DeleteFileW(szFile);
1159
1160 return S_OK;
1161 }
1162