From owner-freebsd-hackers@FreeBSD.ORG Fri Nov 18 20:32:14 2011 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id BEF4B1065674; Fri, 18 Nov 2011 20:32:14 +0000 (UTC) (envelope-from tim@kientzle.com) Received: from monday.kientzle.com (99-115-135-74.uvs.sntcca.sbcglobal.net [99.115.135.74]) by mx1.freebsd.org (Postfix) with ESMTP id 9CAE08FC16; Fri, 18 Nov 2011 20:32:14 +0000 (UTC) Received: (from root@localhost) by monday.kientzle.com (8.14.4/8.14.4) id pAIKWEGC066606; Fri, 18 Nov 2011 20:32:14 GMT (envelope-from tim@kientzle.com) Received: from [192.168.2.119] (CiscoE3000 [192.168.1.65]) by kientzle.com with SMTP id uirspyp4pbtek6imj8ztbbhjca; Fri, 18 Nov 2011 20:32:14 +0000 (UTC) (envelope-from tim@kientzle.com) Mime-Version: 1.0 (Apple Message framework v1251.1) Content-Type: text/plain; charset=us-ascii From: Tim Kientzle In-Reply-To: <20111117214805.GA96937@freebsd.org> Date: Fri, 18 Nov 2011 12:32:13 -0800 Content-Transfer-Encoding: quoted-printable Message-Id: <34A2877A-977A-4668-8E37-642C6062AAC1@kientzle.com> References: <20111117175514.274040@gmx.com> <20111117214805.GA96937@freebsd.org> To: Alexander Best X-Mailer: Apple Mail (2.1251.1) Cc: freebsd-hackers@freebsd.org Subject: Re: easy way to determine if a stream or fd is seekable X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 18 Nov 2011 20:32:14 -0000 On Nov 17, 2011, at 1:48 PM, Alexander Best wrote: > On Thu Nov 17 11, Dieter BSD wrote: >>> lseek() on a tape drive does not return an error, nor does it >>> actually do anything. >>=20 >> IIRC some tape drives can seek, while others cannot. >> Vague memories that it is supposed to be possible to put a >> filesystem on a DECtape and mount the filesystem. >=20 > or how about the following: >=20 > 1) if the file argument we're seeking on is a tape drive, just do a = regular > seek operation. > 2) afterwards use ftell() to verify that the seek REALLY happend. if = it didn't, > return -1 and set errno =3D EBADF. ftell() can't tell whether the seek really happened or not. All it can do is ask the kernel, and the kernel doesn't know. Here is my current understanding: When you call lseek(), the kernel just stores the requested offset in the file descriptor. lseek() always succeeds because storing the requested offset in the file descriptor always succeeds. (Except that the kernel specially checks if the file descriptor is a pipe.) ftell() just obtains the data from the file descriptor. So it always succeeds and always returns the data from the previous seek request, regardless of whether that seek actually did anything. With the next read or write request, the device driver may inspect the offset information in the file descriptor. Or it can ignore it. Almost all tape device drivers ignore it. Filesystems uniformly support it. Disk drivers uniformly support it. Other drivers vary considerably. In short: No, there is no "easy way" to determine if an arbitrary stream or fd is seekable. If you try to create a function that determines this, be certain to include "Don't Know" as a possible return value. Tim