1. Hideya OHASHI
  2. icedfs

Commits

Hideya OHASHI  committed 697a9ed

put: support multiple files

  • Participants
  • Parent commits 7cd6d99
  • Branches default

Comments (0)

Files changed (3)

File sdfs.rb

View file
  • Ignore whitespace
 require 'tempfile'
 require 'yaml'
 require 'uri'
+require 'find'
 
 class DFS
 
     @storagetable[num]
   end
 
-  def put(src, dest = nil)
+  def putimpl(src, dest)
     dest = File.basename(src) unless dest
     destdir = File.dirname(dest)
     return false if destdir != '.' and file?(destdir)
     true
   end
 
+  def put(srcanddest)
+    case srcanddest.size
+    when 1, 2
+      src = srcanddest[0]
+      dest = srcanddest[1]
+      if File.directory?(src)
+        dest = File.basename(src) unless dest
+        return false if file?(dest)
+        dest3 = dest
+        dest3 = '%s/%s' % [ dest, File.basename(src) ] if dir?(dest)
+        Find.find(src) do |i|
+          next if File.directory?(i)
+          dest2 = '%s/%s' % [ dest3, i[src.size + 1, i.size] ]
+          dircheck = dest2.split('/')
+          dircheck.pop
+          dirn = []
+          dircheck.each do |d|
+            dirn << d
+            return false if file?(dirn.join('/'))
+          end
+          return false unless putimpl(i, dest2)
+        end
+        true
+      else
+        putimpl(src, dest)
+      end
+    else
+      dest = srcanddest.pop
+      return false unless dir?(dest)
+      srcanddest.each do |src|
+        return false unless putimpl(src, dest)
+      end
+      true
+    end
+  end
+
   def get(name, verbose = true)
     index = ls(:raw => true)
     searchptn = /^#{name} /
 
   def put(src, digest)
     return false unless ssh("mkdir -p #{@path}/chunk")
-    return false unless system("scp #{src} #{@host}:#{@path}/chunk/#{digest}")
+    unless system("scp #{src} #{@host}:#{@path}/chunk/#{digest} " +
+        ">/dev/null 2>/dev/null")
+      return false
+    end
     true
   end
 
 
   def getspecial(name, dest = nil)
     dest = name unless dest
-    system("scp #{@host}:#{@path}/#{name} #{dest}")
+    system("scp #{@host}:#{@path}/#{name} #{dest} >/dev/null 2>/dev/null")
   end
 
   def rm(digest)

File test.rb

View file
  • Ignore whitespace
   def test_basic
     URLS.each {|url| @dfs.add(url) }
     @dfs.putconfig('tmp/config')
-    @dfs.put('tmp/a', 'd/a')
+    @dfs.put(['tmp/a', 'd/a'])
     lsstr = @dfs.ls
     @dfs.clear
     @dfs.add(URLS[0])
   def test_dir
     URLS.each {|url| @dfs.add(url) }
     @dfs.putconfig('tmp/config')
-    @dfs.put('tmp/a', 'd')
-    assert(@dfs.put('tmp/a', 'd/a') == false)
-    @dfs.put('tmp/a', 'd2/b')
-    @dfs.put('tmp/a', 'd2')
+    @dfs.put(['tmp/a', 'd'])
+    assert(@dfs.put(['tmp/a', 'd/a']) == false)
+    @dfs.put(['tmp/a', 'd2/b'])
+    @dfs.put(['tmp/a', 'd2'])
     assert_equal(EXPECTED_DIR, @dfs.ls(:raw => true))
   end
 
   def test_badchunk
     URLS.each {|url| @dfs.add(url) }
     @dfs.putconfig('tmp/config')
-    @dfs.put('tmp/a', 'a')
+    @dfs.put(['tmp/a', 'a'])
     assert(@dfs.check(true))
     digest = 'c865f6c5ab8d1b0bcd383a5e1e3879d22681c96bf462c269b7581d523fbe70ab'
     open("tmp/dfs/d/chunk/#{digest}", 'w') do |f|
       FileUtils.mv(url, 'tmp/datanode')
       FileUtils.touch(url)
       30.times do |i|
-        @dfs.put("tmp/#{i}")
+        @dfs.put(["tmp/#{i}"])
       end
       FileUtils.rm(url)
       FileUtils.mv('tmp/datanode', url)

File ui.rb

View file
  • Ignore whitespace
     f.write(dfs.get(src))
   end
 when 'put'
-  src = ARGV.shift
-  dest = ARGV.shift
-  if src == nil
+  if ARGV.size < 1
     puts 'error'
     exit 1
   end
-  put 'error' unless dfs.put(src, dest)
+  puts 'error' unless dfs.put(ARGV)
 when 'check'
   dfs.check
 else