2 * Implementation of VERSION.DLL - File Installer routines
4 * Copyright 1996,1997 Marcus Meissner
5 * Copyright 1997 David Cuthbert
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * o Check the installation functions.
34 #include "wine/unicode.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ver
);
42 /******************************************************************************
45 * Tests whether a given path/file combination exists. If the file does
46 * not exist, the return value is zero. If it does exist, the return
50 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
51 * Original implementation
54 static int testFileExistenceA( char const * path
, char const * file
, BOOL excl
)
60 fileinfo
.cBytes
= sizeof(OFSTRUCT
);
62 strcpy(filename
, path
);
63 filenamelen
= strlen(filename
);
65 /* Add a trailing \ if necessary */
67 if(filename
[filenamelen
- 1] != '\\')
68 strcat(filename
, "\\");
70 else /* specify the current directory */
71 strcpy(filename
, ".\\");
73 /* Create the full pathname */
74 strcat(filename
, file
);
76 return (OpenFile(filename
, &fileinfo
,
77 OF_EXIST
| (excl
? OF_SHARE_EXCLUSIVE
: 0)) != HFILE_ERROR
);
80 /******************************************************************************
83 static int testFileExistenceW( const WCHAR
*path
, const WCHAR
*file
, BOOL excl
)
86 DWORD pathlen
, filelen
;
90 fileinfo
.cBytes
= sizeof(OFSTRUCT
);
92 pathlen
= WideCharToMultiByte( CP_ACP
, 0, path
, -1, NULL
, 0, NULL
, NULL
);
93 filelen
= WideCharToMultiByte( CP_ACP
, 0, file
, -1, NULL
, 0, NULL
, NULL
);
94 filename
= HeapAlloc( GetProcessHeap(), 0, pathlen
+filelen
+2 );
96 WideCharToMultiByte( CP_ACP
, 0, path
, -1, filename
, pathlen
, NULL
, NULL
);
97 /* Add a trailing \ if necessary */
100 if (filename
[pathlen
-2] != '\\') strcpy( &filename
[pathlen
-1], "\\" );
102 else /* specify the current directory */
103 strcpy(filename
, ".\\");
105 WideCharToMultiByte( CP_ACP
, 0, file
, -1, filename
+strlen(filename
), filelen
, NULL
, NULL
);
107 ret
= (OpenFile(filename
, &fileinfo
,
108 OF_EXIST
| (excl
? OF_SHARE_EXCLUSIVE
: 0)) != HFILE_ERROR
);
109 HeapFree( GetProcessHeap(), 0, filename
);
113 /*****************************************************************************
114 * VerFindFileA [VERSION.@]
116 * Determines where to install a file based on whether it locates another
117 * version of the file in the system. The values VerFindFile returns are
118 * used in a subsequent call to the VerInstallFile function.
121 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
122 * Reimplementation of VerFindFile from original stub.
124 DWORD WINAPI
VerFindFileA(
132 UINT
*lpuDestDirLen
)
137 unsigned int curDirSizeReq
;
138 unsigned int destDirSizeReq
;
139 char systemDir
[MAX_PATH
];
141 /* Print out debugging information */
142 TRACE("flags = %x filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)\n",
143 flags
, debugstr_a(lpszFilename
), debugstr_a(lpszWinDir
), debugstr_a(lpszAppDir
),
144 lpuCurDirLen
, lpuCurDirLen
? *lpuCurDirLen
: 0,
145 lpuDestDirLen
, lpuDestDirLen
? *lpuDestDirLen
: 0 );
147 /* Figure out where the file should go; shared files default to the
150 GetSystemDirectoryA(systemDir
, sizeof(systemDir
));
154 if(flags
& VFFF_ISSHAREDFILE
)
157 /* Were we given a filename? If so, try to find the file. */
160 if(testFileExistenceA(destDir
, lpszFilename
, FALSE
)) curDir
= destDir
;
161 else if(lpszAppDir
&& testFileExistenceA(lpszAppDir
, lpszFilename
, FALSE
))
164 retval
|= VFF_CURNEDEST
;
168 else /* not a shared file */
172 destDir
= lpszAppDir
;
175 if(testFileExistenceA(destDir
, lpszFilename
, FALSE
)) curDir
= destDir
;
176 else if(testFileExistenceA(systemDir
, lpszFilename
, FALSE
))
179 retval
|= VFF_CURNEDEST
;
185 /* Check to see if the file exists and is inuse by another application */
186 if (lpszFilename
&& testFileExistenceA(curDir
, lpszFilename
, FALSE
)) {
187 if (lpszFilename
&& !testFileExistenceA(curDir
, lpszFilename
, TRUE
))
188 retval
|= VFF_FILEINUSE
;
191 curDirSizeReq
= strlen(curDir
) + 1;
192 destDirSizeReq
= strlen(destDir
) + 1;
194 /* Make sure that the pointers to the size of the buffers are
195 valid; if not, do NOTHING with that buffer. If that pointer
196 is valid, then make sure that the buffer pointer is valid, too! */
198 if(lpuDestDirLen
&& lpszDestDir
)
200 if (*lpuDestDirLen
< destDirSizeReq
) retval
|= VFF_BUFFTOOSMALL
;
201 lstrcpynA(lpszDestDir
, destDir
, *lpuDestDirLen
);
202 *lpuDestDirLen
= destDirSizeReq
;
204 if(lpuCurDirLen
&& lpszCurDir
)
206 if(*lpuCurDirLen
< curDirSizeReq
) retval
|= VFF_BUFFTOOSMALL
;
207 lstrcpynA(lpszCurDir
, curDir
, *lpuCurDirLen
);
208 *lpuCurDirLen
= curDirSizeReq
;
211 TRACE("ret = %lu (%s%s%s) curdir=%s destdir=%s\n", retval
,
212 (retval
& VFF_CURNEDEST
) ? "VFF_CURNEDEST " : "",
213 (retval
& VFF_FILEINUSE
) ? "VFF_FILEINUSE " : "",
214 (retval
& VFF_BUFFTOOSMALL
) ? "VFF_BUFFTOOSMALL " : "",
215 debugstr_a(lpszCurDir
), debugstr_a(lpszDestDir
));
220 /*****************************************************************************
221 * VerFindFileW [VERSION.@]
223 DWORD WINAPI
VerFindFileW( UINT flags
,LPWSTR lpszFilename
,LPWSTR lpszWinDir
,
224 LPWSTR lpszAppDir
, LPWSTR lpszCurDir
,UINT
*lpuCurDirLen
,
225 LPWSTR lpszDestDir
,UINT
*lpuDestDirLen
)
227 static const WCHAR emptyW
;
230 const WCHAR
*destDir
;
231 unsigned int curDirSizeReq
;
232 unsigned int destDirSizeReq
;
233 WCHAR systemDir
[MAX_PATH
];
235 /* Print out debugging information */
236 TRACE("flags = %x filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)\n",
237 flags
, debugstr_w(lpszFilename
), debugstr_w(lpszWinDir
), debugstr_w(lpszAppDir
),
238 lpuCurDirLen
, lpuCurDirLen
? *lpuCurDirLen
: 0,
239 lpuDestDirLen
, lpuDestDirLen
? *lpuDestDirLen
: 0 );
241 /* Figure out where the file should go; shared files default to the
244 GetSystemDirectoryW(systemDir
, sizeof(systemDir
)/sizeof(WCHAR
));
248 if(flags
& VFFF_ISSHAREDFILE
)
251 /* Were we given a filename? If so, try to find the file. */
254 if(testFileExistenceW(destDir
, lpszFilename
, FALSE
)) curDir
= destDir
;
255 else if(lpszAppDir
&& testFileExistenceW(lpszAppDir
, lpszFilename
, FALSE
))
258 retval
|= VFF_CURNEDEST
;
262 else /* not a shared file */
266 destDir
= lpszAppDir
;
269 if(testFileExistenceW(destDir
, lpszFilename
, FALSE
)) curDir
= destDir
;
270 else if(testFileExistenceW(systemDir
, lpszFilename
, FALSE
))
273 retval
|= VFF_CURNEDEST
;
279 if (lpszFilename
&& !testFileExistenceW(curDir
, lpszFilename
, TRUE
))
280 retval
|= VFF_FILEINUSE
;
282 curDirSizeReq
= strlenW(curDir
) + 1;
283 destDirSizeReq
= strlenW(destDir
) + 1;
285 /* Make sure that the pointers to the size of the buffers are
286 valid; if not, do NOTHING with that buffer. If that pointer
287 is valid, then make sure that the buffer pointer is valid, too! */
289 if(lpuDestDirLen
&& lpszDestDir
)
291 if (*lpuDestDirLen
< destDirSizeReq
) retval
|= VFF_BUFFTOOSMALL
;
292 lstrcpynW(lpszDestDir
, destDir
, *lpuDestDirLen
);
293 *lpuDestDirLen
= destDirSizeReq
;
295 if(lpuCurDirLen
&& lpszCurDir
)
297 if(*lpuCurDirLen
< curDirSizeReq
) retval
|= VFF_BUFFTOOSMALL
;
298 lstrcpynW(lpszCurDir
, curDir
, *lpuCurDirLen
);
299 *lpuCurDirLen
= curDirSizeReq
;
302 TRACE("ret = %lu (%s%s%s) curdir=%s destdir=%s\n", retval
,
303 (retval
& VFF_CURNEDEST
) ? "VFF_CURNEDEST " : "",
304 (retval
& VFF_FILEINUSE
) ? "VFF_FILEINUSE " : "",
305 (retval
& VFF_BUFFTOOSMALL
) ? "VFF_BUFFTOOSMALL " : "",
306 debugstr_w(lpszCurDir
), debugstr_w(lpszDestDir
));
311 _fetch_versioninfo(LPSTR fn
,VS_FIXEDFILEINFO
**vffi
) {
317 buf
=HeapAlloc(GetProcessHeap(), 0, alloclen
);
319 WARN("Memory exausted while fetching version info!\n");
323 ret
= GetFileVersionInfoA(fn
,0,alloclen
,buf
);
325 HeapFree(GetProcessHeap(), 0, buf
);
328 if (alloclen
<*(WORD
*)buf
) {
329 alloclen
= *(WORD
*)buf
;
330 HeapFree(GetProcessHeap(), 0, buf
);
331 buf
= HeapAlloc(GetProcessHeap(), 0, alloclen
);
333 WARN("Memory exausted while fetching version info!\n");
337 *vffi
= (VS_FIXEDFILEINFO
*)(buf
+0x14);
338 if ((*vffi
)->dwSignature
== 0x004f0049) /* hack to detect unicode */
339 *vffi
= (VS_FIXEDFILEINFO
*)(buf
+0x28);
340 if ((*vffi
)->dwSignature
!= VS_FFI_SIGNATURE
)
341 WARN("Bad VS_FIXEDFILEINFO signature 0x%08lx\n",(*vffi
)->dwSignature
);
348 _error2vif(DWORD error
) {
350 case ERROR_ACCESS_DENIED
:
351 return VIF_ACCESSVIOLATION
;
352 case ERROR_SHARING_VIOLATION
:
353 return VIF_SHARINGVIOLATION
;
360 /******************************************************************************
361 * VerInstallFileA [VERSION.@]
363 DWORD WINAPI
VerInstallFileA(
364 UINT flags
,LPSTR srcfilename
,LPSTR destfilename
,LPSTR srcdir
,
365 LPSTR destdir
,LPSTR curdir
,LPSTR tmpfile
,UINT
*tmpfilelen
)
368 char destfn
[260],tmpfn
[260],srcfn
[260];
370 DWORD attr
,ret
,xret
,tmplast
;
374 TRACE("(%x,%s,%s,%s,%s,%s,%p,%d)\n",
375 flags
,srcfilename
,destfilename
,srcdir
,destdir
,curdir
,tmpfile
,*tmpfilelen
378 sprintf(srcfn
,"%s\\%s",srcdir
,srcfilename
);
379 if (!destdir
|| !*destdir
) pdest
= srcdir
;
380 else pdest
= destdir
;
381 sprintf(destfn
,"%s\\%s",pdest
,destfilename
);
382 hfsrc
=LZOpenFileA(srcfn
,&ofs
,OF_READ
);
384 return VIF_CANNOTREADSRC
;
385 sprintf(tmpfn
,"%s\\%s",pdest
,destfilename
);
386 tmplast
=strlen(pdest
)+1;
387 attr
= GetFileAttributesA(tmpfn
);
388 if (attr
!= INVALID_FILE_ATTRIBUTES
) {
389 if (attr
& FILE_ATTRIBUTE_READONLY
) {
391 return VIF_WRITEPROT
;
393 /* FIXME: check if file currently in use and return VIF_FILEINUSE */
395 attr
= INVALID_FILE_ATTRIBUTES
;
396 if (flags
& VIFF_FORCEINSTALL
) {
398 sprintf(tmpfn
,"%s\\%s",pdest
,tmpfile
);
399 tmplast
= strlen(pdest
)+1;
400 attr
= GetFileAttributesA(tmpfn
);
401 /* if it exists, it has been copied by the call before.
402 * we jump over the copy part...
406 if (attr
== INVALID_FILE_ATTRIBUTES
) {
409 GetTempFileNameA(pdest
,"ver",0,tmpfn
); /* should not fail ... */
410 s
=strrchr(tmpfn
,'\\');
415 hfdst
= OpenFile(tmpfn
,&ofs
,OF_CREATE
);
416 if (hfdst
== HFILE_ERROR
) {
418 return VIF_CANNOTCREATE
; /* | translated dos error */
420 ret
= LZCopy(hfsrc
,hfdst
);
422 if (((LONG
)ret
) < 0) {
423 /* translate LZ errors into VIF_xxx */
425 case LZERROR_BADINHANDLE
:
427 case LZERROR_BADVALUE
:
428 case LZERROR_UNKNOWNALG
:
429 ret
= VIF_CANNOTREADSRC
;
431 case LZERROR_BADOUTHANDLE
:
433 ret
= VIF_OUTOFSPACE
;
435 case LZERROR_GLOBALLOC
:
436 case LZERROR_GLOBLOCK
:
437 ret
= VIF_OUTOFMEMORY
;
439 default: /* unknown error, should not happen */
450 if (!(flags
& VIFF_FORCEINSTALL
)) {
451 VS_FIXEDFILEINFO
*destvffi
,*tmpvffi
;
452 buf1
= _fetch_versioninfo(destfn
,&destvffi
);
454 buf2
= _fetch_versioninfo(tmpfn
,&tmpvffi
);
457 static CHAR trans_array
[] = "\\VarFileInfo\\Translation";
462 /* compare file versions */
463 if ((destvffi
->dwFileVersionMS
> tmpvffi
->dwFileVersionMS
)||
464 ((destvffi
->dwFileVersionMS
==tmpvffi
->dwFileVersionMS
)&&
465 (destvffi
->dwFileVersionLS
> tmpvffi
->dwFileVersionLS
)
468 xret
|= VIF_MISMATCH
|VIF_SRCOLD
;
469 /* compare filetypes and filesubtypes */
470 if ((destvffi
->dwFileType
!=tmpvffi
->dwFileType
) ||
471 (destvffi
->dwFileSubtype
!=tmpvffi
->dwFileSubtype
)
473 xret
|= VIF_MISMATCH
|VIF_DIFFTYPE
;
474 if (VerQueryValueA(buf1
,trans_array
,(LPVOID
*)&tbuf1
,&len1
) &&
475 VerQueryValueA(buf2
,trans_array
,(LPVOID
*)&tbuf2
,&len2
)
477 /* Do something with tbuf1 and tbuf2
478 * generates DIFFLANG|MISMATCH
481 HeapFree(GetProcessHeap(), 0, buf2
);
483 xret
=VIF_MISMATCH
|VIF_SRCOLD
;
484 HeapFree(GetProcessHeap(), 0, buf1
);
488 if (*tmpfilelen
<strlen(tmpfn
+tmplast
)) {
489 xret
|=VIF_BUFFTOOSMALL
;
492 strcpy(tmpfile
,tmpfn
+tmplast
);
493 *tmpfilelen
= strlen(tmpfn
+tmplast
)+1;
497 if (INVALID_FILE_ATTRIBUTES
!=GetFileAttributesA(destfn
))
498 if (!DeleteFileA(destfn
)) {
499 xret
|=_error2vif(GetLastError())|VIF_CANNOTDELETE
;
504 if ((!(flags
& VIFF_DONTDELETEOLD
)) &&
507 lstrcmpiA(curdir
,pdest
)
511 sprintf(curfn
,"%s\\%s",curdir
,destfilename
);
512 if (INVALID_FILE_ATTRIBUTES
!= GetFileAttributesA(curfn
)) {
513 /* FIXME: check if in use ... if it is, VIF_CANNOTDELETECUR */
514 if (!DeleteFileA(curfn
))
515 xret
|=_error2vif(GetLastError())|VIF_CANNOTDELETECUR
;
518 if (!MoveFileA(tmpfn
,destfn
)) {
519 xret
|=_error2vif(GetLastError())|VIF_CANNOTRENAME
;
528 /******************************************************************************
529 * VerInstallFileW [VERSION.@]
531 DWORD WINAPI
VerInstallFileW(
532 UINT flags
,LPWSTR srcfilename
,LPWSTR destfilename
,LPWSTR srcdir
,
533 LPWSTR destdir
,LPWSTR curdir
,LPWSTR tmpfile
,UINT
*tmpfilelen
)
535 LPSTR wsrcf
= NULL
, wsrcd
= NULL
, wdestf
= NULL
, wdestd
= NULL
, wtmpf
= NULL
, wcurd
= NULL
;
541 len
= WideCharToMultiByte( CP_ACP
, 0, srcfilename
, -1, NULL
, 0, NULL
, NULL
);
542 if ((wsrcf
= HeapAlloc( GetProcessHeap(), 0, len
)))
543 WideCharToMultiByte( CP_ACP
, 0, srcfilename
, -1, wsrcf
, len
, NULL
, NULL
);
547 len
= WideCharToMultiByte( CP_ACP
, 0, srcdir
, -1, NULL
, 0, NULL
, NULL
);
548 if ((wsrcd
= HeapAlloc( GetProcessHeap(), 0, len
)))
549 WideCharToMultiByte( CP_ACP
, 0, srcdir
, -1, wsrcd
, len
, NULL
, NULL
);
553 len
= WideCharToMultiByte( CP_ACP
, 0, destfilename
, -1, NULL
, 0, NULL
, NULL
);
554 if ((wdestf
= HeapAlloc( GetProcessHeap(), 0, len
)))
555 WideCharToMultiByte( CP_ACP
, 0, destfilename
, -1, wdestf
, len
, NULL
, NULL
);
559 len
= WideCharToMultiByte( CP_ACP
, 0, destdir
, -1, NULL
, 0, NULL
, NULL
);
560 if ((wdestd
= HeapAlloc( GetProcessHeap(), 0, len
)))
561 WideCharToMultiByte( CP_ACP
, 0, destdir
, -1, wdestd
, len
, NULL
, NULL
);
565 len
= WideCharToMultiByte( CP_ACP
, 0, curdir
, -1, NULL
, 0, NULL
, NULL
);
566 if ((wcurd
= HeapAlloc( GetProcessHeap(), 0, len
)))
567 WideCharToMultiByte( CP_ACP
, 0, curdir
, -1, wcurd
, len
, NULL
, NULL
);
569 len
= *tmpfilelen
* sizeof(WCHAR
);
570 wtmpf
= HeapAlloc( GetProcessHeap(), 0, len
);
571 ret
= VerInstallFileA(flags
,wsrcf
,wdestf
,wsrcd
,wdestd
,wcurd
,wtmpf
,&len
);
573 *tmpfilelen
= MultiByteToWideChar( CP_ACP
, 0, wtmpf
, -1, tmpfile
, *tmpfilelen
);
574 else if (ret
& VIF_BUFFTOOSMALL
)
575 *tmpfilelen
= len
; /* FIXME: not correct */
577 HeapFree( GetProcessHeap(), 0, wsrcf
);
578 HeapFree( GetProcessHeap(), 0, wsrcd
);
579 HeapFree( GetProcessHeap(), 0, wdestf
);
580 HeapFree( GetProcessHeap(), 0, wdestd
);
581 HeapFree( GetProcessHeap(), 0, wtmpf
);
582 HeapFree( GetProcessHeap(), 0, wcurd
);