2 * Implementation of VERSION.DLL - File Installer routines
4 * Copyright 1996,1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
13 #include "wine/winestring.h"
22 /******************************************************************************
25 * char const * prologue,
26 * char const * teststring,
27 * char const * epilogue )
29 * This function will print via dprintf[_]ver to stddeb the prologue string,
30 * followed by the address of teststring and the string it contains if
31 * teststring is non-null or "(null)" otherwise, and then the epilogue
32 * string followed by a new line.
35 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
36 * Original implementation as dprintf[_]ver_string
37 * 05-Jul-1997 Dave Cuthbert (dacut@ece.cmu.edu)
38 * Fixed problem that caused bug with tools/make_debug -- renaming
39 * this function should fix the problem.
40 * 15-Feb-1998 Dimitrie Paun (dimi@cs.toronto.edu)
41 * Modified it to make it print the message using only one
44 *****************************************************************************/
46 static void ver_dstring(
47 char const * prologue,
48 char const * teststring,
49 char const * epilogue )
51 TRACE(ver, "%s %p (\"%s\") %s\n", prologue,
52 (void const *) teststring,
53 teststring ? teststring : "(null)",
57 /******************************************************************************
59 * int testFileExistence(
63 * Tests whether a given path/file combination exists. If the file does
64 * not exist, the return value is zero. If it does exist, the return
68 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
69 * Original implementation
71 *****************************************************************************/
73 static int testFileExistence(
82 fileinfo.cBytes = sizeof(OFSTRUCT);
84 strcpy(filename, path);
85 filenamelen = strlen(filename);
87 /* Add a trailing \ if necessary */
89 if(filename[filenamelen - 1] != '\\')
90 strcat(filename, "\\");
92 else /* specify the current directory */
93 strcpy(filename, ".\\");
95 /* Create the full pathname */
96 strcat(filename, file);
98 if(OpenFile(filename, &fileinfo, OF_EXIST) == HFILE_ERROR)
106 /******************************************************************************
108 * int testFileExclusiveExistence(
110 * char const * file )
112 * Tests whether a given path/file combination exists and ensures that no
113 * other programs have handles to the given file. If the file does not
114 * exist or is open, the return value is zero. If it does exist, the
115 * return value is non-zero.
118 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
119 * Original implementation
121 *****************************************************************************/
123 static int testFileExclusiveExistence(
132 fileinfo.cBytes = sizeof(OFSTRUCT);
134 strcpy(filename, path);
135 filenamelen = strlen(filename);
137 /* Add a trailing \ if necessary */
139 if(filename[filenamelen - 1] != '\\')
140 strcat(filename, "\\");
142 else /* specify the current directory */
143 strcpy(filename, ".\\");
145 /* Create the full pathname */
146 strcat(filename, file);
148 if(OpenFile(filename, &fileinfo, OF_EXIST | OF_SHARE_EXCLUSIVE) ==
157 /*****************************************************************************
159 * VerFindFile() [VER.8]
160 * Determines where to install a file based on whether it locates another
161 * version of the file in the system. The values VerFindFile returns are
162 * used in a subsequent call to the VerInstallFile function.
165 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
166 * Reimplementation of VerFindFile from original stub.
168 ****************************************************************************/
170 /* VerFindFile32A [VERSION.5] */
171 DWORD WINAPI VerFindFileA(
179 UINT *lpuDestDirLen )
184 unsigned int curDirSizeReq;
185 unsigned int destDirSizeReq;
189 /* Print out debugging information */
190 TRACE(ver, "called with parameters:\n"
191 "\tflags = %x", flags);
192 if(flags & VFFF_ISSHAREDFILE)
193 TRACE(ver, " (VFFF_ISSHAREDFILE)\n");
197 ver_dstring("\tlpszFilename = ", lpszFilename, "");
198 ver_dstring("\tlpszWinDir = ", lpszWinDir, "");
199 ver_dstring("\tlpszAppDir = ", lpszAppDir, "");
201 TRACE(ver, "\tlpszCurDir = %p\n", lpszCurDir);
203 TRACE(ver, "\tlpuCurDirLen = %p (%u)\n",
204 lpuCurDirLen, *lpuCurDirLen);
206 TRACE(ver, "\tlpuCurDirLen = (null)\n");
208 TRACE(ver, "\tlpszDestDir = %p\n", lpszDestDir);
210 TRACE(ver, "\tlpuDestDirLen = %p (%u)\n",
211 lpuDestDirLen, *lpuDestDirLen);
213 /* Figure out where the file should go; shared files default to the
219 if(flags & VFFF_ISSHAREDFILE) {
220 GetSystemDirectoryA(destDir, 256);
222 /* Were we given a filename? If so, try to find the file. */
224 if(testFileExistence(destDir, lpszFilename)) {
225 strcpy(curDir, destDir);
227 if(!testFileExclusiveExistence(destDir, lpszFilename))
228 retval |= VFF_FILEINUSE;
230 else if(lpszAppDir && testFileExistence(lpszAppDir,
232 strcpy(curDir, lpszAppDir);
233 retval |= VFF_CURNEDEST;
235 if(!testFileExclusiveExistence(lpszAppDir, lpszFilename))
236 retval |= VFF_FILEINUSE;
240 else if(!(flags & VFFF_ISSHAREDFILE)) { /* not a shared file */
243 GetSystemDirectoryA(systemDir, 256);
245 strcpy(destDir, lpszAppDir);
248 if(testFileExistence(lpszAppDir, lpszFilename)) {
249 strcpy(curDir, lpszAppDir);
251 if(!testFileExclusiveExistence(lpszAppDir, lpszFilename))
252 retval |= VFF_FILEINUSE;
254 else if(testFileExistence(systemDir, lpszFilename)) {
255 strcpy(curDir, systemDir);
256 retval |= VFF_CURNEDEST;
258 if(!testFileExclusiveExistence(systemDir, lpszFilename))
259 retval |= VFF_FILEINUSE;
265 curDirSizeReq = strlen(curDir) + 1;
266 destDirSizeReq = strlen(destDir) + 1;
270 /* Make sure that the pointers to the size of the buffers are
271 valid; if not, do NOTHING with that buffer. If that pointer
272 is valid, then make sure that the buffer pointer is valid, too! */
274 if(lpuDestDirLen && lpszDestDir) {
275 if(*lpuDestDirLen < destDirSizeReq) {
276 retval |= VFF_BUFFTOOSMALL;
277 if (*lpuDestDirLen) {
278 strncpy(lpszDestDir, destDir, *lpuDestDirLen - 1);
279 lpszDestDir[*lpuDestDirLen - 1] = '\0';
283 strcpy(lpszDestDir, destDir);
285 *lpuDestDirLen = destDirSizeReq;
288 if(lpuCurDirLen && lpszCurDir) {
289 if(*lpuCurDirLen < curDirSizeReq) {
290 retval |= VFF_BUFFTOOSMALL;
292 strncpy(lpszCurDir, curDir, *lpuCurDirLen - 1);
293 lpszCurDir[*lpuCurDirLen - 1] = '\0';
297 strcpy(lpszCurDir, curDir);
299 *lpuCurDirLen = curDirSizeReq;
302 TRACE(ver, "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(ver, "\t(Exit) lpuCurDirLen = %p (%u)\n",
310 lpuCurDirLen, *lpuCurDirLen);
312 TRACE(ver, "\t(Exit) lpuCurDirLen = (null)\n");
314 ver_dstring("\t(Exit) lpszDestDir = ", lpszDestDir, "");
316 TRACE(ver, "\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(ver,"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(ver,"(%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 );