lseek() 对目录文件描述符意味着什么?

What does lseek() mean for a directory file descriptor?

根据 stracelseek(fd, 0, SEEK_END) = 9223372036854775807fd 指目录时。为什么这个系统调用会成功? lseek() 对目录 fd 意味着什么?

在我的测试系统上,如果您使用 opendir()readdir() 通过目录中的所有条目,telldir() 然后 returns 相同的值:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>

int main(int argc, char *argv[]) {
  int fd = open(".", O_RDONLY);
  if (fd < 0) {
    perror("open");
    return 1;
  }

  off_t o = lseek(fd, 0, SEEK_END);
  if (o == (off_t)-1) {
    perror("lseek");
    return 1;
  }

  printf("Via lseek: %ld\n", (long)o);
  close(fd);

  DIR *d = opendir(".");
  if (!d) {
    perror("opendir");
    return 1;
  }
  while (readdir(d)) {
  }

  printf("via telldir: %ld\n", telldir(d));
  closedir(d);

  return 0;
}

产出

Via lseek: 9223372036854775807
via telldir: 9223372036854775807

引自telldir(3) man page

In early filesystems, the value returned by telldir() was a simple file offset within a directory. Modern filesystems use tree or hash structures, rather than flat tables, to represent directories. On such filesystems, the value returned by telldir() (and used internally by readdir(3)) is a "cookie" that is used by the implementation to derive a position within a directory. Application programs should treat this strictly as an opaque value, making no assumptions about its contents.

这是一个神奇的数字,表示目录内容的索引在末尾。不要指望数字总是相同或便携。这是一个黑盒子。并坚持使用 dirent API 来遍历目录内容,除非你 真的 确切地知道你在做什么(在 Linux + glibc 的幕后,opendir(3) 在目录上调用 openat(2)readdir(3) 使用 getdents(2) 获取有关其内容的信息,seekdir(3) 调用 lseek(2),但这只是实现细节)