Allocate the correct nr of bytes for lpszCookies in HTTP_HttpOpenRequestA.
[wine.git] / dlls / version / install.c
blobd9ae145dcc9140dea1d78c2ffb6e5682a15c711f
1 /*
2 * Implementation of VERSION.DLL - File Installer routines
4 * Copyright 1996,1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * TODO
22 * o Check the installation functions.
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winver.h"
33 #include "winnls.h"
34 #include "wine/unicode.h"
35 #include "winerror.h"
36 #include "lzexpand.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ver);
42 /******************************************************************************
43 * testFileExistenceA
45 * Tests whether a given path/file combination exists. If the file does
46 * not exist, the return value is zero. If it does exist, the return
47 * value is non-zero.
49 * Revision history
50 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
51 * Original implementation
54 static int testFileExistenceA( char const * path, char const * file, BOOL excl )
56 char filename[1024];
57 int filenamelen;
58 OFSTRUCT fileinfo;
60 fileinfo.cBytes = sizeof(OFSTRUCT);
62 strcpy(filename, path);
63 filenamelen = strlen(filename);
65 /* Add a trailing \ if necessary */
66 if(filenamelen) {
67 if(filename[filenamelen - 1] != '\\')
68 strcat(filename, "\\");
70 else /* specify the current directory */
71 strcpy(filename, ".\\");
73 /* Create the full pathname */
74 strcat(filename, file);
76 return (OpenFile(filename, &fileinfo,
77 OF_EXIST | (excl ? OF_SHARE_EXCLUSIVE : 0)) != HFILE_ERROR);
80 /******************************************************************************
81 * testFileExistenceW
83 static int testFileExistenceW( const WCHAR *path, const WCHAR *file, BOOL excl )
85 char *filename;
86 DWORD pathlen, filelen;
87 int ret;
88 OFSTRUCT fileinfo;
90 fileinfo.cBytes = sizeof(OFSTRUCT);
92 pathlen = WideCharToMultiByte( CP_ACP, 0, path, -1, NULL, 0, NULL, NULL );
93 filelen = WideCharToMultiByte( CP_ACP, 0, file, -1, NULL, 0, NULL, NULL );
94 filename = HeapAlloc( GetProcessHeap(), 0, pathlen+filelen+2 );
96 WideCharToMultiByte( CP_ACP, 0, path, -1, filename, pathlen, NULL, NULL );
97 /* Add a trailing \ if necessary */
98 if (pathlen > 1)
100 if (filename[pathlen-2] != '\\') strcpy( &filename[pathlen-1], "\\" );
102 else /* specify the current directory */
103 strcpy(filename, ".\\");
105 WideCharToMultiByte( CP_ACP, 0, file, -1, filename+strlen(filename), filelen, NULL, NULL );
107 ret = (OpenFile(filename, &fileinfo,
108 OF_EXIST | (excl ? OF_SHARE_EXCLUSIVE : 0)) != HFILE_ERROR);
109 HeapFree( GetProcessHeap(), 0, filename );
110 return ret;
113 /*****************************************************************************
114 * VerFindFileA [VERSION.@]
116 * Determines where to install a file based on whether it locates another
117 * version of the file in the system. The values VerFindFile returns are
118 * used in a subsequent call to the VerInstallFile function.
120 * Revision history:
121 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
122 * Reimplementation of VerFindFile from original stub.
124 DWORD WINAPI VerFindFileA(
125 UINT flags,
126 LPCSTR lpszFilename,
127 LPCSTR lpszWinDir,
128 LPCSTR lpszAppDir,
129 LPSTR lpszCurDir,
130 UINT *lpuCurDirLen,
131 LPSTR lpszDestDir,
132 UINT *lpuDestDirLen )
134 DWORD retval = 0;
135 const char *curDir;
136 const char *destDir;
137 unsigned int curDirSizeReq;
138 unsigned int destDirSizeReq;
139 char systemDir[MAX_PATH];
141 /* Print out debugging information */
142 TRACE("flags = %x filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)\n",
143 flags, debugstr_a(lpszFilename), debugstr_a(lpszWinDir), debugstr_a(lpszAppDir),
144 lpuCurDirLen, lpuCurDirLen ? *lpuCurDirLen : 0,
145 lpuDestDirLen, lpuDestDirLen ? *lpuDestDirLen : 0 );
147 /* Figure out where the file should go; shared files default to the
148 system directory */
150 GetSystemDirectoryA(systemDir, sizeof(systemDir));
151 curDir = "";
152 destDir = "";
154 if(flags & VFFF_ISSHAREDFILE)
156 destDir = systemDir;
157 /* Were we given a filename? If so, try to find the file. */
158 if(lpszFilename)
160 if(testFileExistenceA(destDir, lpszFilename, FALSE)) curDir = destDir;
161 else if(lpszAppDir && testFileExistenceA(lpszAppDir, lpszFilename, FALSE))
163 curDir = lpszAppDir;
164 retval |= VFF_CURNEDEST;
168 else /* not a shared file */
170 if(lpszAppDir)
172 destDir = lpszAppDir;
173 if(lpszFilename)
175 if(testFileExistenceA(destDir, lpszFilename, FALSE)) curDir = destDir;
176 else if(testFileExistenceA(systemDir, lpszFilename, FALSE))
178 curDir = systemDir;
179 retval |= VFF_CURNEDEST;
185 if (lpszFilename && !testFileExistenceA(curDir, lpszFilename, TRUE))
186 retval |= VFF_FILEINUSE;
188 curDirSizeReq = strlen(curDir) + 1;
189 destDirSizeReq = strlen(destDir) + 1;
191 /* Make sure that the pointers to the size of the buffers are
192 valid; if not, do NOTHING with that buffer. If that pointer
193 is valid, then make sure that the buffer pointer is valid, too! */
195 if(lpuDestDirLen && lpszDestDir)
197 if (*lpuDestDirLen < destDirSizeReq) retval |= VFF_BUFFTOOSMALL;
198 lstrcpynA(lpszDestDir, destDir, *lpuDestDirLen);
199 *lpuDestDirLen = destDirSizeReq;
201 if(lpuCurDirLen && lpszCurDir)
203 if(*lpuCurDirLen < curDirSizeReq) retval |= VFF_BUFFTOOSMALL;
204 lstrcpynA(lpszCurDir, curDir, *lpuCurDirLen);
205 *lpuCurDirLen = curDirSizeReq;
208 TRACE("ret = %lu (%s%s%s) curdir=%s destdir=%s\n", retval,
209 (retval & VFF_CURNEDEST) ? "VFF_CURNEDEST " : "",
210 (retval & VFF_FILEINUSE) ? "VFF_FILEINUSE " : "",
211 (retval & VFF_BUFFTOOSMALL) ? "VFF_BUFFTOOSMALL " : "",
212 debugstr_a(lpszCurDir), debugstr_a(lpszDestDir));
214 return retval;
217 /*****************************************************************************
218 * VerFindFileW [VERSION.@]
220 DWORD WINAPI VerFindFileW( UINT flags,LPCWSTR lpszFilename,LPCWSTR lpszWinDir,
221 LPCWSTR lpszAppDir, LPWSTR lpszCurDir,UINT *lpuCurDirLen,
222 LPWSTR lpszDestDir,UINT *lpuDestDirLen )
224 static const WCHAR emptyW;
225 DWORD retval = 0;
226 const WCHAR *curDir;
227 const WCHAR *destDir;
228 unsigned int curDirSizeReq;
229 unsigned int destDirSizeReq;
230 WCHAR systemDir[MAX_PATH];
232 /* Print out debugging information */
233 TRACE("flags = %x filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)\n",
234 flags, debugstr_w(lpszFilename), debugstr_w(lpszWinDir), debugstr_w(lpszAppDir),
235 lpuCurDirLen, lpuCurDirLen ? *lpuCurDirLen : 0,
236 lpuDestDirLen, lpuDestDirLen ? *lpuDestDirLen : 0 );
238 /* Figure out where the file should go; shared files default to the
239 system directory */
241 GetSystemDirectoryW(systemDir, sizeof(systemDir)/sizeof(WCHAR));
242 curDir = &emptyW;
243 destDir = &emptyW;
245 if(flags & VFFF_ISSHAREDFILE)
247 destDir = systemDir;
248 /* Were we given a filename? If so, try to find the file. */
249 if(lpszFilename)
251 if(testFileExistenceW(destDir, lpszFilename, FALSE)) curDir = destDir;
252 else if(lpszAppDir && testFileExistenceW(lpszAppDir, lpszFilename, FALSE))
254 curDir = lpszAppDir;
255 retval |= VFF_CURNEDEST;
259 else /* not a shared file */
261 if(lpszAppDir)
263 destDir = lpszAppDir;
264 if(lpszFilename)
266 if(testFileExistenceW(destDir, lpszFilename, FALSE)) curDir = destDir;
267 else if(testFileExistenceW(systemDir, lpszFilename, FALSE))
269 curDir = systemDir;
270 retval |= VFF_CURNEDEST;
276 if (lpszFilename && !testFileExistenceW(curDir, lpszFilename, TRUE))
277 retval |= VFF_FILEINUSE;
279 curDirSizeReq = strlenW(curDir) + 1;
280 destDirSizeReq = strlenW(destDir) + 1;
282 /* Make sure that the pointers to the size of the buffers are
283 valid; if not, do NOTHING with that buffer. If that pointer
284 is valid, then make sure that the buffer pointer is valid, too! */
286 if(lpuDestDirLen && lpszDestDir)
288 if (*lpuDestDirLen < destDirSizeReq) retval |= VFF_BUFFTOOSMALL;
289 lstrcpynW(lpszDestDir, destDir, *lpuDestDirLen);
290 *lpuDestDirLen = destDirSizeReq;
292 if(lpuCurDirLen && lpszCurDir)
294 if(*lpuCurDirLen < curDirSizeReq) retval |= VFF_BUFFTOOSMALL;
295 lstrcpynW(lpszCurDir, curDir, *lpuCurDirLen);
296 *lpuCurDirLen = curDirSizeReq;
299 TRACE("ret = %lu (%s%s%s) curdir=%s destdir=%s\n", retval,
300 (retval & VFF_CURNEDEST) ? "VFF_CURNEDEST " : "",
301 (retval & VFF_FILEINUSE) ? "VFF_FILEINUSE " : "",
302 (retval & VFF_BUFFTOOSMALL) ? "VFF_BUFFTOOSMALL " : "",
303 debugstr_w(lpszCurDir), debugstr_w(lpszDestDir));
304 return retval;
307 static LPBYTE
308 _fetch_versioninfo(LPSTR fn,VS_FIXEDFILEINFO **vffi) {
309 DWORD alloclen;
310 LPBYTE buf;
311 DWORD ret;
313 alloclen = 1000;
314 buf=HeapAlloc(GetProcessHeap(), 0, alloclen);
315 if(buf == NULL) {
316 WARN("Memory exausted while fetching version info!\n");
317 return NULL;
319 while (1) {
320 ret = GetFileVersionInfoA(fn,0,alloclen,buf);
321 if (!ret) {
322 HeapFree(GetProcessHeap(), 0, buf);
323 return NULL;
325 if (alloclen<*(WORD*)buf) {
326 alloclen = *(WORD*)buf;
327 HeapFree(GetProcessHeap(), 0, buf);
328 buf = HeapAlloc(GetProcessHeap(), 0, alloclen);
329 if(buf == NULL) {
330 WARN("Memory exausted while fetching version info!\n");
331 return NULL;
333 } else {
334 *vffi = (VS_FIXEDFILEINFO*)(buf+0x14);
335 if ((*vffi)->dwSignature == 0x004f0049) /* hack to detect unicode */
336 *vffi = (VS_FIXEDFILEINFO*)(buf+0x28);
337 if ((*vffi)->dwSignature != VS_FFI_SIGNATURE)
338 WARN("Bad VS_FIXEDFILEINFO signature 0x%08lx\n",(*vffi)->dwSignature);
339 return buf;
344 static DWORD
345 _error2vif(DWORD error) {
346 switch (error) {
347 case ERROR_ACCESS_DENIED:
348 return VIF_ACCESSVIOLATION;
349 case ERROR_SHARING_VIOLATION:
350 return VIF_SHARINGVIOLATION;
351 default:
352 return 0;
357 /******************************************************************************
358 * VerInstallFileA [VERSION.@]
360 DWORD WINAPI VerInstallFileA(
361 UINT flags,LPCSTR srcfilename,LPCSTR destfilename,LPCSTR srcdir,
362 LPCSTR destdir,LPCSTR curdir,LPSTR tmpfile,UINT *tmpfilelen )
364 LPCSTR pdest;
365 char destfn[260],tmpfn[260],srcfn[260];
366 HFILE hfsrc,hfdst;
367 DWORD attr,ret,xret,tmplast;
368 LPBYTE buf1,buf2;
369 OFSTRUCT ofs;
371 TRACE("(%x,%s,%s,%s,%s,%s,%p,%d)\n",
372 flags,srcfilename,destfilename,srcdir,destdir,curdir,tmpfile,*tmpfilelen
374 xret = 0;
375 sprintf(srcfn,"%s\\%s",srcdir,srcfilename);
376 if (!destdir || !*destdir) pdest = srcdir;
377 else pdest = destdir;
378 sprintf(destfn,"%s\\%s",pdest,destfilename);
379 hfsrc=LZOpenFileA(srcfn,&ofs,OF_READ);
380 if (hfsrc < 0)
381 return VIF_CANNOTREADSRC;
382 sprintf(tmpfn,"%s\\%s",pdest,destfilename);
383 tmplast=strlen(pdest)+1;
384 attr = GetFileAttributesA(tmpfn);
385 if (attr != INVALID_FILE_ATTRIBUTES) {
386 if (attr & FILE_ATTRIBUTE_READONLY) {
387 LZClose(hfsrc);
388 return VIF_WRITEPROT;
390 /* FIXME: check if file currently in use and return VIF_FILEINUSE */
392 attr = INVALID_FILE_ATTRIBUTES;
393 if (flags & VIFF_FORCEINSTALL) {
394 if (tmpfile[0]) {
395 sprintf(tmpfn,"%s\\%s",pdest,tmpfile);
396 tmplast = strlen(pdest)+1;
397 attr = GetFileAttributesA(tmpfn);
398 /* if it exists, it has been copied by the call before.
399 * we jump over the copy part...
403 if (attr == INVALID_FILE_ATTRIBUTES) {
404 char *s;
406 GetTempFileNameA(pdest,"ver",0,tmpfn); /* should not fail ... */
407 s=strrchr(tmpfn,'\\');
408 if (s)
409 tmplast = s-tmpfn;
410 else
411 tmplast = 0;
412 hfdst = OpenFile(tmpfn,&ofs,OF_CREATE);
413 if (hfdst == HFILE_ERROR) {
414 LZClose(hfsrc);
415 return VIF_CANNOTCREATE; /* | translated dos error */
417 ret = LZCopy(hfsrc,hfdst);
418 _lclose(hfdst);
419 if (((long) ret) < 0) {
420 /* translate LZ errors into VIF_xxx */
421 switch (ret) {
422 case LZERROR_BADINHANDLE:
423 case LZERROR_READ:
424 case LZERROR_BADVALUE:
425 case LZERROR_UNKNOWNALG:
426 ret = VIF_CANNOTREADSRC;
427 break;
428 case LZERROR_BADOUTHANDLE:
429 case LZERROR_WRITE:
430 ret = VIF_OUTOFSPACE;
431 break;
432 case LZERROR_GLOBALLOC:
433 case LZERROR_GLOBLOCK:
434 ret = VIF_OUTOFMEMORY;
435 break;
436 default: /* unknown error, should not happen */
437 ret = 0;
438 break;
440 if (ret) {
441 LZClose(hfsrc);
442 return ret;
446 xret = 0;
447 if (!(flags & VIFF_FORCEINSTALL)) {
448 VS_FIXEDFILEINFO *destvffi,*tmpvffi;
449 buf1 = _fetch_versioninfo(destfn,&destvffi);
450 if (buf1) {
451 buf2 = _fetch_versioninfo(tmpfn,&tmpvffi);
452 if (buf2) {
453 char *tbuf1,*tbuf2;
454 UINT len1,len2;
456 len1=len2=40;
458 /* compare file versions */
459 if ((destvffi->dwFileVersionMS > tmpvffi->dwFileVersionMS)||
460 ((destvffi->dwFileVersionMS==tmpvffi->dwFileVersionMS)&&
461 (destvffi->dwFileVersionLS > tmpvffi->dwFileVersionLS)
464 xret |= VIF_MISMATCH|VIF_SRCOLD;
465 /* compare filetypes and filesubtypes */
466 if ((destvffi->dwFileType!=tmpvffi->dwFileType) ||
467 (destvffi->dwFileSubtype!=tmpvffi->dwFileSubtype)
469 xret |= VIF_MISMATCH|VIF_DIFFTYPE;
470 if (VerQueryValueA(buf1,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf1,&len1) &&
471 VerQueryValueA(buf2,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf2,&len2)
473 /* irgendwas mit tbuf1 und tbuf2 machen
474 * generiert DIFFLANG|MISMATCH
477 HeapFree(GetProcessHeap(), 0, buf2);
478 } else
479 xret=VIF_MISMATCH|VIF_SRCOLD;
480 HeapFree(GetProcessHeap(), 0, buf1);
483 if (xret) {
484 if (*tmpfilelen<strlen(tmpfn+tmplast)) {
485 xret|=VIF_BUFFTOOSMALL;
486 DeleteFileA(tmpfn);
487 } else {
488 strcpy(tmpfile,tmpfn+tmplast);
489 *tmpfilelen = strlen(tmpfn+tmplast)+1;
490 xret|=VIF_TEMPFILE;
492 } else {
493 if (-1!=GetFileAttributesA(destfn))
494 if (!DeleteFileA(destfn)) {
495 xret|=_error2vif(GetLastError())|VIF_CANNOTDELETE;
496 DeleteFileA(tmpfn);
497 LZClose(hfsrc);
498 return xret;
500 if ((!(flags & VIFF_DONTDELETEOLD)) &&
501 curdir &&
502 *curdir &&
503 lstrcmpiA(curdir,pdest)
505 char curfn[260];
507 sprintf(curfn,"%s\\%s",curdir,destfilename);
508 if (INVALID_FILE_ATTRIBUTES != GetFileAttributesA(curfn)) {
509 /* FIXME: check if in use ... if it is, VIF_CANNOTDELETECUR */
510 if (!DeleteFileA(curfn))
511 xret|=_error2vif(GetLastError())|VIF_CANNOTDELETECUR;
514 if (!MoveFileA(tmpfn,destfn)) {
515 xret|=_error2vif(GetLastError())|VIF_CANNOTRENAME;
516 DeleteFileA(tmpfn);
519 LZClose(hfsrc);
520 return xret;
524 /******************************************************************************
525 * VerInstallFileW [VERSION.@]
527 DWORD WINAPI VerInstallFileW(
528 UINT flags,LPCWSTR srcfilename,LPCWSTR destfilename,LPCWSTR srcdir,
529 LPCWSTR destdir,LPCWSTR curdir,LPWSTR tmpfile,UINT *tmpfilelen )
531 LPSTR wsrcf = NULL, wsrcd = NULL, wdestf = NULL, wdestd = NULL, wtmpf = NULL, wcurd = NULL;
532 DWORD ret;
533 UINT len;
535 if (srcfilename)
537 len = WideCharToMultiByte( CP_ACP, 0, srcfilename, -1, NULL, 0, NULL, NULL );
538 if ((wsrcf = HeapAlloc( GetProcessHeap(), 0, len )))
539 WideCharToMultiByte( CP_ACP, 0, srcfilename, -1, wsrcf, len, NULL, NULL );
541 if (srcdir)
543 len = WideCharToMultiByte( CP_ACP, 0, srcdir, -1, NULL, 0, NULL, NULL );
544 if ((wsrcd = HeapAlloc( GetProcessHeap(), 0, len )))
545 WideCharToMultiByte( CP_ACP, 0, srcdir, -1, wsrcd, len, NULL, NULL );
547 if (destfilename)
549 len = WideCharToMultiByte( CP_ACP, 0, destfilename, -1, NULL, 0, NULL, NULL );
550 if ((wdestf = HeapAlloc( GetProcessHeap(), 0, len )))
551 WideCharToMultiByte( CP_ACP, 0, destfilename, -1, wdestf, len, NULL, NULL );
553 if (destdir)
555 len = WideCharToMultiByte( CP_ACP, 0, destdir, -1, NULL, 0, NULL, NULL );
556 if ((wdestd = HeapAlloc( GetProcessHeap(), 0, len )))
557 WideCharToMultiByte( CP_ACP, 0, destdir, -1, wdestd, len, NULL, NULL );
559 if (curdir)
561 len = WideCharToMultiByte( CP_ACP, 0, curdir, -1, NULL, 0, NULL, NULL );
562 if ((wcurd = HeapAlloc( GetProcessHeap(), 0, len )))
563 WideCharToMultiByte( CP_ACP, 0, curdir, -1, wcurd, len, NULL, NULL );
565 len = *tmpfilelen * sizeof(WCHAR);
566 wtmpf = HeapAlloc( GetProcessHeap(), 0, len );
567 ret = VerInstallFileA(flags,wsrcf,wdestf,wsrcd,wdestd,wcurd,wtmpf,&len);
568 if (!ret)
569 *tmpfilelen = MultiByteToWideChar( CP_ACP, 0, wtmpf, -1, tmpfile, *tmpfilelen );
570 else if (ret & VIF_BUFFTOOSMALL)
571 *tmpfilelen = len; /* FIXME: not correct */
573 HeapFree( GetProcessHeap(), 0, wsrcf );
574 HeapFree( GetProcessHeap(), 0, wsrcd );
575 HeapFree( GetProcessHeap(), 0, wdestf );
576 HeapFree( GetProcessHeap(), 0, wdestd );
577 HeapFree( GetProcessHeap(), 0, wtmpf );
578 HeapFree( GetProcessHeap(), 0, wcurd );
579 return ret;