Minor updates of Slovenian translations.
[wine/wine64.git] / dlls / setupapi / install.c
blobf6cf6007cf593445609a6c0644c9992756ba385f
1 /*
2 * Setupapi install routines
4 * Copyright 2002 Alexandre Julliard for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winternl.h"
24 #include "winerror.h"
25 #include "setupapi.h"
26 #include "wine/unicode.h"
27 #include "setupapi_private.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
32 /* info passed to callback functions dealing with files */
33 struct files_callback_info
35 HSPFILEQ queue;
36 PCWSTR src_root;
37 UINT copy_flags;
38 HINF layout;
41 /* info passed to callback functions dealing with the registry */
42 struct registry_callback_info
44 HKEY default_root;
45 BOOL delete;
48 typedef BOOL (*iterate_fields_func)( HINF hinf, PCWSTR field, void *arg );
50 /* Unicode constants */
51 static const WCHAR CopyFiles[] = {'C','o','p','y','F','i','l','e','s',0};
52 static const WCHAR DelFiles[] = {'D','e','l','F','i','l','e','s',0};
53 static const WCHAR RenFiles[] = {'R','e','n','F','i','l','e','s',0};
54 static const WCHAR Ini2Reg[] = {'I','n','i','2','R','e','g',0};
55 static const WCHAR LogConf[] = {'L','o','g','C','o','n','f',0};
56 static const WCHAR AddReg[] = {'A','d','d','R','e','g',0};
57 static const WCHAR DelReg[] = {'D','e','l','R','e','g',0};
58 static const WCHAR UpdateInis[] = {'U','p','d','a','t','e','I','n','i','s',0};
59 static const WCHAR UpdateIniFields[] = {'U','p','d','a','t','e','I','n','i','F','i','e','l','d','s',0};
62 /***********************************************************************
63 * get_field_string
65 * Retrieve the contents of a field, dynamically growing the buffer if necessary.
67 static WCHAR *get_field_string( INFCONTEXT *context, DWORD index, WCHAR *buffer,
68 WCHAR *static_buffer, DWORD *size )
70 DWORD required;
72 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
73 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
75 /* now grow the buffer */
76 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
77 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, required*sizeof(WCHAR) ))) return NULL;
78 *size = required;
79 if (SetupGetStringFieldW( context, index, buffer, *size, &required )) return buffer;
81 if (buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
82 return NULL;
86 /***********************************************************************
87 * copy_files_callback
89 * Called once for each CopyFiles entry in a given section.
91 static BOOL copy_files_callback( HINF hinf, PCWSTR field, void *arg )
93 struct files_callback_info *info = arg;
95 if (field[0] == '@') /* special case: copy single file */
96 SetupQueueDefaultCopyW( info->queue, info->layout, info->src_root, NULL, field, info->copy_flags );
97 else
98 SetupQueueCopySectionW( info->queue, info->src_root, info->layout, hinf, field, info->copy_flags );
99 return TRUE;
103 /***********************************************************************
104 * delete_files_callback
106 * Called once for each DelFiles entry in a given section.
108 static BOOL delete_files_callback( HINF hinf, PCWSTR field, void *arg )
110 struct files_callback_info *info = arg;
111 SetupQueueDeleteSectionW( info->queue, hinf, 0, field );
112 return TRUE;
116 /***********************************************************************
117 * rename_files_callback
119 * Called once for each RenFiles entry in a given section.
121 static BOOL rename_files_callback( HINF hinf, PCWSTR field, void *arg )
123 struct files_callback_info *info = arg;
124 SetupQueueRenameSectionW( info->queue, hinf, 0, field );
125 return TRUE;
129 /***********************************************************************
130 * get_root_key
132 * Retrieve the registry root key from its name.
134 static HKEY get_root_key( const WCHAR *name, HKEY def_root )
136 static const WCHAR HKCR[] = {'H','K','C','R',0};
137 static const WCHAR HKCU[] = {'H','K','C','U',0};
138 static const WCHAR HKLM[] = {'H','K','L','M',0};
139 static const WCHAR HKU[] = {'H','K','U',0};
140 static const WCHAR HKR[] = {'H','K','R',0};
142 if (!strcmpiW( name, HKCR )) return HKEY_CLASSES_ROOT;
143 if (!strcmpiW( name, HKCU )) return HKEY_CURRENT_USER;
144 if (!strcmpiW( name, HKLM )) return HKEY_LOCAL_MACHINE;
145 if (!strcmpiW( name, HKU )) return HKEY_USERS;
146 if (!strcmpiW( name, HKR )) return def_root;
147 return 0;
151 /***********************************************************************
152 * append_multi_sz_value
154 * Append a multisz string to a multisz registry value.
156 static void append_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *strings,
157 DWORD str_size )
159 DWORD size, type, total;
160 WCHAR *buffer, *p;
162 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
163 if (type != REG_MULTI_SZ) return;
165 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, (size + str_size) * sizeof(WCHAR) ))) return;
166 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
168 /* compare each string against all the existing ones */
169 total = size;
170 while (*strings)
172 int len = strlenW(strings) + 1;
174 for (p = buffer; *p; p += strlenW(p) + 1)
175 if (!strcmpiW( p, strings )) break;
177 if (!*p) /* not found, need to append it */
179 memcpy( p, strings, len * sizeof(WCHAR) );
180 p[len] = 0;
181 total += len;
183 strings += len;
185 if (total != size)
187 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer) );
188 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ, (BYTE *)buffer, total );
190 done:
191 HeapFree( GetProcessHeap(), 0, buffer );
195 /***********************************************************************
196 * delete_multi_sz_value
198 * Remove a string from a multisz registry value.
200 static void delete_multi_sz_value( HKEY hkey, const WCHAR *value, const WCHAR *string )
202 DWORD size, type;
203 WCHAR *buffer, *src, *dst;
205 if (RegQueryValueExW( hkey, value, NULL, &type, NULL, &size )) return;
206 if (type != REG_MULTI_SZ) return;
207 /* allocate double the size, one for value before and one for after */
208 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size * 2 * sizeof(WCHAR) ))) return;
209 if (RegQueryValueExW( hkey, value, NULL, NULL, (BYTE *)buffer, &size )) goto done;
210 src = buffer;
211 dst = buffer + size;
212 while (*src)
214 int len = strlenW(src) + 1;
215 if (strcmpiW( src, string ))
217 memcpy( dst, src, len * sizeof(WCHAR) );
218 dst += len;
220 src += len;
222 *dst++ = 0;
223 if (dst != buffer + 2*size) /* did we remove something? */
225 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(buffer + size) );
226 RegSetValueExW( hkey, value, 0, REG_MULTI_SZ,
227 (BYTE *)(buffer + size), dst - (buffer + size) );
229 done:
230 HeapFree( GetProcessHeap(), 0, buffer );
234 /***********************************************************************
235 * do_reg_operation
237 * Perform an add/delete registry operation depending on the flags.
239 static BOOL do_reg_operation( HKEY hkey, const WCHAR *value, INFCONTEXT *context, INT flags )
241 DWORD type, size;
243 if (flags & (FLG_ADDREG_DELREG_BIT | FLG_ADDREG_DELVAL)) /* deletion */
245 if (*value && !(flags & FLG_DELREG_KEYONLY_COMMON))
247 if ((flags & FLG_DELREG_MULTI_SZ_DELSTRING) == FLG_DELREG_MULTI_SZ_DELSTRING)
249 WCHAR *str;
251 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size ) || !size) return TRUE;
252 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
253 SetupGetStringFieldW( context, 5, str, size, NULL );
254 delete_multi_sz_value( hkey, value, str );
255 HeapFree( GetProcessHeap(), 0, str );
257 else RegDeleteValueW( hkey, value );
259 else RegDeleteKeyW( hkey, NULL );
260 return TRUE;
263 if (flags & (FLG_ADDREG_KEYONLY|FLG_ADDREG_KEYONLY_COMMON)) return TRUE;
265 if (flags & (FLG_ADDREG_NOCLOBBER|FLG_ADDREG_OVERWRITEONLY))
267 BOOL exists = !RegQueryValueExW( hkey, value, NULL, NULL, NULL, NULL );
268 if (exists && (flags & FLG_ADDREG_NOCLOBBER)) return TRUE;
269 if (!exists & (flags & FLG_ADDREG_OVERWRITEONLY)) return TRUE;
272 switch(flags & FLG_ADDREG_TYPE_MASK)
274 case FLG_ADDREG_TYPE_SZ: type = REG_SZ; break;
275 case FLG_ADDREG_TYPE_MULTI_SZ: type = REG_MULTI_SZ; break;
276 case FLG_ADDREG_TYPE_EXPAND_SZ: type = REG_EXPAND_SZ; break;
277 case FLG_ADDREG_TYPE_BINARY: type = REG_BINARY; break;
278 case FLG_ADDREG_TYPE_DWORD: type = REG_DWORD; break;
279 case FLG_ADDREG_TYPE_NONE: type = REG_NONE; break;
280 default: type = flags >> 16; break;
283 if (!(flags & FLG_ADDREG_BINVALUETYPE) ||
284 (type == REG_DWORD && SetupGetFieldCount(context) == 5))
286 static const WCHAR empty;
287 WCHAR *str = NULL;
289 if (type == REG_MULTI_SZ)
291 if (!SetupGetMultiSzFieldW( context, 5, NULL, 0, &size )) size = 0;
292 if (size)
294 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
295 SetupGetMultiSzFieldW( context, 5, str, size, NULL );
297 if (flags & FLG_ADDREG_APPEND)
299 if (!str) return TRUE;
300 append_multi_sz_value( hkey, value, str, size );
301 HeapFree( GetProcessHeap(), 0, str );
302 return TRUE;
304 /* else fall through to normal string handling */
306 else
308 if (!SetupGetStringFieldW( context, 5, NULL, 0, &size )) size = 0;
309 if (size)
311 if (!(str = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
312 SetupGetStringFieldW( context, 5, str, size, NULL );
316 if (type == REG_DWORD)
318 DWORD dw = str ? strtoulW( str, NULL, 16 ) : 0;
319 TRACE( "setting dword %s to %lx\n", debugstr_w(value), dw );
320 RegSetValueExW( hkey, value, 0, type, (BYTE *)&dw, sizeof(dw) );
322 else
324 TRACE( "setting value %s to %s\n", debugstr_w(value), debugstr_w(str) );
325 if (str) RegSetValueExW( hkey, value, 0, type, (BYTE *)str, size * sizeof(WCHAR) );
326 else RegSetValueExW( hkey, value, 0, type, (BYTE *)&empty, sizeof(WCHAR) );
328 HeapFree( GetProcessHeap(), 0, str );
329 return TRUE;
331 else /* get the binary data */
333 BYTE *data = NULL;
335 if (!SetupGetBinaryField( context, 5, NULL, 0, &size )) size = 0;
336 if (size)
338 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
339 TRACE( "setting binary data %s len %ld\n", debugstr_w(value), size );
340 SetupGetBinaryField( context, 5, data, size, NULL );
342 RegSetValueExW( hkey, value, 0, type, data, size );
343 HeapFree( GetProcessHeap(), 0, data );
344 return TRUE;
349 /***********************************************************************
350 * registry_callback
352 * Called once for each AddReg and DelReg entry in a given section.
354 static BOOL registry_callback( HINF hinf, PCWSTR field, void *arg )
356 struct registry_callback_info *info = arg;
357 INFCONTEXT context;
358 HKEY root_key, hkey;
360 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
362 for (; ok; ok = SetupFindNextLine( &context, &context ))
364 WCHAR buffer[MAX_INF_STRING_LENGTH];
365 INT flags;
367 /* get root */
368 if (!SetupGetStringFieldW( &context, 1, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
369 continue;
370 if (!(root_key = get_root_key( buffer, info->default_root )))
371 continue;
373 /* get key */
374 if (!SetupGetStringFieldW( &context, 2, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
375 *buffer = 0;
377 /* get flags */
378 if (!SetupGetIntField( &context, 4, &flags )) flags = 0;
380 if (!info->delete)
382 if (flags & FLG_ADDREG_DELREG_BIT) continue; /* ignore this entry */
384 else
386 if (!flags) flags = FLG_ADDREG_DELREG_BIT;
387 else if (!(flags & FLG_ADDREG_DELREG_BIT)) continue; /* ignore this entry */
390 if (info->delete || (flags & FLG_ADDREG_OVERWRITEONLY))
392 if (RegOpenKeyW( root_key, buffer, &hkey )) continue; /* ignore if it doesn't exist */
394 else if (RegCreateKeyW( root_key, buffer, &hkey ))
396 ERR( "could not create key %p %s\n", root_key, debugstr_w(buffer) );
397 continue;
399 TRACE( "key %p %s\n", root_key, debugstr_w(buffer) );
401 /* get value name */
402 if (!SetupGetStringFieldW( &context, 3, buffer, sizeof(buffer)/sizeof(WCHAR), NULL ))
403 *buffer = 0;
405 /* and now do it */
406 if (!do_reg_operation( hkey, buffer, &context, flags ))
408 RegCloseKey( hkey );
409 return FALSE;
411 RegCloseKey( hkey );
413 return TRUE;
417 static BOOL update_ini_callback( HINF hinf, PCWSTR field, void *arg )
419 INFCONTEXT context;
421 BOOL ok = SetupFindFirstLineW( hinf, field, NULL, &context );
423 for (; ok; ok = SetupFindNextLine( &context, &context ))
425 WCHAR buffer[MAX_INF_STRING_LENGTH];
426 WCHAR filename[MAX_INF_STRING_LENGTH];
427 WCHAR section[MAX_INF_STRING_LENGTH];
428 WCHAR entry[MAX_INF_STRING_LENGTH];
429 WCHAR string[MAX_INF_STRING_LENGTH];
430 LPWSTR divider;
432 if (!SetupGetStringFieldW( &context, 1, filename,
433 sizeof(filename)/sizeof(WCHAR), NULL ))
434 continue;
436 if (!SetupGetStringFieldW( &context, 2, section,
437 sizeof(section)/sizeof(WCHAR), NULL ))
438 continue;
440 if (!SetupGetStringFieldW( &context, 4, buffer,
441 sizeof(buffer)/sizeof(WCHAR), NULL ))
442 continue;
444 divider = strchrW(buffer,'=');
445 if (divider)
447 *divider = 0;
448 strcpyW(entry,buffer);
449 divider++;
450 strcpyW(string,divider);
452 else
454 strcpyW(entry,buffer);
455 string[0]=0;
458 TRACE("Writing %s = %s in %s of file %s\n",debugstr_w(entry),
459 debugstr_w(string),debugstr_w(section),debugstr_w(filename));
460 WritePrivateProfileStringW(section,entry,string,filename);
463 return TRUE;
466 static BOOL update_ini_fields_callback( HINF hinf, PCWSTR field, void *arg )
468 FIXME( "should update ini fields %s\n", debugstr_w(field) );
469 return TRUE;
472 static BOOL ini2reg_callback( HINF hinf, PCWSTR field, void *arg )
474 FIXME( "should do ini2reg %s\n", debugstr_w(field) );
475 return TRUE;
478 static BOOL logconf_callback( HINF hinf, PCWSTR field, void *arg )
480 FIXME( "should do logconf %s\n", debugstr_w(field) );
481 return TRUE;
485 /***********************************************************************
486 * iterate_section_fields
488 * Iterate over all fields of a certain key of a certain section
490 static BOOL iterate_section_fields( HINF hinf, PCWSTR section, PCWSTR key,
491 iterate_fields_func callback, void *arg )
493 WCHAR static_buffer[200];
494 WCHAR *buffer = static_buffer;
495 DWORD size = sizeof(static_buffer)/sizeof(WCHAR);
496 INFCONTEXT context;
497 BOOL ret = FALSE;
499 BOOL ok = SetupFindFirstLineW( hinf, section, key, &context );
500 while (ok)
502 UINT i, count = SetupGetFieldCount( &context );
503 for (i = 1; i <= count; i++)
505 if (!(buffer = get_field_string( &context, i, buffer, static_buffer, &size )))
506 goto done;
507 if (!callback( hinf, buffer, arg ))
509 ERR("callback failed for %s %s\n", debugstr_w(section), debugstr_w(buffer) );
510 goto done;
513 ok = SetupFindNextMatchLineW( &context, key, &context );
515 ret = TRUE;
516 done:
517 if (buffer && buffer != static_buffer) HeapFree( GetProcessHeap(), 0, buffer );
518 return ret;
522 /***********************************************************************
523 * SetupInstallFilesFromInfSectionA (SETUPAPI.@)
525 BOOL WINAPI SetupInstallFilesFromInfSectionA( HINF hinf, HINF hlayout, HSPFILEQ queue,
526 PCSTR section, PCSTR src_root, UINT flags )
528 UNICODE_STRING sectionW;
529 BOOL ret = FALSE;
531 if (!RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
533 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
534 return FALSE;
536 if (!src_root)
537 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
538 NULL, flags );
539 else
541 UNICODE_STRING srcW;
542 if (RtlCreateUnicodeStringFromAsciiz( &srcW, src_root ))
544 ret = SetupInstallFilesFromInfSectionW( hinf, hlayout, queue, sectionW.Buffer,
545 srcW.Buffer, flags );
546 RtlFreeUnicodeString( &srcW );
548 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
550 RtlFreeUnicodeString( &sectionW );
551 return ret;
555 /***********************************************************************
556 * SetupInstallFilesFromInfSectionW (SETUPAPI.@)
558 BOOL WINAPI SetupInstallFilesFromInfSectionW( HINF hinf, HINF hlayout, HSPFILEQ queue,
559 PCWSTR section, PCWSTR src_root, UINT flags )
561 struct files_callback_info info;
563 info.queue = queue;
564 info.src_root = src_root;
565 info.copy_flags = flags;
566 info.layout = hlayout;
567 return iterate_section_fields( hinf, section, CopyFiles, copy_files_callback, &info );
571 /***********************************************************************
572 * SetupInstallFromInfSectionA (SETUPAPI.@)
574 BOOL WINAPI SetupInstallFromInfSectionA( HWND owner, HINF hinf, PCSTR section, UINT flags,
575 HKEY key_root, PCSTR src_root, UINT copy_flags,
576 PSP_FILE_CALLBACK_A callback, PVOID context,
577 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
579 UNICODE_STRING sectionW, src_rootW;
580 struct callback_WtoA_context ctx;
581 BOOL ret = FALSE;
583 src_rootW.Buffer = NULL;
584 if (src_root && !RtlCreateUnicodeStringFromAsciiz( &src_rootW, src_root ))
586 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
587 return FALSE;
590 if (RtlCreateUnicodeStringFromAsciiz( &sectionW, section ))
592 ctx.orig_context = context;
593 ctx.orig_handler = callback;
594 ret = SetupInstallFromInfSectionW( owner, hinf, sectionW.Buffer, flags, key_root,
595 src_rootW.Buffer, copy_flags, QUEUE_callback_WtoA,
596 &ctx, devinfo, devinfo_data );
597 RtlFreeUnicodeString( &sectionW );
599 else SetLastError( ERROR_NOT_ENOUGH_MEMORY );
601 RtlFreeUnicodeString( &src_rootW );
602 return ret;
606 /***********************************************************************
607 * SetupInstallFromInfSectionW (SETUPAPI.@)
609 BOOL WINAPI SetupInstallFromInfSectionW( HWND owner, HINF hinf, PCWSTR section, UINT flags,
610 HKEY key_root, PCWSTR src_root, UINT copy_flags,
611 PSP_FILE_CALLBACK_W callback, PVOID context,
612 HDEVINFO devinfo, PSP_DEVINFO_DATA devinfo_data )
614 if (flags & SPINST_FILES)
616 struct files_callback_info info;
617 HSPFILEQ queue;
618 BOOL ret;
620 if (!(queue = SetupOpenFileQueue())) return FALSE;
621 info.queue = queue;
622 info.src_root = src_root;
623 info.copy_flags = copy_flags;
624 info.layout = hinf;
625 ret = (iterate_section_fields( hinf, section, CopyFiles, copy_files_callback, &info ) &&
626 iterate_section_fields( hinf, section, DelFiles, delete_files_callback, &info ) &&
627 iterate_section_fields( hinf, section, RenFiles, rename_files_callback, &info ) &&
628 SetupCommitFileQueueW( owner, queue, callback, context ));
629 SetupCloseFileQueue( queue );
630 if (!ret) return FALSE;
632 if (flags & SPINST_INIFILES)
634 if (!iterate_section_fields( hinf, section, UpdateInis, update_ini_callback, NULL ) ||
635 !iterate_section_fields( hinf, section, UpdateIniFields,
636 update_ini_fields_callback, NULL ))
637 return FALSE;
639 if (flags & SPINST_INI2REG)
641 if (!iterate_section_fields( hinf, section, Ini2Reg, ini2reg_callback, NULL ))
642 return FALSE;
645 if (flags & SPINST_LOGCONFIG)
647 if (!iterate_section_fields( hinf, section, LogConf, logconf_callback, NULL ))
648 return FALSE;
651 if (flags & SPINST_REGISTRY)
653 struct registry_callback_info info;
655 info.default_root = key_root;
656 info.delete = TRUE;
657 if (!iterate_section_fields( hinf, section, DelReg, registry_callback, &info ))
658 return FALSE;
659 info.delete = FALSE;
660 if (!iterate_section_fields( hinf, section, AddReg, registry_callback, &info ))
661 return FALSE;
663 if (flags & (SPINST_BITREG|SPINST_REGSVR|SPINST_UNREGSVR|SPINST_PROFILEITEMS|SPINST_COPYINF))
664 FIXME( "unsupported flags %x\n", flags );
665 return TRUE;