Skip to content

Commit

Permalink
Leak fewer file descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradIrwin committed Dec 23, 2013
1 parent fc4a478 commit 2846396
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/dotgpg/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def read_key_file_for_add(file)
$stdin.reopen "/dev/tty"
end
elsif File.readable?(file)
key = Dotgpg.read_key(File.open(file))
key = Dotgpg.read_key(File.read(file))
end
end

Expand All @@ -187,7 +187,7 @@ def read_key_file_for_rm(file)
end

if File.readable?(file)
Dotgpg.read_key(File.open(file))
Dotgpg.read_key(File.read(file))
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/dotgpg/dir.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def known_keys
# @param [IO] output The IO to write to
# @return [Boolean] false if decryption failed for an understandable reason
def decrypt(path, output)
Dotgpg.decrypt File.open(path), output
File.open(path) do |f|
Dotgpg.decrypt f, output
end
true
rescue GPGME::Error::NoData, SystemCallError => e
Dotgpg.warn path, e
Expand Down Expand Up @@ -102,7 +104,9 @@ def reencrypt(files, &block)
yield tempfiles if block_given?

tempfiles.each_pair do |f, temp|
encrypt f, File.open(temp.path)
temp.open
temp.seek(0)
encrypt f, temp
end

nil
Expand Down
8 changes: 4 additions & 4 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
Dir.chdir @path do
@dotgpg.invoke(:add, [key_path])
end
assert Dotgpg::Dir.new(@path).has_key? Dotgpg.read_key(File.open(key_path))
assert Dotgpg::Dir.new(@path).has_key? Dotgpg.read_key(File.read(key_path))
end

it "should abort if the current working directory is not dotgpg" do
Expand Down Expand Up @@ -120,14 +120,14 @@
Dir.chdir @path do
@dotgpg.invoke :rm, [".gpg/[email protected]"]
end
refute Dotgpg::Dir.new(@path).has_key? Dotgpg.read_key(File.open($fixture + "add1.key"))
refute Dotgpg::Dir.new(@path).has_key? Dotgpg.read_key(File.read($fixture + "add1.key"))
end

it "should find the key by email" do
Dir.chdir @path do
@dotgpg.invoke :rm, ["[email protected]"]
end
refute Dotgpg::Dir.new(@path).has_key? Dotgpg.read_key(File.open($fixture + "add1.key"))
refute Dotgpg::Dir.new(@path).has_key? Dotgpg.read_key(File.read($fixture + "add1.key"))
end

it "should abort if the key doesn't exist" do
Expand All @@ -153,7 +153,7 @@
end

it "should remove a secret key if --force is given" do
key = Dotgpg.read_key(File.open(@path + ".gpg" + "[email protected]"))
key = Dotgpg.read_key(File.read(@path + ".gpg" + "[email protected]"))
Dir.chdir @path do
@dotgpg.invoke :rm, ["[email protected]"], force: true
end
Expand Down

0 comments on commit 2846396

Please sign in to comment.