2 * Implementation of VERSION.DLL - File Installer routines
4 * Copyright 1996,1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
14 #include "wine/winestring.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(ver);
24 /******************************************************************************
27 * char const * prologue,
28 * char const * teststring,
29 * char const * epilogue )
31 * This function will print via dprintf[_]ver to stddeb the prologue string,
32 * followed by the address of teststring and the string it contains if
33 * teststring is non-null or "(null)" otherwise, and then the epilogue
34 * string followed by a new line.
37 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
38 * Original implementation as dprintf[_]ver_string
39 * 05-Jul-1997 Dave Cuthbert (dacut@ece.cmu.edu)
40 * Fixed problem that caused bug with tools/make_debug -- renaming
41 * this function should fix the problem.
42 * 15-Feb-1998 Dimitrie Paun (dimi@cs.toronto.edu)
43 * Modified it to make it print the message using only one
46 *****************************************************************************/
48 static void ver_dstring(
49 char const * prologue,
50 char const * teststring,
51 char const * epilogue )
53 TRACE("%s %p (\"%s\") %s\n", prologue,
54 (void const *) teststring,
55 teststring ? teststring : "(null)",
59 /******************************************************************************
61 * int testFileExistence(
65 * Tests whether a given path/file combination exists. If the file does
66 * not exist, the return value is zero. If it does exist, the return
70 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
71 * Original implementation
73 *****************************************************************************/
75 static int testFileExistence(
84 fileinfo.cBytes = sizeof(OFSTRUCT);
86 strcpy(filename, path);
87 filenamelen = strlen(filename);
89 /* Add a trailing \ if necessary */
91 if(filename[filenamelen - 1] != '\\')
92 strcat(filename, "\\");
94 else /* specify the current directory */
95 strcpy(filename, ".\\");
97 /* Create the full pathname */
98 strcat(filename, file);
100 if(OpenFile(filename, &fileinfo, OF_EXIST) == HFILE_ERROR)
108 /******************************************************************************
110 * int testFileExclusiveExistence(
112 * char const * file )
114 * Tests whether a given path/file combination exists and ensures that no
115 * other programs have handles to the given file. If the file does not
116 * exist or is open, the return value is zero. If it does exist, the
117 * return value is non-zero.
120 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
121 * Original implementation
123 *****************************************************************************/
125 static int testFileExclusiveExistence(
134 fileinfo.cBytes = sizeof(OFSTRUCT);
136 strcpy(filename, path);
137 filenamelen = strlen(filename);
139 /* Add a trailing \ if necessary */
141 if(filename[filenamelen - 1] != '\\')
142 strcat(filename, "\\");
144 else /* specify the current directory */
145 strcpy(filename, ".\\");
147 /* Create the full pathname */
148 strcat(filename, file);
150 if(OpenFile(filename, &fileinfo, OF_EXIST | OF_SHARE_EXCLUSIVE) ==
159 /*****************************************************************************
161 * VerFindFile() [VER.8]
162 * Determines where to install a file based on whether it locates another
163 * version of the file in the system. The values VerFindFile returns are
164 * used in a subsequent call to the VerInstallFile function.
167 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
168 * Reimplementation of VerFindFile from original stub.
170 ****************************************************************************/
172 /* VerFindFile32A [VERSION.5] */
173 DWORD WINAPI VerFindFileA(
181 UINT *lpuDestDirLen )
186 unsigned int curDirSizeReq;
187 unsigned int destDirSizeReq;
191 /* Print out debugging information */
192 TRACE("called with parameters:\n"
193 "\tflags = %x", flags);
194 if(flags & VFFF_ISSHAREDFILE)
195 TRACE(" (VFFF_ISSHAREDFILE)\n");
199 ver_dstring("\tlpszFilename = ", lpszFilename, "");
200 ver_dstring("\tlpszWinDir = ", lpszWinDir, "");
201 ver_dstring("\tlpszAppDir = ", lpszAppDir, "");
203 TRACE("\tlpszCurDir = %p\n", lpszCurDir);
205 TRACE("\tlpuCurDirLen = %p (%u)\n",
206 lpuCurDirLen, *lpuCurDirLen);
208 TRACE("\tlpuCurDirLen = (null)\n");
210 TRACE("\tlpszDestDir = %p\n", lpszDestDir);
212 TRACE("\tlpuDestDirLen = %p (%u)\n",
213 lpuDestDirLen, *lpuDestDirLen);
215 /* Figure out where the file should go; shared files default to the
221 if(flags & VFFF_ISSHAREDFILE) {
222 GetSystemDirectoryA(destDir, 256);
224 /* Were we given a filename? If so, try to find the file. */
226 if(testFileExistence(destDir, lpszFilename)) {
227 strcpy(curDir, destDir);
229 if(!testFileExclusiveExistence(destDir, lpszFilename))
230 retval |= VFF_FILEINUSE;
232 else if(lpszAppDir && testFileExistence(lpszAppDir,
234 strcpy(curDir, lpszAppDir);
235 retval |= VFF_CURNEDEST;
237 if(!testFileExclusiveExistence(lpszAppDir, lpszFilename))
238 retval |= VFF_FILEINUSE;
242 else if(!(flags & VFFF_ISSHAREDFILE)) { /* not a shared file */
245 GetSystemDirectoryA(systemDir, 256);
247 strcpy(destDir, lpszAppDir);
250 if(testFileExistence(lpszAppDir, lpszFilename)) {
251 strcpy(curDir, lpszAppDir);
253 if(!testFileExclusiveExistence(lpszAppDir, lpszFilename))
254 retval |= VFF_FILEINUSE;
256 else if(testFileExistence(systemDir, lpszFilename)) {
257 strcpy(curDir, systemDir);
258 retval |= VFF_CURNEDEST;
260 if(!testFileExclusiveExistence(systemDir, lpszFilename))
261 retval |= VFF_FILEINUSE;
267 curDirSizeReq = strlen(curDir) + 1;
268 destDirSizeReq = strlen(destDir) + 1;
272 /* Make sure that the pointers to the size of the buffers are
273 valid; if not, do NOTHING with that buffer. If that pointer
274 is valid, then make sure that the buffer pointer is valid, too! */
276 if(lpuDestDirLen && lpszDestDir) {
277 if(*lpuDestDirLen < destDirSizeReq) {
278 retval |= VFF_BUFFTOOSMALL;
279 if (*lpuDestDirLen) {
280 lstrcpynA(lpszDestDir, destDir, *lpuDestDirLen);
284 strcpy(lpszDestDir, destDir);
286 *lpuDestDirLen = destDirSizeReq;
289 if(lpuCurDirLen && lpszCurDir) {
290 if(*lpuCurDirLen < curDirSizeReq) {
291 retval |= VFF_BUFFTOOSMALL;
293 lstrcpynA(lpszCurDir, curDir, *lpuCurDirLen);
297 strcpy(lpszCurDir, curDir);
299 *lpuCurDirLen = curDirSizeReq;
302 TRACE("ret = %lu (%s%s%s)\n", retval,
303 (retval & VFF_CURNEDEST) ? "VFF_CURNEDEST " : "",
304 (retval & VFF_FILEINUSE) ? "VFF_FILEINUSE " : "",
305 (retval & VFF_BUFFTOOSMALL) ? "VFF_BUFFTOOSMALL " : "");
307 ver_dstring("\t(Exit) lpszCurDir = ", lpszCurDir, "");
309 TRACE("\t(Exit) lpuCurDirLen = %p (%u)\n",
310 lpuCurDirLen, *lpuCurDirLen);
312 TRACE("\t(Exit) lpuCurDirLen = (null)\n");
314 ver_dstring("\t(Exit) lpszDestDir = ", lpszDestDir, "");
316 TRACE("\t(Exit) lpuDestDirLen = %p (%u)\n",
317 lpuDestDirLen, *lpuDestDirLen);
322 /* VerFindFile32W [VERSION.6] */
323 DWORD WINAPI VerFindFileW(
324 UINT flags,LPCWSTR filename,LPCWSTR windir,LPCWSTR appdir,
325 LPWSTR curdir,UINT *pcurdirlen,LPWSTR destdir,UINT *pdestdirlen )
327 UINT curdirlen, destdirlen;
328 LPSTR wfn,wwd,wad,wdd,wcd;
331 wfn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
332 wwd = HEAP_strdupWtoA( GetProcessHeap(), 0, windir );
333 wad = HEAP_strdupWtoA( GetProcessHeap(), 0, appdir );
334 wcd = HeapAlloc( GetProcessHeap(), 0, *pcurdirlen );
335 wdd = HeapAlloc( GetProcessHeap(), 0, *pdestdirlen );
336 ret = VerFindFileA(flags,wfn,wwd,wad,wcd,&curdirlen,wdd,&destdirlen);
337 lstrcpynAtoW(curdir,wcd,*pcurdirlen);
338 lstrcpynAtoW(destdir,wdd,*pdestdirlen);
339 *pcurdirlen = strlen(wcd);
340 *pdestdirlen = strlen(wdd);
341 HeapFree( GetProcessHeap(), 0, wfn );
342 HeapFree( GetProcessHeap(), 0, wwd );
343 HeapFree( GetProcessHeap(), 0, wad );
344 HeapFree( GetProcessHeap(), 0, wcd );
345 HeapFree( GetProcessHeap(), 0, wdd );
350 _fetch_versioninfo(LPSTR fn,VS_FIXEDFILEINFO **vffi) {
356 buf= xmalloc(alloclen);
358 ret = GetFileVersionInfoA(fn,0,alloclen,buf);
363 if (alloclen<*(WORD*)buf) {
365 alloclen = *(WORD*)buf;
366 buf = xmalloc(alloclen);
368 *vffi = (VS_FIXEDFILEINFO*)(buf+0x14);
369 if ((*vffi)->dwSignature == 0x004f0049) /* hack to detect unicode */
370 *vffi = (VS_FIXEDFILEINFO*)(buf+0x28);
371 if ((*vffi)->dwSignature != VS_FFI_SIGNATURE)
372 WARN("Bad VS_FIXEDFILEINFO signature 0x%08lx\n",(*vffi)->dwSignature);
379 _error2vif(DWORD error) {
381 case ERROR_ACCESS_DENIED:
382 return VIF_ACCESSVIOLATION;
383 case ERROR_SHARING_VIOLATION:
384 return VIF_SHARINGVIOLATION;
391 /******************************************************************************
392 * VerInstallFile32A [VERSION.7]
394 DWORD WINAPI VerInstallFileA(
395 UINT flags,LPCSTR srcfilename,LPCSTR destfilename,LPCSTR srcdir,
396 LPCSTR destdir,LPCSTR curdir,LPSTR tmpfile,UINT *tmpfilelen )
399 char destfn[260],tmpfn[260],srcfn[260];
401 DWORD attr,ret,xret,tmplast;
405 TRACE("(%x,%s,%s,%s,%s,%s,%p,%d)\n",
406 flags,srcfilename,destfilename,srcdir,destdir,curdir,tmpfile,*tmpfilelen
409 sprintf(srcfn,"%s\\%s",srcdir,srcfilename);
410 if (!destdir || !*destdir) pdest = srcdir;
411 else pdest = destdir;
412 sprintf(destfn,"%s\\%s",pdest,destfilename);
413 hfsrc=LZOpenFileA(srcfn,&ofs,OF_READ);
414 if (hfsrc==HFILE_ERROR)
415 return VIF_CANNOTREADSRC;
416 sprintf(tmpfn,"%s\\%s",pdest,destfilename);
417 tmplast=strlen(pdest)+1;
418 attr = GetFileAttributesA(tmpfn);
420 if (attr & FILE_ATTRIBUTE_READONLY) {
422 return VIF_WRITEPROT;
424 /* FIXME: check if file currently in use and return VIF_FILEINUSE */
427 if (flags & VIFF_FORCEINSTALL) {
429 sprintf(tmpfn,"%s\\%s",pdest,tmpfile);
430 tmplast = strlen(pdest)+1;
431 attr = GetFileAttributesA(tmpfn);
432 /* if it exists, it has been copied by the call before.
433 * we jump over the copy part...
440 GetTempFileNameA(pdest,"ver",0,tmpfn); /* should not fail ... */
441 s=strrchr(tmpfn,'\\');
446 hfdst = OpenFile(tmpfn,&ofs,OF_CREATE);
447 if (hfdst == HFILE_ERROR) {
449 return VIF_CANNOTCREATE; /* | translated dos error */
451 ret = LZCopy(hfsrc,hfdst);
453 if (((long) ret) < 0) {
454 /* translate LZ errors into VIF_xxx */
456 case LZERROR_BADINHANDLE:
458 case LZERROR_BADVALUE:
459 case LZERROR_UNKNOWNALG:
460 ret = VIF_CANNOTREADSRC;
462 case LZERROR_BADOUTHANDLE:
464 ret = VIF_OUTOFMEMORY; /* FIXME: correct? */
466 case LZERROR_GLOBALLOC:
467 case LZERROR_GLOBLOCK:
468 ret = VIF_OUTOFSPACE;
470 default: /* unknown error, should not happen */
481 if (!(flags & VIFF_FORCEINSTALL)) {
482 VS_FIXEDFILEINFO *destvffi,*tmpvffi;
483 buf1 = _fetch_versioninfo(destfn,&destvffi);
485 buf2 = _fetch_versioninfo(tmpfn,&tmpvffi);
492 /* compare file versions */
493 if ((destvffi->dwFileVersionMS > tmpvffi->dwFileVersionMS)||
494 ((destvffi->dwFileVersionMS==tmpvffi->dwFileVersionMS)&&
495 (destvffi->dwFileVersionLS > tmpvffi->dwFileVersionLS)
498 xret |= VIF_MISMATCH|VIF_SRCOLD;
499 /* compare filetypes and filesubtypes */
500 if ((destvffi->dwFileType!=tmpvffi->dwFileType) ||
501 (destvffi->dwFileSubtype!=tmpvffi->dwFileSubtype)
503 xret |= VIF_MISMATCH|VIF_DIFFTYPE;
504 if (VerQueryValueA(buf1,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf1,&len1) &&
505 VerQueryValueA(buf2,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf2,&len2)
507 /* irgendwas mit tbuf1 und tbuf2 machen
508 * generiert DIFFLANG|MISMATCH
513 xret=VIF_MISMATCH|VIF_SRCOLD;
518 if (*tmpfilelen<strlen(tmpfn+tmplast)) {
519 xret|=VIF_BUFFTOOSMALL;
522 strcpy(tmpfile,tmpfn+tmplast);
523 *tmpfilelen = strlen(tmpfn+tmplast)+1;
527 if (-1!=GetFileAttributesA(destfn))
528 if (!DeleteFileA(destfn)) {
529 xret|=_error2vif(GetLastError())|VIF_CANNOTDELETE;
534 if ((!(flags & VIFF_DONTDELETEOLD)) &&
537 lstrcmpiA(curdir,pdest)
541 sprintf(curfn,"%s\\%s",curdir,destfilename);
542 if (-1!=GetFileAttributesA(curfn)) {
543 /* FIXME: check if in use ... if it is, VIF_CANNOTDELETECUR */
544 if (!DeleteFileA(curfn))
545 xret|=_error2vif(GetLastError())|VIF_CANNOTDELETECUR;
548 if (!MoveFileA(tmpfn,destfn)) {
549 xret|=_error2vif(GetLastError())|VIF_CANNOTRENAME;
558 /* VerInstallFile32W [VERSION.8] */
559 DWORD WINAPI VerInstallFileW(
560 UINT flags,LPCWSTR srcfilename,LPCWSTR destfilename,LPCWSTR srcdir,
561 LPCWSTR destdir,LPCWSTR curdir,LPWSTR tmpfile,UINT *tmpfilelen )
563 LPSTR wsrcf,wsrcd,wdestf,wdestd,wtmpf,wcurd;
566 wsrcf = HEAP_strdupWtoA( GetProcessHeap(), 0, srcfilename );
567 wsrcd = HEAP_strdupWtoA( GetProcessHeap(), 0, srcdir );
568 wdestf = HEAP_strdupWtoA( GetProcessHeap(), 0, destfilename );
569 wdestd = HEAP_strdupWtoA( GetProcessHeap(), 0, destdir );
570 wtmpf = HEAP_strdupWtoA( GetProcessHeap(), 0, tmpfile );
571 wcurd = HEAP_strdupWtoA( GetProcessHeap(), 0, curdir );
572 ret = VerInstallFileA(flags,wsrcf,wdestf,wsrcd,wdestd,wcurd,wtmpf,tmpfilelen);
574 lstrcpynAtoW(tmpfile,wtmpf,*tmpfilelen);
575 HeapFree( GetProcessHeap(), 0, wsrcf );
576 HeapFree( GetProcessHeap(), 0, wsrcd );
577 HeapFree( GetProcessHeap(), 0, wdestf );
578 HeapFree( GetProcessHeap(), 0, wdestd );
579 HeapFree( GetProcessHeap(), 0, wtmpf );
581 HeapFree( GetProcessHeap(), 0, wcurd );