-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add internal registry implementation for win32 #11137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
straight-shoota
merged 15 commits into
crystal-lang:master
from
straight-shoota:feature/win32-registry-internal
Sep 10, 2021
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2c356a4
Add minimal system registry implementation
straight-shoota 6a62070
Load MIME database from registry
straight-shoota ccaa2ee
Load time zone information from registry
straight-shoota 5a215ed
crystal tool format
straight-shoota bb47e2c
Rename to WindowsRegistry
straight-shoota b11e68e
[CI] Add debug output
straight-shoota 8423656
[CI] Add more debugging
straight-shoota aecb16f
Fix time spec for CI environment
straight-shoota 2c960db
Improve spec to test actual values
straight-shoota d100237
Fix spec
straight-shoota c70ba84
Fix setup time zone information
straight-shoota 919c68c
Make sure to reset TimeZoneInformation
straight-shoota d246767
Add comment about missing key
straight-shoota 25074f6
Add method docs
straight-shoota a32c69e
Raise RuntimeError
straight-shoota File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,20 @@ | ||
| require "./windows_registry" | ||
|
|
||
| module Crystal::System::MIME | ||
| CONTENT_TYPE = "Content Type".to_utf16 | ||
|
|
||
| # Load MIME types from operating system source. | ||
| def self.load | ||
| # TODO: MIME types in Windows are provided by the registry. This needs to be | ||
| # implemented when registry access it is available. | ||
| # Until then, there will no system-provided MIME types in Windows. | ||
| WindowsRegistry.each_name(LibC::HKEY_CLASSES_ROOT) do |name| | ||
| # skip anything that is not a file extension | ||
| next if name.size < 2 || !(name[0] === '.') | ||
|
|
||
| WindowsRegistry.open?(LibC::HKEY_CLASSES_ROOT, name) do |sub_handle| | ||
| content_type = WindowsRegistry.get_string(sub_handle, CONTENT_TYPE) | ||
| if content_type | ||
| ::MIME.register String.from_utf16(name), content_type | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| require "c/winreg" | ||
| require "c/regapix" | ||
|
|
||
| module Crystal::System::WindowsRegistry | ||
| # Opens a subkey at path *name* and returns the new key handle or `nil` if it | ||
| # does not exist. | ||
| # | ||
| # Users need to ensure the opened key will be closed after usage (see `#close`). | ||
| # *sam* specifies desired access rights to the key to be opened. | ||
| def self.open?(handle : LibC::HKEY, name : Slice(UInt16), sam = LibC::REGSAM::READ) | ||
| status = LibC.RegOpenKeyExW(handle, name, 0, sam, out sub_handle) | ||
| status = WinError.new(status) | ||
|
|
||
| case status | ||
| when .error_success? | ||
| sub_handle | ||
| when .error_file_not_found? | ||
| # key does not exist | ||
| nil | ||
| else | ||
| raise RuntimeError.from_os_error("RegOpenKeyExW", status) | ||
| end | ||
| end | ||
|
|
||
| def self.open?(handle : LibC::HKEY, name : Slice(UInt16), sam = LibC::REGSAM::READ) | ||
| key_handle = open?(handle, name, sam) | ||
|
|
||
| return unless key_handle | ||
|
|
||
| begin | ||
| yield key_handle | ||
| ensure | ||
| close key_handle | ||
| end | ||
| end | ||
|
|
||
| # Closes the handle. | ||
| def self.close(handle : LibC::HKEY) : Nil | ||
| status = LibC.RegCloseKey(handle) | ||
| status = WinError.new(status) | ||
|
|
||
| unless status.error_success? || status.error_invalid_handle? | ||
| raise RuntimeError.from_os_error("RegCloseKey", status) | ||
| end | ||
| end | ||
|
|
||
| # Iterates all value names in this key and yields them to the block. | ||
| def self.each_name(handle : LibC::HKEY, &block : Slice(UInt16) -> Nil) : Nil | ||
| status = LibC.RegQueryInfoKeyW(handle, nil, nil, nil, out sub_key_count, out max_sub_key_length, nil, nil, nil, nil, nil, nil) | ||
| status = WinError.new(status) | ||
|
|
||
| return unless status.error_success? | ||
|
|
||
| buffer = Slice(UInt16).new(max_sub_key_length + 1) | ||
|
|
||
| sub_key_count.times do |i| | ||
| length = buffer.size.to_u32 | ||
| status = LibC.RegEnumKeyExW(handle, i, buffer, pointerof(length), nil, nil, nil, nil) | ||
| status = WinError.new(status) | ||
|
|
||
| case status | ||
| when .error_success? | ||
| yield buffer[0, length] | ||
| when .error_no_more_items? | ||
| break | ||
| else | ||
| raise RuntimeError.from_os_error("RegEnumValueW", status) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Reads a raw value into a buffer and creates a string from it. | ||
| def self.get_string(handle : LibC::HKEY, name : Slice(UInt16)) | ||
|
beta-ziliani marked this conversation as resolved.
|
||
| Crystal::System.retry_wstr_buffer do |buffer, small_buf| | ||
| raw = get_raw(handle, name, Bytes.new(buffer.to_unsafe.as(UInt8*), buffer.bytesize)) || return | ||
| _, length = raw | ||
|
|
||
| if 0 <= length <= buffer.size | ||
| return String.from_utf16(buffer[0, length // 2 - 1]) | ||
| elsif small_buf && length > 0 | ||
| next length | ||
| else | ||
| raise RuntimeError.new("RegQueryValueExW retry buffer") | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Reads a raw value into a buffer. | ||
| def self.get_raw(handle : LibC::HKEY, name : Slice(UInt16), buffer : Slice(UInt8)) | ||
| length = buffer.size.to_u32 | ||
| status = LibC.RegQueryValueExW(handle, name, nil, out valtype, buffer, pointerof(length)) | ||
| status = WinError.new(status) | ||
| case status | ||
| when .error_success? | ||
| {valtype, length} | ||
| when .error_file_not_found? | ||
| nil | ||
| when .error_more_data? | ||
| {valtype, length} | ||
| else | ||
| raise RuntimeError.from_os_error("RegQueryValueExW", status) | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| lib LibC | ||
| type HKEY = Void* | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| lib LibC | ||
| alias LSTATUS = DWORD | ||
|
|
||
| enum RegistryRoutineFlags | ||
| NONE = 0 | ||
| SZ = 1 | ||
| EXPAND_SZ = 2 | ||
| BINARY = 3 | ||
| DWORD = 4 | ||
| DWORD_LITTLE_ENDIAN = DWORD | ||
| DWORD_BIG_ENDIAN = 5 | ||
| LINK = 6 | ||
| MULTI_SZ = 7 | ||
| RESOURCE_LIST = 8 | ||
| FULL_RESOURCE_DESCRIPTOR = 9 | ||
| RESOURCE_REQUIREMENTS_LIST = 10 | ||
| QWORD = 11 | ||
| QWORD_LITTLE_ENDIAN = QWORD | ||
| end | ||
|
|
||
| HKEY_CLASSES_ROOT = Pointer(Void).new(0x80000000).as(HKEY) | ||
| HKEY_CURRENT_USER = Pointer(Void).new(0x80000001).as(HKEY) | ||
| HKEY_LOCAL_MACHINE = Pointer(Void).new(0x80000002).as(HKEY) | ||
| HKEY_USERS = Pointer(Void).new(0x80000003).as(HKEY) | ||
| HKEY_PERFORMANCE_DATA = Pointer(Void).new(0x80000004).as(HKEY) | ||
| HKEY_CURRENT_CONFIG = Pointer(Void).new(0x80000005).as(HKEY) | ||
| HKEY_DYN_DATA = Pointer(Void).new(0x8000006).as(HKEY) | ||
|
|
||
| fun RegOpenKeyExW(hKey : HKEY, lpSubKey : LPWSTR, ulOptions : DWORD, samDesired : REGSAM, phkResult : HKEY*) : LSTATUS | ||
| fun RegCloseKey(hKey : HKEY) : LSTATUS | ||
| fun RegEnumKeyExW(hKey : HKEY, dwIndex : DWORD, | ||
| lpName : LPWSTR, lpcchName : DWORD*, | ||
| lpReserved : DWORD*, | ||
| lpClass : LPWSTR, lpcchClass : DWORD*, | ||
| lpftLastWriteTime : FILETIME*) : LSTATUS | ||
| fun RegQueryInfoKeyW(hKey : HKEY, lpClass : LPSTR, lpcchClass : DWORD*, lpReserved : DWORD*, | ||
| lpcSubKeys : DWORD*, lpcbMaxSubKeyLen : DWORD*, | ||
| lpcbMaxClassLen : DWORD*, | ||
| lpcValues : DWORD*, lpcbMaxValueNameLen : DWORD*, lpcbMaxValueLen : DWORD*, | ||
| lpcbSecurityDescriptor : DWORD*, lpftLastWriteTime : FILETIME*) : DWORD | ||
| fun RegQueryValueExW(hKey : HKEY, lpValueName : LPWSTR, lpReserved : DWORD*, lpType : RegistryRoutineFlags*, lpData : BYTE*, lpcbData : DWORD*) : LSTATUS | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.