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(OpenFile32(filename, &fileinfo, OF_EXIST) == HFILE_ERROR32)
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(OpenFile32(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 VerFindFile32A(
177 UINT32 *lpuCurDirLen,
179 UINT32 *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 GetSystemDirectory32A(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 GetSystemDirectory32A(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 strncpy(lpszDestDir, destDir, *lpuDestDirLen - 1);
278 lpszDestDir[*lpuDestDirLen - 1] = '\0';
281 strcpy(lpszDestDir, destDir);
283 *lpuDestDirLen = destDirSizeReq;
286 if(lpuCurDirLen && lpszCurDir) {
287 if(*lpuCurDirLen < curDirSizeReq) {
288 retval |= VFF_BUFFTOOSMALL;
289 strncpy(lpszCurDir, curDir, *lpuCurDirLen - 1);
290 lpszCurDir[*lpuCurDirLen - 1] = '\0';
293 strcpy(lpszCurDir, curDir);
295 *lpuCurDirLen = curDirSizeReq;
298 TRACE(ver, "ret = %lu (%s%s%s)\n", retval,
299 (retval & VFF_CURNEDEST) ? "VFF_CURNEDEST " : "",
300 (retval & VFF_FILEINUSE) ? "VFF_FILEINUSE " : "",
301 (retval & VFF_BUFFTOOSMALL) ? "VFF_BUFFTOOSMALL " : "");
303 ver_dstring("\t(Exit) lpszCurDir = ", lpszCurDir, "");
305 TRACE(ver, "\t(Exit) lpuCurDirLen = %p (%u)\n",
306 lpuCurDirLen, *lpuCurDirLen);
308 TRACE(ver, "\t(Exit) lpuCurDirLen = (null)\n");
310 ver_dstring("\t(Exit) lpszDestDir = ", lpszDestDir, "");
312 TRACE(ver, "\t(Exit) lpuDestDirLen = %p (%u)\n",
313 lpuDestDirLen, *lpuDestDirLen);
318 /* VerFindFile32W [VERSION.6] */
319 DWORD WINAPI VerFindFile32W(
320 UINT32 flags,LPCWSTR filename,LPCWSTR windir,LPCWSTR appdir,
321 LPWSTR curdir,UINT32 *pcurdirlen,LPWSTR destdir,UINT32 *pdestdirlen )
323 UINT32 curdirlen, destdirlen;
324 LPSTR wfn,wwd,wad,wdd,wcd;
327 wfn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
328 wwd = HEAP_strdupWtoA( GetProcessHeap(), 0, windir );
329 wad = HEAP_strdupWtoA( GetProcessHeap(), 0, appdir );
330 wcd = HeapAlloc( GetProcessHeap(), 0, *pcurdirlen );
331 wdd = HeapAlloc( GetProcessHeap(), 0, *pdestdirlen );
332 ret = VerFindFile32A(flags,wfn,wwd,wad,wcd,&curdirlen,wdd,&destdirlen);
333 lstrcpynAtoW(curdir,wcd,*pcurdirlen);
334 lstrcpynAtoW(destdir,wdd,*pdestdirlen);
335 *pcurdirlen = strlen(wcd);
336 *pdestdirlen = strlen(wdd);
337 HeapFree( GetProcessHeap(), 0, wfn );
338 HeapFree( GetProcessHeap(), 0, wwd );
339 HeapFree( GetProcessHeap(), 0, wad );
340 HeapFree( GetProcessHeap(), 0, wcd );
341 HeapFree( GetProcessHeap(), 0, wdd );
346 _fetch_versioninfo(LPSTR fn,VS_FIXEDFILEINFO **vffi) {
352 buf= xmalloc(alloclen);
354 ret = GetFileVersionInfo32A(fn,0,alloclen,buf);
359 if (alloclen<*(WORD*)buf) {
361 alloclen = *(WORD*)buf;
362 buf = xmalloc(alloclen);
364 *vffi = (VS_FIXEDFILEINFO*)(buf+0x14);
365 if ((*vffi)->dwSignature == 0x004f0049) /* hack to detect unicode */
366 *vffi = (VS_FIXEDFILEINFO*)(buf+0x28);
367 if ((*vffi)->dwSignature != VS_FFI_SIGNATURE)
368 WARN(ver,"Bad VS_FIXEDFILEINFO signature 0x%08lx\n",(*vffi)->dwSignature);
375 _error2vif(DWORD error) {
377 case ERROR_ACCESS_DENIED:
378 return VIF_ACCESSVIOLATION;
379 case ERROR_SHARING_VIOLATION:
380 return VIF_SHARINGVIOLATION;
387 /******************************************************************************
388 * VerInstallFile32A [VERSION.7]
390 DWORD WINAPI VerInstallFile32A(
391 UINT32 flags,LPCSTR srcfilename,LPCSTR destfilename,LPCSTR srcdir,
392 LPCSTR destdir,LPCSTR curdir,LPSTR tmpfile,UINT32 *tmpfilelen )
395 char destfn[260],tmpfn[260],srcfn[260];
397 DWORD attr,ret,xret,tmplast;
401 TRACE(ver,"(%x,%s,%s,%s,%s,%s,%p,%d)\n",
402 flags,srcfilename,destfilename,srcdir,destdir,curdir,tmpfile,*tmpfilelen
405 sprintf(srcfn,"%s\\%s",srcdir,srcfilename);
406 if (!destdir || !*destdir) pdest = srcdir;
407 else pdest = destdir;
408 sprintf(destfn,"%s\\%s",pdest,destfilename);
409 hfsrc=LZOpenFile32A(srcfn,&ofs,OF_READ);
410 if (hfsrc==HFILE_ERROR32)
411 return VIF_CANNOTREADSRC;
412 sprintf(tmpfn,"%s\\%s",pdest,destfilename);
413 tmplast=strlen(pdest)+1;
414 attr = GetFileAttributes32A(tmpfn);
416 if (attr & FILE_ATTRIBUTE_READONLY) {
418 return VIF_WRITEPROT;
420 /* FIXME: check if file currently in use and return VIF_FILEINUSE */
423 if (flags & VIFF_FORCEINSTALL) {
425 sprintf(tmpfn,"%s\\%s",pdest,tmpfile);
426 tmplast = strlen(pdest)+1;
427 attr = GetFileAttributes32A(tmpfn);
428 /* if it exists, it has been copied by the call before.
429 * we jump over the copy part...
436 GetTempFileName32A(pdest,"ver",0,tmpfn); /* should not fail ... */
437 s=strrchr(tmpfn,'\\');
442 hfdst = OpenFile32(tmpfn,&ofs,OF_CREATE);
443 if (hfdst == HFILE_ERROR32) {
445 return VIF_CANNOTCREATE; /* | translated dos error */
447 ret = LZCopy32(hfsrc,hfdst);
449 if (((long) ret) < 0) {
450 /* translate LZ errors into VIF_xxx */
452 case LZERROR_BADINHANDLE:
454 case LZERROR_BADVALUE:
455 case LZERROR_UNKNOWNALG:
456 ret = VIF_CANNOTREADSRC;
458 case LZERROR_BADOUTHANDLE:
460 ret = VIF_OUTOFMEMORY; /* FIXME: correct? */
462 case LZERROR_GLOBALLOC:
463 case LZERROR_GLOBLOCK:
464 ret = VIF_OUTOFSPACE;
466 default: /* unknown error, should not happen */
477 if (!(flags & VIFF_FORCEINSTALL)) {
478 VS_FIXEDFILEINFO *destvffi,*tmpvffi;
479 buf1 = _fetch_versioninfo(destfn,&destvffi);
481 buf2 = _fetch_versioninfo(tmpfn,&tmpvffi);
488 /* compare file versions */
489 if ((destvffi->dwFileVersionMS > tmpvffi->dwFileVersionMS)||
490 ((destvffi->dwFileVersionMS==tmpvffi->dwFileVersionMS)&&
491 (destvffi->dwFileVersionLS > tmpvffi->dwFileVersionLS)
494 xret |= VIF_MISMATCH|VIF_SRCOLD;
495 /* compare filetypes and filesubtypes */
496 if ((destvffi->dwFileType!=tmpvffi->dwFileType) ||
497 (destvffi->dwFileSubtype!=tmpvffi->dwFileSubtype)
499 xret |= VIF_MISMATCH|VIF_DIFFTYPE;
500 if (VerQueryValue32A(buf1,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf1,&len1) &&
501 VerQueryValue32A(buf2,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf2,&len2)
503 /* irgendwas mit tbuf1 und tbuf2 machen
504 * generiert DIFFLANG|MISMATCH
509 xret=VIF_MISMATCH|VIF_SRCOLD;
514 if (*tmpfilelen<strlen(tmpfn+tmplast)) {
515 xret|=VIF_BUFFTOOSMALL;
516 DeleteFile32A(tmpfn);
518 strcpy(tmpfile,tmpfn+tmplast);
519 *tmpfilelen = strlen(tmpfn+tmplast)+1;
523 if (-1!=GetFileAttributes32A(destfn))
524 if (!DeleteFile32A(destfn)) {
525 xret|=_error2vif(GetLastError())|VIF_CANNOTDELETE;
526 DeleteFile32A(tmpfn);
530 if ((!(flags & VIFF_DONTDELETEOLD)) &&
533 lstrcmpi32A(curdir,pdest)
537 sprintf(curfn,"%s\\%s",curdir,destfilename);
538 if (-1!=GetFileAttributes32A(curfn)) {
539 /* FIXME: check if in use ... if it is, VIF_CANNOTDELETECUR */
540 if (!DeleteFile32A(curfn))
541 xret|=_error2vif(GetLastError())|VIF_CANNOTDELETECUR;
544 if (!MoveFile32A(tmpfn,destfn)) {
545 xret|=_error2vif(GetLastError())|VIF_CANNOTRENAME;
546 DeleteFile32A(tmpfn);
554 /* VerInstallFile32W [VERSION.8] */
555 DWORD WINAPI VerInstallFile32W(
556 UINT32 flags,LPCWSTR srcfilename,LPCWSTR destfilename,LPCWSTR srcdir,
557 LPCWSTR destdir,LPCWSTR curdir,LPWSTR tmpfile,UINT32 *tmpfilelen )
559 LPSTR wsrcf,wsrcd,wdestf,wdestd,wtmpf,wcurd;
562 wsrcf = HEAP_strdupWtoA( GetProcessHeap(), 0, srcfilename );
563 wsrcd = HEAP_strdupWtoA( GetProcessHeap(), 0, srcdir );
564 wdestf = HEAP_strdupWtoA( GetProcessHeap(), 0, destfilename );
565 wdestd = HEAP_strdupWtoA( GetProcessHeap(), 0, destdir );
566 wtmpf = HEAP_strdupWtoA( GetProcessHeap(), 0, tmpfile );
567 wcurd = HEAP_strdupWtoA( GetProcessHeap(), 0, curdir );
568 ret = VerInstallFile32A(flags,wsrcf,wdestf,wsrcd,wdestd,wcurd,wtmpf,tmpfilelen);
570 lstrcpynAtoW(tmpfile,wtmpf,*tmpfilelen);
571 HeapFree( GetProcessHeap(), 0, wsrcf );
572 HeapFree( GetProcessHeap(), 0, wsrcd );
573 HeapFree( GetProcessHeap(), 0, wdestf );
574 HeapFree( GetProcessHeap(), 0, wdestd );
575 HeapFree( GetProcessHeap(), 0, wtmpf );
577 HeapFree( GetProcessHeap(), 0, wcurd );