pdk 2.1.0 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1329 -1292
- data/LICENSE +201 -201
- data/README.md +163 -163
- data/exe/pdk +10 -10
- data/lib/pdk/analytics/client/google_analytics.rb +143 -143
- data/lib/pdk/analytics/client/noop.rb +25 -25
- data/lib/pdk/analytics/util.rb +19 -19
- data/lib/pdk/analytics.rb +30 -30
- data/lib/pdk/answer_file.rb +12 -12
- data/lib/pdk/bolt.rb +19 -19
- data/lib/pdk/cli/build.rb +82 -82
- data/lib/pdk/cli/bundle.rb +48 -48
- data/lib/pdk/cli/config/get.rb +26 -26
- data/lib/pdk/cli/config.rb +22 -22
- data/lib/pdk/cli/console.rb +148 -148
- data/lib/pdk/cli/convert.rb +52 -52
- data/lib/pdk/cli/env.rb +52 -52
- data/lib/pdk/cli/errors.rb +25 -25
- data/lib/pdk/cli/exec/command.rb +293 -283
- data/lib/pdk/cli/exec/interactive_command.rb +114 -114
- data/lib/pdk/cli/exec.rb +84 -84
- data/lib/pdk/cli/exec_group.rb +104 -104
- data/lib/pdk/cli/get/config.rb +24 -24
- data/lib/pdk/cli/get.rb +20 -20
- data/lib/pdk/cli/module/build.rb +12 -12
- data/lib/pdk/cli/module/generate.rb +47 -47
- data/lib/pdk/cli/module.rb +14 -14
- data/lib/pdk/cli/new/class.rb +32 -32
- data/lib/pdk/cli/new/defined_type.rb +32 -32
- data/lib/pdk/cli/new/fact.rb +29 -29
- data/lib/pdk/cli/new/function.rb +29 -29
- data/lib/pdk/cli/new/module.rb +53 -53
- data/lib/pdk/cli/new/provider.rb +29 -29
- data/lib/pdk/cli/new/task.rb +34 -34
- data/lib/pdk/cli/new/test.rb +52 -52
- data/lib/pdk/cli/new/transport.rb +27 -27
- data/lib/pdk/cli/new.rb +21 -21
- data/lib/pdk/cli/release/prep.rb +39 -39
- data/lib/pdk/cli/release/publish.rb +50 -50
- data/lib/pdk/cli/release.rb +194 -192
- data/lib/pdk/cli/remove/config.rb +80 -80
- data/lib/pdk/cli/remove.rb +20 -20
- data/lib/pdk/cli/set/config.rb +119 -119
- data/lib/pdk/cli/set.rb +20 -20
- data/lib/pdk/cli/test/unit.rb +90 -90
- data/lib/pdk/cli/test.rb +11 -11
- data/lib/pdk/cli/update.rb +64 -64
- data/lib/pdk/cli/util/command_redirector.rb +27 -27
- data/lib/pdk/cli/util/interview.rb +72 -72
- data/lib/pdk/cli/util/option_normalizer.rb +55 -55
- data/lib/pdk/cli/util/option_validator.rb +68 -68
- data/lib/pdk/cli/util/spinner.rb +13 -13
- data/lib/pdk/cli/util/update_manager_printer.rb +82 -82
- data/lib/pdk/cli/util.rb +305 -305
- data/lib/pdk/cli/validate.rb +116 -116
- data/lib/pdk/cli.rb +175 -175
- data/lib/pdk/config/analytics_schema.json +26 -26
- data/lib/pdk/config/errors.rb +5 -5
- data/lib/pdk/config/ini_file.rb +183 -183
- data/lib/pdk/config/ini_file_setting.rb +39 -39
- data/lib/pdk/config/json.rb +34 -34
- data/lib/pdk/config/json_schema_namespace.rb +142 -142
- data/lib/pdk/config/json_schema_setting.rb +53 -53
- data/lib/pdk/config/json_with_schema.rb +49 -49
- data/lib/pdk/config/namespace.rb +354 -354
- data/lib/pdk/config/setting.rb +135 -135
- data/lib/pdk/config/validator.rb +31 -31
- data/lib/pdk/config/yaml.rb +46 -42
- data/lib/pdk/config/yaml_with_schema.rb +59 -59
- data/lib/pdk/config.rb +390 -384
- data/lib/pdk/context/control_repo.rb +60 -60
- data/lib/pdk/context/module.rb +28 -28
- data/lib/pdk/context/none.rb +22 -22
- data/lib/pdk/context.rb +99 -99
- data/lib/pdk/control_repo.rb +90 -90
- data/lib/pdk/generate/defined_type.rb +43 -43
- data/lib/pdk/generate/fact.rb +25 -25
- data/lib/pdk/generate/function.rb +48 -48
- data/lib/pdk/generate/module.rb +352 -352
- data/lib/pdk/generate/provider.rb +28 -28
- data/lib/pdk/generate/puppet_class.rb +43 -43
- data/lib/pdk/generate/puppet_object.rb +232 -232
- data/lib/pdk/generate/task.rb +68 -68
- data/lib/pdk/generate/transport.rb +33 -33
- data/lib/pdk/generate.rb +24 -24
- data/lib/pdk/i18n.rb +4 -4
- data/lib/pdk/logger.rb +45 -45
- data/lib/pdk/module/build.rb +322 -322
- data/lib/pdk/module/convert.rb +296 -296
- data/lib/pdk/module/metadata.rb +202 -202
- data/lib/pdk/module/release.rb +260 -254
- data/lib/pdk/module/update.rb +131 -131
- data/lib/pdk/module/update_manager.rb +227 -227
- data/lib/pdk/module.rb +30 -30
- data/lib/pdk/report/event.rb +370 -370
- data/lib/pdk/report.rb +121 -121
- data/lib/pdk/template/fetcher/git.rb +85 -85
- data/lib/pdk/template/fetcher/local.rb +28 -28
- data/lib/pdk/template/fetcher.rb +98 -98
- data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -116
- data/lib/pdk/template/renderer/v1/renderer.rb +132 -132
- data/lib/pdk/template/renderer/v1/template_file.rb +102 -102
- data/lib/pdk/template/renderer/v1.rb +25 -25
- data/lib/pdk/template/renderer.rb +96 -96
- data/lib/pdk/template/template_dir.rb +67 -67
- data/lib/pdk/template.rb +59 -59
- data/lib/pdk/tests/unit.rb +252 -252
- data/lib/pdk/util/bundler.rb +259 -259
- data/lib/pdk/util/changelog_generator.rb +137 -124
- data/lib/pdk/util/env.rb +47 -47
- data/lib/pdk/util/filesystem.rb +138 -138
- data/lib/pdk/util/git.rb +179 -179
- data/lib/pdk/util/json_finder.rb +85 -84
- data/lib/pdk/util/puppet_strings.rb +125 -125
- data/lib/pdk/util/puppet_version.rb +266 -266
- data/lib/pdk/util/ruby_version.rb +179 -179
- data/lib/pdk/util/template_uri.rb +295 -295
- data/lib/pdk/util/vendored_file.rb +93 -93
- data/lib/pdk/util/version.rb +43 -43
- data/lib/pdk/util/windows/api_types.rb +82 -82
- data/lib/pdk/util/windows/file.rb +36 -36
- data/lib/pdk/util/windows/process.rb +79 -79
- data/lib/pdk/util/windows/string.rb +16 -16
- data/lib/pdk/util/windows.rb +15 -15
- data/lib/pdk/util.rb +278 -277
- data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -23
- data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -98
- data/lib/pdk/validate/external_command_validator.rb +208 -208
- data/lib/pdk/validate/internal_ruby_validator.rb +100 -100
- data/lib/pdk/validate/invokable_validator.rb +228 -220
- data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +86 -86
- data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +78 -78
- data/lib/pdk/validate/metadata/metadata_validator_group.rb +20 -20
- data/lib/pdk/validate/puppet/puppet_epp_validator.rb +133 -133
- data/lib/pdk/validate/puppet/puppet_lint_validator.rb +66 -66
- data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +137 -137
- data/lib/pdk/validate/puppet/puppet_validator_group.rb +21 -21
- data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +80 -80
- data/lib/pdk/validate/ruby/ruby_validator_group.rb +19 -19
- data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +88 -88
- data/lib/pdk/validate/tasks/tasks_name_validator.rb +50 -50
- data/lib/pdk/validate/tasks/tasks_validator_group.rb +20 -20
- data/lib/pdk/validate/validator.rb +118 -118
- data/lib/pdk/validate/validator_group.rb +104 -104
- data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +95 -95
- data/lib/pdk/validate/yaml/yaml_validator_group.rb +19 -19
- data/lib/pdk/validate.rb +94 -94
- data/lib/pdk/version.rb +4 -4
- data/lib/pdk.rb +76 -76
- data/locales/config.yaml +21 -21
- data/locales/pdk.pot +2094 -2094
- metadata +19 -13
@@ -1,93 +1,93 @@
|
|
1
|
-
require 'pdk'
|
2
|
-
|
3
|
-
module PDK
|
4
|
-
module Util
|
5
|
-
class VendoredFile
|
6
|
-
class DownloadError < StandardError; end
|
7
|
-
|
8
|
-
attr_reader :file_name
|
9
|
-
attr_reader :url
|
10
|
-
|
11
|
-
def initialize(file_name, url)
|
12
|
-
@file_name = file_name
|
13
|
-
@url = url
|
14
|
-
end
|
15
|
-
|
16
|
-
def read
|
17
|
-
require 'pdk/util'
|
18
|
-
require 'pdk/util/filesystem'
|
19
|
-
|
20
|
-
return PDK::Util::Filesystem.read_file(package_vendored_path) if PDK::Util.package_install?
|
21
|
-
return PDK::Util::Filesystem.read_file(gem_vendored_path) if PDK::Util::Filesystem.file?(gem_vendored_path)
|
22
|
-
|
23
|
-
content = download_file
|
24
|
-
|
25
|
-
# TODO: should only write if it's valid JSON
|
26
|
-
# TODO: need a way to invalidate if out of date
|
27
|
-
PDK::Util::Filesystem.mkdir_p(File.dirname(gem_vendored_path))
|
28
|
-
PDK::Util::Filesystem.write_file(gem_vendored_path, content)
|
29
|
-
content
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def download_file
|
35
|
-
require 'uri'
|
36
|
-
require 'net/https'
|
37
|
-
require 'openssl'
|
38
|
-
|
39
|
-
http_errors = [
|
40
|
-
EOFError,
|
41
|
-
Errno::ECONNRESET,
|
42
|
-
Errno::EINVAL,
|
43
|
-
Errno::ECONNREFUSED,
|
44
|
-
Net::HTTPBadResponse,
|
45
|
-
Net::HTTPHeaderSyntaxError,
|
46
|
-
Net::ProtocolError,
|
47
|
-
Timeout::Error,
|
48
|
-
]
|
49
|
-
|
50
|
-
PDK.logger.debug _('%{file_name} was not found in the cache, downloading it from %{url}.') % {
|
51
|
-
file_name: file_name,
|
52
|
-
url: url,
|
53
|
-
}
|
54
|
-
|
55
|
-
uri = URI.parse(url)
|
56
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
57
|
-
http.use_ssl = true
|
58
|
-
# TODO: Get rid of this
|
59
|
-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if Gem.win_platform?
|
60
|
-
request = Net::HTTP::Get.new(uri.request_uri)
|
61
|
-
response = http.request(request)
|
62
|
-
|
63
|
-
unless response.code == '200'
|
64
|
-
raise DownloadError, _('Unable to download %{url}. %{code}: %{message}.') % {
|
65
|
-
url: url,
|
66
|
-
code: response.code,
|
67
|
-
message: response.message,
|
68
|
-
}
|
69
|
-
end
|
70
|
-
|
71
|
-
response.body
|
72
|
-
rescue *http_errors => e
|
73
|
-
raise DownloadError, _('Unable to download %{url}. Check internet connectivity and try again. %{error}') % {
|
74
|
-
url: url,
|
75
|
-
error: e,
|
76
|
-
}
|
77
|
-
end
|
78
|
-
|
79
|
-
def package_vendored_path
|
80
|
-
require 'pdk/util'
|
81
|
-
|
82
|
-
@package_vendored_path ||= File.join(PDK::Util.package_cachedir, file_name)
|
83
|
-
end
|
84
|
-
|
85
|
-
def gem_vendored_path
|
86
|
-
require 'pdk/util'
|
87
|
-
require 'pdk/version'
|
88
|
-
|
89
|
-
@gem_vendored_path ||= File.join(PDK::Util.cachedir, PDK::VERSION, file_name)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
1
|
+
require 'pdk'
|
2
|
+
|
3
|
+
module PDK
|
4
|
+
module Util
|
5
|
+
class VendoredFile
|
6
|
+
class DownloadError < StandardError; end
|
7
|
+
|
8
|
+
attr_reader :file_name
|
9
|
+
attr_reader :url
|
10
|
+
|
11
|
+
def initialize(file_name, url)
|
12
|
+
@file_name = file_name
|
13
|
+
@url = url
|
14
|
+
end
|
15
|
+
|
16
|
+
def read
|
17
|
+
require 'pdk/util'
|
18
|
+
require 'pdk/util/filesystem'
|
19
|
+
|
20
|
+
return PDK::Util::Filesystem.read_file(package_vendored_path) if PDK::Util.package_install?
|
21
|
+
return PDK::Util::Filesystem.read_file(gem_vendored_path) if PDK::Util::Filesystem.file?(gem_vendored_path)
|
22
|
+
|
23
|
+
content = download_file
|
24
|
+
|
25
|
+
# TODO: should only write if it's valid JSON
|
26
|
+
# TODO: need a way to invalidate if out of date
|
27
|
+
PDK::Util::Filesystem.mkdir_p(File.dirname(gem_vendored_path))
|
28
|
+
PDK::Util::Filesystem.write_file(gem_vendored_path, content)
|
29
|
+
content
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def download_file
|
35
|
+
require 'uri'
|
36
|
+
require 'net/https'
|
37
|
+
require 'openssl'
|
38
|
+
|
39
|
+
http_errors = [
|
40
|
+
EOFError,
|
41
|
+
Errno::ECONNRESET,
|
42
|
+
Errno::EINVAL,
|
43
|
+
Errno::ECONNREFUSED,
|
44
|
+
Net::HTTPBadResponse,
|
45
|
+
Net::HTTPHeaderSyntaxError,
|
46
|
+
Net::ProtocolError,
|
47
|
+
Timeout::Error,
|
48
|
+
]
|
49
|
+
|
50
|
+
PDK.logger.debug _('%{file_name} was not found in the cache, downloading it from %{url}.') % {
|
51
|
+
file_name: file_name,
|
52
|
+
url: url,
|
53
|
+
}
|
54
|
+
|
55
|
+
uri = URI.parse(url)
|
56
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
57
|
+
http.use_ssl = true
|
58
|
+
# TODO: Get rid of this
|
59
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if Gem.win_platform?
|
60
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
61
|
+
response = http.request(request)
|
62
|
+
|
63
|
+
unless response.code == '200'
|
64
|
+
raise DownloadError, _('Unable to download %{url}. %{code}: %{message}.') % {
|
65
|
+
url: url,
|
66
|
+
code: response.code,
|
67
|
+
message: response.message,
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
response.body
|
72
|
+
rescue *http_errors => e
|
73
|
+
raise DownloadError, _('Unable to download %{url}. Check internet connectivity and try again. %{error}') % {
|
74
|
+
url: url,
|
75
|
+
error: e,
|
76
|
+
}
|
77
|
+
end
|
78
|
+
|
79
|
+
def package_vendored_path
|
80
|
+
require 'pdk/util'
|
81
|
+
|
82
|
+
@package_vendored_path ||= File.join(PDK::Util.package_cachedir, file_name)
|
83
|
+
end
|
84
|
+
|
85
|
+
def gem_vendored_path
|
86
|
+
require 'pdk/util'
|
87
|
+
require 'pdk/version'
|
88
|
+
|
89
|
+
@gem_vendored_path ||= File.join(PDK::Util.cachedir, PDK::VERSION, file_name)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
data/lib/pdk/util/version.rb
CHANGED
@@ -1,43 +1,43 @@
|
|
1
|
-
require 'pdk'
|
2
|
-
|
3
|
-
module PDK
|
4
|
-
module Util
|
5
|
-
module Version
|
6
|
-
def self.version_string
|
7
|
-
require 'pdk/version'
|
8
|
-
|
9
|
-
"#{PDK::VERSION} #{pdk_ref}".strip.freeze
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.pdk_ref
|
13
|
-
ref = "#{pkg_sha} #{git_ref}".strip
|
14
|
-
ref.empty? ? nil : "(#{ref})"
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.pkg_sha
|
18
|
-
if version_file && PDK::Util::Filesystem.exist?(version_file)
|
19
|
-
ver = PDK::Util::Filesystem.read_file(version_file)
|
20
|
-
sha = ver.strip.split('.')[5] unless ver.nil?
|
21
|
-
end
|
22
|
-
|
23
|
-
sha
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.git_ref
|
27
|
-
require 'pdk/util/git'
|
28
|
-
source_git_dir = File.join(PDK::Util::Filesystem.expand_path('../../..', File.dirname(__FILE__)), '.git')
|
29
|
-
|
30
|
-
return unless PDK::Util::Filesystem.directory?(source_git_dir)
|
31
|
-
|
32
|
-
PDK::Util::Git.describe(source_git_dir)
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.version_file
|
36
|
-
require 'pdk/util'
|
37
|
-
|
38
|
-
# FIXME: this gets called a LOT and doesn't currently get cached
|
39
|
-
PDK::Util.find_upwards('PDK_VERSION', File.dirname(__FILE__))
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
1
|
+
require 'pdk'
|
2
|
+
|
3
|
+
module PDK
|
4
|
+
module Util
|
5
|
+
module Version
|
6
|
+
def self.version_string
|
7
|
+
require 'pdk/version'
|
8
|
+
|
9
|
+
"#{PDK::VERSION} #{pdk_ref}".strip.freeze
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.pdk_ref
|
13
|
+
ref = "#{pkg_sha} #{git_ref}".strip
|
14
|
+
ref.empty? ? nil : "(#{ref})"
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.pkg_sha
|
18
|
+
if version_file && PDK::Util::Filesystem.exist?(version_file)
|
19
|
+
ver = PDK::Util::Filesystem.read_file(version_file)
|
20
|
+
sha = ver.strip.split('.')[5] unless ver.nil?
|
21
|
+
end
|
22
|
+
|
23
|
+
sha
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.git_ref
|
27
|
+
require 'pdk/util/git'
|
28
|
+
source_git_dir = File.join(PDK::Util::Filesystem.expand_path('../../..', File.dirname(__FILE__)), '.git')
|
29
|
+
|
30
|
+
return unless PDK::Util::Filesystem.directory?(source_git_dir)
|
31
|
+
|
32
|
+
PDK::Util::Git.describe(source_git_dir)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.version_file
|
36
|
+
require 'pdk/util'
|
37
|
+
|
38
|
+
# FIXME: this gets called a LOT and doesn't currently get cached
|
39
|
+
PDK::Util.find_upwards('PDK_VERSION', File.dirname(__FILE__))
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -1,82 +1,82 @@
|
|
1
|
-
require 'ffi'
|
2
|
-
require 'pdk/util/windows/string'
|
3
|
-
|
4
|
-
module PDK::Util::Windows::APITypes
|
5
|
-
module ::FFI::Library
|
6
|
-
def attach_function_private(*args)
|
7
|
-
attach_function(*args)
|
8
|
-
private args[0]
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
class ::FFI::Pointer
|
13
|
-
def self.from_string_to_wide_string(str, &_block)
|
14
|
-
str = PDK::Util::Windows::String.wide_string(str)
|
15
|
-
FFI::MemoryPointer.new(:byte, str.bytesize) do |ptr|
|
16
|
-
# uchar here is synonymous with byte
|
17
|
-
ptr.put_array_of_uchar(0, str.bytes.to_a)
|
18
|
-
|
19
|
-
yield ptr
|
20
|
-
end
|
21
|
-
|
22
|
-
# ptr has already had free called, so nothing to return
|
23
|
-
nil
|
24
|
-
end
|
25
|
-
|
26
|
-
def read_wide_string(char_length, dst_encoding = Encoding::UTF_8, encode_options = {})
|
27
|
-
# char_length is number of wide chars (typically excluding NULLs), *not* bytes
|
28
|
-
str = get_bytes(0, char_length * 2).force_encoding('UTF-16LE')
|
29
|
-
str.encode(dst_encoding, str.encoding, encode_options)
|
30
|
-
rescue StandardError => e
|
31
|
-
PDK.logger.debug _('Unable to convert value %{string} to encoding %{encoding} due to %{error}') % {
|
32
|
-
string: str.dump,
|
33
|
-
encoding: dst_encoding,
|
34
|
-
error: e.inspect,
|
35
|
-
}
|
36
|
-
raise
|
37
|
-
end
|
38
|
-
|
39
|
-
def read_arbitrary_wide_string_up_to(max_char_length = 512, null_terminator = :single_null, encode_options = {})
|
40
|
-
unless [:single_null, :double_null].include?(null_terminator)
|
41
|
-
raise ArgumentError, _(
|
42
|
-
'Unable to read wide strings with %{null_terminator} terminal nulls',
|
43
|
-
) % { null_terminator: null_terminator }
|
44
|
-
end
|
45
|
-
|
46
|
-
terminator_width = (null_terminator == :single_null) ? 1 : 2
|
47
|
-
reader_method = (null_terminator == :single_null) ? :get_uint16 : :get_uint32
|
48
|
-
|
49
|
-
# Look for the null_terminator; if found, read up to that null
|
50
|
-
# (exclusive)
|
51
|
-
(0...max_char_length - terminator_width).each do |i|
|
52
|
-
return read_wide_string(i, Encoding::UTF_8, encode_options) if send(reader_method, (i * 2)).zero?
|
53
|
-
end
|
54
|
-
|
55
|
-
# String is longer than the max, read just up to the max
|
56
|
-
read_wide_string(max_char_length, Encoding::UTF_8, encode_options)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
# FFI Types
|
61
|
-
# https://github.com/ffi/ffi/wiki/Types
|
62
|
-
|
63
|
-
# Windows - Common Data Types
|
64
|
-
# https://msdn.microsoft.com/en-us/library/cc230309.aspx
|
65
|
-
|
66
|
-
# Windows Data Types
|
67
|
-
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx
|
68
|
-
|
69
|
-
FFI.typedef :uint32, :dword
|
70
|
-
# buffer_inout is similar to pointer (platform specific), but optimized for buffers
|
71
|
-
FFI.typedef :buffer_inout, :lpwstr
|
72
|
-
# buffer_in is similar to pointer (platform specific), but optimized for CONST read only buffers
|
73
|
-
FFI.typedef :buffer_in, :lpcwstr
|
74
|
-
# 8 bits per byte
|
75
|
-
FFI.typedef :uchar, :byte
|
76
|
-
FFI.typedef :uint16, :wchar
|
77
|
-
|
78
|
-
# FFI bool can be only 1 byte at times,
|
79
|
-
# Win32 BOOL is a signed int, and is always 4 bytes, even on x64
|
80
|
-
# https://blogs.msdn.com/b/oldnewthing/archive/2011/03/28/10146459.aspx
|
81
|
-
FFI.typedef :int32, :win32_bool
|
82
|
-
end
|
1
|
+
require 'ffi'
|
2
|
+
require 'pdk/util/windows/string'
|
3
|
+
|
4
|
+
module PDK::Util::Windows::APITypes
|
5
|
+
module ::FFI::Library
|
6
|
+
def attach_function_private(*args)
|
7
|
+
attach_function(*args)
|
8
|
+
private args[0]
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class ::FFI::Pointer
|
13
|
+
def self.from_string_to_wide_string(str, &_block)
|
14
|
+
str = PDK::Util::Windows::String.wide_string(str)
|
15
|
+
FFI::MemoryPointer.new(:byte, str.bytesize) do |ptr|
|
16
|
+
# uchar here is synonymous with byte
|
17
|
+
ptr.put_array_of_uchar(0, str.bytes.to_a)
|
18
|
+
|
19
|
+
yield ptr
|
20
|
+
end
|
21
|
+
|
22
|
+
# ptr has already had free called, so nothing to return
|
23
|
+
nil
|
24
|
+
end
|
25
|
+
|
26
|
+
def read_wide_string(char_length, dst_encoding = Encoding::UTF_8, encode_options = {})
|
27
|
+
# char_length is number of wide chars (typically excluding NULLs), *not* bytes
|
28
|
+
str = get_bytes(0, char_length * 2).force_encoding('UTF-16LE')
|
29
|
+
str.encode(dst_encoding, str.encoding, encode_options)
|
30
|
+
rescue StandardError => e
|
31
|
+
PDK.logger.debug _('Unable to convert value %{string} to encoding %{encoding} due to %{error}') % {
|
32
|
+
string: str.dump,
|
33
|
+
encoding: dst_encoding,
|
34
|
+
error: e.inspect,
|
35
|
+
}
|
36
|
+
raise
|
37
|
+
end
|
38
|
+
|
39
|
+
def read_arbitrary_wide_string_up_to(max_char_length = 512, null_terminator = :single_null, encode_options = {})
|
40
|
+
unless [:single_null, :double_null].include?(null_terminator)
|
41
|
+
raise ArgumentError, _(
|
42
|
+
'Unable to read wide strings with %{null_terminator} terminal nulls',
|
43
|
+
) % { null_terminator: null_terminator }
|
44
|
+
end
|
45
|
+
|
46
|
+
terminator_width = (null_terminator == :single_null) ? 1 : 2
|
47
|
+
reader_method = (null_terminator == :single_null) ? :get_uint16 : :get_uint32
|
48
|
+
|
49
|
+
# Look for the null_terminator; if found, read up to that null
|
50
|
+
# (exclusive)
|
51
|
+
(0...max_char_length - terminator_width).each do |i|
|
52
|
+
return read_wide_string(i, Encoding::UTF_8, encode_options) if send(reader_method, (i * 2)).zero?
|
53
|
+
end
|
54
|
+
|
55
|
+
# String is longer than the max, read just up to the max
|
56
|
+
read_wide_string(max_char_length, Encoding::UTF_8, encode_options)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# FFI Types
|
61
|
+
# https://github.com/ffi/ffi/wiki/Types
|
62
|
+
|
63
|
+
# Windows - Common Data Types
|
64
|
+
# https://msdn.microsoft.com/en-us/library/cc230309.aspx
|
65
|
+
|
66
|
+
# Windows Data Types
|
67
|
+
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx
|
68
|
+
|
69
|
+
FFI.typedef :uint32, :dword
|
70
|
+
# buffer_inout is similar to pointer (platform specific), but optimized for buffers
|
71
|
+
FFI.typedef :buffer_inout, :lpwstr
|
72
|
+
# buffer_in is similar to pointer (platform specific), but optimized for CONST read only buffers
|
73
|
+
FFI.typedef :buffer_in, :lpcwstr
|
74
|
+
# 8 bits per byte
|
75
|
+
FFI.typedef :uchar, :byte
|
76
|
+
FFI.typedef :uint16, :wchar
|
77
|
+
|
78
|
+
# FFI bool can be only 1 byte at times,
|
79
|
+
# Win32 BOOL is a signed int, and is always 4 bytes, even on x64
|
80
|
+
# https://blogs.msdn.com/b/oldnewthing/archive/2011/03/28/10146459.aspx
|
81
|
+
FFI.typedef :int32, :win32_bool
|
82
|
+
end
|
@@ -1,36 +1,36 @@
|
|
1
|
-
require 'pdk/util/windows'
|
2
|
-
|
3
|
-
module PDK::Util::Windows::File
|
4
|
-
require 'ffi'
|
5
|
-
extend FFI::Library
|
6
|
-
extend PDK::Util::Windows::String
|
7
|
-
|
8
|
-
def get_long_pathname(path)
|
9
|
-
converted = ''
|
10
|
-
FFI::Pointer.from_string_to_wide_string(path) do |path_ptr|
|
11
|
-
# includes terminating NULL
|
12
|
-
buffer_size = GetLongPathNameW(path_ptr, FFI::Pointer::NULL, 0)
|
13
|
-
FFI::MemoryPointer.new(:wchar, buffer_size) do |converted_ptr|
|
14
|
-
if GetLongPathNameW(path_ptr, converted_ptr, buffer_size) == PDK::Util::Windows::WIN32_FALSE
|
15
|
-
raise _('Failed to call GetLongPathName')
|
16
|
-
end
|
17
|
-
|
18
|
-
converted = converted_ptr.read_wide_string(buffer_size - 1)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
converted
|
23
|
-
end
|
24
|
-
module_function :get_long_pathname
|
25
|
-
|
26
|
-
ffi_convention :stdcall
|
27
|
-
|
28
|
-
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa364980(v=vs.85).aspx
|
29
|
-
# DWORD WINAPI GetLongPathName(
|
30
|
-
# _In_ LPCTSTR lpszShortPath,
|
31
|
-
# _Out_ LPTSTR lpszLongPath,
|
32
|
-
# _In_ DWORD cchBuffer
|
33
|
-
# );
|
34
|
-
ffi_lib :kernel32
|
35
|
-
attach_function :GetLongPathNameW, [:lpcwstr, :lpwstr, :dword], :dword
|
36
|
-
end
|
1
|
+
require 'pdk/util/windows'
|
2
|
+
|
3
|
+
module PDK::Util::Windows::File
|
4
|
+
require 'ffi'
|
5
|
+
extend FFI::Library
|
6
|
+
extend PDK::Util::Windows::String
|
7
|
+
|
8
|
+
def get_long_pathname(path)
|
9
|
+
converted = ''
|
10
|
+
FFI::Pointer.from_string_to_wide_string(path) do |path_ptr|
|
11
|
+
# includes terminating NULL
|
12
|
+
buffer_size = GetLongPathNameW(path_ptr, FFI::Pointer::NULL, 0)
|
13
|
+
FFI::MemoryPointer.new(:wchar, buffer_size) do |converted_ptr|
|
14
|
+
if GetLongPathNameW(path_ptr, converted_ptr, buffer_size) == PDK::Util::Windows::WIN32_FALSE
|
15
|
+
raise _('Failed to call GetLongPathName')
|
16
|
+
end
|
17
|
+
|
18
|
+
converted = converted_ptr.read_wide_string(buffer_size - 1)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
converted
|
23
|
+
end
|
24
|
+
module_function :get_long_pathname
|
25
|
+
|
26
|
+
ffi_convention :stdcall
|
27
|
+
|
28
|
+
# https://msdn.microsoft.com/en-us/library/windows/desktop/aa364980(v=vs.85).aspx
|
29
|
+
# DWORD WINAPI GetLongPathName(
|
30
|
+
# _In_ LPCTSTR lpszShortPath,
|
31
|
+
# _Out_ LPTSTR lpszLongPath,
|
32
|
+
# _In_ DWORD cchBuffer
|
33
|
+
# );
|
34
|
+
ffi_lib :kernel32
|
35
|
+
attach_function :GetLongPathNameW, [:lpcwstr, :lpwstr, :dword], :dword
|
36
|
+
end
|
@@ -1,79 +1,79 @@
|
|
1
|
-
require 'pdk/util/windows'
|
2
|
-
|
3
|
-
module PDK::Util::Windows::Process
|
4
|
-
require 'ffi'
|
5
|
-
extend PDK::Util::Windows::String
|
6
|
-
extend FFI::Library
|
7
|
-
|
8
|
-
def environment_hash
|
9
|
-
env_ptr = GetEnvironmentStringsW()
|
10
|
-
|
11
|
-
contains_unicode_replacement = ->(string) do
|
12
|
-
return false unless string.include?("\uFFFD")
|
13
|
-
|
14
|
-
PDK.logger.warning _(
|
15
|
-
'Discarding environment variable %{string} which contains invalid ' \
|
16
|
-
'bytes',
|
17
|
-
) % { string: string }
|
18
|
-
true
|
19
|
-
end
|
20
|
-
|
21
|
-
# pass :invalid => :replace to the Ruby String#encode to use replacement
|
22
|
-
# characters
|
23
|
-
pairs = env_ptr.read_arbitrary_wide_string_up_to(65_534, :double_null, invalid: :replace)
|
24
|
-
.split(?\x00)
|
25
|
-
.reject { |env_str| env_str.nil? || env_str.empty? || env_str[0] == '=' }
|
26
|
-
.reject { |env_str| contains_unicode_replacement.call(env_str) }
|
27
|
-
.map { |env_pair| env_pair.split('=', 2) }
|
28
|
-
Hash[pairs]
|
29
|
-
ensure
|
30
|
-
if env_ptr && !env_ptr.null?
|
31
|
-
if FreeEnvironmentStringsW(env_ptr) == PDK::Util::Windows::WIN32_FALSE
|
32
|
-
PDK.logger.debug _('FreeEnvironmentStringsW memory leak')
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
module_function :environment_hash
|
37
|
-
|
38
|
-
def set_environment_variable(name, val)
|
39
|
-
raise ArgumentError, _('Environment variable name must not be nil or empty') if name.nil? || name.empty?
|
40
|
-
|
41
|
-
FFI::MemoryPointer.from_string_to_wide_string(name) do |name_ptr|
|
42
|
-
if val.nil?
|
43
|
-
if SetEnvironmentVariableW(name_ptr, FFI::MemoryPointer::NULL) == PDK::Util::Windows::WIN32_FALSE
|
44
|
-
raise _('Failed to remove environment variable: %{name}') % { name: name }
|
45
|
-
end
|
46
|
-
else
|
47
|
-
FFI::MemoryPointer.from_string_to_wide_string(val) do |val_ptr|
|
48
|
-
if SetEnvironmentVariableW(name_ptr, val_ptr) == PDK::Util::Windows::WIN32_FALSE
|
49
|
-
raise _('Failed to set environment variaible: %{name}') % { name: name }
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
module_function :set_environment_variable
|
56
|
-
|
57
|
-
ffi_convention :stdcall
|
58
|
-
|
59
|
-
# https://msdn.microsoft.com/en-us/library/windows/desktop/ms683187(v=vs.85).aspx
|
60
|
-
# LPTCH GetEnvironmentStrings(void);
|
61
|
-
ffi_lib :kernel32
|
62
|
-
attach_function_private :GetEnvironmentStringsW, [], :pointer
|
63
|
-
|
64
|
-
# https://msdn.microsoft.com/en-us/library/windows/desktop/ms683151(v=vs.85).aspx
|
65
|
-
# BOOL FreeEnvironmentStrings(
|
66
|
-
# _In_ LPTCH lpszEnvironmentBlock
|
67
|
-
# );
|
68
|
-
ffi_lib :kernel32
|
69
|
-
attach_function_private :FreeEnvironmentStringsW, [:pointer], :win32_bool
|
70
|
-
|
71
|
-
# https://msdn.microsoft.com/en-us/library/windows/desktop/ms686206(v=vs.85).aspx
|
72
|
-
# BOOL WINAPI SetEnvironmentVariableW(
|
73
|
-
# _In_ LPCTSTR lpName,
|
74
|
-
# _In_opt_ LPCTSTR lpValue
|
75
|
-
# );
|
76
|
-
ffi_lib :kernel32
|
77
|
-
attach_function_private :SetEnvironmentVariableW, [:lpcwstr, :lpcwstr],
|
78
|
-
:win32_bool
|
79
|
-
end
|
1
|
+
require 'pdk/util/windows'
|
2
|
+
|
3
|
+
module PDK::Util::Windows::Process
|
4
|
+
require 'ffi'
|
5
|
+
extend PDK::Util::Windows::String
|
6
|
+
extend FFI::Library
|
7
|
+
|
8
|
+
def environment_hash
|
9
|
+
env_ptr = GetEnvironmentStringsW()
|
10
|
+
|
11
|
+
contains_unicode_replacement = ->(string) do
|
12
|
+
return false unless string.include?("\uFFFD")
|
13
|
+
|
14
|
+
PDK.logger.warning _(
|
15
|
+
'Discarding environment variable %{string} which contains invalid ' \
|
16
|
+
'bytes',
|
17
|
+
) % { string: string }
|
18
|
+
true
|
19
|
+
end
|
20
|
+
|
21
|
+
# pass :invalid => :replace to the Ruby String#encode to use replacement
|
22
|
+
# characters
|
23
|
+
pairs = env_ptr.read_arbitrary_wide_string_up_to(65_534, :double_null, invalid: :replace)
|
24
|
+
.split(?\x00)
|
25
|
+
.reject { |env_str| env_str.nil? || env_str.empty? || env_str[0] == '=' }
|
26
|
+
.reject { |env_str| contains_unicode_replacement.call(env_str) }
|
27
|
+
.map { |env_pair| env_pair.split('=', 2) }
|
28
|
+
Hash[pairs]
|
29
|
+
ensure
|
30
|
+
if env_ptr && !env_ptr.null?
|
31
|
+
if FreeEnvironmentStringsW(env_ptr) == PDK::Util::Windows::WIN32_FALSE
|
32
|
+
PDK.logger.debug _('FreeEnvironmentStringsW memory leak')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
module_function :environment_hash
|
37
|
+
|
38
|
+
def set_environment_variable(name, val)
|
39
|
+
raise ArgumentError, _('Environment variable name must not be nil or empty') if name.nil? || name.empty?
|
40
|
+
|
41
|
+
FFI::MemoryPointer.from_string_to_wide_string(name) do |name_ptr|
|
42
|
+
if val.nil?
|
43
|
+
if SetEnvironmentVariableW(name_ptr, FFI::MemoryPointer::NULL) == PDK::Util::Windows::WIN32_FALSE
|
44
|
+
raise _('Failed to remove environment variable: %{name}') % { name: name }
|
45
|
+
end
|
46
|
+
else
|
47
|
+
FFI::MemoryPointer.from_string_to_wide_string(val) do |val_ptr|
|
48
|
+
if SetEnvironmentVariableW(name_ptr, val_ptr) == PDK::Util::Windows::WIN32_FALSE
|
49
|
+
raise _('Failed to set environment variaible: %{name}') % { name: name }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
module_function :set_environment_variable
|
56
|
+
|
57
|
+
ffi_convention :stdcall
|
58
|
+
|
59
|
+
# https://msdn.microsoft.com/en-us/library/windows/desktop/ms683187(v=vs.85).aspx
|
60
|
+
# LPTCH GetEnvironmentStrings(void);
|
61
|
+
ffi_lib :kernel32
|
62
|
+
attach_function_private :GetEnvironmentStringsW, [], :pointer
|
63
|
+
|
64
|
+
# https://msdn.microsoft.com/en-us/library/windows/desktop/ms683151(v=vs.85).aspx
|
65
|
+
# BOOL FreeEnvironmentStrings(
|
66
|
+
# _In_ LPTCH lpszEnvironmentBlock
|
67
|
+
# );
|
68
|
+
ffi_lib :kernel32
|
69
|
+
attach_function_private :FreeEnvironmentStringsW, [:pointer], :win32_bool
|
70
|
+
|
71
|
+
# https://msdn.microsoft.com/en-us/library/windows/desktop/ms686206(v=vs.85).aspx
|
72
|
+
# BOOL WINAPI SetEnvironmentVariableW(
|
73
|
+
# _In_ LPCTSTR lpName,
|
74
|
+
# _In_opt_ LPCTSTR lpValue
|
75
|
+
# );
|
76
|
+
ffi_lib :kernel32
|
77
|
+
attach_function_private :SetEnvironmentVariableW, [:lpcwstr, :lpcwstr],
|
78
|
+
:win32_bool
|
79
|
+
end
|