Fixed definitions of TTTOOLINFOA/W_V1_SIZE and
[wine] / dlls / version / install.c
1 /*
2  * Implementation of VERSION.DLL - File Installer routines
3  *
4  * Copyright 1996,1997 Marcus Meissner
5  * Copyright 1997 David Cuthbert
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winver.h"
30 #include "winnls.h"
31 #include "wine/unicode.h"
32 #include "winerror.h"
33 #include "lzexpand.h"
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(ver);
37
38
39 /******************************************************************************
40  *   testFileExistenceA
41  *
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
44  *   value is non-zero.
45  *
46  *   Revision history
47  *      30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
48  *         Original implementation
49  *
50  */
51 static int testFileExistenceA( char const * path, char const * file, BOOL excl )
52 {
53     char  filename[1024];
54     int  filenamelen;
55     OFSTRUCT  fileinfo;
56
57     fileinfo.cBytes = sizeof(OFSTRUCT);
58
59     strcpy(filename, path);
60     filenamelen = strlen(filename);
61
62     /* Add a trailing \ if necessary */
63     if(filenamelen) {
64         if(filename[filenamelen - 1] != '\\')
65             strcat(filename, "\\");
66     }
67     else /* specify the current directory */
68         strcpy(filename, ".\\");
69
70     /* Create the full pathname */
71     strcat(filename, file);
72
73     return (OpenFile(filename, &fileinfo,
74                      OF_EXIST | (excl ? OF_SHARE_EXCLUSIVE : 0)) != HFILE_ERROR);
75 }
76
77 /******************************************************************************
78  *   testFileExistenceW
79  */
80 static int testFileExistenceW( const WCHAR *path, const WCHAR *file, BOOL excl )
81 {
82     char *filename;
83     DWORD pathlen, filelen;
84     int ret;
85     OFSTRUCT fileinfo;
86
87     fileinfo.cBytes = sizeof(OFSTRUCT);
88
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 );
92
93     WideCharToMultiByte( CP_ACP, 0, path, -1, filename, pathlen, NULL, NULL );
94     /* Add a trailing \ if necessary */
95     if (pathlen > 1)
96     {
97         if (filename[pathlen-2] != '\\') strcpy( &filename[pathlen-1], "\\" );
98     }
99     else /* specify the current directory */
100         strcpy(filename, ".\\");
101
102     WideCharToMultiByte( CP_ACP, 0, file, -1, filename+strlen(filename), filelen, NULL, NULL );
103
104     ret = (OpenFile(filename, &fileinfo,
105                     OF_EXIST | (excl ? OF_SHARE_EXCLUSIVE : 0)) != HFILE_ERROR);
106     HeapFree( GetProcessHeap(), 0, filename );
107     return ret;
108 }
109
110 /*****************************************************************************
111  *   VerFindFileA [VERSION.@]
112  *
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.
116  *
117  *   Revision history:
118  *      30-May-1997   Dave Cuthbert (dacut@ece.cmu.edu)
119  *         Reimplementation of VerFindFile from original stub.
120  */
121 DWORD WINAPI VerFindFileA(
122     UINT flags,
123     LPCSTR lpszFilename,
124     LPCSTR lpszWinDir,
125     LPCSTR lpszAppDir,
126     LPSTR lpszCurDir,
127     UINT *lpuCurDirLen,
128     LPSTR lpszDestDir,
129     UINT *lpuDestDirLen )
130 {
131     DWORD  retval = 0;
132     const char *curDir;
133     const char *destDir;
134     unsigned int  curDirSizeReq;
135     unsigned int  destDirSizeReq;
136     char  systemDir[MAX_PATH];
137
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 );
143
144     /* Figure out where the file should go; shared files default to the
145        system directory */
146
147     GetSystemDirectoryA(systemDir, sizeof(systemDir));
148     curDir = "";
149     destDir = "";
150
151     if(flags & VFFF_ISSHAREDFILE)
152     {
153         destDir = systemDir;
154         /* Were we given a filename?  If so, try to find the file. */
155         if(lpszFilename)
156         {
157             if(testFileExistenceA(destDir, lpszFilename, FALSE)) curDir = destDir;
158             else if(lpszAppDir && testFileExistenceA(lpszAppDir, lpszFilename, FALSE))
159             {
160                 curDir = lpszAppDir;
161                 retval |= VFF_CURNEDEST;
162             }
163         }
164     }
165     else /* not a shared file */
166     {
167         if(lpszAppDir)
168         {
169             destDir = lpszAppDir;
170             if(lpszFilename)
171             {
172                 if(testFileExistenceA(destDir, lpszFilename, FALSE)) curDir = destDir;
173                 else if(testFileExistenceA(systemDir, lpszFilename, FALSE))
174                 {
175                     curDir = systemDir;
176                     retval |= VFF_CURNEDEST;
177                 }
178             }
179         }
180     }
181
182     if (lpszFilename && !testFileExistenceA(curDir, lpszFilename, TRUE))
183         retval |= VFF_FILEINUSE;
184
185     curDirSizeReq = strlen(curDir) + 1;
186     destDirSizeReq = strlen(destDir) + 1;
187
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! */
191
192     if(lpuDestDirLen && lpszDestDir)
193     {
194         if (*lpuDestDirLen < destDirSizeReq) retval |= VFF_BUFFTOOSMALL;
195         lstrcpynA(lpszDestDir, destDir, *lpuDestDirLen);
196         *lpuDestDirLen = destDirSizeReq;
197     }
198     if(lpuCurDirLen && lpszCurDir)
199     {
200         if(*lpuCurDirLen < curDirSizeReq) retval |= VFF_BUFFTOOSMALL;
201         lstrcpynA(lpszCurDir, curDir, *lpuCurDirLen);
202         *lpuCurDirLen = curDirSizeReq;
203     }
204
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));
210
211     return retval;
212 }
213
214 /*****************************************************************************
215  * VerFindFileW                                         [VERSION.@]
216  */
217 DWORD WINAPI VerFindFileW( UINT flags,LPCWSTR lpszFilename,LPCWSTR lpszWinDir,
218                            LPCWSTR lpszAppDir, LPWSTR lpszCurDir,UINT *lpuCurDirLen,
219                            LPWSTR lpszDestDir,UINT *lpuDestDirLen )
220 {
221     static const WCHAR emptyW;
222     DWORD retval = 0;
223     const WCHAR *curDir;
224     const WCHAR *destDir;
225     unsigned int curDirSizeReq;
226     unsigned int destDirSizeReq;
227     WCHAR systemDir[MAX_PATH];
228
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 );
234
235     /* Figure out where the file should go; shared files default to the
236        system directory */
237
238     GetSystemDirectoryW(systemDir, sizeof(systemDir)/sizeof(WCHAR));
239     curDir = &emptyW;
240     destDir = &emptyW;
241
242     if(flags & VFFF_ISSHAREDFILE)
243     {
244         destDir = systemDir;
245         /* Were we given a filename?  If so, try to find the file. */
246         if(lpszFilename)
247         {
248             if(testFileExistenceW(destDir, lpszFilename, FALSE)) curDir = destDir;
249             else if(lpszAppDir && testFileExistenceW(lpszAppDir, lpszFilename, FALSE))
250             {
251                 curDir = lpszAppDir;
252                 retval |= VFF_CURNEDEST;
253             }
254         }
255     }
256     else /* not a shared file */
257     {
258         if(lpszAppDir)
259         {
260             destDir = lpszAppDir;
261             if(lpszFilename)
262             {
263                 if(testFileExistenceW(destDir, lpszFilename, FALSE)) curDir = destDir;
264                 else if(testFileExistenceW(systemDir, lpszFilename, FALSE))
265                 {
266                     curDir = systemDir;
267                     retval |= VFF_CURNEDEST;
268                 }
269             }
270         }
271     }
272
273     if (lpszFilename && !testFileExistenceW(curDir, lpszFilename, TRUE))
274         retval |= VFF_FILEINUSE;
275
276     curDirSizeReq = strlenW(curDir) + 1;
277     destDirSizeReq = strlenW(destDir) + 1;
278
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! */
282
283     if(lpuDestDirLen && lpszDestDir)
284     {
285         if (*lpuDestDirLen < destDirSizeReq) retval |= VFF_BUFFTOOSMALL;
286         lstrcpynW(lpszDestDir, destDir, *lpuDestDirLen);
287         *lpuDestDirLen = destDirSizeReq;
288     }
289     if(lpuCurDirLen && lpszCurDir)
290     {
291         if(*lpuCurDirLen < curDirSizeReq) retval |= VFF_BUFFTOOSMALL;
292         lstrcpynW(lpszCurDir, curDir, *lpuCurDirLen);
293         *lpuCurDirLen = curDirSizeReq;
294     }
295
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));
301     return retval;
302 }
303
304 static LPBYTE
305 _fetch_versioninfo(LPSTR fn,VS_FIXEDFILEINFO **vffi) {
306     DWORD       alloclen;
307     LPBYTE      buf;
308     DWORD       ret;
309
310     alloclen = 1000;
311     buf=HeapAlloc(GetProcessHeap(), 0, alloclen);
312     if(buf == NULL) {
313         WARN("Memory exausted while fetching version info!\n");
314         return NULL;
315     }
316     while (1) {
317         ret = GetFileVersionInfoA(fn,0,alloclen,buf);
318         if (!ret) {
319             HeapFree(GetProcessHeap(), 0, buf);
320             return NULL;
321         }
322         if (alloclen<*(WORD*)buf) {
323             alloclen = *(WORD*)buf;
324             HeapFree(GetProcessHeap(), 0, buf);
325             buf = HeapAlloc(GetProcessHeap(), 0, alloclen);
326             if(buf == NULL) {
327                WARN("Memory exausted while fetching version info!\n");
328                return NULL;
329             }
330         } else {
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);
336             return buf;
337         }
338     }
339 }
340
341 static DWORD
342 _error2vif(DWORD error) {
343     switch (error) {
344     case ERROR_ACCESS_DENIED:
345         return VIF_ACCESSVIOLATION;
346     case ERROR_SHARING_VIOLATION:
347         return VIF_SHARINGVIOLATION;
348     default:
349         return 0;
350     }
351 }
352
353
354 /******************************************************************************
355  * VerInstallFileA [VERSION.@]
356  */
357 DWORD WINAPI VerInstallFileA(
358         UINT flags,LPCSTR srcfilename,LPCSTR destfilename,LPCSTR srcdir,
359         LPCSTR destdir,LPCSTR curdir,LPSTR tmpfile,UINT *tmpfilelen )
360 {
361     LPCSTR pdest;
362     char        destfn[260],tmpfn[260],srcfn[260];
363     HFILE       hfsrc,hfdst;
364     DWORD       attr,ret,xret,tmplast;
365     LPBYTE      buf1,buf2;
366     OFSTRUCT    ofs;
367
368     TRACE("(%x,%s,%s,%s,%s,%s,%p,%d)\n",
369             flags,srcfilename,destfilename,srcdir,destdir,curdir,tmpfile,*tmpfilelen
370     );
371     xret = 0;
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);
377     if (hfsrc < 0)
378         return VIF_CANNOTREADSRC;
379     sprintf(tmpfn,"%s\\%s",pdest,destfilename);
380     tmplast=strlen(pdest)+1;
381     attr = GetFileAttributesA(tmpfn);
382     if (attr!=-1) {
383         if (attr & FILE_ATTRIBUTE_READONLY) {
384             LZClose(hfsrc);
385             return VIF_WRITEPROT;
386         }
387         /* FIXME: check if file currently in use and return VIF_FILEINUSE */
388     }
389     attr = -1;
390     if (flags & VIFF_FORCEINSTALL) {
391         if (tmpfile[0]) {
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...
397              */
398         }
399     }
400     if (attr == -1) {
401         char    *s;
402
403         GetTempFileNameA(pdest,"ver",0,tmpfn); /* should not fail ... */
404         s=strrchr(tmpfn,'\\');
405         if (s)
406             tmplast = s-tmpfn;
407         else
408             tmplast = 0;
409         hfdst = OpenFile(tmpfn,&ofs,OF_CREATE);
410         if (hfdst == HFILE_ERROR) {
411             LZClose(hfsrc);
412             return VIF_CANNOTCREATE; /* | translated dos error */
413         }
414         ret = LZCopy(hfsrc,hfdst);
415         _lclose(hfdst);
416         if (((long) ret) < 0) {
417             /* translate LZ errors into VIF_xxx */
418             switch (ret) {
419             case LZERROR_BADINHANDLE:
420             case LZERROR_READ:
421             case LZERROR_BADVALUE:
422             case LZERROR_UNKNOWNALG:
423                 ret = VIF_CANNOTREADSRC;
424                 break;
425             case LZERROR_BADOUTHANDLE:
426             case LZERROR_WRITE:
427                 ret = VIF_OUTOFSPACE;
428                 break;
429             case LZERROR_GLOBALLOC:
430             case LZERROR_GLOBLOCK:
431                 ret = VIF_OUTOFMEMORY;
432                 break;
433             default: /* unknown error, should not happen */
434                 ret = 0;
435                 break;
436             }
437             if (ret) {
438                 LZClose(hfsrc);
439                 return ret;
440             }
441         }
442     }
443     xret = 0;
444     if (!(flags & VIFF_FORCEINSTALL)) {
445         VS_FIXEDFILEINFO *destvffi,*tmpvffi;
446         buf1 = _fetch_versioninfo(destfn,&destvffi);
447         if (buf1) {
448             buf2 = _fetch_versioninfo(tmpfn,&tmpvffi);
449             if (buf2) {
450                 char    *tbuf1,*tbuf2;
451                 UINT    len1,len2;
452
453                 len1=len2=40;
454
455                 /* compare file versions */
456                 if ((destvffi->dwFileVersionMS > tmpvffi->dwFileVersionMS)||
457                     ((destvffi->dwFileVersionMS==tmpvffi->dwFileVersionMS)&&
458                      (destvffi->dwFileVersionLS > tmpvffi->dwFileVersionLS)
459                     )
460                 )
461                     xret |= VIF_MISMATCH|VIF_SRCOLD;
462                 /* compare filetypes and filesubtypes */
463                 if ((destvffi->dwFileType!=tmpvffi->dwFileType) ||
464                     (destvffi->dwFileSubtype!=tmpvffi->dwFileSubtype)
465                 )
466                     xret |= VIF_MISMATCH|VIF_DIFFTYPE;
467                 if (VerQueryValueA(buf1,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf1,&len1) &&
468                     VerQueryValueA(buf2,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf2,&len2)
469                 ) {
470                     /* irgendwas mit tbuf1 und tbuf2 machen
471                      * generiert DIFFLANG|MISMATCH
472                      */
473                 }
474                 HeapFree(GetProcessHeap(), 0, buf2);
475             } else
476                 xret=VIF_MISMATCH|VIF_SRCOLD;
477             HeapFree(GetProcessHeap(), 0, buf1);
478         }
479     }
480     if (xret) {
481         if (*tmpfilelen<strlen(tmpfn+tmplast)) {
482             xret|=VIF_BUFFTOOSMALL;
483             DeleteFileA(tmpfn);
484         } else {
485             strcpy(tmpfile,tmpfn+tmplast);
486             *tmpfilelen = strlen(tmpfn+tmplast)+1;
487             xret|=VIF_TEMPFILE;
488         }
489     } else {
490         if (-1!=GetFileAttributesA(destfn))
491             if (!DeleteFileA(destfn)) {
492                 xret|=_error2vif(GetLastError())|VIF_CANNOTDELETE;
493                 DeleteFileA(tmpfn);
494                 LZClose(hfsrc);
495                 return xret;
496             }
497         if ((!(flags & VIFF_DONTDELETEOLD))     &&
498             curdir                              &&
499             *curdir                             &&
500             lstrcmpiA(curdir,pdest)
501         ) {
502             char curfn[260];
503
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;
509             }
510         }
511         if (!MoveFileA(tmpfn,destfn)) {
512             xret|=_error2vif(GetLastError())|VIF_CANNOTRENAME;
513             DeleteFileA(tmpfn);
514         }
515     }
516     LZClose(hfsrc);
517     return xret;
518 }
519
520
521 /******************************************************************************
522  * VerInstallFileW                              [VERSION.@]
523  */
524 DWORD WINAPI VerInstallFileW(
525         UINT flags,LPCWSTR srcfilename,LPCWSTR destfilename,LPCWSTR srcdir,
526         LPCWSTR destdir,LPCWSTR curdir,LPWSTR tmpfile,UINT *tmpfilelen )
527 {
528     LPSTR wsrcf = NULL, wsrcd = NULL, wdestf = NULL, wdestd = NULL, wtmpf = NULL, wcurd = NULL;
529     DWORD ret;
530     UINT len;
531
532     if (srcfilename)
533     {
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 );
537     }
538     if (srcdir)
539     {
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 );
543     }
544     if (destfilename)
545     {
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 );
549     }
550     if (destdir)
551     {
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 );
555     }
556     if (curdir)
557     {
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 );
561     }
562     len = *tmpfilelen * sizeof(WCHAR);
563     wtmpf = HeapAlloc( GetProcessHeap(), 0, len );
564     ret = VerInstallFileA(flags,wsrcf,wdestf,wsrcd,wdestd,wcurd,wtmpf,&len);
565     if (!ret)
566         *tmpfilelen = MultiByteToWideChar( CP_ACP, 0, wtmpf, -1, tmpfile, *tmpfilelen );
567     else if (ret & VIF_BUFFTOOSMALL)
568         *tmpfilelen = len;  /* FIXME: not correct */
569
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 );
576     return ret;
577 }
578