瀏覽代碼

HADOOP-520. Fix a bug in libhdfs, where write failures were not correctly returning error codes. Contributed by Arun.

git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk@442668 13f79535-47bb-0310-9956-ffa450edef68
Doug Cutting 19 年之前
父節點
當前提交
9a3b97ad9f
共有 2 個文件被更改,包括 28 次插入15 次删除
  1. 6 0
      CHANGES.txt
  2. 22 15
      src/c++/libhdfs/hdfs.c

+ 6 - 0
CHANGES.txt

@@ -1,6 +1,12 @@
 Hadoop Change Log
 Hadoop Change Log
 
 
 
 
+Trunk (unreleased changes)
+
+ 1. HADOOP-520.  Fix a bug in libhdfs, where write failures were not
+    correctly returning error codes.  (Arun C Murthy via cutting)
+
+
 Release 0.6.0 - 2006-08-08
 Release 0.6.0 - 2006-08-08
 
 
  1. HADOOP-427.  Replace some uses of DatanodeDescriptor in the DFS
  1. HADOOP-427.  Replace some uses of DatanodeDescriptor in the DFS

+ 22 - 15
src/c++/libhdfs/hdfs.c

@@ -574,13 +574,17 @@ tSize hdfsWrite(hdfsFS fs, hdfsFile f, const void* buffer, tSize length)
 
 
     jthrowable jException;
     jthrowable jException;
     jbyteArray jbWarray;
     jbyteArray jbWarray;
-    jint noWrittenBytes = 0;
 
 
     //Sanity check
     //Sanity check
     if (!f || f->type == UNINITIALIZED) {
     if (!f || f->type == UNINITIALIZED) {
         errno = EBADF;
         errno = EBADF;
         return -1;
         return -1;
     }
     }
+    
+    if (length < 0) {
+    	errno = EINVAL;
+    	return -1;
+    }
 
 
     //Error checking... make sure that this file is 'writable'
     //Error checking... make sure that this file is 'writable'
     if (f->type != OUTPUT) {
     if (f->type != OUTPUT) {
@@ -589,20 +593,23 @@ tSize hdfsWrite(hdfsFS fs, hdfsFile f, const void* buffer, tSize length)
         return -1;
         return -1;
     }
     }
 
 
-    //Write the requisite bytes into the file
-    jbWarray = (*env)->NewByteArray(env, length);
-    (*env)->SetByteArrayRegion(env, jbWarray, 0, length, buffer);
-    if (invokeMethod(env, NULL, &jException, INSTANCE, jOutputStream,
-                "org/apache/hadoop/fs/FSDataOutputStream", "write", 
-                "([B)V", jbWarray)) {
-        fprintf(stderr, 
-            "Call to org.apache.hadoop.fs.FSDataOutputStream::write failed!\n"
-            );
-        errno = EINTERNAL;
-        noWrittenBytes = -1;
-    } 
-    (*env)->ReleaseByteArrayElements(env, jbWarray, 
-                (*env)->GetByteArrayElements(env, jbWarray, 0), JNI_ABORT);
+	// 'length' equals 'zero' is a valid use-case according to Posix!
+	if (length != 0) {
+	    //Write the requisite bytes into the file
+	    jbWarray = (*env)->NewByteArray(env, length);
+	    (*env)->SetByteArrayRegion(env, jbWarray, 0, length, buffer);
+	    if (invokeMethod(env, NULL, &jException, INSTANCE, jOutputStream,
+	                "org/apache/hadoop/fs/FSDataOutputStream", "write", 
+	                "([B)V", jbWarray)) {
+	        fprintf(stderr, 
+	            "Call to org.apache.hadoop.fs.FSDataOutputStream::write failed!\n"
+	            );
+	        errno = EINTERNAL;
+	        length = -1;
+	    } 
+	    (*env)->ReleaseByteArrayElements(env, jbWarray, 
+	                (*env)->GetByteArrayElements(env, jbWarray, 0), JNI_ABORT);
+	}
 
 
     //Return no. of bytes succesfully written (libc way)
     //Return no. of bytes succesfully written (libc way)
     //i.e. 'length' itself! ;-)
     //i.e. 'length' itself! ;-)