mfreadwrite/reader: Enable DXGI device manager path.
[wine/zf.git] / dlls / advpack / files.c
blob58ff76e05050fc149c8d2a48902c97a9e8ae674b
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 "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"
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 TRACE("(%s, %s, %s, %d)\n", debugstr_w(lpcszFileList),
124 debugstr_w(lpcszBackupDir), debugstr_w(lpcszBaseName), dwFlags);
126 if (!lpcszFileList || !*lpcszFileList)
127 return S_OK;
129 if (lpcszBackupDir)
130 lstrcpyW(szIniPath, lpcszBackupDir);
131 else
132 GetWindowsDirectoryW(szIniPath, MAX_PATH);
134 lstrcatW(szIniPath, L"\\");
135 lstrcatW(szIniPath, lpcszBaseName);
136 lstrcatW(szIniPath, L".ini");
138 SetFileAttributesW(szIniPath, FILE_ATTRIBUTE_NORMAL);
140 if (dwFlags & AADBE_ADD_ENTRY)
141 szString = L"-1,0,0,0,0,0,-1";
142 else if (dwFlags & AADBE_DEL_ENTRY)
143 szString = NULL;
145 /* add or delete the INI entries */
146 while (*lpcszFileList)
148 WritePrivateProfileStringW(L"backup", lpcszFileList, szString, szIniPath);
149 lpcszFileList += lstrlenW(lpcszFileList) + 1;
152 /* hide the INI file */
153 SetFileAttributesW(szIniPath, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN);
155 return S_OK;
158 /* FIXME: this is only for the local case, X:\ */
159 #define ROOT_LENGTH 3
161 static UINT CALLBACK pQuietQueueCallback(PVOID Context, UINT Notification,
162 UINT_PTR Param1, UINT_PTR Param2)
164 return 1;
167 static UINT CALLBACK pQueueCallback(PVOID Context, UINT Notification,
168 UINT_PTR Param1, UINT_PTR Param2)
170 /* only be verbose for error notifications */
171 if (!Notification ||
172 Notification == SPFILENOTIFY_RENAMEERROR ||
173 Notification == SPFILENOTIFY_DELETEERROR ||
174 Notification == SPFILENOTIFY_COPYERROR)
176 return SetupDefaultQueueCallbackW(Context, Notification,
177 Param1, Param2);
180 return 1;
183 /***********************************************************************
184 * AdvInstallFileA (ADVPACK.@)
186 * See AdvInstallFileW.
188 HRESULT WINAPI AdvInstallFileA(HWND hwnd, LPCSTR lpszSourceDir, LPCSTR lpszSourceFile,
189 LPCSTR lpszDestDir, LPCSTR lpszDestFile,
190 DWORD dwFlags, DWORD dwReserved)
192 UNICODE_STRING sourcedir, sourcefile;
193 UNICODE_STRING destdir, destfile;
194 HRESULT res;
196 TRACE("(%p, %s, %s, %s, %s, %d, %d)\n", hwnd, debugstr_a(lpszSourceDir),
197 debugstr_a(lpszSourceFile), debugstr_a(lpszDestDir),
198 debugstr_a(lpszDestFile), dwFlags, dwReserved);
200 if (!lpszSourceDir || !lpszSourceFile || !lpszDestDir)
201 return E_INVALIDARG;
203 RtlCreateUnicodeStringFromAsciiz(&sourcedir, lpszSourceDir);
204 RtlCreateUnicodeStringFromAsciiz(&sourcefile, lpszSourceFile);
205 RtlCreateUnicodeStringFromAsciiz(&destdir, lpszDestDir);
206 RtlCreateUnicodeStringFromAsciiz(&destfile, lpszDestFile);
208 res = AdvInstallFileW(hwnd, sourcedir.Buffer, sourcefile.Buffer,
209 destdir.Buffer, destfile.Buffer, dwFlags, dwReserved);
211 RtlFreeUnicodeString(&sourcedir);
212 RtlFreeUnicodeString(&sourcefile);
213 RtlFreeUnicodeString(&destdir);
214 RtlFreeUnicodeString(&destfile);
216 return res;
219 /***********************************************************************
220 * AdvInstallFileW (ADVPACK.@)
222 * Copies a file from the source to a destination.
224 * PARAMS
225 * hwnd [I] Handle to the window used for messages.
226 * lpszSourceDir [I] Source directory.
227 * lpszSourceFile [I] Source filename.
228 * lpszDestDir [I] Destination directory.
229 * lpszDestFile [I] Optional destination filename.
230 * dwFlags [I] See advpub.h.
231 * dwReserved [I] Reserved. Must be 0.
233 * RETURNS
234 * Success: S_OK.
235 * Failure: E_FAIL.
237 * NOTES
238 * If lpszDestFile is NULL, the destination filename is the same as
239 * lpszSourceFIle.
241 HRESULT WINAPI AdvInstallFileW(HWND hwnd, LPCWSTR lpszSourceDir, LPCWSTR lpszSourceFile,
242 LPCWSTR lpszDestDir, LPCWSTR lpszDestFile,
243 DWORD dwFlags, DWORD dwReserved)
245 PSP_FILE_CALLBACK_W pFileCallback;
246 LPWSTR szDestFilename;
247 LPCWSTR szPath;
248 WCHAR szRootPath[ROOT_LENGTH];
249 DWORD dwLen, dwLastError;
250 HSPFILEQ fileQueue;
251 PVOID pContext;
253 TRACE("(%p, %s, %s, %s, %s, %d, %d)\n", hwnd, debugstr_w(lpszSourceDir),
254 debugstr_w(lpszSourceFile), debugstr_w(lpszDestDir),
255 debugstr_w(lpszDestFile), dwFlags, dwReserved);
257 if (!lpszSourceDir || !lpszSourceFile || !lpszDestDir)
258 return E_INVALIDARG;
260 fileQueue = SetupOpenFileQueue();
261 if (fileQueue == INVALID_HANDLE_VALUE)
262 return HRESULT_FROM_WIN32(GetLastError());
264 pContext = NULL;
265 dwLastError = ERROR_SUCCESS;
267 lstrcpynW(szRootPath, lpszSourceDir, ROOT_LENGTH);
268 szPath = lpszSourceDir + ROOT_LENGTH;
270 /* use lpszSourceFile as destination filename if lpszDestFile is NULL */
271 if (lpszDestFile)
273 dwLen = lstrlenW(lpszDestFile);
274 szDestFilename = HeapAlloc(GetProcessHeap(), 0, (dwLen+1) * sizeof(WCHAR));
275 lstrcpyW(szDestFilename, lpszDestFile);
277 else
279 dwLen = lstrlenW(lpszSourceFile);
280 szDestFilename = HeapAlloc(GetProcessHeap(), 0, (dwLen+1) * sizeof(WCHAR));
281 lstrcpyW(szDestFilename, lpszSourceFile);
284 /* add the file copy operation to the setup queue */
285 if (!SetupQueueCopyW(fileQueue, szRootPath, szPath, lpszSourceFile, NULL,
286 NULL, lpszDestDir, szDestFilename, dwFlags))
288 dwLastError = GetLastError();
289 goto done;
292 pContext = SetupInitDefaultQueueCallbackEx(hwnd, INVALID_HANDLE_VALUE,
293 0, 0, NULL);
294 if (!pContext)
296 dwLastError = GetLastError();
297 goto done;
300 /* don't output anything for AIF_QUIET */
301 if (dwFlags & AIF_QUIET)
302 pFileCallback = pQuietQueueCallback;
303 else
304 pFileCallback = pQueueCallback;
306 /* perform the file copy */
307 if (!SetupCommitFileQueueW(hwnd, fileQueue, pFileCallback, pContext))
309 dwLastError = GetLastError();
310 goto done;
313 done:
314 SetupTermDefaultQueueCallback(pContext);
315 SetupCloseFileQueue(fileQueue);
317 HeapFree(GetProcessHeap(), 0, szDestFilename);
319 return HRESULT_FROM_WIN32(dwLastError);
322 static HRESULT DELNODE_recurse_dirtree(LPWSTR fname, DWORD flags)
324 DWORD fattrs = GetFileAttributesW(fname);
325 HRESULT ret = E_FAIL;
327 if (fattrs & FILE_ATTRIBUTE_DIRECTORY)
329 HANDLE hFindFile;
330 WIN32_FIND_DATAW w32fd;
331 BOOL done = TRUE;
332 int fname_len = lstrlenW(fname);
334 /* Generate a path with wildcard suitable for iterating */
335 if (fname_len && fname[fname_len-1] != '\\') fname[fname_len++] = '\\';
336 lstrcpyW(fname + fname_len, L"*");
338 if ((hFindFile = FindFirstFileW(fname, &w32fd)) != INVALID_HANDLE_VALUE)
340 /* Iterate through the files in the directory */
341 for (done = FALSE; !done; done = !FindNextFileW(hFindFile, &w32fd))
343 TRACE("%s\n", debugstr_w(w32fd.cFileName));
344 if (lstrcmpW(L".", w32fd.cFileName) != 0 && lstrcmpW(L"..", w32fd.cFileName) != 0)
346 lstrcpyW(fname + fname_len, w32fd.cFileName);
347 if (DELNODE_recurse_dirtree(fname, flags) != S_OK)
349 break; /* Failure */
353 FindClose(hFindFile);
356 /* We're done with this directory, so restore the old path without wildcard */
357 *(fname + fname_len) = '\0';
359 if (done)
361 TRACE("%s: directory\n", debugstr_w(fname));
362 if (SetFileAttributesW(fname, FILE_ATTRIBUTE_NORMAL) && RemoveDirectoryW(fname))
364 ret = S_OK;
368 else
370 TRACE("%s: file\n", debugstr_w(fname));
371 if (SetFileAttributesW(fname, FILE_ATTRIBUTE_NORMAL) && DeleteFileW(fname))
373 ret = S_OK;
377 return ret;
380 /***********************************************************************
381 * DelNodeA (ADVPACK.@)
383 * See DelNodeW.
385 HRESULT WINAPI DelNodeA(LPCSTR pszFileOrDirName, DWORD dwFlags)
387 UNICODE_STRING fileordirname;
388 HRESULT res;
390 TRACE("(%s, %d)\n", debugstr_a(pszFileOrDirName), dwFlags);
392 RtlCreateUnicodeStringFromAsciiz(&fileordirname, pszFileOrDirName);
394 res = DelNodeW(fileordirname.Buffer, dwFlags);
396 RtlFreeUnicodeString(&fileordirname);
398 return res;
401 /***********************************************************************
402 * DelNodeW (ADVPACK.@)
404 * Deletes a file or directory
406 * PARAMS
407 * pszFileOrDirName [I] Name of file or directory to delete
408 * dwFlags [I] Flags; see include/advpub.h
410 * RETURNS
411 * Success: S_OK
412 * Failure: E_FAIL
414 * BUGS
415 * - Ignores flags
416 * - Native version apparently does a lot of checking to make sure
417 * we're not trying to delete a system directory etc.
419 HRESULT WINAPI DelNodeW(LPCWSTR pszFileOrDirName, DWORD dwFlags)
421 WCHAR fname[MAX_PATH];
422 HRESULT ret = E_FAIL;
424 TRACE("(%s, %d)\n", debugstr_w(pszFileOrDirName), dwFlags);
426 if (dwFlags)
427 FIXME("Flags ignored!\n");
429 if (pszFileOrDirName && *pszFileOrDirName)
431 lstrcpyW(fname, pszFileOrDirName);
433 /* TODO: Should check for system directory deletion etc. here */
435 ret = DELNODE_recurse_dirtree(fname, dwFlags);
438 return ret;
441 /***********************************************************************
442 * DelNodeRunDLL32A (ADVPACK.@)
444 * See DelNodeRunDLL32W.
446 HRESULT WINAPI DelNodeRunDLL32A(HWND hWnd, HINSTANCE hInst, LPSTR cmdline, INT show)
448 UNICODE_STRING params;
449 HRESULT hr;
451 TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_a(cmdline), show);
453 RtlCreateUnicodeStringFromAsciiz(&params, cmdline);
455 hr = DelNodeRunDLL32W(hWnd, hInst, params.Buffer, show);
457 RtlFreeUnicodeString(&params);
459 return hr;
462 /***********************************************************************
463 * DelNodeRunDLL32W (ADVPACK.@)
465 * Deletes a file or directory, WinMain style.
467 * PARAMS
468 * hWnd [I] Handle to the window used for the display.
469 * hInst [I] Instance of the process.
470 * cmdline [I] Contains parameters in the order FileOrDirName,Flags.
471 * show [I] How the window should be shown.
473 * RETURNS
474 * Success: S_OK.
475 * Failure: E_FAIL.
477 HRESULT WINAPI DelNodeRunDLL32W(HWND hWnd, HINSTANCE hInst, LPWSTR cmdline, INT show)
479 LPWSTR szFilename, szFlags;
480 LPWSTR cmdline_copy, cmdline_ptr;
481 DWORD dwFlags = 0;
482 HRESULT res;
484 TRACE("(%p, %p, %s, %i)\n", hWnd, hInst, debugstr_w(cmdline), show);
486 cmdline_copy = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(cmdline) + 1) * sizeof(WCHAR));
487 cmdline_ptr = cmdline_copy;
488 lstrcpyW(cmdline_copy, cmdline);
490 /* get the parameters at indexes 0 and 1 respectively */
491 szFilename = get_parameter(&cmdline_ptr, ',', TRUE);
492 szFlags = get_parameter(&cmdline_ptr, ',', TRUE);
494 if (szFlags)
495 dwFlags = wcstol(szFlags, NULL, 10);
497 res = DelNodeW(szFilename, dwFlags);
499 HeapFree(GetProcessHeap(), 0, cmdline_copy);
501 return res;
504 /* The following definitions were copied from dlls/cabinet/cabinet.h */
506 /* SESSION Operation */
507 #define EXTRACT_FILLFILELIST 0x00000001
508 #define EXTRACT_EXTRACTFILES 0x00000002
510 struct FILELIST{
511 LPSTR FileName;
512 struct FILELIST *next;
513 BOOL DoExtract;
516 typedef struct {
517 INT FileSize;
518 ERF Error;
519 struct FILELIST *FileList;
520 INT FileCount;
521 INT Operation;
522 CHAR Destination[MAX_PATH];
523 CHAR CurrentFile[MAX_PATH];
524 CHAR Reserved[MAX_PATH];
525 struct FILELIST *FilterList;
526 } SESSION;
528 static HRESULT (WINAPI *pExtract)(SESSION*, LPCSTR);
530 /* removes legal characters before and after file list, and
531 * converts the file list to a NULL-separated list
533 static LPSTR convert_file_list(LPCSTR FileList, DWORD *dwNumFiles)
535 DWORD dwLen;
536 const char *first = FileList;
537 const char *last = FileList + strlen(FileList) - 1;
538 LPSTR szConvertedList, temp;
540 /* any number of these chars before the list is OK */
541 while (first < last && (*first == ' ' || *first == '\t' || *first == ':'))
542 first++;
544 /* any number of these chars after the list is OK */
545 while (last > first && (*last == ' ' || *last == '\t' || *last == ':'))
546 last--;
548 if (first == last)
549 return NULL;
551 dwLen = last - first + 3; /* room for double-null termination */
552 szConvertedList = HeapAlloc(GetProcessHeap(), 0, dwLen);
553 lstrcpynA(szConvertedList, first, dwLen - 1);
554 szConvertedList[dwLen - 1] = '\0';
556 /* empty list */
557 if (!szConvertedList[0])
559 HeapFree(GetProcessHeap(), 0, szConvertedList);
560 return NULL;
563 *dwNumFiles = 1;
565 /* convert the colons to double-null termination */
566 temp = szConvertedList;
567 while (*temp)
569 if (*temp == ':')
571 *temp = '\0';
572 (*dwNumFiles)++;
575 temp++;
578 return szConvertedList;
581 static void free_file_node(struct FILELIST *pNode)
583 HeapFree(GetProcessHeap(), 0, pNode->FileName);
584 HeapFree(GetProcessHeap(), 0, pNode);
587 /* determines whether szFile is in the NULL-separated szFileList */
588 static BOOL file_in_list(LPCSTR szFile, LPCSTR szFileList)
590 DWORD dwLen = lstrlenA(szFile);
591 DWORD dwTestLen;
593 while (*szFileList)
595 dwTestLen = lstrlenA(szFileList);
597 if (dwTestLen == dwLen)
599 if (!lstrcmpiA(szFile, szFileList))
600 return TRUE;
603 szFileList += dwTestLen + 1;
606 return FALSE;
610 /* returns the number of files that are in both the linked list and szFileList */
611 static DWORD fill_file_list(SESSION *session, LPCSTR szCabName, LPCSTR szFileList)
613 DWORD dwNumFound = 0;
614 struct FILELIST *pNode;
616 session->Operation |= EXTRACT_FILLFILELIST;
617 if (pExtract(session, szCabName) != S_OK)
619 session->Operation &= ~EXTRACT_FILLFILELIST;
620 return -1;
623 pNode = session->FileList;
624 while (pNode)
626 if (!file_in_list(pNode->FileName, szFileList))
627 pNode->DoExtract = FALSE;
628 else
629 dwNumFound++;
631 pNode = pNode->next;
634 session->Operation &= ~EXTRACT_FILLFILELIST;
635 return dwNumFound;
638 static void free_file_list(SESSION* session)
640 struct FILELIST *next, *curr = session->FileList;
642 while (curr)
644 next = curr->next;
645 free_file_node(curr);
646 curr = next;
650 /***********************************************************************
651 * ExtractFilesA (ADVPACK.@)
653 * Extracts the specified files from a cab archive into
654 * a destination directory.
656 * PARAMS
657 * CabName [I] Filename of the cab archive.
658 * ExpandDir [I] Destination directory for the extracted files.
659 * Flags [I] Reserved.
660 * FileList [I] Optional list of files to extract. See NOTES.
661 * LReserved [I] Reserved. Must be NULL.
662 * Reserved [I] Reserved. Must be 0.
664 * RETURNS
665 * Success: S_OK.
666 * Failure: E_FAIL.
668 * NOTES
669 * FileList is a colon-separated list of filenames. If FileList is
670 * non-NULL, only the files in the list will be extracted from the
671 * cab file, otherwise all files will be extracted. Any number of
672 * spaces, tabs, or colons can be before or after the list, but
673 * the list itself must only be separated by colons.
675 HRESULT WINAPI ExtractFilesA(LPCSTR CabName, LPCSTR ExpandDir, DWORD Flags,
676 LPCSTR FileList, LPVOID LReserved, DWORD Reserved)
678 SESSION session;
679 HMODULE hCabinet;
680 HRESULT res = S_OK;
681 DWORD dwFileCount = 0;
682 DWORD dwFilesFound = 0;
683 LPSTR szConvertedList = NULL;
685 TRACE("(%s, %s, %d, %s, %p, %d)\n", debugstr_a(CabName), debugstr_a(ExpandDir),
686 Flags, debugstr_a(FileList), LReserved, Reserved);
688 if (!CabName || !ExpandDir)
689 return E_INVALIDARG;
691 if (GetFileAttributesA(ExpandDir) == INVALID_FILE_ATTRIBUTES)
692 return HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND);
694 hCabinet = LoadLibraryA("cabinet.dll");
695 if (!hCabinet)
696 return E_FAIL;
698 ZeroMemory(&session, sizeof(SESSION));
700 pExtract = (void *)GetProcAddress(hCabinet, "Extract");
701 if (!pExtract)
703 res = E_FAIL;
704 goto done;
707 lstrcpyA(session.Destination, ExpandDir);
709 if (FileList)
711 szConvertedList = convert_file_list(FileList, &dwFileCount);
712 if (!szConvertedList)
714 res = E_FAIL;
715 goto done;
718 dwFilesFound = fill_file_list(&session, CabName, szConvertedList);
719 if (dwFilesFound != dwFileCount)
721 res = E_FAIL;
722 goto done;
725 else
726 session.Operation |= EXTRACT_FILLFILELIST;
728 session.Operation |= EXTRACT_EXTRACTFILES;
729 res = pExtract(&session, CabName);
731 done:
732 free_file_list(&session);
733 FreeLibrary(hCabinet);
734 HeapFree(GetProcessHeap(), 0, szConvertedList);
736 return res;
739 /***********************************************************************
740 * ExtractFilesW (ADVPACK.@)
742 * Extracts the specified files from a cab archive into
743 * a destination directory.
745 * PARAMS
746 * CabName [I] Filename of the cab archive.
747 * ExpandDir [I] Destination directory for the extracted files.
748 * Flags [I] Reserved.
749 * FileList [I] Optional list of files to extract. See NOTES.
750 * LReserved [I] Reserved. Must be NULL.
751 * Reserved [I] Reserved. Must be 0.
753 * RETURNS
754 * Success: S_OK.
755 * Failure: E_FAIL.
757 * NOTES
758 * FileList is a colon-separated list of filenames. If FileList is
759 * non-NULL, only the files in the list will be extracted from the
760 * cab file, otherwise all files will be extracted. Any number of
761 * spaces, tabs, or colons can be before or after the list, but
762 * the list itself must only be separated by colons.
764 * BUGS
765 * Unimplemented.
767 HRESULT WINAPI ExtractFilesW(LPCWSTR CabName, LPCWSTR ExpandDir, DWORD Flags,
768 LPCWSTR FileList, LPVOID LReserved, DWORD Reserved)
770 char *cab_name = NULL, *expand_dir = NULL, *file_list = NULL;
771 HRESULT hres = S_OK;
773 TRACE("(%s, %s, %d, %s, %p, %d)\n", debugstr_w(CabName), debugstr_w(ExpandDir),
774 Flags, debugstr_w(FileList), LReserved, Reserved);
776 if(CabName) {
777 cab_name = heap_strdupWtoA(CabName);
778 if(!cab_name)
779 return E_OUTOFMEMORY;
782 if(ExpandDir) {
783 expand_dir = heap_strdupWtoA(ExpandDir);
784 if(!expand_dir)
785 hres = E_OUTOFMEMORY;
788 if(SUCCEEDED(hres) && FileList) {
789 file_list = heap_strdupWtoA(FileList);
790 if(!file_list)
791 hres = E_OUTOFMEMORY;
794 /* cabinet.dll, which does the real job of extracting files, doesn't have UNICODE API,
795 so we need W->A conversion at some point anyway. */
796 if(SUCCEEDED(hres))
797 hres = ExtractFilesA(cab_name, expand_dir, Flags, file_list, LReserved, Reserved);
799 heap_free(cab_name);
800 heap_free(expand_dir);
801 heap_free(file_list);
802 return hres;
805 /***********************************************************************
806 * FileSaveMarkNotExistA (ADVPACK.@)
808 * See FileSaveMarkNotExistW.
810 HRESULT WINAPI FileSaveMarkNotExistA(LPSTR pszFileList, LPSTR pszDir, LPSTR pszBaseName)
812 TRACE("(%s, %s, %s)\n", debugstr_a(pszFileList),
813 debugstr_a(pszDir), debugstr_a(pszBaseName));
815 return AddDelBackupEntryA(pszFileList, pszDir, pszBaseName, AADBE_DEL_ENTRY);
818 /***********************************************************************
819 * FileSaveMarkNotExistW (ADVPACK.@)
821 * Marks the files in the file list as not existing so they won't be
822 * backed up during a save.
824 * PARAMS
825 * pszFileList [I] NULL-separated list of filenames.
826 * pszDir [I] Path of the backup directory.
827 * pszBaseName [I] Basename of the INI file.
829 * RETURNS
830 * Success: S_OK.
831 * Failure: E_FAIL.
833 HRESULT WINAPI FileSaveMarkNotExistW(LPWSTR pszFileList, LPWSTR pszDir, LPWSTR pszBaseName)
835 TRACE("(%s, %s, %s)\n", debugstr_w(pszFileList),
836 debugstr_w(pszDir), debugstr_w(pszBaseName));
838 return AddDelBackupEntryW(pszFileList, pszDir, pszBaseName, AADBE_DEL_ENTRY);
841 /***********************************************************************
842 * FileSaveRestoreA (ADVPACK.@)
844 * See FileSaveRestoreW.
846 HRESULT WINAPI FileSaveRestoreA(HWND hDlg, LPSTR pszFileList, LPSTR pszDir,
847 LPSTR pszBaseName, DWORD dwFlags)
849 UNICODE_STRING filelist, dir, basename;
850 HRESULT hr;
852 TRACE("(%p, %s, %s, %s, %d)\n", hDlg, debugstr_a(pszFileList),
853 debugstr_a(pszDir), debugstr_a(pszBaseName), dwFlags);
855 RtlCreateUnicodeStringFromAsciiz(&filelist, pszFileList);
856 RtlCreateUnicodeStringFromAsciiz(&dir, pszDir);
857 RtlCreateUnicodeStringFromAsciiz(&basename, pszBaseName);
859 hr = FileSaveRestoreW(hDlg, filelist.Buffer, dir.Buffer,
860 basename.Buffer, dwFlags);
862 RtlFreeUnicodeString(&filelist);
863 RtlFreeUnicodeString(&dir);
864 RtlFreeUnicodeString(&basename);
866 return hr;
869 /***********************************************************************
870 * FileSaveRestoreW (ADVPACK.@)
872 * Saves or restores the files in the specified file list.
874 * PARAMS
875 * hDlg [I] Handle to the dialog used for the display.
876 * pszFileList [I] NULL-separated list of filenames.
877 * pszDir [I] Path of the backup directory.
878 * pszBaseName [I] Basename of the backup files.
879 * dwFlags [I] See advpub.h.
881 * RETURNS
882 * Success: S_OK.
883 * Failure: E_FAIL.
885 * NOTES
886 * If pszFileList is NULL on restore, all files will be restored.
888 * BUGS
889 * Unimplemented.
891 HRESULT WINAPI FileSaveRestoreW(HWND hDlg, LPWSTR pszFileList, LPWSTR pszDir,
892 LPWSTR pszBaseName, DWORD dwFlags)
894 FIXME("(%p, %s, %s, %s, %d) stub\n", hDlg, debugstr_w(pszFileList),
895 debugstr_w(pszDir), debugstr_w(pszBaseName), dwFlags);
897 return E_FAIL;
900 /***********************************************************************
901 * FileSaveRestoreOnINFA (ADVPACK.@)
903 * See FileSaveRestoreOnINFW.
905 HRESULT WINAPI FileSaveRestoreOnINFA(HWND hWnd, LPCSTR pszTitle, LPCSTR pszINF,
906 LPCSTR pszSection, LPCSTR pszBackupDir,
907 LPCSTR pszBaseBackupFile, DWORD dwFlags)
909 UNICODE_STRING title, inf, section;
910 UNICODE_STRING backupdir, backupfile;
911 HRESULT hr;
913 TRACE("(%p, %s, %s, %s, %s, %s, %d)\n", hWnd, debugstr_a(pszTitle),
914 debugstr_a(pszINF), debugstr_a(pszSection), debugstr_a(pszBackupDir),
915 debugstr_a(pszBaseBackupFile), dwFlags);
917 RtlCreateUnicodeStringFromAsciiz(&title, pszTitle);
918 RtlCreateUnicodeStringFromAsciiz(&inf, pszINF);
919 RtlCreateUnicodeStringFromAsciiz(&section, pszSection);
920 RtlCreateUnicodeStringFromAsciiz(&backupdir, pszBackupDir);
921 RtlCreateUnicodeStringFromAsciiz(&backupfile, pszBaseBackupFile);
923 hr = FileSaveRestoreOnINFW(hWnd, title.Buffer, inf.Buffer, section.Buffer,
924 backupdir.Buffer, backupfile.Buffer, dwFlags);
926 RtlFreeUnicodeString(&title);
927 RtlFreeUnicodeString(&inf);
928 RtlFreeUnicodeString(&section);
929 RtlFreeUnicodeString(&backupdir);
930 RtlFreeUnicodeString(&backupfile);
932 return hr;
935 /***********************************************************************
936 * FileSaveRestoreOnINFW (ADVPACK.@)
939 * PARAMS
940 * hWnd [I] Handle to the window used for the display.
941 * pszTitle [I] Title of the window.
942 * pszINF [I] Fully-qualified INF filename.
943 * pszSection [I] GenInstall INF section name.
944 * pszBackupDir [I] Directory to store the backup file.
945 * pszBaseBackupFile [I] Basename of the backup files.
946 * dwFlags [I] See advpub.h
948 * RETURNS
949 * Success: S_OK.
950 * Failure: E_FAIL.
952 * NOTES
953 * If pszSection is NULL, the default section will be used.
955 * BUGS
956 * Unimplemented.
958 HRESULT WINAPI FileSaveRestoreOnINFW(HWND hWnd, LPCWSTR pszTitle, LPCWSTR pszINF,
959 LPCWSTR pszSection, LPCWSTR pszBackupDir,
960 LPCWSTR pszBaseBackupFile, DWORD dwFlags)
962 FIXME("(%p, %s, %s, %s, %s, %s, %d): stub\n", hWnd, debugstr_w(pszTitle),
963 debugstr_w(pszINF), debugstr_w(pszSection), debugstr_w(pszBackupDir),
964 debugstr_w(pszBaseBackupFile), dwFlags);
966 return E_FAIL;
969 /***********************************************************************
970 * GetVersionFromFileA (ADVPACK.@)
972 * See GetVersionFromFileExW.
974 HRESULT WINAPI GetVersionFromFileA(LPCSTR Filename, LPDWORD MajorVer,
975 LPDWORD MinorVer, BOOL Version )
977 TRACE("(%s, %p, %p, %d)\n", debugstr_a(Filename), MajorVer, MinorVer, Version);
978 return GetVersionFromFileExA(Filename, MajorVer, MinorVer, Version);
981 /***********************************************************************
982 * GetVersionFromFileW (ADVPACK.@)
984 * See GetVersionFromFileExW.
986 HRESULT WINAPI GetVersionFromFileW(LPCWSTR Filename, LPDWORD MajorVer,
987 LPDWORD MinorVer, BOOL Version )
989 TRACE("(%s, %p, %p, %d)\n", debugstr_w(Filename), MajorVer, MinorVer, Version);
990 return GetVersionFromFileExW(Filename, MajorVer, MinorVer, Version);
993 /* data for GetVersionFromFileEx */
994 typedef struct tagLANGANDCODEPAGE
996 WORD wLanguage;
997 WORD wCodePage;
998 } LANGANDCODEPAGE;
1000 /***********************************************************************
1001 * GetVersionFromFileExA (ADVPACK.@)
1003 * See GetVersionFromFileExW.
1005 HRESULT WINAPI GetVersionFromFileExA(LPCSTR lpszFilename, LPDWORD pdwMSVer,
1006 LPDWORD pdwLSVer, BOOL bVersion )
1008 UNICODE_STRING filename;
1009 HRESULT res;
1011 TRACE("(%s, %p, %p, %d)\n", debugstr_a(lpszFilename),
1012 pdwMSVer, pdwLSVer, bVersion);
1014 RtlCreateUnicodeStringFromAsciiz(&filename, lpszFilename);
1016 res = GetVersionFromFileExW(filename.Buffer, pdwMSVer, pdwLSVer, bVersion);
1018 RtlFreeUnicodeString(&filename);
1020 return res;
1023 /***********************************************************************
1024 * GetVersionFromFileExW (ADVPACK.@)
1026 * Gets the files version or language information.
1028 * PARAMS
1029 * lpszFilename [I] The file to get the info from.
1030 * pdwMSVer [O] Major version.
1031 * pdwLSVer [O] Minor version.
1032 * bVersion [I] Whether to retrieve version or language info.
1034 * RETURNS
1035 * Always returns S_OK.
1037 * NOTES
1038 * If bVersion is TRUE, version information is retrieved, else
1039 * pdwMSVer gets the language ID and pdwLSVer gets the codepage ID.
1041 HRESULT WINAPI GetVersionFromFileExW(LPCWSTR lpszFilename, LPDWORD pdwMSVer,
1042 LPDWORD pdwLSVer, BOOL bVersion )
1044 VS_FIXEDFILEINFO *pFixedVersionInfo;
1045 LANGANDCODEPAGE *pLangAndCodePage;
1046 DWORD dwHandle, dwInfoSize;
1047 WCHAR szWinDir[MAX_PATH];
1048 WCHAR szFile[MAX_PATH];
1049 LPVOID pVersionInfo = NULL;
1050 BOOL bFileCopied = FALSE;
1051 UINT uValueLen;
1053 TRACE("(%s, %p, %p, %d)\n", debugstr_w(lpszFilename),
1054 pdwMSVer, pdwLSVer, bVersion);
1056 *pdwLSVer = 0;
1057 *pdwMSVer = 0;
1059 lstrcpynW(szFile, lpszFilename, MAX_PATH);
1061 dwInfoSize = GetFileVersionInfoSizeW(szFile, &dwHandle);
1062 if (!dwInfoSize)
1064 /* check that the file exists */
1065 if (GetFileAttributesW(szFile) == INVALID_FILE_ATTRIBUTES)
1066 return S_OK;
1068 /* file exists, but won't be found by GetFileVersionInfoSize,
1069 * so copy it to the temp dir where it will be found.
1071 GetWindowsDirectoryW(szWinDir, MAX_PATH);
1072 GetTempFileNameW(szWinDir, NULL, 0, szFile);
1073 CopyFileW(lpszFilename, szFile, FALSE);
1074 bFileCopied = TRUE;
1076 dwInfoSize = GetFileVersionInfoSizeW(szFile, &dwHandle);
1077 if (!dwInfoSize)
1078 goto done;
1081 pVersionInfo = HeapAlloc(GetProcessHeap(), 0, dwInfoSize);
1082 if (!pVersionInfo)
1083 goto done;
1085 if (!GetFileVersionInfoW(szFile, dwHandle, dwInfoSize, pVersionInfo))
1086 goto done;
1088 if (bVersion)
1090 if (!VerQueryValueW(pVersionInfo, L"\\", (void **)&pFixedVersionInfo, &uValueLen))
1091 goto done;
1093 if (!uValueLen)
1094 goto done;
1096 *pdwMSVer = pFixedVersionInfo->dwFileVersionMS;
1097 *pdwLSVer = pFixedVersionInfo->dwFileVersionLS;
1099 else
1101 if (!VerQueryValueW(pVersionInfo, L"\\VarFileInfo\\Translation",
1102 (LPVOID *)&pLangAndCodePage, &uValueLen))
1103 goto done;
1105 if (!uValueLen)
1106 goto done;
1108 *pdwMSVer = pLangAndCodePage->wLanguage;
1109 *pdwLSVer = pLangAndCodePage->wCodePage;
1112 done:
1113 HeapFree(GetProcessHeap(), 0, pVersionInfo);
1115 if (bFileCopied)
1116 DeleteFileW(szFile);
1118 return S_OK;