From owner-freebsd-hackers@FreeBSD.ORG Wed Nov 16 03:01:42 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 5BF3C1065670; Wed, 16 Nov 2011 03:01:42 +0000 (UTC) (envelope-from jamesbrandongooch@gmail.com) Received: from mail-ww0-f42.google.com (mail-ww0-f42.google.com [74.125.82.42]) by mx1.freebsd.org (Postfix) with ESMTP id A3E688FC0A; Wed, 16 Nov 2011 03:01:41 +0000 (UTC) Received: by wwe3 with SMTP id 3so623221wwe.1 for ; Tue, 15 Nov 2011 19:01:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=jeYNm+2ZiwsTRtGFEmbHD38nbh/qug9AcgBGdyVoQsg=; b=Stb2cKiRm0LNmpRHKMBcF8lyQRGB27/q/xASz0iCwhw4oaFuFZt7xNv5EMB2v+HFSX woCJMLXUg22XkOpKlFeyEWrm8aEKafBlaemHo2BZNmtj8oSaviRbXCCqUTV++ObmZ8Bx zv5va1gw4jisE1cl5S+VUKFUvHq4MhcYbw+5g= MIME-Version: 1.0 Received: by 10.216.24.93 with SMTP id w71mr211744wew.11.1321411020085; Tue, 15 Nov 2011 18:37:00 -0800 (PST) Received: by 10.216.63.143 with HTTP; Tue, 15 Nov 2011 18:36:59 -0800 (PST) Received: by 10.216.63.143 with HTTP; Tue, 15 Nov 2011 18:36:59 -0800 (PST) In-Reply-To: <20111115202450.GA73512@freebsd.org> References: <20111115202450.GA73512@freebsd.org> Date: Tue, 15 Nov 2011 20:36:59 -0600 Message-ID: From: Brandon Gooch To: Alexander Best Content-Type: text/plain; charset=ISO-8859-1 X-Content-Filtered-By: Mailman/MimeDel 2.1.5 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: Wed, 16 Nov 2011 03:01:42 -0000 On Nov 15, 2011 2:25 PM, "Alexander Best" wrote: > > hi there, > > one of the things i'm missing is an easy way to determine, whether a stream or > fd is seekable. i checked the dd(1) and hd(1) sources and those tools are > performing so much stuff just to find out if this is the case, and they still > are doing a very poor job. > > dd(1) e.g. identifies /dev/zero as non-seekable, even though it is. the result: > > `dd if=/dev/zero bs=1m count=1 skip=100000`: > > on freebsd: > 57,41 real 0,05 user 43,21 sys > > on openbsd: > 0,88 real 0,00 user 0,07 sys > > on freebsd dd(1) is very easy fixable (1 line diff). the point however is: > > there doesn't seem to exist a unified way of checking seekable == TRUE. so > every userspace application seems to do it differently and most of them (dd(1) > and hd(1) e.g) aren't doing it right. hd(1) e.g. believes that only regular > files are seekable. this means that hd(1) fired at /dev/ada* with a high skip > value takes ages! dd(1) is a bit smarter in this case, but still not correct. > > idealy there would be something like stat.st_mode (see stat(2)) where one > could simply do a S_ISSEEK(m). so far the best and easiest solution i've seen > is: > > fd = open(argv[1], O_RDONLY); > if (fd == -1) > exit(1); > if (lseek(fd, 0, SEEK_CUR) != -1) > printf ("%d is seekable.\n", fd); > else > printf ("%d is not seekable.\n", fd); > > seeing an application iterate through a stream or fd via getchar(), although > a simple seek() would work is very frustrating. i think what would be needed > is a simple function or macro that userspace applications can make use of. > maybe such a thing already exists in linux, openbsd, netbsd, dragonflybsd, > solaris or plan9? > > cheers. > alex > > references: > [1] http://www.mavetju.org/mail/view_thread.php?list=freebsd-hackers&id=3290708&thread=yes > [2] http://www.freebsd.org/cgi/query-pr.cgi?pr=152485 > [3] http://www.freebsd.org/cgi/query-pr.cgi?pr=86485 So, according to APUE 2nd Edition, seek should be supported on anything that's not a pipe, FIFO, or socket. In fact, the "test for seekability" you've provided above closely resembles the example given in that text. Need to think about this more before commenting further... -Brandon