Revision aec96bfd
Added by Marc Dequènes over 15 years ago
- ID aec96bfd4618f88ab2fb03c7e629d1cf4aabebd3
bin/shadowwalker | ||
---|---|---|
end
|
||
cmdparser.add_command(cmd)
|
||
|
||
cmd = CmdParse::Command.new('getbin', false)
|
||
cmd.short_desc = "download object binary information"
|
||
cmd.set_execution_block do |args|
|
||
if args.size < 1
|
||
STDERR.puts "syntax error: no object name given"
|
||
exit 1
|
||
end
|
||
if args.size < 2
|
||
STDERR.puts "syntax error: no item name given"
|
||
exit 1
|
||
end
|
||
if args.size < 3
|
||
STDERR.puts "syntax error: no field name given"
|
||
exit 1
|
||
end
|
||
if args.size < 4
|
||
STDERR.puts "syntax error: no file name given"
|
||
exit 1
|
||
end
|
||
|
||
obj_hdl = args.shift
|
||
obj_klass = ldapctl.find_klass(obj_hdl)
|
||
if obj_klass.nil?
|
||
STDERR.puts "No such object '#{obj_hdl}'."
|
||
exit 2
|
||
end
|
||
|
||
item_hdl = args.shift
|
||
begin
|
||
item = obj_klass.find(item_hdl)
|
||
rescue ActiveLdap::EntryNotFound
|
||
STDERR.puts "No such item '#{obj_hdl}/#{item_hdl}'"
|
||
exit 2
|
||
end
|
||
|
||
field_info = args.shift
|
||
field_name, file_no = field_info.split("#")
|
||
file_no = file_no.to_i
|
||
|
||
unless item.has_attribute?(field_name)
|
||
STDERR.puts "No such field '#{field_name}' in object '#{obj_hdl}'."
|
||
exit 2
|
||
end
|
||
unless item.attribute_present?(field_name)
|
||
STDERR.puts "Field '#{field_name}' in item '#{obj_hdl}/#{item_hdl}' is not present."
|
||
exit 2
|
||
end
|
||
|
||
field_single = ActiveLdap::Base.schema.attribute(field_name).single_value?
|
||
if file_no.nil? and not field_single
|
||
STDERR.puts "Binary field '#{field_name}' in object '#{obj_hdl}' has multiple values, but you didn't select one."
|
||
exit 2
|
||
end
|
||
if file_no > 0 and field_single
|
||
STDERR.puts "Binary field '#{field_name}' in object '#{obj_hdl}' has only a single value"
|
||
exit 2
|
||
end
|
||
|
||
field_data = item.send(field_name)
|
||
if file_no < 0 and file_no >= field_data.size
|
||
STDERR.puts "Binary field '#{field_name}' in object '#{obj_hdl}' doesn't have such file number '#{file_no}' (select in range [0, #{file_no - 1}])"
|
||
exit 2
|
||
end
|
||
|
||
file_name = args.shift
|
||
if File.exists?(file_name)
|
||
STDERR.puts "File '#{file_name}' already exists, and i won't overwrite it."
|
||
exit 2
|
||
end
|
||
|
||
begin
|
||
open(file_name, "w") do |fp|
|
||
fp.write field_data
|
||
end
|
||
rescue
|
||
STDERR.puts "Cannot save file: " + $!
|
||
exit 3
|
||
end
|
||
|
||
puts "File saved."
|
||
end
|
||
cmdparser.add_command(cmd)
|
||
|
||
cmdparser.parse
|
Also available in: Unified diff
[evol] added binary fields download support ('getbin' command)