2 * Implementation of VERSION.DLL - File Installer routines
4 * Copyright 1996,1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
16 #include "wine/unicode.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(ver);
24 /******************************************************************************
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
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 )
42 fileinfo.cBytes = sizeof(OFSTRUCT);
44 strcpy(filename, path);
45 filenamelen = strlen(filename);
47 /* Add a trailing \ if necessary */
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 /******************************************************************************
65 static int testFileExistenceW( const WCHAR *path, const WCHAR *file, BOOL excl )
68 DWORD pathlen, filelen;
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 */
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 );
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.
103 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
104 * Reimplementation of VerFindFile from original stub.
106 DWORD WINAPI VerFindFileA(
114 UINT *lpuDestDirLen )
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
132 GetSystemDirectoryA(systemDir, sizeof(systemDir));
136 if(flags & VFFF_ISSHAREDFILE)
139 /* Were we given a filename? If so, try to find the file. */
142 if(testFileExistenceA(destDir, lpszFilename, FALSE)) curDir = destDir;
143 else if(lpszAppDir && testFileExistenceA(lpszAppDir, lpszFilename, FALSE))
146 retval |= VFF_CURNEDEST;
150 else /* not a shared file */
154 destDir = lpszAppDir;
157 if(testFileExistenceA(destDir, lpszFilename, FALSE)) curDir = destDir;
158 else if(testFileExistenceA(systemDir, lpszFilename, FALSE))
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));
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;
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
223 GetSystemDirectoryW(systemDir, sizeof(systemDir)/sizeof(WCHAR));
227 if(flags & VFFF_ISSHAREDFILE)
230 /* Were we given a filename? If so, try to find the file. */
233 if(testFileExistenceW(destDir, lpszFilename, FALSE)) curDir = destDir;
234 else if(lpszAppDir && testFileExistenceW(lpszAppDir, lpszFilename, FALSE))
237 retval |= VFF_CURNEDEST;
241 else /* not a shared file */
245 destDir = lpszAppDir;
248 if(testFileExistenceW(destDir, lpszFilename, FALSE)) curDir = destDir;
249 else if(testFileExistenceW(systemDir, lpszFilename, FALSE))
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));
290 _fetch_versioninfo(LPSTR fn,VS_FIXEDFILEINFO **vffi) {
296 buf=HeapAlloc(GetProcessHeap(), 0, alloclen);
298 WARN("Memory exausted while fetching version info!");
302 ret = GetFileVersionInfoA(fn,0,alloclen,buf);
304 HeapFree(GetProcessHeap(), 0, buf);
307 if (alloclen<*(WORD*)buf) {
308 alloclen = *(WORD*)buf;
309 HeapFree(GetProcessHeap(), 0, buf);
310 buf = HeapAlloc(GetProcessHeap(), 0, alloclen);
312 WARN("Memory exausted while fetching version info!");
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);
327 _error2vif(DWORD error) {
329 case ERROR_ACCESS_DENIED:
330 return VIF_ACCESSVIOLATION;
331 case ERROR_SHARING_VIOLATION:
332 return VIF_SHARINGVIOLATION;
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 )
347 char destfn[260],tmpfn[260],srcfn[260];
349 DWORD attr,ret,xret,tmplast;
353 TRACE("(%x,%s,%s,%s,%s,%s,%p,%d)\n",
354 flags,srcfilename,destfilename,srcdir,destdir,curdir,tmpfile,*tmpfilelen
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==HFILE_ERROR)
363 return VIF_CANNOTREADSRC;
364 sprintf(tmpfn,"%s\\%s",pdest,destfilename);
365 tmplast=strlen(pdest)+1;
366 attr = GetFileAttributesA(tmpfn);
368 if (attr & FILE_ATTRIBUTE_READONLY) {
370 return VIF_WRITEPROT;
372 /* FIXME: check if file currently in use and return VIF_FILEINUSE */
375 if (flags & VIFF_FORCEINSTALL) {
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...
388 GetTempFileNameA(pdest,"ver",0,tmpfn); /* should not fail ... */
389 s=strrchr(tmpfn,'\\');
394 hfdst = OpenFile(tmpfn,&ofs,OF_CREATE);
395 if (hfdst == HFILE_ERROR) {
397 return VIF_CANNOTCREATE; /* | translated dos error */
399 ret = LZCopy(hfsrc,hfdst);
401 if (((long) ret) < 0) {
402 /* translate LZ errors into VIF_xxx */
404 case LZERROR_BADINHANDLE:
406 case LZERROR_BADVALUE:
407 case LZERROR_UNKNOWNALG:
408 ret = VIF_CANNOTREADSRC;
410 case LZERROR_BADOUTHANDLE:
412 ret = VIF_OUTOFSPACE;
414 case LZERROR_GLOBALLOC:
415 case LZERROR_GLOBLOCK:
416 ret = VIF_OUTOFMEMORY;
418 default: /* unknown error, should not happen */
429 if (!(flags & VIFF_FORCEINSTALL)) {
430 VS_FIXEDFILEINFO *destvffi,*tmpvffi;
431 buf1 = _fetch_versioninfo(destfn,&destvffi);
433 buf2 = _fetch_versioninfo(tmpfn,&tmpvffi);
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);
461 xret=VIF_MISMATCH|VIF_SRCOLD;
462 HeapFree(GetProcessHeap(), 0, buf1);
466 if (*tmpfilelen<strlen(tmpfn+tmplast)) {
467 xret|=VIF_BUFFTOOSMALL;
470 strcpy(tmpfile,tmpfn+tmplast);
471 *tmpfilelen = strlen(tmpfn+tmplast)+1;
475 if (-1!=GetFileAttributesA(destfn))
476 if (!DeleteFileA(destfn)) {
477 xret|=_error2vif(GetLastError())|VIF_CANNOTDELETE;
482 if ((!(flags & VIFF_DONTDELETEOLD)) &&
485 lstrcmpiA(curdir,pdest)
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;
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;
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 );
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 );
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 );
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 );
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);
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 );