Skip to content

Commit 9c5e5ce

Browse files
ekohlbastelfreak
authored andcommitted
Avoid opening the file in postgresql_conf
Using File.open without closing it can leak file descriptors. It's actually not needed at all because File.foreach, File.readlines and File.write all accept a filename. This simplifies the code in the process.
1 parent 1a5ad40 commit 9c5e5ce

File tree

1 file changed

+11
-18
lines changed
  • lib/puppet/provider/postgresql_conf

1 file changed

+11
-18
lines changed

lib/puppet/provider/postgresql_conf/ruby.rb

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,13 @@
1313
# The function parses the postgresql.conf and figures out which active settings exist in a config file and returns an array of hashes
1414
#
1515
def parse_config
16-
# open the config file
17-
file = File.open(resource[:target])
1816
# regex to match active keys, values and comments
1917
active_values_regex = %r{^\s*(?<key>[\w.]+)\s*=?\s*(?<value>.*?)(?:\s*#\s*(?<comment>.*))?\s*$}
2018
# empty array to be filled with hashes
2119
active_settings = []
2220
# iterate the file and construct a hash for every matching/active setting
2321
# the hash is pushed to the array and the array is returned
24-
File.foreach(file).with_index do |line, index|
22+
File.foreach(resource[:target]).with_index do |line, index|
2523
line_number = index + 1
2624
matches = line.match(active_values_regex)
2725
if matches
@@ -63,12 +61,11 @@ def add_header(lines)
6361

6462
# This function writes the config file, it removes the old header, adds a new one and writes the file
6563
#
66-
# @param [File] the file object of the postgresql configuration file
6764
# @param [Array] lines of the parsed postgresql configuration file
68-
def write_config(file, lines)
65+
def write_config(lines)
6966
lines = delete_header(lines)
7067
lines = add_header(lines)
71-
File.write(file, lines.join)
68+
File.write(resource[:target], lines.join)
7269
end
7370

7471
# check, if resource exists in postgresql.conf file
@@ -85,23 +82,21 @@ def exists?
8582
# remove resource if exists and is set to absent
8683
def destroy
8784
entry_regex = %r{#{resource[:key]}.*=.*#{resource[:value]}}
88-
file = File.open(resource[:target])
89-
lines = File.readlines(file)
85+
lines = File.readlines(resource[:target])
9086

9187
lines.delete_if do |entry|
9288
entry.match?(entry_regex)
9389
end
94-
write_config(file, lines)
90+
write_config(lines)
9591
end
9692

9793
# create resource if it does not exists
9894
def create
99-
file = File.open(resource[:target])
100-
lines = File.readlines(file)
95+
lines = File.readlines(resource[:target])
10196
new_line = line(key: resource[:key], value: resource[:value], comment: resource[:comment])
10297

10398
lines.push(new_line)
104-
write_config(file, lines)
99+
write_config(lines)
105100
end
106101

107102
# getter - get value of a resource
@@ -116,30 +111,28 @@ def comment
116111

117112
# setter - set value of a resource
118113
def value=(_value)
119-
file = File.open(resource[:target])
120-
lines = File.readlines(file)
114+
lines = File.readlines(resource[:target])
121115
active_values_regex = %r{^\s*(?<key>[\w.]+)\s*=?\s*(?<value>.*?)(?:\s*#\s*(?<comment>.*))?\s*$}
122116
new_line = line(key: resource[:key], value: resource[:value], comment: resource[:comment])
123117

124118
lines.each_with_index do |line, index|
125119
matches = line.to_s.match(active_values_regex)
126120
lines[index] = new_line if matches && (matches[:key] == resource[:key] && matches[:value] != resource[:value])
127121
end
128-
write_config(file, lines)
122+
write_config(lines)
129123
end
130124

131125
# setter - set comment of a resource
132126
def comment=(_comment)
133-
file = File.open(resource[:target])
134-
lines = File.readlines(file)
127+
lines = File.readlines(resource[:target])
135128
active_values_regex = %r{^\s*(?<key>[\w.]+)\s*=?\s*(?<value>.*?)(?:\s*#\s*(?<comment>.*))?\s*$}
136129
new_line = line(key: resource[:key], value: resource[:value], comment: resource[:comment])
137130

138131
lines.each_with_index do |line, index|
139132
matches = line.to_s.match(active_values_regex)
140133
lines[index] = new_line if matches && (matches[:key] == resource[:key] && matches[:comment] != resource[:comment])
141134
end
142-
write_config(file, lines)
135+
write_config(lines)
143136
end
144137

145138
private

0 commit comments

Comments
 (0)