From: Aditya Agarwal Date: Wed, 24 Jan 2007 02:24:35 +0000 (+0000) Subject: -- thrift logfile fixes X-Git-Tag: 0.2.0~1536 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=502ea8a3c835e5807f6980d8d1157d5fd0b3b657;p=common%2Fthrift.git -- thrift logfile fixes Summary: -- numChunks was broken and that was causing a lot of strange downstream behaviour Reviewed By: tbr slee git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664936 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/cpp/src/transport/TFileTransport.cpp b/lib/cpp/src/transport/TFileTransport.cpp index 05dd25d7..e4510ecf 100644 --- a/lib/cpp/src/transport/TFileTransport.cpp +++ b/lib/cpp/src/transport/TFileTransport.cpp @@ -557,21 +557,32 @@ void TFileTransport::seekToChunk(int32_t chunk) { throw TTransportException("File not open"); } - int32_t lastChunk = getNumChunks(); + int32_t numChunks = getNumChunks(); + + // file is empty, seeking to chunk is pointless + if (numChunks == 0) { + return; + } // negative indicates reverse seek (from the end) if (chunk < 0) { - chunk += lastChunk; + chunk += numChunks; + } + + // too large a value for reverse seek, just seek to beginning + if (chunk < 0) { + T_DEBUG("Incorrect value for reverse seek. Seeking to beginning...", chunk) + chunk = 0; } // cannot seek past EOF - if (chunk > lastChunk) { - T_DEBUG("Trying to seek past EOF. Seeking to EOF instead"); - chunk = lastChunk; - } - + bool seekToEnd = false; uint32_t minEndOffset = 0; - if (chunk == lastChunk) { + if (chunk >= numChunks) { + T_DEBUG("Trying to seek past EOF. Seeking to EOF instead..."); + seekToEnd = true; + chunk = numChunks - 1; + // this is the min offset to process events till minEndOffset = lseek(fd_, 0, SEEK_END); } @@ -583,13 +594,13 @@ void TFileTransport::seekToChunk(int32_t chunk) { } // seek to EOF if user wanted to go to last chunk - uint32_t oldReadTimeout = getReadTimeout(); - setReadTimeout(0); - if (chunk == lastChunk) { + if (seekToEnd) { + uint32_t oldReadTimeout = getReadTimeout(); + setReadTimeout(0); // keep on reading unti the last event at point of seekChunk call while( readEvent() && ((offset_ + readState_.bufferPtr_) < minEndOffset)) {}; + setReadTimeout(oldReadTimeout); } - setReadTimeout(oldReadTimeout); } @@ -603,7 +614,12 @@ uint32_t TFileTransport::getNumChunks() { } struct stat f_info; fstat(fd_, &f_info); - return (f_info.st_size)/chunkSize_; + if (f_info.st_size > 0) { + return ((f_info.st_size)/chunkSize_) + 1; + } + + // empty file has no chunks + return 0; } uint32_t TFileTransport::getCurChunk() {