- New implementation of SendMessage, ReceiveMessage, ReplyMessage functions
[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
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "windef.h"
12 #include "winver.h"
13 #include "wine/winestring.h"
14 #include "winerror.h"
15 #include "heap.h"
16 #include "ver.h"
17 #include "lzexpand.h"
18 #include "xmalloc.h"
19 #include "debug.h"
20
21
22 /******************************************************************************
23  *
24  *   void  ver_dstring(
25  *      char const * prologue,
26  *      char const * teststring,
27  *      char const * epilogue )
28  *
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.
33  *
34  *   Revision history
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
42  *         dprintf[_]ver call.
43  *
44  *****************************************************************************/
45
46 static void  ver_dstring(
47     char const * prologue,
48     char const * teststring,
49     char const * epilogue )
50 {
51     TRACE(ver, "%s %p (\"%s\") %s\n", prologue,
52                 (void const *) teststring,
53                 teststring ? teststring : "(null)",
54                 epilogue);
55 }
56
57 /******************************************************************************
58  *
59  *   int  testFileExistence(
60  *      char const * path,
61  *      char const * file )
62  *
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
65  *   value is non-zero.
66  *
67  *   Revision history
68  *      30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
69  *         Original implementation
70  *
71  *****************************************************************************/
72
73 static int  testFileExistence(
74    char const * path,
75    char const * file )
76 {
77     char  filename[1024];
78     int  filenamelen;
79     OFSTRUCT  fileinfo;
80     int  retval;
81
82     fileinfo.cBytes = sizeof(OFSTRUCT);
83
84     strcpy(filename, path);
85     filenamelen = strlen(filename);
86
87     /* Add a trailing \ if necessary */
88     if(filenamelen) {
89         if(filename[filenamelen - 1] != '\\')
90             strcat(filename, "\\");
91     }
92     else /* specify the current directory */
93         strcpy(filename, ".\\");
94
95     /* Create the full pathname */
96     strcat(filename, file);
97
98     if(OpenFile32(filename, &fileinfo, OF_EXIST) == HFILE_ERROR32)
99         retval = 0;
100     else
101         retval = 1;
102
103     return  retval;
104 }
105
106 /******************************************************************************
107  *
108  *   int  testFileExclusiveExistence(
109  *      char const * path,
110  *      char const * file )
111  *
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.
116  *
117  *   Revision history
118  *      30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
119  *         Original implementation
120  *
121  *****************************************************************************/
122
123 static int  testFileExclusiveExistence(
124    char const * path,
125    char const * file )
126 {
127     char  filename[1024];
128     int  filenamelen;
129     OFSTRUCT  fileinfo;
130     int  retval;
131
132     fileinfo.cBytes = sizeof(OFSTRUCT);
133
134     strcpy(filename, path);
135     filenamelen = strlen(filename);
136
137     /* Add a trailing \ if necessary */
138     if(filenamelen) {
139         if(filename[filenamelen - 1] != '\\')
140             strcat(filename, "\\");
141     }
142     else /* specify the current directory */
143         strcpy(filename, ".\\");
144
145     /* Create the full pathname */
146     strcat(filename, file);
147
148     if(OpenFile32(filename, &fileinfo, OF_EXIST | OF_SHARE_EXCLUSIVE) ==
149        HFILE_ERROR32)
150         retval = 0;
151     else
152         retval = 1;
153
154     return retval;
155 }
156
157 /*****************************************************************************
158  *
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.
163  *
164  *   Revision history:
165  *      30-May-1997   Dave Cuthbert (dacut@ece.cmu.edu)
166  *         Reimplementation of VerFindFile from original stub.
167  *
168  ****************************************************************************/
169
170 /* VerFindFile32A                                               [VERSION.5] */
171 DWORD WINAPI VerFindFile32A(
172     UINT32 flags,
173     LPCSTR lpszFilename,
174     LPCSTR lpszWinDir,
175     LPCSTR lpszAppDir,
176     LPSTR lpszCurDir,
177     UINT32 *lpuCurDirLen,
178     LPSTR lpszDestDir,
179     UINT32 *lpuDestDirLen )
180 {
181     DWORD  retval;
182     char  curDir[256];
183     char  destDir[256];
184     unsigned int  curDirSizeReq;
185     unsigned int  destDirSizeReq;
186
187     retval = 0;
188
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");
194     else
195         TRACE(ver, "\n");
196
197     ver_dstring("\tlpszFilename = ", lpszFilename, "");
198     ver_dstring("\tlpszWinDir = ", lpszWinDir, "");
199     ver_dstring("\tlpszAppDir = ", lpszAppDir, "");
200
201     TRACE(ver, "\tlpszCurDir = %p\n", lpszCurDir);
202     if(lpuCurDirLen)
203         TRACE(ver, "\tlpuCurDirLen = %p (%u)\n",
204                     lpuCurDirLen, *lpuCurDirLen);
205     else
206         TRACE(ver, "\tlpuCurDirLen = (null)\n");
207
208     TRACE(ver, "\tlpszDestDir = %p\n", lpszDestDir);
209     if(lpuDestDirLen)
210         TRACE(ver, "\tlpuDestDirLen = %p (%u)\n",
211                     lpuDestDirLen, *lpuDestDirLen);
212
213     /* Figure out where the file should go; shared files default to the
214        system directory */
215
216     strcpy(curDir, "");
217     strcpy(destDir, "");
218
219     if(flags & VFFF_ISSHAREDFILE) {
220         GetSystemDirectory32A(destDir, 256);
221
222         /* Were we given a filename?  If so, try to find the file. */
223         if(lpszFilename) {
224             if(testFileExistence(destDir, lpszFilename)) {
225                 strcpy(curDir, destDir);
226
227                 if(!testFileExclusiveExistence(destDir, lpszFilename))
228                     retval |= VFF_FILEINUSE;
229             }
230             else if(lpszAppDir && testFileExistence(lpszAppDir,
231                                                     lpszFilename)) {
232                 strcpy(curDir, lpszAppDir);
233                 retval |= VFF_CURNEDEST;
234
235                 if(!testFileExclusiveExistence(lpszAppDir, lpszFilename))
236                     retval |= VFF_FILEINUSE;
237             }
238         }
239     }
240     else if(!(flags & VFFF_ISSHAREDFILE)) { /* not a shared file */
241         if(lpszAppDir) {
242             char  systemDir[256];
243             GetSystemDirectory32A(systemDir, 256);
244
245             strcpy(destDir, lpszAppDir);
246
247             if(lpszFilename) {
248                 if(testFileExistence(lpszAppDir, lpszFilename)) {
249                     strcpy(curDir, lpszAppDir);
250
251                     if(!testFileExclusiveExistence(lpszAppDir, lpszFilename))
252                         retval |= VFF_FILEINUSE;
253                 }
254                 else if(testFileExistence(systemDir, lpszFilename)) {
255                     strcpy(curDir, systemDir);
256                     retval |= VFF_CURNEDEST;
257
258                     if(!testFileExclusiveExistence(systemDir, lpszFilename))
259                         retval |= VFF_FILEINUSE;
260                 }
261             }
262         }
263     }
264
265     curDirSizeReq = strlen(curDir) + 1;
266     destDirSizeReq = strlen(destDir) + 1;
267
268
269
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! */
273
274     if(lpuDestDirLen && lpszDestDir) {
275         if(*lpuDestDirLen < destDirSizeReq) {
276             retval |= VFF_BUFFTOOSMALL;
277             strncpy(lpszDestDir, destDir, *lpuDestDirLen - 1);
278             lpszDestDir[*lpuDestDirLen - 1] = '\0';
279         }
280         else
281             strcpy(lpszDestDir, destDir);
282
283         *lpuDestDirLen = destDirSizeReq;
284     }
285     
286     if(lpuCurDirLen && lpszCurDir) {
287         if(*lpuCurDirLen < curDirSizeReq) {
288             retval |= VFF_BUFFTOOSMALL;
289             strncpy(lpszCurDir, curDir, *lpuCurDirLen - 1);
290             lpszCurDir[*lpuCurDirLen - 1] = '\0';
291         }
292         else
293             strcpy(lpszCurDir, curDir);
294
295         *lpuCurDirLen = curDirSizeReq;
296     }
297
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 " : "");
302
303     ver_dstring("\t(Exit) lpszCurDir = ", lpszCurDir, "");
304     if(lpuCurDirLen)
305         TRACE(ver, "\t(Exit) lpuCurDirLen = %p (%u)\n",
306                     lpuCurDirLen, *lpuCurDirLen);
307     else
308         TRACE(ver, "\t(Exit) lpuCurDirLen = (null)\n");
309
310     ver_dstring("\t(Exit) lpszDestDir = ", lpszDestDir, "");
311     if(lpuDestDirLen)
312         TRACE(ver, "\t(Exit) lpuDestDirLen = %p (%u)\n",
313                     lpuDestDirLen, *lpuDestDirLen);
314
315     return retval;
316 }
317
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 )
322 {
323     UINT32 curdirlen, destdirlen;
324     LPSTR wfn,wwd,wad,wdd,wcd;
325     DWORD ret;
326
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 );
342     return ret;
343 }
344
345 static LPBYTE
346 _fetch_versioninfo(LPSTR fn,VS_FIXEDFILEINFO **vffi) {
347     DWORD       alloclen;
348     LPBYTE      buf;
349     DWORD       ret;
350
351     alloclen = 1000;
352     buf= xmalloc(alloclen);
353     while (1) {
354         ret = GetFileVersionInfo32A(fn,0,alloclen,buf);
355         if (!ret) {
356             free(buf);
357             return 0;
358         }
359         if (alloclen<*(WORD*)buf) {
360             free(buf);
361             alloclen = *(WORD*)buf;
362             buf = xmalloc(alloclen);
363         } else {
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);
369             return buf;
370         }
371     }
372 }
373
374 static DWORD
375 _error2vif(DWORD error) {
376     switch (error) {
377     case ERROR_ACCESS_DENIED:
378         return VIF_ACCESSVIOLATION;
379     case ERROR_SHARING_VIOLATION:
380         return VIF_SHARINGVIOLATION;
381     default:
382         return 0;
383     }
384 }
385
386
387 /******************************************************************************
388  * VerInstallFile32A [VERSION.7]
389  */
390 DWORD WINAPI VerInstallFile32A(
391         UINT32 flags,LPCSTR srcfilename,LPCSTR destfilename,LPCSTR srcdir,
392         LPCSTR destdir,LPCSTR curdir,LPSTR tmpfile,UINT32 *tmpfilelen )
393 {
394     LPCSTR pdest;
395     char        destfn[260],tmpfn[260],srcfn[260];
396     HFILE32     hfsrc,hfdst;
397     DWORD       attr,ret,xret,tmplast;
398     LPBYTE      buf1,buf2;
399     OFSTRUCT    ofs;
400
401     TRACE(ver,"(%x,%s,%s,%s,%s,%s,%p,%d)\n",
402             flags,srcfilename,destfilename,srcdir,destdir,curdir,tmpfile,*tmpfilelen
403     );
404     xret = 0;
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);
415     if (attr!=-1) {
416         if (attr & FILE_ATTRIBUTE_READONLY) {
417             LZClose32(hfsrc);
418             return VIF_WRITEPROT;
419         }
420         /* FIXME: check if file currently in use and return VIF_FILEINUSE */
421     }
422     attr = -1;
423     if (flags & VIFF_FORCEINSTALL) {
424         if (tmpfile[0]) {
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... 
430              */
431         }
432     }
433     if (attr == -1) {
434         char    *s;
435
436         GetTempFileName32A(pdest,"ver",0,tmpfn); /* should not fail ... */
437         s=strrchr(tmpfn,'\\');
438         if (s)
439             tmplast = s-tmpfn;
440         else
441             tmplast = 0;
442         hfdst = OpenFile32(tmpfn,&ofs,OF_CREATE);
443         if (hfdst == HFILE_ERROR32) {
444             LZClose32(hfsrc);
445             return VIF_CANNOTCREATE; /* | translated dos error */
446         }
447         ret = LZCopy32(hfsrc,hfdst);
448         _lclose32(hfdst);
449         if (((long) ret) < 0) {
450             /* translate LZ errors into VIF_xxx */
451             switch (ret) {
452             case LZERROR_BADINHANDLE:
453             case LZERROR_READ:
454             case LZERROR_BADVALUE:
455             case LZERROR_UNKNOWNALG:
456                 ret = VIF_CANNOTREADSRC;
457                 break;
458             case LZERROR_BADOUTHANDLE:
459             case LZERROR_WRITE:
460                 ret = VIF_OUTOFMEMORY; /* FIXME: correct? */
461                 break;
462             case LZERROR_GLOBALLOC:
463             case LZERROR_GLOBLOCK:
464                 ret = VIF_OUTOFSPACE;
465                 break;
466             default: /* unknown error, should not happen */
467                 ret = 0;
468                 break;
469             }
470             if (ret) {
471                 LZClose32(hfsrc);
472                 return ret;
473             }
474         }
475     }
476     xret = 0;
477     if (!(flags & VIFF_FORCEINSTALL)) {
478         VS_FIXEDFILEINFO *destvffi,*tmpvffi;
479         buf1 = _fetch_versioninfo(destfn,&destvffi);
480         if (buf1) {
481             buf2 = _fetch_versioninfo(tmpfn,&tmpvffi);
482             if (buf2) {
483                 char    *tbuf1,*tbuf2;
484                 UINT32  len1,len2;
485
486                 len1=len2=40;
487
488                 /* compare file versions */
489                 if ((destvffi->dwFileVersionMS > tmpvffi->dwFileVersionMS)||
490                     ((destvffi->dwFileVersionMS==tmpvffi->dwFileVersionMS)&&
491                      (destvffi->dwFileVersionLS > tmpvffi->dwFileVersionLS)
492                     )
493                 )
494                     xret |= VIF_MISMATCH|VIF_SRCOLD;
495                 /* compare filetypes and filesubtypes */
496                 if ((destvffi->dwFileType!=tmpvffi->dwFileType) ||
497                     (destvffi->dwFileSubtype!=tmpvffi->dwFileSubtype)
498                 )
499                     xret |= VIF_MISMATCH|VIF_DIFFTYPE;
500                 if (VerQueryValue32A(buf1,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf1,&len1) &&
501                     VerQueryValue32A(buf2,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf2,&len2)
502                 ) {
503                     /* irgendwas mit tbuf1 und tbuf2 machen 
504                      * generiert DIFFLANG|MISMATCH
505                      */
506                 }
507                 free(buf2);
508             } else
509                 xret=VIF_MISMATCH|VIF_SRCOLD;
510             free(buf1);
511         }
512     }
513     if (xret) {
514         if (*tmpfilelen<strlen(tmpfn+tmplast)) {
515             xret|=VIF_BUFFTOOSMALL;
516             DeleteFile32A(tmpfn);
517         } else {
518             strcpy(tmpfile,tmpfn+tmplast);
519             *tmpfilelen = strlen(tmpfn+tmplast)+1;
520             xret|=VIF_TEMPFILE;
521         }
522     } else {
523         if (-1!=GetFileAttributes32A(destfn))
524             if (!DeleteFile32A(destfn)) {
525                 xret|=_error2vif(GetLastError())|VIF_CANNOTDELETE;
526                 DeleteFile32A(tmpfn);
527                 LZClose32(hfsrc);
528                 return xret;
529             }
530         if ((!(flags & VIFF_DONTDELETEOLD))     && 
531             curdir                              && 
532             *curdir                             &&
533             lstrcmpi32A(curdir,pdest)
534         ) {
535             char curfn[260];
536
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;
542             }
543         }
544         if (!MoveFile32A(tmpfn,destfn)) {
545             xret|=_error2vif(GetLastError())|VIF_CANNOTRENAME;
546             DeleteFile32A(tmpfn);
547         }
548     }
549     LZClose32(hfsrc);
550     return xret;
551 }
552
553
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 )
558 {
559     LPSTR wsrcf,wsrcd,wdestf,wdestd,wtmpf,wcurd;
560     DWORD ret;
561
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);
569     if (!ret)
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 );
576     if (wcurd) 
577         HeapFree( GetProcessHeap(), 0, wcurd );
578     return ret;
579 }
580