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
31 #include "wine/unicode.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(ver
);
39 /******************************************************************************
42 * Tests whether a given path/file combination exists. If the file does
43 * not exist, the return value is zero. If it does exist, the return
47 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
48 * Original implementation
51 static int testFileExistenceA( char const * path
, char const * file
, BOOL excl
)
57 fileinfo
.cBytes
= sizeof(OFSTRUCT
);
59 strcpy(filename
, path
);
60 filenamelen
= strlen(filename
);
62 /* Add a trailing \ if necessary */
64 if(filename
[filenamelen
- 1] != '\\')
65 strcat(filename
, "\\");
67 else /* specify the current directory */
68 strcpy(filename
, ".\\");
70 /* Create the full pathname */
71 strcat(filename
, file
);
73 return (OpenFile(filename
, &fileinfo
,
74 OF_EXIST
| (excl
? OF_SHARE_EXCLUSIVE
: 0)) != HFILE_ERROR
);
77 /******************************************************************************
80 static int testFileExistenceW( const WCHAR
*path
, const WCHAR
*file
, BOOL excl
)
83 DWORD pathlen
, filelen
;
87 fileinfo
.cBytes
= sizeof(OFSTRUCT
);
89 pathlen
= WideCharToMultiByte( CP_ACP
, 0, path
, -1, NULL
, 0, NULL
, NULL
);
90 filelen
= WideCharToMultiByte( CP_ACP
, 0, file
, -1, NULL
, 0, NULL
, NULL
);
91 filename
= HeapAlloc( GetProcessHeap(), 0, pathlen
+filelen
+2 );
93 WideCharToMultiByte( CP_ACP
, 0, path
, -1, filename
, pathlen
, NULL
, NULL
);
94 /* Add a trailing \ if necessary */
97 if (filename
[pathlen
-2] != '\\') strcpy( &filename
[pathlen
-1], "\\" );
99 else /* specify the current directory */
100 strcpy(filename
, ".\\");
102 WideCharToMultiByte( CP_ACP
, 0, file
, -1, filename
+strlen(filename
), filelen
, NULL
, NULL
);
104 ret
= (OpenFile(filename
, &fileinfo
,
105 OF_EXIST
| (excl
? OF_SHARE_EXCLUSIVE
: 0)) != HFILE_ERROR
);
106 HeapFree( GetProcessHeap(), 0, filename
);
110 /*****************************************************************************
111 * VerFindFileA [VERSION.@]
113 * Determines where to install a file based on whether it locates another
114 * version of the file in the system. The values VerFindFile returns are
115 * used in a subsequent call to the VerInstallFile function.
118 * 30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
119 * Reimplementation of VerFindFile from original stub.
121 DWORD WINAPI
VerFindFileA(
129 UINT
*lpuDestDirLen
)
134 unsigned int curDirSizeReq
;
135 unsigned int destDirSizeReq
;
136 char systemDir
[MAX_PATH
];
138 /* Print out debugging information */
139 TRACE("flags = %x filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)\n",
140 flags
, debugstr_a(lpszFilename
), debugstr_a(lpszWinDir
), debugstr_a(lpszAppDir
),
141 lpuCurDirLen
, lpuCurDirLen
? *lpuCurDirLen
: 0,
142 lpuDestDirLen
, lpuDestDirLen
? *lpuDestDirLen
: 0 );
144 /* Figure out where the file should go; shared files default to the
147 GetSystemDirectoryA(systemDir
, sizeof(systemDir
));
151 if(flags
& VFFF_ISSHAREDFILE
)
154 /* Were we given a filename? If so, try to find the file. */
157 if(testFileExistenceA(destDir
, lpszFilename
, FALSE
)) curDir
= destDir
;
158 else if(lpszAppDir
&& testFileExistenceA(lpszAppDir
, lpszFilename
, FALSE
))
161 retval
|= VFF_CURNEDEST
;
165 else /* not a shared file */
169 destDir
= lpszAppDir
;
172 if(testFileExistenceA(destDir
, lpszFilename
, FALSE
)) curDir
= destDir
;
173 else if(testFileExistenceA(systemDir
, lpszFilename
, FALSE
))
176 retval
|= VFF_CURNEDEST
;
182 if (lpszFilename
&& !testFileExistenceA(curDir
, lpszFilename
, TRUE
))
183 retval
|= VFF_FILEINUSE
;
185 curDirSizeReq
= strlen(curDir
) + 1;
186 destDirSizeReq
= strlen(destDir
) + 1;
188 /* Make sure that the pointers to the size of the buffers are
189 valid; if not, do NOTHING with that buffer. If that pointer
190 is valid, then make sure that the buffer pointer is valid, too! */
192 if(lpuDestDirLen
&& lpszDestDir
)
194 if (*lpuDestDirLen
< destDirSizeReq
) retval
|= VFF_BUFFTOOSMALL
;
195 lstrcpynA(lpszDestDir
, destDir
, *lpuDestDirLen
);
196 *lpuDestDirLen
= destDirSizeReq
;
198 if(lpuCurDirLen
&& lpszCurDir
)
200 if(*lpuCurDirLen
< curDirSizeReq
) retval
|= VFF_BUFFTOOSMALL
;
201 lstrcpynA(lpszCurDir
, curDir
, *lpuCurDirLen
);
202 *lpuCurDirLen
= curDirSizeReq
;
205 TRACE("ret = %lu (%s%s%s) curdir=%s destdir=%s\n", retval
,
206 (retval
& VFF_CURNEDEST
) ? "VFF_CURNEDEST " : "",
207 (retval
& VFF_FILEINUSE
) ? "VFF_FILEINUSE " : "",
208 (retval
& VFF_BUFFTOOSMALL
) ? "VFF_BUFFTOOSMALL " : "",
209 debugstr_a(lpszCurDir
), debugstr_a(lpszDestDir
));
214 /*****************************************************************************
215 * VerFindFileW [VERSION.@]
217 DWORD WINAPI
VerFindFileW( UINT flags
,LPCWSTR lpszFilename
,LPCWSTR lpszWinDir
,
218 LPCWSTR lpszAppDir
, LPWSTR lpszCurDir
,UINT
*lpuCurDirLen
,
219 LPWSTR lpszDestDir
,UINT
*lpuDestDirLen
)
221 static const WCHAR emptyW
;
224 const WCHAR
*destDir
;
225 unsigned int curDirSizeReq
;
226 unsigned int destDirSizeReq
;
227 WCHAR systemDir
[MAX_PATH
];
229 /* Print out debugging information */
230 TRACE("flags = %x filename=%s windir=%s appdir=%s curdirlen=%p(%u) destdirlen=%p(%u)\n",
231 flags
, debugstr_w(lpszFilename
), debugstr_w(lpszWinDir
), debugstr_w(lpszAppDir
),
232 lpuCurDirLen
, lpuCurDirLen
? *lpuCurDirLen
: 0,
233 lpuDestDirLen
, lpuDestDirLen
? *lpuDestDirLen
: 0 );
235 /* Figure out where the file should go; shared files default to the
238 GetSystemDirectoryW(systemDir
, sizeof(systemDir
)/sizeof(WCHAR
));
242 if(flags
& VFFF_ISSHAREDFILE
)
245 /* Were we given a filename? If so, try to find the file. */
248 if(testFileExistenceW(destDir
, lpszFilename
, FALSE
)) curDir
= destDir
;
249 else if(lpszAppDir
&& testFileExistenceW(lpszAppDir
, lpszFilename
, FALSE
))
252 retval
|= VFF_CURNEDEST
;
256 else /* not a shared file */
260 destDir
= lpszAppDir
;
263 if(testFileExistenceW(destDir
, lpszFilename
, FALSE
)) curDir
= destDir
;
264 else if(testFileExistenceW(systemDir
, lpszFilename
, FALSE
))
267 retval
|= VFF_CURNEDEST
;
273 if (lpszFilename
&& !testFileExistenceW(curDir
, lpszFilename
, TRUE
))
274 retval
|= VFF_FILEINUSE
;
276 curDirSizeReq
= strlenW(curDir
) + 1;
277 destDirSizeReq
= strlenW(destDir
) + 1;
279 /* Make sure that the pointers to the size of the buffers are
280 valid; if not, do NOTHING with that buffer. If that pointer
281 is valid, then make sure that the buffer pointer is valid, too! */
283 if(lpuDestDirLen
&& lpszDestDir
)
285 if (*lpuDestDirLen
< destDirSizeReq
) retval
|= VFF_BUFFTOOSMALL
;
286 lstrcpynW(lpszDestDir
, destDir
, *lpuDestDirLen
);
287 *lpuDestDirLen
= destDirSizeReq
;
289 if(lpuCurDirLen
&& lpszCurDir
)
291 if(*lpuCurDirLen
< curDirSizeReq
) retval
|= VFF_BUFFTOOSMALL
;
292 lstrcpynW(lpszCurDir
, curDir
, *lpuCurDirLen
);
293 *lpuCurDirLen
= curDirSizeReq
;
296 TRACE("ret = %lu (%s%s%s) curdir=%s destdir=%s\n", retval
,
297 (retval
& VFF_CURNEDEST
) ? "VFF_CURNEDEST " : "",
298 (retval
& VFF_FILEINUSE
) ? "VFF_FILEINUSE " : "",
299 (retval
& VFF_BUFFTOOSMALL
) ? "VFF_BUFFTOOSMALL " : "",
300 debugstr_w(lpszCurDir
), debugstr_w(lpszDestDir
));
305 _fetch_versioninfo(LPSTR fn
,VS_FIXEDFILEINFO
**vffi
) {
311 buf
=HeapAlloc(GetProcessHeap(), 0, alloclen
);
313 WARN("Memory exausted while fetching version info!\n");
317 ret
= GetFileVersionInfoA(fn
,0,alloclen
,buf
);
319 HeapFree(GetProcessHeap(), 0, buf
);
322 if (alloclen
<*(WORD
*)buf
) {
323 alloclen
= *(WORD
*)buf
;
324 HeapFree(GetProcessHeap(), 0, buf
);
325 buf
= HeapAlloc(GetProcessHeap(), 0, alloclen
);
327 WARN("Memory exausted while fetching version info!\n");
331 *vffi
= (VS_FIXEDFILEINFO
*)(buf
+0x14);
332 if ((*vffi
)->dwSignature
== 0x004f0049) /* hack to detect unicode */
333 *vffi
= (VS_FIXEDFILEINFO
*)(buf
+0x28);
334 if ((*vffi
)->dwSignature
!= VS_FFI_SIGNATURE
)
335 WARN("Bad VS_FIXEDFILEINFO signature 0x%08lx\n",(*vffi
)->dwSignature
);
342 _error2vif(DWORD error
) {
344 case ERROR_ACCESS_DENIED
:
345 return VIF_ACCESSVIOLATION
;
346 case ERROR_SHARING_VIOLATION
:
347 return VIF_SHARINGVIOLATION
;
354 /******************************************************************************
355 * VerInstallFileA [VERSION.@]
357 DWORD WINAPI
VerInstallFileA(
358 UINT flags
,LPCSTR srcfilename
,LPCSTR destfilename
,LPCSTR srcdir
,
359 LPCSTR destdir
,LPCSTR curdir
,LPSTR tmpfile
,UINT
*tmpfilelen
)
362 char destfn
[260],tmpfn
[260],srcfn
[260];
364 DWORD attr
,ret
,xret
,tmplast
;
368 TRACE("(%x,%s,%s,%s,%s,%s,%p,%d)\n",
369 flags
,srcfilename
,destfilename
,srcdir
,destdir
,curdir
,tmpfile
,*tmpfilelen
372 sprintf(srcfn
,"%s\\%s",srcdir
,srcfilename
);
373 if (!destdir
|| !*destdir
) pdest
= srcdir
;
374 else pdest
= destdir
;
375 sprintf(destfn
,"%s\\%s",pdest
,destfilename
);
376 hfsrc
=LZOpenFileA(srcfn
,&ofs
,OF_READ
);
378 return VIF_CANNOTREADSRC
;
379 sprintf(tmpfn
,"%s\\%s",pdest
,destfilename
);
380 tmplast
=strlen(pdest
)+1;
381 attr
= GetFileAttributesA(tmpfn
);
383 if (attr
& FILE_ATTRIBUTE_READONLY
) {
385 return VIF_WRITEPROT
;
387 /* FIXME: check if file currently in use and return VIF_FILEINUSE */
390 if (flags
& VIFF_FORCEINSTALL
) {
392 sprintf(tmpfn
,"%s\\%s",pdest
,tmpfile
);
393 tmplast
= strlen(pdest
)+1;
394 attr
= GetFileAttributesA(tmpfn
);
395 /* if it exists, it has been copied by the call before.
396 * we jump over the copy part...
403 GetTempFileNameA(pdest
,"ver",0,tmpfn
); /* should not fail ... */
404 s
=strrchr(tmpfn
,'\\');
409 hfdst
= OpenFile(tmpfn
,&ofs
,OF_CREATE
);
410 if (hfdst
== HFILE_ERROR
) {
412 return VIF_CANNOTCREATE
; /* | translated dos error */
414 ret
= LZCopy(hfsrc
,hfdst
);
416 if (((long) ret
) < 0) {
417 /* translate LZ errors into VIF_xxx */
419 case LZERROR_BADINHANDLE
:
421 case LZERROR_BADVALUE
:
422 case LZERROR_UNKNOWNALG
:
423 ret
= VIF_CANNOTREADSRC
;
425 case LZERROR_BADOUTHANDLE
:
427 ret
= VIF_OUTOFSPACE
;
429 case LZERROR_GLOBALLOC
:
430 case LZERROR_GLOBLOCK
:
431 ret
= VIF_OUTOFMEMORY
;
433 default: /* unknown error, should not happen */
444 if (!(flags
& VIFF_FORCEINSTALL
)) {
445 VS_FIXEDFILEINFO
*destvffi
,*tmpvffi
;
446 buf1
= _fetch_versioninfo(destfn
,&destvffi
);
448 buf2
= _fetch_versioninfo(tmpfn
,&tmpvffi
);
455 /* compare file versions */
456 if ((destvffi
->dwFileVersionMS
> tmpvffi
->dwFileVersionMS
)||
457 ((destvffi
->dwFileVersionMS
==tmpvffi
->dwFileVersionMS
)&&
458 (destvffi
->dwFileVersionLS
> tmpvffi
->dwFileVersionLS
)
461 xret
|= VIF_MISMATCH
|VIF_SRCOLD
;
462 /* compare filetypes and filesubtypes */
463 if ((destvffi
->dwFileType
!=tmpvffi
->dwFileType
) ||
464 (destvffi
->dwFileSubtype
!=tmpvffi
->dwFileSubtype
)
466 xret
|= VIF_MISMATCH
|VIF_DIFFTYPE
;
467 if (VerQueryValueA(buf1
,"\\VarFileInfo\\Translation",(LPVOID
*)&tbuf1
,&len1
) &&
468 VerQueryValueA(buf2
,"\\VarFileInfo\\Translation",(LPVOID
*)&tbuf2
,&len2
)
470 /* irgendwas mit tbuf1 und tbuf2 machen
471 * generiert DIFFLANG|MISMATCH
474 HeapFree(GetProcessHeap(), 0, buf2
);
476 xret
=VIF_MISMATCH
|VIF_SRCOLD
;
477 HeapFree(GetProcessHeap(), 0, buf1
);
481 if (*tmpfilelen
<strlen(tmpfn
+tmplast
)) {
482 xret
|=VIF_BUFFTOOSMALL
;
485 strcpy(tmpfile
,tmpfn
+tmplast
);
486 *tmpfilelen
= strlen(tmpfn
+tmplast
)+1;
490 if (-1!=GetFileAttributesA(destfn
))
491 if (!DeleteFileA(destfn
)) {
492 xret
|=_error2vif(GetLastError())|VIF_CANNOTDELETE
;
497 if ((!(flags
& VIFF_DONTDELETEOLD
)) &&
500 lstrcmpiA(curdir
,pdest
)
504 sprintf(curfn
,"%s\\%s",curdir
,destfilename
);
505 if (-1!=GetFileAttributesA(curfn
)) {
506 /* FIXME: check if in use ... if it is, VIF_CANNOTDELETECUR */
507 if (!DeleteFileA(curfn
))
508 xret
|=_error2vif(GetLastError())|VIF_CANNOTDELETECUR
;
511 if (!MoveFileA(tmpfn
,destfn
)) {
512 xret
|=_error2vif(GetLastError())|VIF_CANNOTRENAME
;
521 /******************************************************************************
522 * VerInstallFileW [VERSION.@]
524 DWORD WINAPI
VerInstallFileW(
525 UINT flags
,LPCWSTR srcfilename
,LPCWSTR destfilename
,LPCWSTR srcdir
,
526 LPCWSTR destdir
,LPCWSTR curdir
,LPWSTR tmpfile
,UINT
*tmpfilelen
)
528 LPSTR wsrcf
= NULL
, wsrcd
= NULL
, wdestf
= NULL
, wdestd
= NULL
, wtmpf
= NULL
, wcurd
= NULL
;
534 len
= WideCharToMultiByte( CP_ACP
, 0, srcfilename
, -1, NULL
, 0, NULL
, NULL
);
535 if ((wsrcf
= HeapAlloc( GetProcessHeap(), 0, len
)))
536 WideCharToMultiByte( CP_ACP
, 0, srcfilename
, -1, wsrcf
, len
, NULL
, NULL
);
540 len
= WideCharToMultiByte( CP_ACP
, 0, srcdir
, -1, NULL
, 0, NULL
, NULL
);
541 if ((wsrcd
= HeapAlloc( GetProcessHeap(), 0, len
)))
542 WideCharToMultiByte( CP_ACP
, 0, srcdir
, -1, wsrcd
, len
, NULL
, NULL
);
546 len
= WideCharToMultiByte( CP_ACP
, 0, destfilename
, -1, NULL
, 0, NULL
, NULL
);
547 if ((wdestf
= HeapAlloc( GetProcessHeap(), 0, len
)))
548 WideCharToMultiByte( CP_ACP
, 0, destfilename
, -1, wdestf
, len
, NULL
, NULL
);
552 len
= WideCharToMultiByte( CP_ACP
, 0, destdir
, -1, NULL
, 0, NULL
, NULL
);
553 if ((wdestd
= HeapAlloc( GetProcessHeap(), 0, len
)))
554 WideCharToMultiByte( CP_ACP
, 0, destdir
, -1, wdestd
, len
, NULL
, NULL
);
558 len
= WideCharToMultiByte( CP_ACP
, 0, curdir
, -1, NULL
, 0, NULL
, NULL
);
559 if ((wcurd
= HeapAlloc( GetProcessHeap(), 0, len
)))
560 WideCharToMultiByte( CP_ACP
, 0, curdir
, -1, wcurd
, len
, NULL
, NULL
);
562 len
= *tmpfilelen
* sizeof(WCHAR
);
563 wtmpf
= HeapAlloc( GetProcessHeap(), 0, len
);
564 ret
= VerInstallFileA(flags
,wsrcf
,wdestf
,wsrcd
,wdestd
,wcurd
,wtmpf
,&len
);
566 *tmpfilelen
= MultiByteToWideChar( CP_ACP
, 0, wtmpf
, -1, tmpfile
, *tmpfilelen
);
567 else if (ret
& VIF_BUFFTOOSMALL
)
568 *tmpfilelen
= len
; /* FIXME: not correct */
570 HeapFree( GetProcessHeap(), 0, wsrcf
);
571 HeapFree( GetProcessHeap(), 0, wsrcd
);
572 HeapFree( GetProcessHeap(), 0, wdestf
);
573 HeapFree( GetProcessHeap(), 0, wdestd
);
574 HeapFree( GetProcessHeap(), 0, wtmpf
);
575 HeapFree( GetProcessHeap(), 0, wcurd
);