From patchwork Wed May 12 14:31:35 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 52387 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id A8E40B7E01 for ; Thu, 13 May 2010 00:34:35 +1000 (EST) Received: from localhost ([127.0.0.1]:44018 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OCD16-0006rz-4s for incoming@patchwork.ozlabs.org; Wed, 12 May 2010 10:34:32 -0400 Received: from [140.186.70.92] (port=49052 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OCCyL-0005ln-80 for qemu-devel@nongnu.org; Wed, 12 May 2010 10:31:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OCCyH-00011g-UG for qemu-devel@nongnu.org; Wed, 12 May 2010 10:31:40 -0400 Received: from verein.lst.de ([213.95.11.210]:35378) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OCCyH-00011N-Js for qemu-devel@nongnu.org; Wed, 12 May 2010 10:31:37 -0400 Received: from verein.lst.de (localhost [127.0.0.1]) by verein.lst.de (8.12.3/8.12.3/Debian-7.1) with ESMTP id o4CEVZWY013741 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Wed, 12 May 2010 16:31:35 +0200 Received: (from hch@localhost) by verein.lst.de (8.12.3/8.12.3/Debian-7.2) id o4CEVZfp013740 for qemu-devel@nongnu.org; Wed, 12 May 2010 16:31:35 +0200 Date: Wed, 12 May 2010 16:31:35 +0200 From: Christoph Hellwig To: qemu-devel@nongnu.org Message-ID: <20100512143135.GA13686@lst.de> References: <20100507145519.GA14157@lst.de> <20100507145603.GB14245@lst.de> Mime-Version: 1.0 Content-Disposition: inline In-Reply-To: <20100507145603.GB14245@lst.de> User-Agent: Mutt/1.3.28i X-Scanned-By: MIMEDefang 2.39 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) Subject: [Qemu-devel] [PATCH 2/3 v2] dmg: use pread X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Use pread instead of lseek + read in preparation of using the qemu block API. Note that dmg actually uses the implicit file offset a lot in dmg_open, and we had to replace it with an offset variable. Signed-off-by: Christoph Hellwig Index: qemu-kevin/block/dmg.c =================================================================== --- qemu-kevin.orig/block/dmg.c 2010-05-12 16:19:59.586012160 +0200 +++ qemu-kevin/block/dmg.c 2010-05-12 16:23:07.510005522 +0200 @@ -58,18 +58,18 @@ static int dmg_probe(const uint8_t *buf, return 0; } -static off_t read_off(int fd) +static off_t read_off(int fd, int64_t offset) { uint64_t buffer; - if(read(fd,&buffer,8)<8) + if (pread(fd, &buffer, 8, offset) < 8) return 0; return be64_to_cpu(buffer); } -static off_t read_uint32(int fd) +static off_t read_uint32(int fd, int64_t offset) { uint32_t buffer; - if(read(fd,&buffer,4)<4) + if (pread(fd, &buffer, 4, offset) < 4) return 0; return be32_to_cpu(buffer); } @@ -80,6 +80,7 @@ static int dmg_open(BlockDriverState *bs off_t info_begin,info_end,last_in_offset,last_out_offset; uint32_t count; uint32_t max_compressed_size=1,max_sectors_per_chunk=1,i; + int64_t offset; s->fd = open(filename, O_RDONLY | O_BINARY); if (s->fd < 0) @@ -89,38 +90,45 @@ static int dmg_open(BlockDriverState *bs s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL; /* read offset of info blocks */ - if(lseek(s->fd,-0x1d8,SEEK_END)<0) { + offset = lseek(s->fd, -0x1d8, SEEK_END); + if (offset < 0) { goto fail; } - info_begin=read_off(s->fd); - if(info_begin==0) - goto fail; - if(lseek(s->fd,info_begin,SEEK_SET)<0) - goto fail; - if(read_uint32(s->fd)!=0x100) - goto fail; - if((count = read_uint32(s->fd))==0) - goto fail; - info_end = info_begin+count; - if(lseek(s->fd,0xf8,SEEK_CUR)<0) + info_begin = read_off(s->fd, offset); + if (info_begin == 0) { goto fail; + } + + if (read_uint32(s->fd, info_begin) != 0x100) { + goto fail; + } + + count = read_uint32(s->fd, info_begin + 4); + if (count == 0) { + goto fail; + } + info_end = info_begin + count; + + offset = info_begin + 0x100; /* read offsets */ last_in_offset = last_out_offset = 0; - while(lseek(s->fd,0,SEEK_CUR)fd); + count = read_uint32(s->fd, offset); if(count==0) goto fail; - type = read_uint32(s->fd); - if(type!=0x6d697368 || count<244) - lseek(s->fd,count-4,SEEK_CUR); - else { + offset += 4; + + type = read_uint32(s->fd, offset); + if (type == 0x6d697368 && count >= 244) { int new_size, chunk_count; - if(lseek(s->fd,200,SEEK_CUR)<0) - goto fail; + + offset += 4; + offset += 200; + chunk_count = (count-204)/40; new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count); s->types = qemu_realloc(s->types, new_size/2); @@ -130,7 +138,8 @@ static int dmg_open(BlockDriverState *bs s->sectorcounts = qemu_realloc(s->sectorcounts, new_size); for(i=s->n_chunks;in_chunks+chunk_count;i++) { - s->types[i] = read_uint32(s->fd); + s->types[i] = read_uint32(s->fd, offset); + offset += 4; if(s->types[i]!=0x80000005 && s->types[i]!=1 && s->types[i]!=2) { if(s->types[i]==0xffffffff) { last_in_offset = s->offsets[i-1]+s->lengths[i-1]; @@ -138,15 +147,23 @@ static int dmg_open(BlockDriverState *bs } chunk_count--; i--; - if(lseek(s->fd,36,SEEK_CUR)<0) - goto fail; + offset += 36; continue; } - read_uint32(s->fd); - s->sectors[i] = last_out_offset+read_off(s->fd); - s->sectorcounts[i] = read_off(s->fd); - s->offsets[i] = last_in_offset+read_off(s->fd); - s->lengths[i] = read_off(s->fd); + offset += 4; + + s->sectors[i] = last_out_offset+read_off(s->fd, offset); + offset += 8; + + s->sectorcounts[i] = read_off(s->fd, offset); + offset += 8; + + s->offsets[i] = last_in_offset+read_off(s->fd, offset); + offset += 8; + + s->lengths[i] = read_off(s->fd, offset); + offset += 8; + if(s->lengths[i]>max_compressed_size) max_compressed_size = s->lengths[i]; if(s->sectorcounts[i]>max_sectors_per_chunk) @@ -210,15 +227,12 @@ static inline int dmg_read_chunk(BDRVDMG case 0x80000005: { /* zlib compressed */ int i; - ret = lseek(s->fd, s->offsets[chunk], SEEK_SET); - if(ret<0) - return -1; - /* we need to buffer, because only the chunk as whole can be * inflated. */ i=0; do { - ret = read(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i); + ret = pread(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i, + s->offsets[chunk] + i); if(ret<0 && errno==EINTR) ret=0; i+=ret;