1

I thought all files were seekable, but I’m going through a codebase that handles cases where a file is not seekable.

Why is that necessary?

In what scenarios would a file be unseekable, and why?

1

1 Answer 1

8

In Unix, almost everything that you do data exchange with is treated like a file, but not everything is a file.

If you are handed an open file descriptor, it might be:

  • A regular file stored on a POSIX conforming file system (and most others) is always seekable
  • A character device (such as a tty) is generally not seekable
  • A block device, which is seekable
  • A FIFO which is not seekable
  • A socket of any type (unix, tcp, udp / stream, datagram), which is not seekable
  • A pipe, which is not seekable

All but the last two can be opened and treated almost like a regular file, until you try to seek on it. (The last two use a function call other than open() that is specific to their type)

All of the non-seekable types above are basically character streams that are consumable rather than repeatable, so seeking on them makes no sense.

10
  • Fifos are named pipes, not a distinct type, meaning that pipes still use open(). Only sockets don't. Commented Mar 17 at 5:03
  • What's an actual file? If you mean a regular file for which S_ISREG(mode) is true, then they are generally seekable, but you can find special filesystems (I've seen some fuse based ones) where they may not be. Commented Mar 17 at 7:13
  • 1
    Block devices are generally fully seekable, not only by block increment Commented Mar 17 at 7:14
  • A fifo may be called a named pipe, but in every other way it doesn't act like a pipe. It acts much more like a file than a pipe. Commented Mar 17 at 11:38
  • I would be interested to know the name of your hypothetical non-seekable filesystem. There's no point in listing ones that it does work for, because it's all the standard ones. Commented Mar 17 at 11:40

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.