push 599a9d3db769aad8dabfd10a59120f719b00f4ee
[wine/hacks.git] / dlls / advpack / files.c
blobd0f948caca3f5e1dcc781c373eba69293af8450f
1 /*
2 * Advpack file functions
4 * Copyright 2006 James Hawkins
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 <stdarg.h>
22 #include <stdlib.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "winver.h"
29 #include "winternl.h"
30 #include "setupapi.h"
31 #include "advpub.h"
32 #include "fdi.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35 #include "advpack_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(advpack);
39 /* converts an ansi double null-terminated list to a unicode list */
40 static LPWSTR ansi_to_unicode_list(LPCSTR ansi_list)
42 DWORD len, wlen = 0;
43 LPWSTR list;
44 LPCSTR ptr = ansi_list;
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;
54 /***********************************************************************
55 * AddDelBackupEntryA (ADVPACK.@)
57 * See AddDelBackupEntryW.
59 HRESULT WINAPI AddDelBackupEntryA(LPCSTR lpcszFileList, LPCSTR lpcszBackupDir,
60 LPCSTR lpcszBaseName, DWORD dwFlags)
62 UNICODE_STRING backupdir, basename;
63 LPWSTR filelist;
64 LPCWSTR backup;
65 HRESULT res;
67 TRACE("(%s, %s, %s, %d)\n", debugstr_a(lpcszFileList),
68 debugstr_a(lpcszBackupDir), debugstr_a(lpcszBaseName), dwFlags);
70 if (lpcszFileList)
71 filelist = ansi_to_unicode_list(lpcszFileList);
72 else
73 filelist = NULL;
75 RtlCreateUnicodeStringFromAsciiz(&backupdir, lpcszBackupDir);
76 RtlCreateUnicodeStringFromAsciiz(&basename, lpcszBaseName);
78 if (lpcszBackupDir)
79 backup = backupdir.Buffer;
80 else
81 backup = NULL;
83 res = AddDelBackupEntryW(filelist, backup, basename.Buffer, dwFlags);
85 HeapFree(GetProcessHeap(), 0, filelist);
87 RtlFreeUnicodeString(&backupdir);
88 RtlFreeUnicodeString(&basename);
90 return res;
93 /***********************************************************************
94 * AddDelBackupEntryW (ADVPACK.@)
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.
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.
107 * RETURNS
108 * S_OK in all cases.
110 * NOTES
111 * If the INI file does not exist before adding entries to it, the file
112 * will be created.
114 * If lpcszBackupDir is NULL, the INI file is assumed to exist in
115 * c:\windows or created there if it does not exist.
117 HRESULT WINAPI AddDelBackupEntryW(LPCWSTR lpcszFileList, LPCWSTR lpcszBackupDir,
118 LPCWSTR lpcszBaseName, DWORD dwFlags)
120 WCHAR szIniPath[MAX_PATH];
121 LPCWSTR szString = NULL;
123 static const WCHAR szBackupEntry[] = {
124 '-','1',',','0',',','0',',','0',',','0',',','0',',','-','1',0
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};
131 TRACE("(%s, %s, %s, %d)\n", debugstr_w(lpcszFileList),
132 debugstr_w(lpcszBackupDir), debugstr_w(lpcszBaseName), dwFlags);
134 if (!lpcszFileList || !*lpcszFileList)
135 return S_OK;
137 if (lpcszBackupDir)
138 lstrcpyW(szIniPath, lpcszBackupDir);
139 else
140 GetWindowsDirectoryW(szIniPath, MAX_PATH);
142 lstrcatW(szIniPath, backslash);
143 lstrcatW(szIniPath, lpcszBaseName);
144 lstrcatW(szIniPath, ini);
146 SetFileAttributesW(szIniPath, FILE_ATTRIBUTE_NORMAL);
148 if (dwFlags & AADBE_ADD_ENTRY)
149 szString = szBackupEntry;
150 else if (dwFlags & AADBE_DEL_ENTRY)
151 szString = NULL;
153 /* add or delete the INI entries */
154 while (*lpcszFileList)
156 WritePrivateProfileStringW(backup, lpcszFileList, szString, szIniPath);
157 lpcszFileList += lstrlenW(lpcszFileList) + 1;
160 /* hide the INI file */
161 SetFileAttributesW(szIniPath, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN);
163 return S_OK;
166 /* FIXME: this is only for the local case, X:\ */
167 #define ROOT_LENGTH 3
169 static UINT CALLBACK pQuietQueueCallback(PVOID Context, UINT Notification,
170 UINT_PTR Param1, UINT_PTR Param2)
172 return 1;
175 static UINT CALLBACK pQueueCallback(PVOID Context, UINT Notification,
176 UINT_PTR Param1, UINT_PTR Param2)
178 /* only be verbose for error notifications */
179 if (!Notification ||
180 Notification == SPFILENOTIFY_RENAMEERROR ||
181 Notification == SPFILENOTIFY_DELETEERROR ||
182 Notification == SPFILENOTIFY_COPYERROR)
184 return SetupDefaultQueueCallbackW(Context, Notification,
185 Param1, Param2);
188 return 1;
191 /***********************************************************************
192 * AdvInstallFileA (ADVPACK.@)
194 * See AdvInstallFileW.
196 HRESULT WINAPI AdvInstallFileA(HWND hwnd, LPCSTR lpszSourceDir, LPCSTR lpszSourceFile,
197 LPCSTR lpszDestDir, LPCSTR lpszDestFile,
198 DWORD dwFlags, DWORD dwReserved)
200 UNICODE_STRING sourcedir, sourcefile;
201 UNICODE_STRING destdir, destfile;
202 HRESULT res;
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);
208 if (!lpszSourceDir || !lpszSourceFile || !lpszDestDir)
209 return E_INVALIDARG;
211 RtlCreateUnicodeStringFromAsciiz(&sourcedir, lpszSourceDir);
212 RtlCreateUnicodeStringFromAsciiz(&sourcefile, lpszSourceFile);
213 RtlCreateUnicodeStringFromAsciiz(&destdir, lpszDestDir);
214 RtlCreateUnicodeStringFromAsciiz(&destfile, lpszDestFile);
216 res = AdvInstallFileW(hwnd, sourcedir.Buffer, sourcefile.Buffer,
217 destdir.Buffer, destfile.Buffer, dwFlags, dwReserved);
219 RtlFreeUnicodeString(&sourcedir);
220 RtlFreeUnicodeString(&sourcefile);
221 RtlFreeUnicodeString(&destdir);
222 RtlFreeUnicodeString(&destfile);
224 return res;
227 /***********************************************************************
228 * AdvInstallFileW (ADVPACK.@)
230 * Copies a file from the source to a destination.
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.
241 * RETURNS
242 * Success: S_OK.
243 * Failure: E_FAIL.
245 * NOTES
246 * If lpszDestFile is NULL, the destination filename is the same as
247 * lpszSourceFIle.
249 HRESULT WINAPI AdvInstallFileW(HWND hwnd, LPCWSTR lpszSourceDir, LPCWSTR lpszSourceFile,
250 LPCWSTR lpszDestDir, LPCWSTR lpszDestFile,
251 DWORD dwFlags, DWORD dwReserved)
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;
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);
265 if (!lpszSourceDir || !lpszSourceFile || !lpszDestDir)
266 return E_INVALIDARG;
268 fileQueue = SetupOpenFileQueue();
269 if (fileQueue == INVALID_HANDLE_VALUE)
270 return HRESULT_FROM_WIN32(GetLastError());
272 pContext = NULL;
273 dwLastError = ERROR_SUCCESS;
275 lstrcpynW(szRootPath, lpszSourceDir, ROOT_LENGTH);
276 szPath = lpszSourceDir + ROOT_LENGTH;
278 /* use lpszSourceFile as destination filename if lpszDestFile is NULL */
279 if (lpszDestFile)
281 dwLen = lstrlenW(lpszDestFile);
282 szDestFilename = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
283 lstrcpyW(szDestFilename, lpszDestFile);
285 else
287 dwLen = lstrlenW(lpszSourceFile);
288 szDestFilename = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
289 lstrcpyW(szDestFilename, lpszSourceFile);
292 /* add the file copy operation to the setup queue */
293 if (!SetupQueueCopyW(fileQueue, szRootPath, szPath, lpszSourceFile, NULL,
294 NULL, lpszDestDir, szDestFilename, dwFlags))
296 dwLastError = GetLastError();
297 goto done;
300 pContext = SetupInitDefaultQueueCallbackEx(hwnd, INVALID_HANDLE_VALUE,
301 0, 0, NULL);
302 if (!pContext)
304 dwLastError = GetLastError();
305 goto done;
308 /* don't output anything for AIF_QUIET */
309 if (dwFlags & AIF_QUIET)
310 pFileCallback = pQuietQueueCallback;
311 else
312 pFileCallback = pQueueCallback;
314 /* perform the file copy */
315 if (!SetupCommitFileQueueW(hwnd, fileQueue, pFileCallback, pContext))
317 dwLastError = GetLastError();
318 goto done;
321 done:
322 SetupTermDefaultQueueCallback(pContext);
323 SetupCloseFileQueue(fileQueue);
325 HeapFree(GetProcessHeap(), 0, szDestFilename);
327 return HRESULT_FROM_WIN32(dwLastError);
330 static HRESULT DELNODE_recurse_dirtree(LPWSTR fname, DWORD flags)
332 DWORD fattrs = GetFileAttributesW(fname);
333 HRESULT ret = E_FAIL;
335 static const WCHAR asterisk[] = {'*',0};
336 static const WCHAR dot[] = {'.',0};
337 static const WCHAR dotdot[] = {'.','.',0};
339 if (fattrs & FILE_ATTRIBUTE_DIRECTORY)
341 HANDLE hFindFile;
342 WIN32_FIND_DATAW w32fd;
343 BOOL done = TRUE;
344 int fname_len = lstrlenW(fname);
346 /* Generate a path with wildcard suitable for iterating */
347 if (fname_len && fname[fname_len-1] != '\\') fname[fname_len++] = '\\';
348 lstrcpyW(fname + fname_len, asterisk);
350 if ((hFindFile = FindFirstFileW(fname, &w32fd)) != INVALID_HANDLE_VALUE)
352 /* Iterate through the files in the directory */
353 for (done = FALSE; !done; done = !FindNextFileW(hFindFile, &w32fd))
355 TRACE("%s\n", debugstr_w(w32fd.cFileName));
356 if (lstrcmpW(dot, w32fd.cFileName) != 0 &&
357 lstrcmpW(dotdot, w32fd.cFileName) != 0)
359 lstrcpyW(fname + fname_len, w32fd.cFileName);
360 if (DELNODE_recurse_dirtree(fname, flags) != S_OK)
362 break; /* Failure */
366 FindClose(hFindFile);
369 /* We're done with this directory, so restore the old path without wildcard */
370 *(fname + fname_len) = '\0';
372 if (done)
374 TRACE("%s: directory\n", debugstr_w(fname));
375 if (SetFileAttributesW(fname, FILE_ATTRIBUTE_NORMAL) && RemoveDirectoryW(fname))
377 ret = S_OK;
381 else
383 TRACE("%s: file\n", debugstr_w(fname));
384 if (SetFileAttributesW(fname, FILE_ATTRIBUTE_NORMAL) && DeleteFileW(fname))
386 ret = S_OK;
390 return ret;
393 /***********************************************************************
394 * DelNodeA (ADVPACK.@)
396 * See DelNodeW.
398 HRESULT WINAPI DelNodeA(LPCSTR pszFileOrDirName, DWORD dwFlags)
400 UNICODE_STRING fileordirname;
401 HRESULT res;
403 TRACE("(%s, %d)\n", debugstr_a(pszFileOrDirName), dwFlags);
405 RtlCreateUnicodeStringFromAsciiz(&fileordirname, pszFileOrDirName);
407 res = DelNodeW(fileordirname.Buffer, dwFlags);
409 RtlFreeUnicodeString(&fileordirname);
411 return res;
414 /***********************************************************************
415 * DelNodeW (ADVPACK.@)
417 * Deletes a file or directory
419 * PARAMS
420 * pszFileOrDirName [I] Name of file or directory to delete
421 * dwFlags [I] Flags; see include/advpub.h
423 * RETURNS
424 * Success: S_OK
425 * Failure: E_FAIL
427 * BUGS
428 * - Ignores flags
429 * - Native version apparently does a lot of checking to make sure
430 * we're not trying to delete a system directory etc.
432 HRESULT WINAPI DelNodeW(LPCWSTR pszFileOrDirName, DWORD dwFlags)
434 WCHAR fname[MAX_PATH];
435 HRESULT ret = E_FAIL;
437 TRACE("(%s, %d)\n", debugstr_w(pszFileOrDirName), dwFlags);
439 if (dwFlags)
440 FIXME("Flags ignored!\n");
442 if (pszFileOrDirName && *pszFileOrDirName)
444 lstrcpyW(fname, pszFileOrDirName);
446 /* TODO: Should check for system directory deletion etc. here */
448 ret = DELNODE_recurse_dirtree(fname, dwFlags);
451 return ret;
454 /***********************************************************************
455 * DelNodeRunDLL32A (ADVPACK.@)
457 * See DelNodeRunDLL32W.
459 HRESULT WINAPI DelNodeRunDLL32A(HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show)
461 UNICODE_STRING params;
462 HRESULT hr;
464 TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_a(cmdline), show);
466 RtlCreateUnicodeStringFromAsciiz(&params, cmdline);
468 hr = DelNodeRunDLL32W(hWnd, hInst, params.Buffer, show);
470 RtlFreeUnicodeString(&params);
472 return hr;
475 /***********************************************************************
476 * DelNodeRunDLL32W (ADVPACK.@)
478 * Deletes a file or directory, WinMain style.
480 * PARAMS
481 * hWnd [I] Handle to the window used for the display.
482 * hInst [I] Instance of the process.
483 * cmdline [I] Contains parameters in the order FileOrDirName,Flags.
484 * show [I] How the window should be shown.
486 * RETURNS
487 * Success: S_OK.
488 * Failure: E_FAIL.
490 HRESULT WINAPI DelNodeRunDLL32W(HWND hWnd, HINSTANCE hInst, LPWSTR cmdline, INT show)
492 LPWSTR szFilename, szFlags;
493 LPWSTR cmdline_copy, cmdline_ptr;
494 DWORD dwFlags = 0;
495 HRESULT res;
497 TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_w(cmdline), show);
499 cmdline_copy = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmdline) + 1) * sizeof(WCHAR));
500 cmdline_ptr = cmdline_copy;
501 lstrcpyW(cmdline_copy, cmdline);
503 /* get the parameters at indexes 0 and 1 respectively */
504 szFilename = get_parameter(&cmdline_ptr, ',');
505 szFlags = get_parameter(&cmdline_ptr, ',');
507 if (szFlags)
508 dwFlags = atolW(szFlags);
510 res = DelNodeW(szFilename, dwFlags);
512 HeapFree(GetProcessHeap(), 0, cmdline_copy);
514 return res;
517 /* The following defintions were copied from dlls/cabinet/cabinet.h */
519 /* SESSION Operation */
520 #define EXTRACT_FILLFILELIST 0x00000001
521 #define EXTRACT_EXTRACTFILES 0x00000002
523 struct FILELIST{
524 LPSTR FileName;
525 struct FILELIST *next;
526 BOOL Extracted;
529 typedef struct {
530 INT FileSize;
531 ERF Error;
532 struct FILELIST *FileList;
533 INT FileCount;
534 INT Operation;
535 CHAR Destination[MAX_PATH];
536 CHAR CurrentFile[MAX_PATH];
537 CHAR Reserved[MAX_PATH];
538 struct FILELIST *FilterList;
539 } SESSION;
541 static HRESULT (WINAPI *pExtract)(SESSION*, LPCSTR);
543 /* removes legal characters before and after file list, and
544 * converts the file list to a NULL-separated list
546 static LPSTR convert_file_list(LPCSTR FileList, DWORD *dwNumFiles)
548 DWORD dwLen;
549 const char *first = FileList;
550 const char *last = FileList + strlen(FileList) - 1;
551 LPSTR szConvertedList, temp;
553 /* any number of these chars before the list is OK */
554 while (first < last && (*first == ' ' || *first == '\t' || *first == ':'))
555 first++;
557 /* any number of these chars after the list is OK */
558 while (last > first && (*last == ' ' || *last == '\t' || *last == ':'))
559 last--;
561 if (first == last)
562 return NULL;
564 dwLen = last - first + 3; /* room for double-null termination */
565 szConvertedList = HeapAlloc(GetProcessHeap(), 0, dwLen);
566 lstrcpynA(szConvertedList, first, dwLen - 1);
568 szConvertedList[dwLen - 1] = '\0';
569 szConvertedList[dwLen] = '\0';
571 /* empty list */
572 if (!lstrlenA(szConvertedList))
574 HeapFree(GetProcessHeap(), 0, szConvertedList);
575 return NULL;
578 *dwNumFiles = 1;
580 /* convert the colons to double-null termination */
581 temp = szConvertedList;
582 while (*temp)
584 if (*temp == ':')
586 *temp = '\0';
587 (*dwNumFiles)++;
590 temp++;
593 return szConvertedList;
596 static void free_file_node(struct FILELIST *pNode)
598 HeapFree(GetProcessHeap(), 0, pNode->FileName);
599 HeapFree(GetProcessHeap(), 0, pNode);
602 /* determines whether szFile is in the NULL-separated szFileList */
603 static BOOL file_in_list(LPCSTR szFile, LPCSTR szFileList)
605 DWORD dwLen = lstrlenA(szFile);
606 DWORD dwTestLen;
608 while (*szFileList)
610 dwTestLen = lstrlenA(szFileList);
612 if (dwTestLen == dwLen)
614 if (!lstrcmpiA(szFile, szFileList))
615 return TRUE;
618 szFileList += dwTestLen + 1;
621 return FALSE;
624 /* removes nodes from the linked list that aren't specified in szFileList
625 * returns the number of files that are in both the linked list and szFileList
627 static DWORD fill_file_list(SESSION *session, LPCSTR szCabName, LPCSTR szFileList)
629 DWORD dwNumFound = 0;
630 struct FILELIST *pNode;
631 struct FILELIST *prev = NULL;
633 session->Operation |= EXTRACT_FILLFILELIST;
634 if (pExtract(session, szCabName))
636 session->Operation &= ~EXTRACT_FILLFILELIST;
637 return -1;
640 pNode = session->FileList;
641 while (pNode)
643 if (file_in_list(pNode->FileName, szFileList))
645 prev = pNode;
646 pNode = pNode->next;
647 dwNumFound++;
649 else if (prev)
651 prev->next = pNode->next;
652 free_file_node(pNode);
653 pNode = prev->next;
655 else
657 session->FileList = pNode->next;
658 free_file_node(pNode);
659 pNode = session->FileList;
663 session->Operation &= ~EXTRACT_FILLFILELIST;
664 return dwNumFound;
667 /***********************************************************************
668 * ExtractFilesA (ADVPACK.@)
670 * Extracts the specified files from a cab archive into
671 * a destination directory.
673 * PARAMS
674 * CabName [I] Filename of the cab archive.
675 * ExpandDir [I] Destination directory for the extracted files.
676 * Flags [I] Reserved.
677 * FileList [I] Optional list of files to extract. See NOTES.
678 * LReserved [I] Reserved. Must be NULL.
679 * Reserved [I] Reserved. Must be 0.
681 * RETURNS
682 * Success: S_OK.
683 * Failure: E_FAIL.
685 * NOTES
686 * FileList is a colon-separated list of filenames. If FileList is
687 * non-NULL, only the files in the list will be extracted from the
688 * cab file, otherwise all files will be extracted. Any number of
689 * spaces, tabs, or colons can be before or after the list, but
690 * the list itself must only be separated by colons.
692 HRESULT WINAPI ExtractFilesA(LPCSTR CabName, LPCSTR ExpandDir, DWORD Flags,
693 LPCSTR FileList, LPVOID LReserved, DWORD Reserved)
695 SESSION session;
696 HMODULE hCabinet;
697 HRESULT res = S_OK;
698 DWORD dwFileCount = 0;
699 DWORD dwFilesFound = 0;
700 LPSTR szConvertedList = NULL;
702 TRACE("(%s, %s, %d, %s, %p, %d)\n", debugstr_a(CabName), debugstr_a(ExpandDir),
703 Flags, debugstr_a(FileList), LReserved, Reserved);
705 if (!CabName || !ExpandDir)
706 return E_INVALIDARG;
708 if (GetFileAttributesA(ExpandDir) == INVALID_FILE_ATTRIBUTES)
709 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
711 hCabinet = LoadLibraryA("cabinet.dll");
712 if (!hCabinet)
713 return E_FAIL;
715 pExtract = (void *)GetProcAddress(hCabinet, "Extract");
716 if (!pExtract)
718 res = E_FAIL;
719 goto done;
722 ZeroMemory(&session, sizeof(SESSION));
723 lstrcpyA(session.Destination, ExpandDir);
725 if (FileList)
727 szConvertedList = convert_file_list(FileList, &dwFileCount);
728 if (!szConvertedList || dwFileCount == -1)
730 res = E_FAIL;
731 goto done;
734 dwFilesFound = fill_file_list(&session, CabName, szConvertedList);
735 if (dwFilesFound != dwFileCount)
737 res = E_FAIL;
738 goto done;
741 else
742 session.Operation |= EXTRACT_FILLFILELIST;
744 session.Operation |= EXTRACT_EXTRACTFILES;
745 res = pExtract(&session, CabName);
747 if (session.FileList)
749 struct FILELIST *curr = session.FileList;
750 struct FILELIST *next;
752 while (curr)
754 next = curr->next;
755 free_file_node(curr);
756 curr = next;
760 done:
761 FreeLibrary(hCabinet);
762 HeapFree(GetProcessHeap(), 0, szConvertedList);
764 return res;
767 /***********************************************************************
768 * FileSaveMarkNotExistA (ADVPACK.@)
770 * See FileSaveMarkNotExistW.
772 HRESULT WINAPI FileSaveMarkNotExistA(LPSTR pszFileList, LPSTR pszDir, LPSTR pszBaseName)
774 TRACE("(%s, %s, %s)\n", debugstr_a(pszFileList),
775 debugstr_a(pszDir), debugstr_a(pszBaseName));
777 return AddDelBackupEntryA(pszFileList, pszDir, pszBaseName, AADBE_DEL_ENTRY);
780 /***********************************************************************
781 * FileSaveMarkNotExistW (ADVPACK.@)
783 * Marks the files in the file list as not existing so they won't be
784 * backed up during a save.
786 * PARAMS
787 * pszFileList [I] NULL-separated list of filenames.
788 * pszDir [I] Path of the backup directory.
789 * pszBaseName [I] Basename of the INI file.
791 * RETURNS
792 * Success: S_OK.
793 * Failure: E_FAIL.
795 HRESULT WINAPI FileSaveMarkNotExistW(LPWSTR pszFileList, LPWSTR pszDir, LPWSTR pszBaseName)
797 TRACE("(%s, %s, %s)\n", debugstr_w(pszFileList),
798 debugstr_w(pszDir), debugstr_w(pszBaseName));
800 return AddDelBackupEntryW(pszFileList, pszDir, pszBaseName, AADBE_DEL_ENTRY);
803 /***********************************************************************
804 * FileSaveRestoreA (ADVPACK.@)
806 * See FileSaveRestoreW.
808 HRESULT WINAPI FileSaveRestoreA(HWND hDlg, LPSTR pszFileList, LPSTR pszDir,
809 LPSTR pszBaseName, DWORD dwFlags)
811 UNICODE_STRING filelist, dir, basename;
812 HRESULT hr;
814 TRACE("(%p, %s, %s, %s, %d)\n", hDlg, debugstr_a(pszFileList),
815 debugstr_a(pszDir), debugstr_a(pszBaseName), dwFlags);
817 RtlCreateUnicodeStringFromAsciiz(&filelist, pszFileList);
818 RtlCreateUnicodeStringFromAsciiz(&dir, pszDir);
819 RtlCreateUnicodeStringFromAsciiz(&basename, pszBaseName);
821 hr = FileSaveRestoreW(hDlg, filelist.Buffer, dir.Buffer,
822 basename.Buffer, dwFlags);
824 RtlFreeUnicodeString(&filelist);
825 RtlFreeUnicodeString(&dir);
826 RtlFreeUnicodeString(&basename);
828 return hr;
831 /***********************************************************************
832 * FileSaveRestoreW (ADVPACK.@)
834 * Saves or restores the files in the specified file list.
836 * PARAMS
837 * hDlg [I] Handle to the dialog used for the display.
838 * pszFileList [I] NULL-separated list of filenames.
839 * pszDir [I] Path of the backup directory.
840 * pszBaseName [I] Basename of the backup files.
841 * dwFlags [I] See advpub.h.
843 * RETURNS
844 * Success: S_OK.
845 * Failure: E_FAIL.
847 * NOTES
848 * If pszFileList is NULL on restore, all files will be restored.
850 * BUGS
851 * Unimplemented.
853 HRESULT WINAPI FileSaveRestoreW(HWND hDlg, LPWSTR pszFileList, LPWSTR pszDir,
854 LPWSTR pszBaseName, DWORD dwFlags)
856 FIXME("(%p, %s, %s, %s, %d) stub\n", hDlg, debugstr_w(pszFileList),
857 debugstr_w(pszDir), debugstr_w(pszBaseName), dwFlags);
859 return E_FAIL;
862 /***********************************************************************
863 * FileSaveRestoreOnINFA (ADVPACK.@)
865 * See FileSaveRestoreOnINFW.
867 HRESULT WINAPI FileSaveRestoreOnINFA(HWND hWnd, LPCSTR pszTitle, LPCSTR pszINF,
868 LPCSTR pszSection, LPCSTR pszBackupDir,
869 LPCSTR pszBaseBackupFile, DWORD dwFlags)
871 UNICODE_STRING title, inf, section;
872 UNICODE_STRING backupdir, backupfile;
873 HRESULT hr;
875 TRACE("(%p, %s, %s, %s, %s, %s, %d)\n", hWnd, debugstr_a(pszTitle),
876 debugstr_a(pszINF), debugstr_a(pszSection), debugstr_a(pszBackupDir),
877 debugstr_a(pszBaseBackupFile), dwFlags);
879 RtlCreateUnicodeStringFromAsciiz(&title, pszTitle);
880 RtlCreateUnicodeStringFromAsciiz(&inf, pszINF);
881 RtlCreateUnicodeStringFromAsciiz(&section, pszSection);
882 RtlCreateUnicodeStringFromAsciiz(&backupdir, pszBackupDir);
883 RtlCreateUnicodeStringFromAsciiz(&backupfile, pszBaseBackupFile);
885 hr = FileSaveRestoreOnINFW(hWnd, title.Buffer, inf.Buffer, section.Buffer,
886 backupdir.Buffer, backupfile.Buffer, dwFlags);
888 RtlFreeUnicodeString(&title);
889 RtlFreeUnicodeString(&inf);
890 RtlFreeUnicodeString(&section);
891 RtlFreeUnicodeString(&backupdir);
892 RtlFreeUnicodeString(&backupfile);
894 return hr;
897 /***********************************************************************
898 * FileSaveRestoreOnINFW (ADVPACK.@)
901 * PARAMS
902 * hWnd [I] Handle to the window used for the display.
903 * pszTitle [I] Title of the window.
904 * pszINF [I] Fully-qualified INF filename.
905 * pszSection [I] GenInstall INF section name.
906 * pszBackupDir [I] Directory to store the backup file.
907 * pszBaseBackupFile [I] Basename of the backup files.
908 * dwFlags [I] See advpub.h
910 * RETURNS
911 * Success: S_OK.
912 * Failure: E_FAIL.
914 * NOTES
915 * If pszSection is NULL, the default section will be used.
917 * BUGS
918 * Unimplemented.
920 HRESULT WINAPI FileSaveRestoreOnINFW(HWND hWnd, LPCWSTR pszTitle, LPCWSTR pszINF,
921 LPCWSTR pszSection, LPCWSTR pszBackupDir,
922 LPCWSTR pszBaseBackupFile, DWORD dwFlags)
924 FIXME("(%p, %s, %s, %s, %s, %s, %d): stub\n", hWnd, debugstr_w(pszTitle),
925 debugstr_w(pszINF), debugstr_w(pszSection), debugstr_w(pszBackupDir),
926 debugstr_w(pszBaseBackupFile), dwFlags);
928 return E_FAIL;
931 /***********************************************************************
932 * GetVersionFromFileA (ADVPACK.@)
934 * See GetVersionFromFileExW.
936 HRESULT WINAPI GetVersionFromFileA(LPCSTR Filename, LPDWORD MajorVer,
937 LPDWORD MinorVer, BOOL Version )
939 TRACE("(%s, %p, %p, %d)\n", debugstr_a(Filename), MajorVer, MinorVer, Version);
940 return GetVersionFromFileExA(Filename, MajorVer, MinorVer, Version);
943 /***********************************************************************
944 * GetVersionFromFileW (ADVPACK.@)
946 * See GetVersionFromFileExW.
948 HRESULT WINAPI GetVersionFromFileW(LPCWSTR Filename, LPDWORD MajorVer,
949 LPDWORD MinorVer, BOOL Version )
951 TRACE("(%s, %p, %p, %d)\n", debugstr_w(Filename), MajorVer, MinorVer, Version);
952 return GetVersionFromFileExW(Filename, MajorVer, MinorVer, Version);
955 /* data for GetVersionFromFileEx */
956 typedef struct tagLANGANDCODEPAGE
958 WORD wLanguage;
959 WORD wCodePage;
960 } LANGANDCODEPAGE;
962 /***********************************************************************
963 * GetVersionFromFileExA (ADVPACK.@)
965 * See GetVersionFromFileExW.
967 HRESULT WINAPI GetVersionFromFileExA(LPCSTR lpszFilename, LPDWORD pdwMSVer,
968 LPDWORD pdwLSVer, BOOL bVersion )
970 UNICODE_STRING filename;
971 HRESULT res;
973 TRACE("(%s, %p, %p, %d)\n", debugstr_a(lpszFilename),
974 pdwMSVer, pdwLSVer, bVersion);
976 RtlCreateUnicodeStringFromAsciiz(&filename, lpszFilename);
978 res = GetVersionFromFileExW(filename.Buffer, pdwMSVer, pdwLSVer, bVersion);
980 RtlFreeUnicodeString(&filename);
982 return res;
985 /***********************************************************************
986 * GetVersionFromFileExW (ADVPACK.@)
988 * Gets the files version or language information.
990 * PARAMS
991 * lpszFilename [I] The file to get the info from.
992 * pdwMSVer [O] Major version.
993 * pdwLSVer [O] Minor version.
994 * bVersion [I] Whether to retrieve version or language info.
996 * RETURNS
997 * Always returns S_OK.
999 * NOTES
1000 * If bVersion is TRUE, version information is retrieved, else
1001 * pdwMSVer gets the language ID and pdwLSVer gets the codepage ID.
1003 HRESULT WINAPI GetVersionFromFileExW(LPCWSTR lpszFilename, LPDWORD pdwMSVer,
1004 LPDWORD pdwLSVer, BOOL bVersion )
1006 VS_FIXEDFILEINFO *pFixedVersionInfo;
1007 LANGANDCODEPAGE *pLangAndCodePage;
1008 DWORD dwHandle, dwInfoSize;
1009 WCHAR szWinDir[MAX_PATH];
1010 WCHAR szFile[MAX_PATH];
1011 LPVOID pVersionInfo = NULL;
1012 BOOL bFileCopied = FALSE;
1013 UINT uValueLen;
1015 static WCHAR backslash[] = {'\\',0};
1016 static WCHAR translation[] = {
1017 '\\','V','a','r','F','i','l','e','I','n','f','o',
1018 '\\','T','r','a','n','s','l','a','t','i','o','n',0
1021 TRACE("(%s, %p, %p, %d)\n", debugstr_w(lpszFilename),
1022 pdwMSVer, pdwLSVer, bVersion);
1024 *pdwLSVer = 0;
1025 *pdwMSVer = 0;
1027 lstrcpynW(szFile, lpszFilename, MAX_PATH);
1029 dwInfoSize = GetFileVersionInfoSizeW(szFile, &dwHandle);
1030 if (!dwInfoSize)
1032 /* check that the file exists */
1033 if (GetFileAttributesW(szFile) == INVALID_FILE_ATTRIBUTES)
1034 return S_OK;
1036 /* file exists, but won't be found by GetFileVersionInfoSize,
1037 * so copy it to the temp dir where it will be found.
1039 GetWindowsDirectoryW(szWinDir, MAX_PATH);
1040 GetTempFileNameW(szWinDir, NULL, 0, szFile);
1041 CopyFileW(lpszFilename, szFile, FALSE);
1042 bFileCopied = TRUE;
1044 dwInfoSize = GetFileVersionInfoSizeW(szFile, &dwHandle);
1045 if (!dwInfoSize)
1046 goto done;
1049 pVersionInfo = HeapAlloc(GetProcessHeap(), 0, dwInfoSize);
1050 if (!pVersionInfo)
1051 goto done;
1053 if (!GetFileVersionInfoW(szFile, dwHandle, dwInfoSize, pVersionInfo))
1054 goto done;
1056 if (bVersion)
1058 if (!VerQueryValueW(pVersionInfo, backslash,
1059 (LPVOID *)&pFixedVersionInfo, &uValueLen))
1060 goto done;
1062 if (!uValueLen)
1063 goto done;
1065 *pdwMSVer = pFixedVersionInfo->dwFileVersionMS;
1066 *pdwLSVer = pFixedVersionInfo->dwFileVersionLS;
1068 else
1070 if (!VerQueryValueW(pVersionInfo, translation,
1071 (LPVOID *)&pLangAndCodePage, &uValueLen))
1072 goto done;
1074 if (!uValueLen)
1075 goto done;
1077 *pdwMSVer = pLangAndCodePage->wLanguage;
1078 *pdwLSVer = pLangAndCodePage->wCodePage;
1081 done:
1082 HeapFree(GetProcessHeap(), 0, pVersionInfo);
1084 if (bFileCopied)
1085 DeleteFileW(szFile);
1087 return S_OK;