Skip to content
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

Close the opened directory after listing it's contents #257

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/ruby_smb/smb2/tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def open_file(opts)
# @raise [RubySMB::Error::InvalidPacket] if the response is not a QueryDirectoryResponse packet
def list(directory: nil, pattern: '*', type: RubySMB::Fscc::FileInformation::FileIdFullDirectoryInformation)
create_response = open_directory(directory: directory)
opened_directory = RubySMB::SMB2::File.new(tree: self, response: create_response, name: directory)
file_id = create_response.file_id

directory_request = RubySMB::SMB2::Packet::QueryDirectoryRequest.new
Expand Down Expand Up @@ -127,8 +128,9 @@ def list(directory: nil, pattern: '*', type: RubySMB::Fscc::FileInformation::Fil
# Reset the message id so the client can update appropriately.
directory_request.smb2_header.message_id = 0
end

files
ensure
opened_directory.close
adfoster-r7 marked this conversation as resolved.
Show resolved Hide resolved
end

# 'Opens' a directory file on the remote end, using a CreateRequest. This
Expand Down
5 changes: 5 additions & 0 deletions spec/lib/ruby_smb/smb2/tree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,14 @@
let(:create_res) { double('create response') }
let(:query_dir_req) { RubySMB::SMB2::Packet::QueryDirectoryRequest.new }
let(:query_dir_res) { RubySMB::SMB2::Packet::QueryDirectoryResponse.new }
let(:open_dir) { instance_double(RubySMB::SMB2::File) }

before :example do
allow(tree).to receive(:open_directory).and_return(create_res)
allow(create_res).to receive(:file_id)
allow(RubySMB::SMB2::File).to receive(:new).and_return(open_dir)
allow(open_dir).to receive(:close)
allow(create_res).to receive(:file_attributes)
allow(RubySMB::SMB2::Packet::QueryDirectoryRequest).to receive(:new).and_return(query_dir_req)
allow(client).to receive(:send_recv)
allow(RubySMB::SMB2::Packet::QueryDirectoryResponse).to receive(:read).and_return(query_dir_res)
Expand All @@ -180,6 +184,7 @@
dir = '/dir'
expect(tree).to receive(:open_directory).with(directory: dir).and_return(create_res)
tree.list(directory: dir)
expect(open_dir).to have_received(:close)
end

it 'uses the File ID from the create response' do
Expand Down
Loading