From owner-p4-projects@FreeBSD.ORG Mon Jul 16 12:41:42 2007 Return-Path: X-Original-To: p4-projects@freebsd.org Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id D696116A401; Mon, 16 Jul 2007 12:41:41 +0000 (UTC) X-Original-To: perforce@freebsd.org Delivered-To: perforce@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 625BC16A402 for ; Mon, 16 Jul 2007 12:41:41 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from speedfactory.net (mail6.speedfactory.net [66.23.216.219]) by mx1.freebsd.org (Postfix) with ESMTP id 1A14D13C4A8 for ; Mon, 16 Jul 2007 12:41:40 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from server.baldwin.cx (unverified [66.23.211.162]) by speedfactory.net (SurgeMail 3.7b8) with ESMTP id 196928936 for multiple; Mon, 16 Jul 2007 08:49:50 -0400 Received: from localhost.corp.yahoo.com (john@localhost [127.0.0.1]) (authenticated bits=0) by server.baldwin.cx (8.13.8/8.13.8) with ESMTP id l6GCfVK6031688; Mon, 16 Jul 2007 08:41:32 -0400 (EDT) (envelope-from jhb@freebsd.org) From: John Baldwin To: Garrett Cooper Date: Mon, 16 Jul 2007 08:39:52 -0400 User-Agent: KMail/1.9.6 References: <200707152011.l6FKBSOJ028661@repoman.freebsd.org> In-Reply-To: <200707152011.l6FKBSOJ028661@repoman.freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200707160839.52369.jhb@freebsd.org> X-Greylist: Sender succeeded SMTP AUTH authentication, not delayed by milter-greylist-2.0.2 (server.baldwin.cx [127.0.0.1]); Mon, 16 Jul 2007 08:41:32 -0400 (EDT) X-Virus-Scanned: ClamAV 0.88.3/3680/Mon Jul 16 01:49:06 2007 on server.baldwin.cx X-Virus-Status: Clean X-Spam-Status: No, score=-4.4 required=4.2 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.1.3 X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on server.baldwin.cx X-Server: High Performance Mail Server - http://surgemail.com r=1653887525 Cc: Perforce Change Reviews Subject: Re: PERFORCE change 123551 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 16 Jul 2007 12:41:42 -0000 On Sunday 15 July 2007 04:11:28 pm Garrett Cooper wrote: > http://perforce.freebsd.org/chv.cgi?CH=123551 > > Change 123551 by gcooper@optimus-revised_pkgtools on 2007/07/15 20:10:53 > > Bad case to exit on. A return code of 0 from read(2) good, -1 is bad.. 0 means EOF. You really want something like this: ssize_t nread; nread = read(fileno(fd), contents, sb.st_size); if (nread < 0) { cleanup(0); err(2, "%s: read('%s')", __func__, fname); } else if (nread != sb.st_size) { cleanup(0); errx(2, "%s: short read on '%s' (%zd of %ju bytes)", __func__, fname, nread, (uintmax_t)sb.st_size); } However, why not just mmap(2) the file rather than read it? struct stat sb; void *p; int fd; fd = open(fname, O_RDONLY); if (fd < 0) err(1, "open"); if (fstat(fd, &sb) < 0) err(1, "fstat"); p = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0); if (p == MAP_FAILED) err(1, "mmap"); Now p points to the file's contents, and it is more efficient than read(2) since you don't copy the data around as often. The only limitation is if the packing list file is ever going to be really big (say larger than 1GB) in which case you might run out of address space on 32-bit machines. However, if you are already reading the entire file into a single buffer then you aren't worried about that. -- John Baldwin