From owner-freebsd-hackers@FreeBSD.ORG Tue Jul 8 13:30:29 2003 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 2EE9737B401 for ; Tue, 8 Jul 2003 13:30:29 -0700 (PDT) Received: from dan.emsphone.com (dan.emsphone.com [199.67.51.101]) by mx1.FreeBSD.org (Postfix) with ESMTP id 584DD43F75 for ; Tue, 8 Jul 2003 13:30:28 -0700 (PDT) (envelope-from dan@dan.emsphone.com) Received: (from dan@localhost) by dan.emsphone.com (8.12.9/8.12.9) id h68KUQVQ042267; Tue, 8 Jul 2003 15:30:26 -0500 (CDT) (envelope-from dan) Date: Tue, 8 Jul 2003 15:30:26 -0500 From: Dan Nelson To: Jp Calderone Message-ID: <20030708203026.GM87950@dan.emsphone.com> References: <20030708192342.GA4899@intarweb.us> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20030708192342.GA4899@intarweb.us> X-OS: FreeBSD 5.1-CURRENT X-message-flag: Outlook Error User-Agent: Mutt/1.5.4i cc: hackers@freebsd.org Subject: Re: telldir()/seekdir() confusion X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 08 Jul 2003 20:30:29 -0000 In the last episode (Jul 08), Jp Calderone said: > I'm trying to work out some inconsistent behavior in my app across > platforms. On FreeBSD, seekdir() doesn't seem to behave as I expect > it to. Here's a short program that demonstrates my confusion: > > off_t pos; > dirp = opendir("."); > ent = readdir(dirp); > pos = telldir(dirp); > ent = readdir(dirp); > seekdir(dirp, pos); > printf("First telldir:%d\nSecond telldir:%d\n", pos, telldir(dirp)); > > On other platforms, the first and second telldir() return the same > value. On the two FreeBSD machines I've tried it on, the first > telldir() returns 1 and the second returns 0. > > Can anyone explain this? Well, your first problem is that pos is a long long, but you only printed a %d value. Change pos to a "long" type, and you'll get the correct result :) First telldir:1 Second telldir:2 But your basic question still stands: why aren't they the same? When telldir() is called, the base offset into the directory and filecount past that offset are stored in a linked list (see the getdirentries manpage; directory entries are read in blocks, so you don't necessarily know the absolute offset to a particular entry). A cookie value is returned to the user, and that value is used to look up the stored values when seekdir() is called later. Each call to telldir will return an integer one higher that the result of the previous call. I don't think there's any pstandard that says that telldir has to return a seekable file offset, or that consecutive calls on the same position must return the same value. Think about a filesystem that uses hashed or btree-based directories, for example; there may not be a way to store the correct position in a single "long" value without using a cookie like FreeBSD does. -- Dan Nelson dnelson@allantgroup.com