Skip to content

Return file type from readdir #13

Open
@sourcefrog

Description

Continuing the thread from #12 and sourcefrog/conserve#174.

In a tree that might have symlinks I'd want an option to open files with O_NOFOLLOW on Linux. (It's in Nix and libc; it doesn't seem to be in std?) (I filed #14.)

If the entry is a symlink, that will fail with ELOOP. I could then try to read it with readlinkat. If it then changes back to a file, readlinkat will give you an error.

I don't think I see the harm in DirEntry giving you an indication of the file type so that you know where to begin. There is a chance of a race, that when you read the dir it's a symlink and then it changes to be a file, or vice versa. But the worst that happens is that the call you use to read the file or link will fail. So I don't think the race is meaningful?

A different approach would be to begin with open O_PATH on Linux, and then you can stat it, find out what type of thing it is, and then proceed to either "really open it" or read the link. In this case you only pass the filename once, which seems nice.

O_PATH seems to be Linux specific https://stackoverflow.com/q/61337371.

This also seems to need more syscalls. The arguably straightforward way is

  1. getdents gives you the name and tells you it's a real file
  2. open the file (with O_NOFOLLOW)
  3. (do stuff then) close

If readdir does not tell you the type, on Linux, you can do

  1. getdents
  2. openat O_PATH
  3. fstatat tells you what you opened is a file
  4. openat "" on that path fd to actually open the file
  5. close the O_PATH fd
  6. (do stuff then) close the second fd

This just seems like more steps though.

On non-Linux Unix... actually I don't see how you can do anything other than attempt to open it or readlink it? But you might as well have a clue which of them is going to work?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions