Removed ordinal from functions that don't need a specific value.
[wine/multimedia.git] / dlls / version / install.c
blobfb4ae91bbb20caaee6ef4312452ab31ad87fdbf0
1 /*
2 * Implementation of VERSION.DLL - File Installer routines
3 *
4 * Copyright 1996,1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
6 */
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
12 #include "windef.h"
13 #include "winbase.h"
14 #include "winver.h"
15 #include "winnls.h"
16 #include "wine/unicode.h"
17 #include "winerror.h"
18 #include "lzexpand.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(ver);
24 /******************************************************************************
25 * testFileExistenceA
27 * Tests whether a given path/file combination exists. If the file does
28 * not exist, the return value is zero. If it does exist, the return
29 * value is non-zero.
31 * Revision history
32 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
33 * Original implementation
36 static int testFileExistenceA( char const * path, char const * file, BOOL excl )
38 char filename[1024];
39 int filenamelen;
40 OFSTRUCT fileinfo;
42 fileinfo.cBytes = sizeof(OFSTRUCT);
44 strcpy(filename, path);
45 filenamelen = strlen(filename);
47 /* Add a trailing \ if necessary */
48 if(filenamelen) {
49 if(filename[filenamelen - 1] != '\\')
50 strcat(filename, "\\");
52 else /* specify the current directory */
53 strcpy(filename, ".\\");
55 /* Create the full pathname */
56 strcat(filename, file);
58 return (OpenFile(filename, &fileinfo,
59 OF_EXIST | (excl ? OF_SHARE_EXCLUSIVE : 0)) != HFILE_ERROR);
62 /******************************************************************************
63 * testFileExistenceW
65 static int testFileExistenceW( const WCHAR *path, const WCHAR *file, BOOL excl )
67 char *filename;
68 DWORD pathlen, filelen;
69 int ret;
70 OFSTRUCT fileinfo;
72 fileinfo.cBytes = sizeof(OFSTRUCT);
74 pathlen = WideCharToMultiByte( CP_ACP, 0, path, -1, NULL, 0, NULL, NULL );
75 filelen = WideCharToMultiByte( CP_ACP, 0, file, -1, NULL, 0, NULL, NULL );
76 filename = HeapAlloc( GetProcessHeap(), 0, pathlen+filelen+2 );
78 WideCharToMultiByte( CP_ACP, 0, path, -1, filename, pathlen, NULL, NULL );
79 /* Add a trailing \ if necessary */
80 if (pathlen > 1)
82 if (filename[pathlen-2] != '\\') strcpy( &filename[pathlen-1], "\\" );
84 else /* specify the current directory */
85 strcpy(filename, ".\\");
87 WideCharToMultiByte( CP_ACP, 0, file, -1, filename+strlen(filename), filelen, NULL, NULL );
89 ret = (OpenFile(filename, &fileinfo,
90 OF_EXIST | (excl ? OF_SHARE_EXCLUSIVE : 0)) != HFILE_ERROR);
91 HeapFree( GetProcessHeap(), 0, filename );
92 return ret;
95 /*****************************************************************************
97 * VerFindFileA() [VER.8]
98 * Determines where to install a file based on whether it locates another
99 * version of the file in the system. The values VerFindFile returns are
100 * used in a subsequent call to the VerInstallFile function.
102 * Revision history:
103 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
104 * Reimplementation of VerFindFile from original stub.
106 DWORD WINAPI VerFindFileA(
107 UINT flags,
108 LPCSTR lpszFilename,
109 LPCSTR lpszWinDir,
110 LPCSTR lpszAppDir,
111 LPSTR lpszCurDir,
112 UINT *lpuCurDirLen,
113 LPSTR lpszDestDir,
114 UINT *lpuDestDirLen )
116 DWORD retval = 0;
117 const char *curDir;
118 const char *destDir;
119 unsigned int curDirSizeReq;
120 unsigned int destDirSizeReq;
121 char systemDir[MAX_PATH];
123 /* Print out debugging information */
124 TRACE("flags = %x filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)",
125 flags, debugstr_a(lpszFilename), debugstr_a(lpszWinDir), debugstr_a(lpszAppDir),
126 lpuCurDirLen, lpuCurDirLen ? *lpuCurDirLen : 0,
127 lpuDestDirLen, lpuDestDirLen ? *lpuDestDirLen : 0 );
129 /* Figure out where the file should go; shared files default to the
130 system directory */
132 GetSystemDirectoryA(systemDir, sizeof(systemDir));
133 curDir = "";
134 destDir = "";
136 if(flags & VFFF_ISSHAREDFILE)
138 destDir = systemDir;
139 /* Were we given a filename? If so, try to find the file. */
140 if(lpszFilename)
142 if(testFileExistenceA(destDir, lpszFilename, FALSE)) curDir = destDir;
143 else if(lpszAppDir && testFileExistenceA(lpszAppDir, lpszFilename, FALSE))
145 curDir = lpszAppDir;
146 retval |= VFF_CURNEDEST;
150 else /* not a shared file */
152 if(lpszAppDir)
154 destDir = lpszAppDir;
155 if(lpszFilename)
157 if(testFileExistenceA(destDir, lpszFilename, FALSE)) curDir = destDir;
158 else if(testFileExistenceA(systemDir, lpszFilename, FALSE))
160 curDir = systemDir;
161 retval |= VFF_CURNEDEST;
167 if (lpszFilename && !testFileExistenceA(curDir, lpszFilename, TRUE))
168 retval |= VFF_FILEINUSE;
170 curDirSizeReq = strlen(curDir) + 1;
171 destDirSizeReq = strlen(destDir) + 1;
173 /* Make sure that the pointers to the size of the buffers are
174 valid; if not, do NOTHING with that buffer. If that pointer
175 is valid, then make sure that the buffer pointer is valid, too! */
177 if(lpuDestDirLen && lpszDestDir)
179 if (*lpuDestDirLen < destDirSizeReq) retval |= VFF_BUFFTOOSMALL;
180 lstrcpynA(lpszDestDir, destDir, *lpuDestDirLen);
181 *lpuDestDirLen = destDirSizeReq;
183 if(lpuCurDirLen && lpszCurDir)
185 if(*lpuCurDirLen < curDirSizeReq) retval |= VFF_BUFFTOOSMALL;
186 lstrcpynA(lpszCurDir, curDir, *lpuCurDirLen);
187 *lpuCurDirLen = curDirSizeReq;
190 TRACE("ret = %lu (%s%s%s) curdir=%s destdir=%s\n", retval,
191 (retval & VFF_CURNEDEST) ? "VFF_CURNEDEST " : "",
192 (retval & VFF_FILEINUSE) ? "VFF_FILEINUSE " : "",
193 (retval & VFF_BUFFTOOSMALL) ? "VFF_BUFFTOOSMALL " : "",
194 debugstr_a(lpszCurDir), debugstr_a(lpszDestDir));
196 return retval;
199 /*****************************************************************************
200 * VerFindFileW [VERSION.6]
202 DWORD WINAPI VerFindFileW( UINT flags,LPCWSTR lpszFilename,LPCWSTR lpszWinDir,
203 LPCWSTR lpszAppDir, LPWSTR lpszCurDir,UINT *lpuCurDirLen,
204 LPWSTR lpszDestDir,UINT *lpuDestDirLen )
206 static const WCHAR emptyW;
207 DWORD retval = 0;
208 const WCHAR *curDir;
209 const WCHAR *destDir;
210 unsigned int curDirSizeReq;
211 unsigned int destDirSizeReq;
212 WCHAR systemDir[MAX_PATH];
214 /* Print out debugging information */
215 TRACE("flags = %x filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)",
216 flags, debugstr_w(lpszFilename), debugstr_w(lpszWinDir), debugstr_w(lpszAppDir),
217 lpuCurDirLen, lpuCurDirLen ? *lpuCurDirLen : 0,
218 lpuDestDirLen, lpuDestDirLen ? *lpuDestDirLen : 0 );
220 /* Figure out where the file should go; shared files default to the
221 system directory */
223 GetSystemDirectoryW(systemDir, sizeof(systemDir)/sizeof(WCHAR));
224 curDir = &emptyW;
225 destDir = &emptyW;
227 if(flags & VFFF_ISSHAREDFILE)
229 destDir = systemDir;
230 /* Were we given a filename? If so, try to find the file. */
231 if(lpszFilename)
233 if(testFileExistenceW(destDir, lpszFilename, FALSE)) curDir = destDir;
234 else if(lpszAppDir && testFileExistenceW(lpszAppDir, lpszFilename, FALSE))
236 curDir = lpszAppDir;
237 retval |= VFF_CURNEDEST;
241 else /* not a shared file */
243 if(lpszAppDir)
245 destDir = lpszAppDir;
246 if(lpszFilename)
248 if(testFileExistenceW(destDir, lpszFilename, FALSE)) curDir = destDir;
249 else if(testFileExistenceW(systemDir, lpszFilename, FALSE))
251 curDir = systemDir;
252 retval |= VFF_CURNEDEST;
258 if (lpszFilename && !testFileExistenceW(curDir, lpszFilename, TRUE))
259 retval |= VFF_FILEINUSE;
261 curDirSizeReq = strlenW(curDir) + 1;
262 destDirSizeReq = strlenW(destDir) + 1;
264 /* Make sure that the pointers to the size of the buffers are
265 valid; if not, do NOTHING with that buffer. If that pointer
266 is valid, then make sure that the buffer pointer is valid, too! */
268 if(lpuDestDirLen && lpszDestDir)
270 if (*lpuDestDirLen < destDirSizeReq) retval |= VFF_BUFFTOOSMALL;
271 lstrcpynW(lpszDestDir, destDir, *lpuDestDirLen);
272 *lpuDestDirLen = destDirSizeReq;
274 if(lpuCurDirLen && lpszCurDir)
276 if(*lpuCurDirLen < curDirSizeReq) retval |= VFF_BUFFTOOSMALL;
277 lstrcpynW(lpszCurDir, curDir, *lpuCurDirLen);
278 *lpuCurDirLen = curDirSizeReq;
281 TRACE("ret = %lu (%s%s%s) curdir=%s destdir=%s\n", retval,
282 (retval & VFF_CURNEDEST) ? "VFF_CURNEDEST " : "",
283 (retval & VFF_FILEINUSE) ? "VFF_FILEINUSE " : "",
284 (retval & VFF_BUFFTOOSMALL) ? "VFF_BUFFTOOSMALL " : "",
285 debugstr_w(lpszCurDir), debugstr_w(lpszDestDir));
286 return retval;
289 static LPBYTE
290 _fetch_versioninfo(LPSTR fn,VS_FIXEDFILEINFO **vffi) {
291 DWORD alloclen;
292 LPBYTE buf;
293 DWORD ret;
295 alloclen = 1000;
296 buf=HeapAlloc(GetProcessHeap(), 0, alloclen);
297 if(buf == NULL) {
298 WARN("Memory exausted while fetching version info!");
299 return NULL;
301 while (1) {
302 ret = GetFileVersionInfoA(fn,0,alloclen,buf);
303 if (!ret) {
304 HeapFree(GetProcessHeap(), 0, buf);
305 return NULL;
307 if (alloclen<*(WORD*)buf) {
308 alloclen = *(WORD*)buf;
309 HeapFree(GetProcessHeap(), 0, buf);
310 buf = HeapAlloc(GetProcessHeap(), 0, alloclen);
311 if(buf == NULL) {
312 WARN("Memory exausted while fetching version info!");
313 return NULL;
315 } else {
316 *vffi = (VS_FIXEDFILEINFO*)(buf+0x14);
317 if ((*vffi)->dwSignature == 0x004f0049) /* hack to detect unicode */
318 *vffi = (VS_FIXEDFILEINFO*)(buf+0x28);
319 if ((*vffi)->dwSignature != VS_FFI_SIGNATURE)
320 WARN("Bad VS_FIXEDFILEINFO signature 0x%08lx\n",(*vffi)->dwSignature);
321 return buf;
326 static DWORD
327 _error2vif(DWORD error) {
328 switch (error) {
329 case ERROR_ACCESS_DENIED:
330 return VIF_ACCESSVIOLATION;
331 case ERROR_SHARING_VIOLATION:
332 return VIF_SHARINGVIOLATION;
333 default:
334 return 0;
339 /******************************************************************************
340 * VerInstallFileA [VERSION.7]
342 DWORD WINAPI VerInstallFileA(
343 UINT flags,LPCSTR srcfilename,LPCSTR destfilename,LPCSTR srcdir,
344 LPCSTR destdir,LPCSTR curdir,LPSTR tmpfile,UINT *tmpfilelen )
346 LPCSTR pdest;
347 char destfn[260],tmpfn[260],srcfn[260];
348 HFILE hfsrc,hfdst;
349 DWORD attr,ret,xret,tmplast;
350 LPBYTE buf1,buf2;
351 OFSTRUCT ofs;
353 TRACE("(%x,%s,%s,%s,%s,%s,%p,%d)\n",
354 flags,srcfilename,destfilename,srcdir,destdir,curdir,tmpfile,*tmpfilelen
356 xret = 0;
357 sprintf(srcfn,"%s\\%s",srcdir,srcfilename);
358 if (!destdir || !*destdir) pdest = srcdir;
359 else pdest = destdir;
360 sprintf(destfn,"%s\\%s",pdest,destfilename);
361 hfsrc=LZOpenFileA(srcfn,&ofs,OF_READ);
362 if (hfsrc < 0)
363 return VIF_CANNOTREADSRC;
364 sprintf(tmpfn,"%s\\%s",pdest,destfilename);
365 tmplast=strlen(pdest)+1;
366 attr = GetFileAttributesA(tmpfn);
367 if (attr!=-1) {
368 if (attr & FILE_ATTRIBUTE_READONLY) {
369 LZClose(hfsrc);
370 return VIF_WRITEPROT;
372 /* FIXME: check if file currently in use and return VIF_FILEINUSE */
374 attr = -1;
375 if (flags & VIFF_FORCEINSTALL) {
376 if (tmpfile[0]) {
377 sprintf(tmpfn,"%s\\%s",pdest,tmpfile);
378 tmplast = strlen(pdest)+1;
379 attr = GetFileAttributesA(tmpfn);
380 /* if it exists, it has been copied by the call before.
381 * we jump over the copy part...
385 if (attr == -1) {
386 char *s;
388 GetTempFileNameA(pdest,"ver",0,tmpfn); /* should not fail ... */
389 s=strrchr(tmpfn,'\\');
390 if (s)
391 tmplast = s-tmpfn;
392 else
393 tmplast = 0;
394 hfdst = OpenFile(tmpfn,&ofs,OF_CREATE);
395 if (hfdst == HFILE_ERROR) {
396 LZClose(hfsrc);
397 return VIF_CANNOTCREATE; /* | translated dos error */
399 ret = LZCopy(hfsrc,hfdst);
400 _lclose(hfdst);
401 if (((long) ret) < 0) {
402 /* translate LZ errors into VIF_xxx */
403 switch (ret) {
404 case LZERROR_BADINHANDLE:
405 case LZERROR_READ:
406 case LZERROR_BADVALUE:
407 case LZERROR_UNKNOWNALG:
408 ret = VIF_CANNOTREADSRC;
409 break;
410 case LZERROR_BADOUTHANDLE:
411 case LZERROR_WRITE:
412 ret = VIF_OUTOFSPACE;
413 break;
414 case LZERROR_GLOBALLOC:
415 case LZERROR_GLOBLOCK:
416 ret = VIF_OUTOFMEMORY;
417 break;
418 default: /* unknown error, should not happen */
419 ret = 0;
420 break;
422 if (ret) {
423 LZClose(hfsrc);
424 return ret;
428 xret = 0;
429 if (!(flags & VIFF_FORCEINSTALL)) {
430 VS_FIXEDFILEINFO *destvffi,*tmpvffi;
431 buf1 = _fetch_versioninfo(destfn,&destvffi);
432 if (buf1) {
433 buf2 = _fetch_versioninfo(tmpfn,&tmpvffi);
434 if (buf2) {
435 char *tbuf1,*tbuf2;
436 UINT len1,len2;
438 len1=len2=40;
440 /* compare file versions */
441 if ((destvffi->dwFileVersionMS > tmpvffi->dwFileVersionMS)||
442 ((destvffi->dwFileVersionMS==tmpvffi->dwFileVersionMS)&&
443 (destvffi->dwFileVersionLS > tmpvffi->dwFileVersionLS)
446 xret |= VIF_MISMATCH|VIF_SRCOLD;
447 /* compare filetypes and filesubtypes */
448 if ((destvffi->dwFileType!=tmpvffi->dwFileType) ||
449 (destvffi->dwFileSubtype!=tmpvffi->dwFileSubtype)
451 xret |= VIF_MISMATCH|VIF_DIFFTYPE;
452 if (VerQueryValueA(buf1,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf1,&len1) &&
453 VerQueryValueA(buf2,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf2,&len2)
455 /* irgendwas mit tbuf1 und tbuf2 machen
456 * generiert DIFFLANG|MISMATCH
459 HeapFree(GetProcessHeap(), 0, buf2);
460 } else
461 xret=VIF_MISMATCH|VIF_SRCOLD;
462 HeapFree(GetProcessHeap(), 0, buf1);
465 if (xret) {
466 if (*tmpfilelen<strlen(tmpfn+tmplast)) {
467 xret|=VIF_BUFFTOOSMALL;
468 DeleteFileA(tmpfn);
469 } else {
470 strcpy(tmpfile,tmpfn+tmplast);
471 *tmpfilelen = strlen(tmpfn+tmplast)+1;
472 xret|=VIF_TEMPFILE;
474 } else {
475 if (-1!=GetFileAttributesA(destfn))
476 if (!DeleteFileA(destfn)) {
477 xret|=_error2vif(GetLastError())|VIF_CANNOTDELETE;
478 DeleteFileA(tmpfn);
479 LZClose(hfsrc);
480 return xret;
482 if ((!(flags & VIFF_DONTDELETEOLD)) &&
483 curdir &&
484 *curdir &&
485 lstrcmpiA(curdir,pdest)
487 char curfn[260];
489 sprintf(curfn,"%s\\%s",curdir,destfilename);
490 if (-1!=GetFileAttributesA(curfn)) {
491 /* FIXME: check if in use ... if it is, VIF_CANNOTDELETECUR */
492 if (!DeleteFileA(curfn))
493 xret|=_error2vif(GetLastError())|VIF_CANNOTDELETECUR;
496 if (!MoveFileA(tmpfn,destfn)) {
497 xret|=_error2vif(GetLastError())|VIF_CANNOTRENAME;
498 DeleteFileA(tmpfn);
501 LZClose(hfsrc);
502 return xret;
506 /******************************************************************************
507 * VerInstallFileW [VERSION.8]
509 DWORD WINAPI VerInstallFileW(
510 UINT flags,LPCWSTR srcfilename,LPCWSTR destfilename,LPCWSTR srcdir,
511 LPCWSTR destdir,LPCWSTR curdir,LPWSTR tmpfile,UINT *tmpfilelen )
513 LPSTR wsrcf = NULL, wsrcd = NULL, wdestf = NULL, wdestd = NULL, wtmpf = NULL, wcurd = NULL;
514 DWORD ret;
515 UINT len;
517 if (srcfilename)
519 len = WideCharToMultiByte( CP_ACP, 0, srcfilename, -1, NULL, 0, NULL, NULL );
520 if ((wsrcf = HeapAlloc( GetProcessHeap(), 0, len )))
521 WideCharToMultiByte( CP_ACP, 0, srcfilename, -1, wsrcf, len, NULL, NULL );
523 if (srcdir)
525 len = WideCharToMultiByte( CP_ACP, 0, srcdir, -1, NULL, 0, NULL, NULL );
526 if ((wsrcd = HeapAlloc( GetProcessHeap(), 0, len )))
527 WideCharToMultiByte( CP_ACP, 0, srcdir, -1, wsrcd, len, NULL, NULL );
529 if (destfilename)
531 len = WideCharToMultiByte( CP_ACP, 0, destfilename, -1, NULL, 0, NULL, NULL );
532 if ((wdestf = HeapAlloc( GetProcessHeap(), 0, len )))
533 WideCharToMultiByte( CP_ACP, 0, destfilename, -1, wdestf, len, NULL, NULL );
535 if (destdir)
537 len = WideCharToMultiByte( CP_ACP, 0, destdir, -1, NULL, 0, NULL, NULL );
538 if ((wdestd = HeapAlloc( GetProcessHeap(), 0, len )))
539 WideCharToMultiByte( CP_ACP, 0, destdir, -1, wdestd, len, NULL, NULL );
541 if (curdir)
543 len = WideCharToMultiByte( CP_ACP, 0, curdir, -1, NULL, 0, NULL, NULL );
544 if ((wcurd = HeapAlloc( GetProcessHeap(), 0, len )))
545 WideCharToMultiByte( CP_ACP, 0, curdir, -1, wcurd, len, NULL, NULL );
547 len = *tmpfilelen * sizeof(WCHAR);
548 wtmpf = HeapAlloc( GetProcessHeap(), 0, len );
549 ret = VerInstallFileA(flags,wsrcf,wdestf,wsrcd,wdestd,wcurd,wtmpf,&len);
550 if (!ret)
551 *tmpfilelen = MultiByteToWideChar( CP_ACP, 0, wtmpf, -1, tmpfile, *tmpfilelen );
552 else if (ret & VIF_BUFFTOOSMALL)
553 *tmpfilelen = len; /* FIXME: not correct */
555 HeapFree( GetProcessHeap(), 0, wsrcf );
556 HeapFree( GetProcessHeap(), 0, wsrcd );
557 HeapFree( GetProcessHeap(), 0, wdestf );
558 HeapFree( GetProcessHeap(), 0, wdestd );
559 HeapFree( GetProcessHeap(), 0, wtmpf );
560 HeapFree( GetProcessHeap(), 0, wcurd );
561 return ret;