rapid7 / ruby_smb

A native Ruby implementation of the SMB Protocol Family
Other
81 stars 83 forks source link

Copy local file to remote #142

Open HarlemSquirrel opened 6 years ago

HarlemSquirrel commented 6 years ago

I see in the write_file example we can write some data to a file.

file = tree.open_file(filename: file, write: true, disposition: RubySMB::Dispositions::FILE_OVERWRITE_IF)

result = file.write(data: data)
puts result.to_s
file.close

If I'm copying from an existing file I could do something like this

file = tree.open_file(filename: file, write: true, disposition: RubySMB::Dispositions::FILE_OVERWRITE_IF)

data = File.read(source_file)
result = file.write(data: data)
puts result.to_s
file.close

Is there perhaps a better way to simply copy an existing file (and overwrite on the destination) without having to read the source file into memory? I'm looking to copy over a file with tens of thousands of lines.

Maybe something like this?

file = tree.open_file(filename: file, write: true, disposition: RubySMB::Dispositions::FILE_OVERWRITE_IF)

line_num = 0
data = File.foreach(source_file) do |line|
  if line_num.zero?
    file.write data: line
  else
    file.append data: line
  end
  line_num += 1
end

puts "Wrote #{line_num} lines"
file.close