]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
java: support ceph_get_file_pool_name
authorNoah Watkins <noahwatkins@gmail.com>
Thu, 27 Dec 2012 23:55:07 +0000 (15:55 -0800)
committerNoah Watkins <noahwatkins@gmail.com>
Thu, 24 Jan 2013 20:54:00 +0000 (12:54 -0800)
Signed-off-by: Noah Watkins <noahwatkins@gmail.com>
src/java/java/com/ceph/fs/CephMount.java
src/java/native/libcephfs_jni.cc
src/java/test/com/ceph/fs/CephMountTest.java

index e1e1466a24d3c4b950d87c98a56ee8cd0506969e..6051cf635d0c3eabf12271157998afedc6725533 100644 (file)
@@ -885,6 +885,23 @@ public class CephMount {
 
   private static native int native_ceph_get_file_stripe_unit(long mountp, int fd);
 
+  /**
+   * Get the name of the pool a file is stored in.
+   *
+   * @param fd An open file descriptor.
+   * @return The pool name.
+   */
+  public String get_file_pool_name(int fd) {
+    rlock.lock();
+    try {
+      return native_ceph_get_file_pool_name(instance_ptr, fd);
+    } finally {
+      rlock.unlock();
+    }
+  }
+
+  private static native String native_ceph_get_file_pool_name(long mountp, int fd);
+
   /**
    * Get the replication of a file.
    *
index ff979f0b168f23727ab66683afd7290cfece0dc4..b04dc3f0aee09736c6bf59936b385fc4e5f01e6c 100644 (file)
@@ -2456,6 +2456,67 @@ JNIEXPORT jint JNICALL Java_com_ceph_fs_CephMount_native_1ceph_1get_1file_1repli
        return ret;
 }
 
+/*
+ * Class:     com_ceph_fs_CephMount
+ * Method:    native_ceph_get_file_pool_name
+ * Signature: (JI)Ljava/lang/String;
+ */
+JNIEXPORT jstring JNICALL Java_com_ceph_fs_CephMount_native_1ceph_1get_1file_1pool_1name
+  (JNIEnv *env, jclass clz, jlong j_mntp, jint j_fd)
+{
+  struct ceph_mount_info *cmount = get_ceph_mount(j_mntp);
+  CephContext *cct = ceph_get_mount_context(cmount);
+  jstring pool = NULL;
+  int ret, buflen = 0;
+  char *buf = NULL;
+
+  CHECK_MOUNTED(cmount, NULL);
+
+  ldout(cct, 10) << "jni: get_file_pool_name: fd " << (int)j_fd << dendl;
+
+  for (;;) {
+    /* get pool name length (len==0) */
+    ret = ceph_get_file_pool_name(cmount, (int)j_fd, NULL, 0);
+    if (ret < 0)
+      break;
+
+    /* allocate buffer */
+    if (buf)
+      delete [] buf;
+    buflen = ret;
+    buf = new (std::nothrow) char[buflen+1]; /* +1 for '\0' */
+    if (!buf) {
+        cephThrowOutOfMemory(env, "head allocation failed");
+        goto out;
+    }
+    memset(buf, 0, (buflen+1)*sizeof(*buf));
+
+    /* handle zero-length pool name!? */
+    if (buflen == 0)
+      break;
+
+    /* fill buffer */
+    ret = ceph_get_file_pool_name(cmount, (int)j_fd, buf, buflen);
+    if (ret == -ERANGE) /* size changed! */
+      continue;
+    else
+      break;
+  }
+
+  ldout(cct, 10) << "jni: get_file_pool_name: ret " << ret << dendl;
+
+  if (ret < 0)
+    handle_error(env, ret);
+  else
+    pool = env->NewStringUTF(buf);
+
+out:
+  if (buf)
+    delete [] buf;
+
+  return pool;
+}
+
 /*
  * Class:     com_ceph_fs_CephMount
  * Method:    native_ceph_localize_reads
index ca0db76255e0a67f75be68b2cffd9d3d49737f74..9d205121cc52b77005093054f8fc0205c1026c3f 100644 (file)
@@ -931,4 +931,21 @@ public class CephMountTest {
     assertTrue(poolid >= 0);
     assertTrue(mount.get_pool_replication(poolid) > 0);
   }
+
+  @Test
+  public void test_get_file_pool_name() throws Exception {
+    String path = makePath();
+    int fd = createFile(path, 1);
+    String pool = mount.get_file_pool_name(fd);
+    mount.close(fd);
+    assertTrue(pool != null);
+    /* assumes using default data pool "data" */
+    assertTrue(pool.compareTo("data") == 0);
+    mount.unlink(path);
+  }
+
+  @Test(expected=IOException.class)
+  public void test_get_file_pool_name_ebadf() throws Exception {
+    String pool = mount.get_file_pool_name(-40);
+  }
 }