about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-11-19 00:00:07 +0000
committerEric Wong <normalperson@yhbt.net>2011-11-19 00:00:07 +0000
commitcbce24009023b7cbd751c8c027281673d0cdaa27 (patch)
treee8a41c2736f170940bf822706bcf875d36628004
parent2ff4eacf708a3f2e42140a34568bf3792c063618 (diff)
downloadmogilefs-client-cbce24009023b7cbd751c8c027281673d0cdaa27.tar.gz
Similar to File.exist? for checking the existence of
a particular file.
-rwxr-xr-xbin/mog13
-rw-r--r--lib/mogilefs/mogilefs.rb9
2 files changed, 11 insertions, 11 deletions
diff --git a/bin/mog b/bin/mog
index e27ac12..5b38bb4 100755
--- a/bin/mog
+++ b/bin/mog
@@ -245,17 +245,8 @@ begin
       raise ArgumentError, "Too many arguments"
     end
 
-    begin
-      paths = mg.get_paths(key)
-      if test[:e]
-        ok = !!(paths && paths.size > 0)
-      else
-        raise ArgumentError, "Unknown flag: -#{test.keys.first}"
-      end
-    rescue MogileFS::Backend::UnknownKeyError
-      ok = false
-    end
-
+    test[:e] or raise ArgumentError, "Unknown flag: -#{test.keys.first}"
+    ok = mg.exist?(key)
     truth or ok = ! ok
     exit ok ? 0 : 1
   else
diff --git a/lib/mogilefs/mogilefs.rb b/lib/mogilefs/mogilefs.rb
index 8cda438..a3d88db 100644
--- a/lib/mogilefs/mogilefs.rb
+++ b/lib/mogilefs/mogilefs.rb
@@ -113,6 +113,15 @@ class MogileFS::MogileFS < MogileFS::Client
     (1..res['paths'].to_i).map { |i| res["path#{i}"] }
   end
 
+  # Returns +true+ if +key+ exists, +false+ if not
+  def exist?(key)
+    rv = nil
+    args = { :key => key, :domain => @domain }
+    @backend.pipeline_dispatch(:get_paths, args) { |x| rv = (Hash === x) }
+    @backend.pipeline_wait(1)
+    rv
+  end
+
   # Get the URIs for +key+ (paths) as URI::HTTP objects
   def get_uris(key, *args)
     get_paths(key, *args).map! { |path| URI.parse(path) }