No longer directly accessing debuggee memory.
[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 <stdio.h>
10 #include <string.h>
11
12 #include "windef.h"
13 #include "winver.h"
14 #include "wine/winestring.h"
15 #include "winerror.h"
16 #include "heap.h"
17 #include "lzexpand.h"
18 #include "xmalloc.h"
19 #include "debugtools.h"
20
21 DEFAULT_DEBUG_CHANNEL(ver);
22
23
24 /******************************************************************************
25  *
26  *   void  ver_dstring(
27  *      char const * prologue,
28  *      char const * teststring,
29  *      char const * epilogue )
30  *
31  *   This function will print via dprintf[_]ver to stddeb the prologue string,
32  *   followed by the address of teststring and the string it contains if
33  *   teststring is non-null or "(null)" otherwise, and then the epilogue
34  *   string followed by a new line.
35  *
36  *   Revision history
37  *      30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
38  *         Original implementation as dprintf[_]ver_string
39  *      05-Jul-1997 Dave Cuthbert (dacut@ece.cmu.edu)
40  *         Fixed problem that caused bug with tools/make_debug -- renaming
41  *         this function should fix the problem.
42  *      15-Feb-1998 Dimitrie Paun (dimi@cs.toronto.edu)
43  *         Modified it to make it print the message using only one
44  *         dprintf[_]ver call.
45  *
46  *****************************************************************************/
47
48 static void  ver_dstring(
49     char const * prologue,
50     char const * teststring,
51     char const * epilogue )
52 {
53     TRACE("%s %p (\"%s\") %s\n", prologue,
54                 (void const *) teststring,
55                 teststring ? teststring : "(null)",
56                 epilogue);
57 }
58
59 /******************************************************************************
60  *
61  *   int  testFileExistence(
62  *      char const * path,
63  *      char const * file )
64  *
65  *   Tests whether a given path/file combination exists.  If the file does
66  *   not exist, the return value is zero.  If it does exist, the return
67  *   value is non-zero.
68  *
69  *   Revision history
70  *      30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
71  *         Original implementation
72  *
73  *****************************************************************************/
74
75 static int  testFileExistence(
76    char const * path,
77    char const * file )
78 {
79     char  filename[1024];
80     int  filenamelen;
81     OFSTRUCT  fileinfo;
82     int  retval;
83
84     fileinfo.cBytes = sizeof(OFSTRUCT);
85
86     strcpy(filename, path);
87     filenamelen = strlen(filename);
88
89     /* Add a trailing \ if necessary */
90     if(filenamelen) {
91         if(filename[filenamelen - 1] != '\\')
92             strcat(filename, "\\");
93     }
94     else /* specify the current directory */
95         strcpy(filename, ".\\");
96
97     /* Create the full pathname */
98     strcat(filename, file);
99
100     if(OpenFile(filename, &fileinfo, OF_EXIST) == HFILE_ERROR)
101         retval = 0;
102     else
103         retval = 1;
104
105     return  retval;
106 }
107
108 /******************************************************************************
109  *
110  *   int  testFileExclusiveExistence(
111  *      char const * path,
112  *      char const * file )
113  *
114  *   Tests whether a given path/file combination exists and ensures that no
115  *   other programs have handles to the given file.  If the file does not
116  *   exist or is open, the return value is zero.  If it does exist, the
117  *   return value is non-zero.
118  *
119  *   Revision history
120  *      30-May-1997 Dave Cuthbert (dacut@ece.cmu.edu)
121  *         Original implementation
122  *
123  *****************************************************************************/
124
125 static int  testFileExclusiveExistence(
126    char const * path,
127    char const * file )
128 {
129     char  filename[1024];
130     int  filenamelen;
131     OFSTRUCT  fileinfo;
132     int  retval;
133
134     fileinfo.cBytes = sizeof(OFSTRUCT);
135
136     strcpy(filename, path);
137     filenamelen = strlen(filename);
138
139     /* Add a trailing \ if necessary */
140     if(filenamelen) {
141         if(filename[filenamelen - 1] != '\\')
142             strcat(filename, "\\");
143     }
144     else /* specify the current directory */
145         strcpy(filename, ".\\");
146
147     /* Create the full pathname */
148     strcat(filename, file);
149
150     if(OpenFile(filename, &fileinfo, OF_EXIST | OF_SHARE_EXCLUSIVE) ==
151        HFILE_ERROR)
152         retval = 0;
153     else
154         retval = 1;
155
156     return retval;
157 }
158
159 /*****************************************************************************
160  *
161  *   VerFindFile() [VER.8]
162  *   Determines where to install a file based on whether it locates another
163  *   version of the file in the system.  The values VerFindFile returns are
164  *   used in a subsequent call to the VerInstallFile function.
165  *
166  *   Revision history:
167  *      30-May-1997   Dave Cuthbert (dacut@ece.cmu.edu)
168  *         Reimplementation of VerFindFile from original stub.
169  *
170  ****************************************************************************/
171
172 /* VerFindFile32A                                               [VERSION.5] */
173 DWORD WINAPI VerFindFileA(
174     UINT flags,
175     LPCSTR lpszFilename,
176     LPCSTR lpszWinDir,
177     LPCSTR lpszAppDir,
178     LPSTR lpszCurDir,
179     UINT *lpuCurDirLen,
180     LPSTR lpszDestDir,
181     UINT *lpuDestDirLen )
182 {
183     DWORD  retval;
184     char  curDir[256];
185     char  destDir[256];
186     unsigned int  curDirSizeReq;
187     unsigned int  destDirSizeReq;
188
189     retval = 0;
190
191     /* Print out debugging information */
192     TRACE("called with parameters:\n"
193                  "\tflags = %x", flags);
194     if(flags & VFFF_ISSHAREDFILE)
195         TRACE(" (VFFF_ISSHAREDFILE)\n");
196     else
197         TRACE("\n");
198
199     ver_dstring("\tlpszFilename = ", lpszFilename, "");
200     ver_dstring("\tlpszWinDir = ", lpszWinDir, "");
201     ver_dstring("\tlpszAppDir = ", lpszAppDir, "");
202
203     TRACE("\tlpszCurDir = %p\n", lpszCurDir);
204     if(lpuCurDirLen)
205         TRACE("\tlpuCurDirLen = %p (%u)\n",
206                     lpuCurDirLen, *lpuCurDirLen);
207     else
208         TRACE("\tlpuCurDirLen = (null)\n");
209
210     TRACE("\tlpszDestDir = %p\n", lpszDestDir);
211     if(lpuDestDirLen)
212         TRACE("\tlpuDestDirLen = %p (%u)\n",
213                     lpuDestDirLen, *lpuDestDirLen);
214
215     /* Figure out where the file should go; shared files default to the
216        system directory */
217
218     strcpy(curDir, "");
219     strcpy(destDir, "");
220
221     if(flags & VFFF_ISSHAREDFILE) {
222         GetSystemDirectoryA(destDir, 256);
223
224         /* Were we given a filename?  If so, try to find the file. */
225         if(lpszFilename) {
226             if(testFileExistence(destDir, lpszFilename)) {
227                 strcpy(curDir, destDir);
228
229                 if(!testFileExclusiveExistence(destDir, lpszFilename))
230                     retval |= VFF_FILEINUSE;
231             }
232             else if(lpszAppDir && testFileExistence(lpszAppDir,
233                                                     lpszFilename)) {
234                 strcpy(curDir, lpszAppDir);
235                 retval |= VFF_CURNEDEST;
236
237                 if(!testFileExclusiveExistence(lpszAppDir, lpszFilename))
238                     retval |= VFF_FILEINUSE;
239             }
240         }
241     }
242     else if(!(flags & VFFF_ISSHAREDFILE)) { /* not a shared file */
243         if(lpszAppDir) {
244             char  systemDir[256];
245             GetSystemDirectoryA(systemDir, 256);
246
247             strcpy(destDir, lpszAppDir);
248
249             if(lpszFilename) {
250                 if(testFileExistence(lpszAppDir, lpszFilename)) {
251                     strcpy(curDir, lpszAppDir);
252
253                     if(!testFileExclusiveExistence(lpszAppDir, lpszFilename))
254                         retval |= VFF_FILEINUSE;
255                 }
256                 else if(testFileExistence(systemDir, lpszFilename)) {
257                     strcpy(curDir, systemDir);
258                     retval |= VFF_CURNEDEST;
259
260                     if(!testFileExclusiveExistence(systemDir, lpszFilename))
261                         retval |= VFF_FILEINUSE;
262                 }
263             }
264         }
265     }
266
267     curDirSizeReq = strlen(curDir) + 1;
268     destDirSizeReq = strlen(destDir) + 1;
269
270
271
272     /* Make sure that the pointers to the size of the buffers are
273        valid; if not, do NOTHING with that buffer.  If that pointer
274        is valid, then make sure that the buffer pointer is valid, too! */
275
276     if(lpuDestDirLen && lpszDestDir) {
277         if(*lpuDestDirLen < destDirSizeReq) {
278             retval |= VFF_BUFFTOOSMALL;
279             if (*lpuDestDirLen) {
280             lstrcpynA(lpszDestDir, destDir, *lpuDestDirLen);
281         }
282         }
283         else
284             strcpy(lpszDestDir, destDir);
285
286         *lpuDestDirLen = destDirSizeReq;
287     }
288     
289     if(lpuCurDirLen && lpszCurDir) {
290         if(*lpuCurDirLen < curDirSizeReq) {
291             retval |= VFF_BUFFTOOSMALL;
292             if (*lpuCurDirLen) {
293             lstrcpynA(lpszCurDir, curDir, *lpuCurDirLen);
294             }
295         }
296         else
297             strcpy(lpszCurDir, curDir);
298
299         *lpuCurDirLen = curDirSizeReq;
300     }
301
302     TRACE("ret = %lu (%s%s%s)\n", retval,
303                  (retval & VFF_CURNEDEST) ? "VFF_CURNEDEST " : "",
304                  (retval & VFF_FILEINUSE) ? "VFF_FILEINUSE " : "",
305                  (retval & VFF_BUFFTOOSMALL) ? "VFF_BUFFTOOSMALL " : "");
306
307     ver_dstring("\t(Exit) lpszCurDir = ", lpszCurDir, "");
308     if(lpuCurDirLen)
309         TRACE("\t(Exit) lpuCurDirLen = %p (%u)\n",
310                     lpuCurDirLen, *lpuCurDirLen);
311     else
312         TRACE("\t(Exit) lpuCurDirLen = (null)\n");
313
314     ver_dstring("\t(Exit) lpszDestDir = ", lpszDestDir, "");
315     if(lpuDestDirLen)
316         TRACE("\t(Exit) lpuDestDirLen = %p (%u)\n",
317                     lpuDestDirLen, *lpuDestDirLen);
318
319     return retval;
320 }
321
322 /* VerFindFile32W                                               [VERSION.6] */
323 DWORD WINAPI VerFindFileW(
324         UINT flags,LPCWSTR filename,LPCWSTR windir,LPCWSTR appdir,
325         LPWSTR curdir,UINT *pcurdirlen,LPWSTR destdir,UINT *pdestdirlen )
326 {
327     UINT curdirlen, destdirlen;
328     LPSTR wfn,wwd,wad,wdd,wcd;
329     DWORD ret;
330
331     wfn = HEAP_strdupWtoA( GetProcessHeap(), 0, filename );
332     wwd = HEAP_strdupWtoA( GetProcessHeap(), 0, windir );
333     wad = HEAP_strdupWtoA( GetProcessHeap(), 0, appdir );
334     wcd = HeapAlloc( GetProcessHeap(), 0, *pcurdirlen );
335     wdd = HeapAlloc( GetProcessHeap(), 0, *pdestdirlen );
336     ret = VerFindFileA(flags,wfn,wwd,wad,wcd,&curdirlen,wdd,&destdirlen);
337     lstrcpynAtoW(curdir,wcd,*pcurdirlen);
338     lstrcpynAtoW(destdir,wdd,*pdestdirlen);
339     *pcurdirlen = strlen(wcd);
340     *pdestdirlen = strlen(wdd);
341     HeapFree( GetProcessHeap(), 0, wfn );
342     HeapFree( GetProcessHeap(), 0, wwd );
343     HeapFree( GetProcessHeap(), 0, wad );
344     HeapFree( GetProcessHeap(), 0, wcd );
345     HeapFree( GetProcessHeap(), 0, wdd );
346     return ret;
347 }
348
349 static LPBYTE
350 _fetch_versioninfo(LPSTR fn,VS_FIXEDFILEINFO **vffi) {
351     DWORD       alloclen;
352     LPBYTE      buf;
353     DWORD       ret;
354
355     alloclen = 1000;
356     buf= xmalloc(alloclen);
357     while (1) {
358         ret = GetFileVersionInfoA(fn,0,alloclen,buf);
359         if (!ret) {
360             free(buf);
361             return 0;
362         }
363         if (alloclen<*(WORD*)buf) {
364             free(buf);
365             alloclen = *(WORD*)buf;
366             buf = xmalloc(alloclen);
367         } else {
368             *vffi = (VS_FIXEDFILEINFO*)(buf+0x14);
369             if ((*vffi)->dwSignature == 0x004f0049) /* hack to detect unicode */
370                 *vffi = (VS_FIXEDFILEINFO*)(buf+0x28);
371             if ((*vffi)->dwSignature != VS_FFI_SIGNATURE)
372                 WARN("Bad VS_FIXEDFILEINFO signature 0x%08lx\n",(*vffi)->dwSignature);
373             return buf;
374         }
375     }
376 }
377
378 static DWORD
379 _error2vif(DWORD error) {
380     switch (error) {
381     case ERROR_ACCESS_DENIED:
382         return VIF_ACCESSVIOLATION;
383     case ERROR_SHARING_VIOLATION:
384         return VIF_SHARINGVIOLATION;
385     default:
386         return 0;
387     }
388 }
389
390
391 /******************************************************************************
392  * VerInstallFile32A [VERSION.7]
393  */
394 DWORD WINAPI VerInstallFileA(
395         UINT flags,LPCSTR srcfilename,LPCSTR destfilename,LPCSTR srcdir,
396         LPCSTR destdir,LPCSTR curdir,LPSTR tmpfile,UINT *tmpfilelen )
397 {
398     LPCSTR pdest;
399     char        destfn[260],tmpfn[260],srcfn[260];
400     HFILE       hfsrc,hfdst;
401     DWORD       attr,ret,xret,tmplast;
402     LPBYTE      buf1,buf2;
403     OFSTRUCT    ofs;
404
405     TRACE("(%x,%s,%s,%s,%s,%s,%p,%d)\n",
406             flags,srcfilename,destfilename,srcdir,destdir,curdir,tmpfile,*tmpfilelen
407     );
408     xret = 0;
409     sprintf(srcfn,"%s\\%s",srcdir,srcfilename);
410     if (!destdir || !*destdir) pdest = srcdir;
411     else pdest = destdir;
412     sprintf(destfn,"%s\\%s",pdest,destfilename);
413     hfsrc=LZOpenFileA(srcfn,&ofs,OF_READ);
414     if (hfsrc==HFILE_ERROR)
415         return VIF_CANNOTREADSRC;
416     sprintf(tmpfn,"%s\\%s",pdest,destfilename);
417     tmplast=strlen(pdest)+1;
418     attr = GetFileAttributesA(tmpfn);
419     if (attr!=-1) {
420         if (attr & FILE_ATTRIBUTE_READONLY) {
421             LZClose(hfsrc);
422             return VIF_WRITEPROT;
423         }
424         /* FIXME: check if file currently in use and return VIF_FILEINUSE */
425     }
426     attr = -1;
427     if (flags & VIFF_FORCEINSTALL) {
428         if (tmpfile[0]) {
429             sprintf(tmpfn,"%s\\%s",pdest,tmpfile);
430             tmplast = strlen(pdest)+1;
431             attr = GetFileAttributesA(tmpfn);
432             /* if it exists, it has been copied by the call before.
433              * we jump over the copy part... 
434              */
435         }
436     }
437     if (attr == -1) {
438         char    *s;
439
440         GetTempFileNameA(pdest,"ver",0,tmpfn); /* should not fail ... */
441         s=strrchr(tmpfn,'\\');
442         if (s)
443             tmplast = s-tmpfn;
444         else
445             tmplast = 0;
446         hfdst = OpenFile(tmpfn,&ofs,OF_CREATE);
447         if (hfdst == HFILE_ERROR) {
448             LZClose(hfsrc);
449             return VIF_CANNOTCREATE; /* | translated dos error */
450         }
451         ret = LZCopy(hfsrc,hfdst);
452         _lclose(hfdst);
453         if (((long) ret) < 0) {
454             /* translate LZ errors into VIF_xxx */
455             switch (ret) {
456             case LZERROR_BADINHANDLE:
457             case LZERROR_READ:
458             case LZERROR_BADVALUE:
459             case LZERROR_UNKNOWNALG:
460                 ret = VIF_CANNOTREADSRC;
461                 break;
462             case LZERROR_BADOUTHANDLE:
463             case LZERROR_WRITE:
464                 ret = VIF_OUTOFMEMORY; /* FIXME: correct? */
465                 break;
466             case LZERROR_GLOBALLOC:
467             case LZERROR_GLOBLOCK:
468                 ret = VIF_OUTOFSPACE;
469                 break;
470             default: /* unknown error, should not happen */
471                 ret = 0;
472                 break;
473             }
474             if (ret) {
475                 LZClose(hfsrc);
476                 return ret;
477             }
478         }
479     }
480     xret = 0;
481     if (!(flags & VIFF_FORCEINSTALL)) {
482         VS_FIXEDFILEINFO *destvffi,*tmpvffi;
483         buf1 = _fetch_versioninfo(destfn,&destvffi);
484         if (buf1) {
485             buf2 = _fetch_versioninfo(tmpfn,&tmpvffi);
486             if (buf2) {
487                 char    *tbuf1,*tbuf2;
488                 UINT    len1,len2;
489
490                 len1=len2=40;
491
492                 /* compare file versions */
493                 if ((destvffi->dwFileVersionMS > tmpvffi->dwFileVersionMS)||
494                     ((destvffi->dwFileVersionMS==tmpvffi->dwFileVersionMS)&&
495                      (destvffi->dwFileVersionLS > tmpvffi->dwFileVersionLS)
496                     )
497                 )
498                     xret |= VIF_MISMATCH|VIF_SRCOLD;
499                 /* compare filetypes and filesubtypes */
500                 if ((destvffi->dwFileType!=tmpvffi->dwFileType) ||
501                     (destvffi->dwFileSubtype!=tmpvffi->dwFileSubtype)
502                 )
503                     xret |= VIF_MISMATCH|VIF_DIFFTYPE;
504                 if (VerQueryValueA(buf1,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf1,&len1) &&
505                     VerQueryValueA(buf2,"\\VarFileInfo\\Translation",(LPVOID*)&tbuf2,&len2)
506                 ) {
507                     /* irgendwas mit tbuf1 und tbuf2 machen 
508                      * generiert DIFFLANG|MISMATCH
509                      */
510                 }
511                 free(buf2);
512             } else
513                 xret=VIF_MISMATCH|VIF_SRCOLD;
514             free(buf1);
515         }
516     }
517     if (xret) {
518         if (*tmpfilelen<strlen(tmpfn+tmplast)) {
519             xret|=VIF_BUFFTOOSMALL;
520             DeleteFileA(tmpfn);
521         } else {
522             strcpy(tmpfile,tmpfn+tmplast);
523             *tmpfilelen = strlen(tmpfn+tmplast)+1;
524             xret|=VIF_TEMPFILE;
525         }
526     } else {
527         if (-1!=GetFileAttributesA(destfn))
528             if (!DeleteFileA(destfn)) {
529                 xret|=_error2vif(GetLastError())|VIF_CANNOTDELETE;
530                 DeleteFileA(tmpfn);
531                 LZClose(hfsrc);
532                 return xret;
533             }
534         if ((!(flags & VIFF_DONTDELETEOLD))     && 
535             curdir                              && 
536             *curdir                             &&
537             lstrcmpiA(curdir,pdest)
538         ) {
539             char curfn[260];
540
541             sprintf(curfn,"%s\\%s",curdir,destfilename);
542             if (-1!=GetFileAttributesA(curfn)) {
543                 /* FIXME: check if in use ... if it is, VIF_CANNOTDELETECUR */
544                 if (!DeleteFileA(curfn))
545                     xret|=_error2vif(GetLastError())|VIF_CANNOTDELETECUR;
546             }
547         }
548         if (!MoveFileA(tmpfn,destfn)) {
549             xret|=_error2vif(GetLastError())|VIF_CANNOTRENAME;
550             DeleteFileA(tmpfn);
551         }
552     }
553     LZClose(hfsrc);
554     return xret;
555 }
556
557
558 /* VerInstallFile32W                            [VERSION.8] */
559 DWORD WINAPI VerInstallFileW(
560         UINT flags,LPCWSTR srcfilename,LPCWSTR destfilename,LPCWSTR srcdir,
561         LPCWSTR destdir,LPCWSTR curdir,LPWSTR tmpfile,UINT *tmpfilelen )
562 {
563     LPSTR wsrcf,wsrcd,wdestf,wdestd,wtmpf,wcurd;
564     DWORD ret;
565
566     wsrcf  = HEAP_strdupWtoA( GetProcessHeap(), 0, srcfilename );
567     wsrcd  = HEAP_strdupWtoA( GetProcessHeap(), 0, srcdir );
568     wdestf = HEAP_strdupWtoA( GetProcessHeap(), 0, destfilename );
569     wdestd = HEAP_strdupWtoA( GetProcessHeap(), 0, destdir );
570     wtmpf  = HEAP_strdupWtoA( GetProcessHeap(), 0, tmpfile );
571     wcurd  = HEAP_strdupWtoA( GetProcessHeap(), 0, curdir );
572     ret = VerInstallFileA(flags,wsrcf,wdestf,wsrcd,wdestd,wcurd,wtmpf,tmpfilelen);
573     if (!ret)
574         lstrcpynAtoW(tmpfile,wtmpf,*tmpfilelen);
575     HeapFree( GetProcessHeap(), 0, wsrcf );
576     HeapFree( GetProcessHeap(), 0, wsrcd );
577     HeapFree( GetProcessHeap(), 0, wdestf );
578     HeapFree( GetProcessHeap(), 0, wdestd );
579     HeapFree( GetProcessHeap(), 0, wtmpf );
580     if (wcurd) 
581         HeapFree( GetProcessHeap(), 0, wcurd );
582     return ret;
583 }
584