You are viewing a plain text version of this content. The canonical link for it is here.
Posted to hdfs-commits@hadoop.apache.org by at...@apache.org on 2012/08/08 21:33:03 UTC

svn commit: r1370906 - in /hadoop/common/branches/branch-2/hadoop-hdfs-project/hadoop-hdfs: CHANGES.txt src/main/native/libhdfs/hdfs.c src/main/native/libhdfs/test_libhdfs_threaded.c

Author: atm
Date: Wed Aug  8 19:33:03 2012
New Revision: 1370906

URL: http://svn.apache.org/viewvc?rev=1370906&view=rev
Log:
HDFS-3710. libhdfs misuses O_RDONLY/WRONLY/RDWR. Contributed by Andy Isaacson.

Modified:
    hadoop/common/branches/branch-2/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt
    hadoop/common/branches/branch-2/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/hdfs.c
    hadoop/common/branches/branch-2/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/test_libhdfs_threaded.c

Modified: hadoop/common/branches/branch-2/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-2/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt?rev=1370906&r1=1370905&r2=1370906&view=diff
==============================================================================
--- hadoop/common/branches/branch-2/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt (original)
+++ hadoop/common/branches/branch-2/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt Wed Aug  8 19:33:03 2012
@@ -414,6 +414,8 @@ Release 2.0.1-alpha - UNRELEASED
 
     HDFS-3760. primitiveCreate is a write, not a read. (Andy Isaacson via atm)
 
+    HDFS-3710. libhdfs misuses O_RDONLY/WRONLY/RDWR. (Andy Isaacson via atm)
+
   BREAKDOWN OF HDFS-3042 SUBTASKS
 
     HDFS-2185. HDFS portion of ZK-based FailoverController (todd)

Modified: hadoop/common/branches/branch-2/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/hdfs.c
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-2/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/hdfs.c?rev=1370906&r1=1370905&r2=1370906&view=diff
==============================================================================
--- hadoop/common/branches/branch-2/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/hdfs.c (original)
+++ hadoop/common/branches/branch-2/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/hdfs.c Wed Aug  8 19:33:03 2012
@@ -657,6 +657,7 @@ hdfsFile hdfsOpenFile(hdfsFS fs, const c
     */
     /* Get the JNIEnv* corresponding to current thread */
     JNIEnv* env = getJNIEnv();
+    int accmode = flags & O_ACCMODE;
 
     if (env == NULL) {
       errno = EINTERNAL;
@@ -672,10 +673,16 @@ hdfsFile hdfsOpenFile(hdfsFS fs, const c
     hdfsFile file = NULL;
     int ret;
 
-    if (flags & O_RDWR) {
+    if (accmode == O_RDONLY || accmode == O_WRONLY) {
+	/* yay */
+    } else if (accmode == O_RDWR) {
       fprintf(stderr, "ERROR: cannot open an hdfs file in O_RDWR mode\n");
       errno = ENOTSUP;
       return NULL;
+    } else {
+      fprintf(stderr, "ERROR: cannot open an hdfs file in mode 0x%x\n", accmode);
+      errno = EINVAL;
+      return NULL;
     }
 
     if ((flags & O_CREAT) && (flags & O_EXCL)) {
@@ -683,12 +690,19 @@ hdfsFile hdfsOpenFile(hdfsFS fs, const c
     }
 
     /* The hadoop java api/signature */
-    const char* method = ((flags & O_WRONLY) == 0) ? "open" : (flags & O_APPEND) ? "append" : "create";
-    const char* signature = ((flags & O_WRONLY) == 0) ?
-        JMETHOD2(JPARAM(HADOOP_PATH), "I", JPARAM(HADOOP_ISTRM)) :
-      (flags & O_APPEND) ?
-      JMETHOD1(JPARAM(HADOOP_PATH), JPARAM(HADOOP_OSTRM)) :
-      JMETHOD2(JPARAM(HADOOP_PATH), "ZISJ", JPARAM(HADOOP_OSTRM));
+    const char* method = NULL;
+    const char* signature = NULL;
+
+    if (accmode == O_RDONLY) {
+	method = "open";
+        signature = JMETHOD2(JPARAM(HADOOP_PATH), "I", JPARAM(HADOOP_ISTRM));
+    } else if (flags & O_APPEND) {
+	method = "append";
+	signature = JMETHOD1(JPARAM(HADOOP_PATH), JPARAM(HADOOP_OSTRM));
+    } else {
+	method = "create";
+	signature = JMETHOD2(JPARAM(HADOOP_PATH), "ZISJ", JPARAM(HADOOP_OSTRM));
+    }
 
     /* Create an object of org.apache.hadoop.fs.Path */
     jthr = constructNewObjectOfPath(env, path, &jPath);
@@ -741,9 +755,7 @@ hdfsFile hdfsOpenFile(hdfsFS fs, const c
         jBufferSize = jVal.i;
     }
 
-    if ((flags & O_WRONLY) && (flags & O_APPEND) == 0) {
-        //replication
-
+    if ((accmode == O_WRONLY) && (flags & O_APPEND) == 0) {
         if (!replication) {
             jthr = invokeMethod(env, &jVal, INSTANCE, jConfiguration, 
                              HADOOP_CONF, "getInt", "(Ljava/lang/String;I)I",
@@ -776,10 +788,10 @@ hdfsFile hdfsOpenFile(hdfsFS fs, const c
        FSDataOutputStream references jobject jStream */
 
     // READ?
-    if ((flags & O_WRONLY) == 0) {
+    if (accmode == O_RDONLY) {
         jthr = invokeMethod(env, &jVal, INSTANCE, jFS, HADOOP_FS,
                        method, signature, jPath, jBufferSize);
-    }  else if ((flags & O_WRONLY) && (flags & O_APPEND)) {
+    }  else if ((accmode == O_WRONLY) && (flags & O_APPEND)) {
         // WRITE/APPEND?
        jthr = invokeMethod(env, &jVal, INSTANCE, jFS, HADOOP_FS,
                        method, signature, jPath);

Modified: hadoop/common/branches/branch-2/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/test_libhdfs_threaded.c
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-2/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/test_libhdfs_threaded.c?rev=1370906&r1=1370905&r2=1370906&view=diff
==============================================================================
--- hadoop/common/branches/branch-2/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/test_libhdfs_threaded.c (original)
+++ hadoop/common/branches/branch-2/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/test_libhdfs_threaded.c Wed Aug  8 19:33:03 2012
@@ -80,6 +80,9 @@ static int doTestHdfsOperations(struct t
     /* There should not be any file to open for reading. */
     EXPECT_NULL(hdfsOpenFile(fs, tmp, O_RDONLY, 0, 0, 0));
 
+    /* hdfsOpenFile should not accept mode = 3 */
+    EXPECT_NULL(hdfsOpenFile(fs, tmp, 3, 0, 0, 0));
+
     file = hdfsOpenFile(fs, tmp, O_WRONLY, 0, 0, 0);
     EXPECT_NONNULL(file);