From b587af4631b64667ad80fe329fc1aa27d8a73d1c Mon Sep 17 00:00:00 2001 From: Aaron Myers Date: Wed, 8 Aug 2012 19:11:30 +0000 Subject: [PATCH] HDFS-3710. libhdfs misuses O_RDONLY/WRONLY/RDWR. Contributed by Andy Isaacson. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1370898 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 2 ++ .../src/main/native/libhdfs/hdfs.c | 36 ++++++++++++------- .../native/libhdfs/test_libhdfs_threaded.c | 3 ++ 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index 7d67ab9a263..2439fccaaf9 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -568,6 +568,8 @@ Branch-2 ( Unreleased changes ) 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) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/hdfs.c b/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/hdfs.c index 63de10ac99a..3165b47b181 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/hdfs.c +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/hdfs.c @@ -657,6 +657,7 @@ hdfsFile hdfsOpenFile(hdfsFS fs, const char* path, int flags, */ /* 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 char* path, int flags, 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 char* path, int flags, } /* 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 char* path, int flags, 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 char* path, int flags, 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); diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/test_libhdfs_threaded.c b/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/test_libhdfs_threaded.c index f415957cfbf..06e0012a5e0 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/test_libhdfs_threaded.c +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/native/libhdfs/test_libhdfs_threaded.c @@ -80,6 +80,9 @@ static int doTestHdfsOperations(struct tlhThreadInfo *ti, hdfsFS fs) /* 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);