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