我正在尝试使用net-sftp库创建文件和目录树。
我可以使用.glob方法获得文件的递归列表,并使用.opendir方法确定其中一个结果是否是目录。
我已经能够创建一个包含文件的散列和另一个包含目录的散列,但我希望能够创建一个树。
files = []
directories = []
sftp.dir.glob("/home/**/**") do |entry|
fullpath = "/home/" + entry.name
file = Hash.new
file[:path] = fullpath
sftp.opendir(fullpath) do |response|
unless response.ok?
files.push(file)
else
directories.push(file)
end
end
else
end
end根据net-sftp返回的结果创建这样的树是可能的吗?
发布于 2016-08-28 00:26:22
我能够用下面的代码生成一个树:
def self.get_tree(host, username, password, path, name=nil)
data = {:text =>(name || path)}
data[:children] = children = []
Net::SFTP.start(host, username, :password => password) do |sftp|
sftp.dir.foreach(path) do |entry|
next if (entry.name == '..' || entry.name == '.')
if entry.longname.start_with?('d')
children << self.get_tree(host,username,password, path + entry.name + '/')
end
if !entry.longname.start_with?('d')
children << entry.name
end
end
end
end这是一个递归函数,当使用Net::SFTP给定目录路径时,它将创建一个完整的树。
https://stackoverflow.com/questions/28326041
复制相似问题