Return-Path: Delivered-To: apmail-lucene-hadoop-commits-archive@locus.apache.org Received: (qmail 7899 invoked from network); 4 Jun 2007 16:47:46 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 4 Jun 2007 16:47:46 -0000 Received: (qmail 67577 invoked by uid 500); 4 Jun 2007 16:47:50 -0000 Delivered-To: apmail-lucene-hadoop-commits-archive@lucene.apache.org Received: (qmail 67553 invoked by uid 500); 4 Jun 2007 16:47:50 -0000 Mailing-List: contact hadoop-commits-help@lucene.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: hadoop-dev@lucene.apache.org Delivered-To: mailing list hadoop-commits@lucene.apache.org Received: (qmail 67544 invoked by uid 99); 4 Jun 2007 16:47:50 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 Jun 2007 09:47:49 -0700 X-ASF-Spam-Status: No, hits=-99.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 Jun 2007 09:47:45 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id 4C6411A981D; Mon, 4 Jun 2007 09:47:25 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r544181 - in /lucene/hadoop/trunk: CHANGES.txt src/java/org/apache/hadoop/dfs/DFSClient.java src/java/org/apache/hadoop/dfs/FSNamesystem.java src/test/org/apache/hadoop/dfs/TestPread.java Date: Mon, 04 Jun 2007 16:47:24 -0000 To: hadoop-commits@lucene.apache.org From: cutting@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070604164725.4C6411A981D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: cutting Date: Mon Jun 4 09:47:20 2007 New Revision: 544181 URL: http://svn.apache.org/viewvc?view=rev&rev=544181 Log: HADOOP-1443. Fix a bug opening zero-length files in HDFS. Contributed by Konstantin. Modified: lucene/hadoop/trunk/CHANGES.txt lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/DFSClient.java lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/FSNamesystem.java lucene/hadoop/trunk/src/test/org/apache/hadoop/dfs/TestPread.java Modified: lucene/hadoop/trunk/CHANGES.txt URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/CHANGES.txt?view=diff&rev=544181&r1=544180&r2=544181 ============================================================================== --- lucene/hadoop/trunk/CHANGES.txt (original) +++ lucene/hadoop/trunk/CHANGES.txt Mon Jun 4 09:47:20 2007 @@ -519,6 +519,9 @@ to a long, permitting map outputs to exceed 2^31 bytes. (omalley via cutting) +133. HADOOP-1443. Fix a bug opening zero-length files in HDFS. + (Konstantin Shvachko via cutting) + Release 0.12.3 - 2007-04-06 Modified: lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/DFSClient.java URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/DFSClient.java?view=diff&rev=544181&r1=544180&r2=544181 ============================================================================== --- lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/DFSClient.java (original) +++ lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/DFSClient.java Mon Jun 4 09:47:20 2007 @@ -961,7 +961,7 @@ throw new IOException("Stream closed"); } long filelen = getFileLength(); - if ((position < 0) || (position > filelen)) { + if ((position < 0) || (position >= filelen)) { return -1; } int realLen = length; Modified: lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/FSNamesystem.java URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/FSNamesystem.java?view=diff&rev=544181&r1=544180&r2=544181 ============================================================================== --- lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/FSNamesystem.java (original) +++ lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/FSNamesystem.java Mon Jun 4 09:47:20 2007 @@ -432,7 +432,14 @@ synchronized LocatedBlocks getBlockLocations(String clientMachine, String src, long offset, - long length) { + long length + ) throws IOException { + if (offset < 0) { + throw new IOException("Negative offset is not supported. File: " + src ); + } + if (length < 0) { + throw new IOException("Negative length is not supported. File: " + src ); + } return getBlockLocations(clientMachine, dir.getFileINode(src), offset, length, Integer.MAX_VALUE); @@ -442,7 +449,8 @@ FSDirectory.INode inode, long offset, long length, - int nrBlocksToReturn) { + int nrBlocksToReturn + ) throws IOException { if(inode == null || inode.isDir()) { return null; } @@ -450,18 +458,24 @@ if (blocks == null) { return null; } + assert blocks.length > 0 : "Array of blocks is empty."; List results; results = new ArrayList(blocks.length); int curBlk = 0; long curPos = 0, blkSize = 0; - for (curBlk = 0; curBlk < blocks.length; curBlk++) { + int nrBlocks = (blocks[0].getNumBytes() == 0) ? 0 : blocks.length; + for (curBlk = 0; curBlk < nrBlocks; curBlk++) { blkSize = blocks[curBlk].getNumBytes(); + assert blkSize > 0 : "Block of size 0"; if (curPos + blkSize > offset) { break; } curPos += blkSize; } + + if (nrBlocks > 0 && curBlk == nrBlocks) // offset >= end of file + return null; long endOff = offset + length; Modified: lucene/hadoop/trunk/src/test/org/apache/hadoop/dfs/TestPread.java URL: http://svn.apache.org/viewvc/lucene/hadoop/trunk/src/test/org/apache/hadoop/dfs/TestPread.java?view=diff&rev=544181&r1=544180&r2=544181 ============================================================================== --- lucene/hadoop/trunk/src/test/org/apache/hadoop/dfs/TestPread.java (original) +++ lucene/hadoop/trunk/src/test/org/apache/hadoop/dfs/TestPread.java Mon Jun 4 09:47:20 2007 @@ -38,7 +38,25 @@ // create and write a file that contains three blocks of data DataOutputStream stm = fileSys.create(name, true, 4096, (short)1, (long)blockSize); + // test empty file open and read + stm.close(); + FSDataInputStream in = fileSys.open(name); byte[] buffer = new byte[(int)(12*blockSize)]; + in.readFully(0, buffer, 0, 0); + IOException res = null; + try { // read beyond the end of the file + in.readFully(0, buffer, 0, 1); + } catch (IOException e) { + // should throw an exception + res = e; + } + assertTrue("Error reading beyond file boundary.", res != null); + in.close(); + if (!fileSys.delete(name)) + assertTrue("Cannot delete file", false); + + // now create the real file + stm = fileSys.create(name, true, 4096, (short)1, (long)blockSize); Random rand = new Random(seed); rand.nextBytes(buffer); stm.write(buffer); @@ -112,6 +130,17 @@ actual = new byte[8*4096]; stm.readFully(3*blockSize, actual, 0, 8*4096); checkAndEraseData(actual, 3*blockSize, expected, "Pread Test 8"); + // read the tail + stm.readFully(11*blockSize+blockSize/2, actual, 0, blockSize/2); + IOException res = null; + try { // read beyond the end of the file + stm.readFully(11*blockSize+blockSize/2, actual, 0, blockSize); + } catch (IOException e) { + // should throw an exception + res = e; + } + assertTrue("Error reading beyond file boundary.", res != null); + stm.close(); }