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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/unicode.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(ver
);
38 /******************************************************************************
41 * Tests whether a given path/file combination exists. If the file does
42 * not exist, the return value is zero. If it does exist, the return
46 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
47 * Original implementation
50 static int testFileExistenceA( char const * path
, char const * file
, BOOL excl
)
56 fileinfo
.cBytes
= sizeof(OFSTRUCT
);
58 strcpy(filename
, path
);
59 filenamelen
= strlen(filename
);
61 /* Add a trailing \ if necessary */
63 if(filename
[filenamelen
- 1] != '\\')
64 strcat(filename
, "\\");
66 else /* specify the current directory */
67 strcpy(filename
, ".\\");
69 /* Create the full pathname */
70 strcat(filename
, file
);
72 return (OpenFile(filename
, &fileinfo
,
73 OF_EXIST
| (excl
? OF_SHARE_EXCLUSIVE
: 0)) != HFILE_ERROR
);
76 /******************************************************************************
79 static int testFileExistenceW( const WCHAR
*path
, const WCHAR
*file
, BOOL excl
)
82 DWORD pathlen
, filelen
;
86 fileinfo
.cBytes
= sizeof(OFSTRUCT
);
88 pathlen
= WideCharToMultiByte( CP_ACP
, 0, path
, -1, NULL
, 0, NULL
, NULL
);
89 filelen
= WideCharToMultiByte( CP_ACP
, 0, file
, -1, NULL
, 0, NULL
, NULL
);
90 filename
= HeapAlloc( GetProcessHeap(), 0, pathlen
+filelen
+2 );
92 WideCharToMultiByte( CP_ACP
, 0, path
, -1, filename
, pathlen
, NULL
, NULL
);
93 /* Add a trailing \ if necessary */
96 if (filename
[pathlen
-2] != '\\') strcpy( &filename
[pathlen
-1], "\\" );
98 else /* specify the current directory */
99 strcpy(filename
, ".\\");
101 WideCharToMultiByte( CP_ACP
, 0, file
, -1, filename
+strlen(filename
), filelen
, NULL
, NULL
);
103 ret
= (OpenFile(filename
, &fileinfo
,
104 OF_EXIST
| (excl
? OF_SHARE_EXCLUSIVE
: 0)) != HFILE_ERROR
);
105 HeapFree( GetProcessHeap(), 0, filename
);
109 /*****************************************************************************
110 * VerFindFileA [VERSION.@]
112 * Determines where to install a file based on whether it locates another
113 * version of the file in the system. The values VerFindFile returns are
114 * used in a subsequent call to the VerInstallFile function.
117 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
118 * Reimplementation of VerFindFile from original stub.
120 DWORD WINAPI
VerFindFileA(
128 UINT
*lpuDestDirLen
)
133 unsigned int curDirSizeReq
;
134 unsigned int destDirSizeReq
;
135 char systemDir
[MAX_PATH
];
137 /* Print out debugging information */
138 TRACE("flags = %x filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)\n",
139 flags
, debugstr_a(lpszFilename
), debugstr_a(lpszWinDir
), debugstr_a(lpszAppDir
),
140 lpuCurDirLen
, lpuCurDirLen
? *lpuCurDirLen
: 0,
141 lpuDestDirLen
, lpuDestDirLen
? *lpuDestDirLen
: 0 );
143 /* Figure out where the file should go; shared files default to the
146 GetSystemDirectoryA(systemDir
, sizeof(systemDir
));
150 if(flags
& VFFF_ISSHAREDFILE
)
153 /* Were we given a filename? If so, try to find the file. */
156 if(testFileExistenceA(destDir
, lpszFilename
, FALSE
)) curDir
= destDir
;
157 else if(lpszAppDir
&& testFileExistenceA(lpszAppDir
, lpszFilename
, FALSE
))
160 retval
|= VFF_CURNEDEST
;
164 else /* not a shared file */
168 destDir
= lpszAppDir
;
171 if(testFileExistenceA(destDir
, lpszFilename
, FALSE
)) curDir
= destDir
;
172 else if(testFileExistenceA(systemDir
, lpszFilename
, FALSE
))
175 retval
|= VFF_CURNEDEST
;
181 if (lpszFilename
&& !testFileExistenceA(curDir
, lpszFilename
, TRUE
))
182 retval
|= VFF_FILEINUSE
;
184 curDirSizeReq
= strlen(curDir
) + 1;
185 destDirSizeReq
= strlen(destDir
) + 1;
187 /* Make sure that the pointers to the size of the buffers are
188 valid; if not, do NOTHING with that buffer. If that pointer
189 is valid, then make sure that the buffer pointer is valid, too! */
191 if(lpuDestDirLen
&& lpszDestDir
)
193 if (*lpuDestDirLen
< destDirSizeReq
) retval
|= VFF_BUFFTOOSMALL
;
194 lstrcpynA(lpszDestDir
, destDir
, *lpuDestDirLen
);
195 *lpuDestDirLen
= destDirSizeReq
;
197 if(lpuCurDirLen
&& lpszCurDir
)
199 if(*lpuCurDirLen
< curDirSizeReq
) retval
|= VFF_BUFFTOOSMALL
;
200 lstrcpynA(lpszCurDir
, curDir
, *lpuCurDirLen
);
201 *lpuCurDirLen
= curDirSizeReq
;
204 TRACE("ret = %lu (%s%s%s) curdir=%s destdir=%s\n", retval
,
205 (retval
& VFF_CURNEDEST
) ? "VFF_CURNEDEST " : "",
206 (retval
& VFF_FILEINUSE
) ? "VFF_FILEINUSE " : "",
207 (retval
& VFF_BUFFTOOSMALL
) ? "VFF_BUFFTOOSMALL " : "",
208 debugstr_a(lpszCurDir
), debugstr_a(lpszDestDir
));
213 /*****************************************************************************
214 * VerFindFileW [VERSION.@]
216 DWORD WINAPI
VerFindFileW( UINT flags
,LPCWSTR lpszFilename
,LPCWSTR lpszWinDir
,
217 LPCWSTR lpszAppDir
, LPWSTR lpszCurDir
,UINT
*lpuCurDirLen
,
218 LPWSTR lpszDestDir
,UINT
*lpuDestDirLen
)
220 static const WCHAR emptyW
;
223 const WCHAR
*destDir
;
224 unsigned int curDirSizeReq
;
225 unsigned int destDirSizeReq
;
226 WCHAR systemDir
[MAX_PATH
];
228 /* Print out debugging information */
229 TRACE("flags = %x filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)\n",
230 flags
, debugstr_w(lpszFilename
), debugstr_w(lpszWinDir
), debugstr_w(lpszAppDir
),
231 lpuCurDirLen
, lpuCurDirLen
? *lpuCurDirLen
: 0,
232 lpuDestDirLen
, lpuDestDirLen
? *lpuDestDirLen
: 0 );
234 /* Figure out where the file should go; shared files default to the
237 GetSystemDirectoryW(systemDir
, sizeof(systemDir
)/sizeof(WCHAR
));
241 if(flags
& VFFF_ISSHAREDFILE
)
244 /* Were we given a filename? If so, try to find the file. */
247 if(testFileExistenceW(destDir
, lpszFilename
, FALSE
)) curDir
= destDir
;
248 else if(lpszAppDir
&& testFileExistenceW(lpszAppDir
, lpszFilename
, FALSE
))
251 retval
|= VFF_CURNEDEST
;
255 else /* not a shared file */
259 destDir
= lpszAppDir
;
262 if(testFileExistenceW(destDir
, lpszFilename
, FALSE
)) curDir
= destDir
;
263 else if(testFileExistenceW(systemDir
, lpszFilename
, FALSE
))
266 retval
|= VFF_CURNEDEST
;
272 if (lpszFilename
&& !testFileExistenceW(curDir
, lpszFilename
, TRUE
))
273 retval
|= VFF_FILEINUSE
;
275 curDirSizeReq
= strlenW(curDir
) + 1;
276 destDirSizeReq
= strlenW(destDir
) + 1;
278 /* Make sure that the pointers to the size of the buffers are
279 valid; if not, do NOTHING with that buffer. If that pointer
280 is valid, then make sure that the buffer pointer is valid, too! */
282 if(lpuDestDirLen
&& lpszDestDir
)
284 if (*lpuDestDirLen
< destDirSizeReq
) retval
|= VFF_BUFFTOOSMALL
;
285 lstrcpynW(lpszDestDir
, destDir
, *lpuDestDirLen
);
286 *lpuDestDirLen
= destDirSizeReq
;
288 if(lpuCurDirLen
&& lpszCurDir
)
290 if(*lpuCurDirLen
< curDirSizeReq
) retval
|= VFF_BUFFTOOSMALL
;
291 lstrcpynW(lpszCurDir
, curDir
, *lpuCurDirLen
);
292 *lpuCurDirLen
= curDirSizeReq
;
295 TRACE("ret = %lu (%s%s%s) curdir=%s destdir=%s\n", retval
,
296 (retval
& VFF_CURNEDEST
) ? "VFF_CURNEDEST " : "",
297 (retval
& VFF_FILEINUSE
) ? "VFF_FILEINUSE " : "",
298 (retval
& VFF_BUFFTOOSMALL
) ? "VFF_BUFFTOOSMALL " : "",
299 debugstr_w(lpszCurDir
), debugstr_w(lpszDestDir
));
304 _fetch_versioninfo(LPSTR fn
,VS_FIXEDFILEINFO
**vffi
) {
310 buf
=HeapAlloc(GetProcessHeap(), 0, alloclen
);
312 WARN("Memory exausted while fetching version info!\n");
316 ret
= GetFileVersionInfoA(fn
,0,alloclen
,buf
);
318 HeapFree(GetProcessHeap(), 0, buf
);
321 if (alloclen
<*(WORD
*)buf
) {
322 alloclen
= *(WORD
*)buf
;
323 HeapFree(GetProcessHeap(), 0, buf
);
324 buf
= HeapAlloc(GetProcessHeap(), 0, alloclen
);
326 WARN("Memory exausted while fetching version info!\n");
330 *vffi
= (VS_FIXEDFILEINFO
*)(buf
+0x14);
331 if ((*vffi
)->dwSignature
== 0x004f0049) /* hack to detect unicode */
332 *vffi
= (VS_FIXEDFILEINFO
*)(buf
+0x28);
333 if ((*vffi
)->dwSignature
!= VS_FFI_SIGNATURE
)
334 WARN("Bad VS_FIXEDFILEINFO signature 0x%08lx\n",(*vffi
)->dwSignature
);
341 _error2vif(DWORD error
) {
343 case ERROR_ACCESS_DENIED
:
344 return VIF_ACCESSVIOLATION
;
345 case ERROR_SHARING_VIOLATION
:
346 return VIF_SHARINGVIOLATION
;
353 /******************************************************************************
354 * VerInstallFileA [VERSION.@]
356 DWORD WINAPI
VerInstallFileA(
357 UINT flags
,LPCSTR srcfilename
,LPCSTR destfilename
,LPCSTR srcdir
,
358 LPCSTR destdir
,LPCSTR curdir
,LPSTR tmpfile
,UINT
*tmpfilelen
)
361 char destfn
[260],tmpfn
[260],srcfn
[260];
363 DWORD attr
,ret
,xret
,tmplast
;
367 TRACE("(%x,%s,%s,%s,%s,%s,%p,%d)\n",
368 flags
,srcfilename
,destfilename
,srcdir
,destdir
,curdir
,tmpfile
,*tmpfilelen
371 sprintf(srcfn
,"%s\\%s",srcdir
,srcfilename
);
372 if (!destdir
|| !*destdir
) pdest
= srcdir
;
373 else pdest
= destdir
;
374 sprintf(destfn
,"%s\\%s",pdest
,destfilename
);
375 hfsrc
=LZOpenFileA(srcfn
,&ofs
,OF_READ
);
377 return VIF_CANNOTREADSRC
;
378 sprintf(tmpfn
,"%s\\%s",pdest
,destfilename
);
379 tmplast
=strlen(pdest
)+1;
380 attr
= GetFileAttributesA(tmpfn
);
382 if (attr
& FILE_ATTRIBUTE_READONLY
) {
384 return VIF_WRITEPROT
;
386 /* FIXME: check if file currently in use and return VIF_FILEINUSE */
389 if (flags
& VIFF_FORCEINSTALL
) {
391 sprintf(tmpfn
,"%s\\%s",pdest
,tmpfile
);
392 tmplast
= strlen(pdest
)+1;
393 attr
= GetFileAttributesA(tmpfn
);
394 /* if it exists, it has been copied by the call before.
395 * we jump over the copy part...
402 GetTempFileNameA(pdest
,"ver",0,tmpfn
); /* should not fail ... */
403 s
=strrchr(tmpfn
,'\\');
408 hfdst
= OpenFile(tmpfn
,&ofs
,OF_CREATE
);
409 if (hfdst
== HFILE_ERROR
) {
411 return VIF_CANNOTCREATE
; /* | translated dos error */
413 ret
= LZCopy(hfsrc
,hfdst
);
415 if (((long) ret
) < 0) {
416 /* translate LZ errors into VIF_xxx */
418 case LZERROR_BADINHANDLE
:
420 case LZERROR_BADVALUE
:
421 case LZERROR_UNKNOWNALG
:
422 ret
= VIF_CANNOTREADSRC
;
424 case LZERROR_BADOUTHANDLE
:
426 ret
= VIF_OUTOFSPACE
;
428 case LZERROR_GLOBALLOC
:
429 case LZERROR_GLOBLOCK
:
430 ret
= VIF_OUTOFMEMORY
;
432 default: /* unknown error, should not happen */
443 if (!(flags
& VIFF_FORCEINSTALL
)) {
444 VS_FIXEDFILEINFO
*destvffi
,*tmpvffi
;
445 buf1
= _fetch_versioninfo(destfn
,&destvffi
);
447 buf2
= _fetch_versioninfo(tmpfn
,&tmpvffi
);
454 /* compare file versions */
455 if ((destvffi
->dwFileVersionMS
> tmpvffi
->dwFileVersionMS
)||
456 ((destvffi
->dwFileVersionMS
==tmpvffi
->dwFileVersionMS
)&&
457 (destvffi
->dwFileVersionLS
> tmpvffi
->dwFileVersionLS
)
460 xret
|= VIF_MISMATCH
|VIF_SRCOLD
;
461 /* compare filetypes and filesubtypes */
462 if ((destvffi
->dwFileType
!=tmpvffi
->dwFileType
) ||
463 (destvffi
->dwFileSubtype
!=tmpvffi
->dwFileSubtype
)
465 xret
|= VIF_MISMATCH
|VIF_DIFFTYPE
;
466 if (VerQueryValueA(buf1
,"\\VarFileInfo\\Translation",(LPVOID
*)&tbuf1
,&len1
) &&
467 VerQueryValueA(buf2
,"\\VarFileInfo\\Translation",(LPVOID
*)&tbuf2
,&len2
)
469 /* irgendwas mit tbuf1 und tbuf2 machen
470 * generiert DIFFLANG|MISMATCH
473 HeapFree(GetProcessHeap(), 0, buf2
);
475 xret
=VIF_MISMATCH
|VIF_SRCOLD
;
476 HeapFree(GetProcessHeap(), 0, buf1
);
480 if (*tmpfilelen
<strlen(tmpfn
+tmplast
)) {
481 xret
|=VIF_BUFFTOOSMALL
;
484 strcpy(tmpfile
,tmpfn
+tmplast
);
485 *tmpfilelen
= strlen(tmpfn
+tmplast
)+1;
489 if (-1!=GetFileAttributesA(destfn
))
490 if (!DeleteFileA(destfn
)) {
491 xret
|=_error2vif(GetLastError())|VIF_CANNOTDELETE
;
496 if ((!(flags
& VIFF_DONTDELETEOLD
)) &&
499 lstrcmpiA(curdir
,pdest
)
503 sprintf(curfn
,"%s\\%s",curdir
,destfilename
);
504 if (-1!=GetFileAttributesA(curfn
)) {
505 /* FIXME: check if in use ... if it is, VIF_CANNOTDELETECUR */
506 if (!DeleteFileA(curfn
))
507 xret
|=_error2vif(GetLastError())|VIF_CANNOTDELETECUR
;
510 if (!MoveFileA(tmpfn
,destfn
)) {
511 xret
|=_error2vif(GetLastError())|VIF_CANNOTRENAME
;
520 /******************************************************************************
521 * VerInstallFileW [VERSION.@]
523 DWORD WINAPI
VerInstallFileW(
524 UINT flags
,LPCWSTR srcfilename
,LPCWSTR destfilename
,LPCWSTR srcdir
,
525 LPCWSTR destdir
,LPCWSTR curdir
,LPWSTR tmpfile
,UINT
*tmpfilelen
)
527 LPSTR wsrcf
= NULL
, wsrcd
= NULL
, wdestf
= NULL
, wdestd
= NULL
, wtmpf
= NULL
, wcurd
= NULL
;
533 len
= WideCharToMultiByte( CP_ACP
, 0, srcfilename
, -1, NULL
, 0, NULL
, NULL
);
534 if ((wsrcf
= HeapAlloc( GetProcessHeap(), 0, len
)))
535 WideCharToMultiByte( CP_ACP
, 0, srcfilename
, -1, wsrcf
, len
, NULL
, NULL
);
539 len
= WideCharToMultiByte( CP_ACP
, 0, srcdir
, -1, NULL
, 0, NULL
, NULL
);
540 if ((wsrcd
= HeapAlloc( GetProcessHeap(), 0, len
)))
541 WideCharToMultiByte( CP_ACP
, 0, srcdir
, -1, wsrcd
, len
, NULL
, NULL
);
545 len
= WideCharToMultiByte( CP_ACP
, 0, destfilename
, -1, NULL
, 0, NULL
, NULL
);
546 if ((wdestf
= HeapAlloc( GetProcessHeap(), 0, len
)))
547 WideCharToMultiByte( CP_ACP
, 0, destfilename
, -1, wdestf
, len
, NULL
, NULL
);
551 len
= WideCharToMultiByte( CP_ACP
, 0, destdir
, -1, NULL
, 0, NULL
, NULL
);
552 if ((wdestd
= HeapAlloc( GetProcessHeap(), 0, len
)))
553 WideCharToMultiByte( CP_ACP
, 0, destdir
, -1, wdestd
, len
, NULL
, NULL
);
557 len
= WideCharToMultiByte( CP_ACP
, 0, curdir
, -1, NULL
, 0, NULL
, NULL
);
558 if ((wcurd
= HeapAlloc( GetProcessHeap(), 0, len
)))
559 WideCharToMultiByte( CP_ACP
, 0, curdir
, -1, wcurd
, len
, NULL
, NULL
);
561 len
= *tmpfilelen
* sizeof(WCHAR
);
562 wtmpf
= HeapAlloc( GetProcessHeap(), 0, len
);
563 ret
= VerInstallFileA(flags
,wsrcf
,wdestf
,wsrcd
,wdestd
,wcurd
,wtmpf
,&len
);
565 *tmpfilelen
= MultiByteToWideChar( CP_ACP
, 0, wtmpf
, -1, tmpfile
, *tmpfilelen
);
566 else if (ret
& VIF_BUFFTOOSMALL
)
567 *tmpfilelen
= len
; /* FIXME: not correct */
569 HeapFree( GetProcessHeap(), 0, wsrcf
);
570 HeapFree( GetProcessHeap(), 0, wsrcd
);
571 HeapFree( GetProcessHeap(), 0, wdestf
);
572 HeapFree( GetProcessHeap(), 0, wdestd
);
573 HeapFree( GetProcessHeap(), 0, wtmpf
);
574 HeapFree( GetProcessHeap(), 0, wcurd
);