Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add check for regular files in os.walkDir and os.getFileInfo (Unix-specific) #20448

Closed
a-mr opened this issue Sep 28, 2022 · 4 comments
Closed

Comments

@a-mr
Copy link
Contributor

a-mr commented Sep 28, 2022

Summary

For nearly all applications we should distinguish regular files and special Unix files.

It's because reading from things like Unix FIFO (named pipe) may hang an application, reading device files may lead to side effects, etc.

For example, after running mkfifo <name> in a directory, nimgrep hangs there because of attempting to read it and not getting any data from FIFO.

Description

The proposal is

  • to add an isRegular field to FileInfo object returned by os.getFileInfo. For normal directories and files it will be true; for Unix FIFOs, Unix domain sockets, device files, etc it will be false.
  • to add an onlyRegular parameter to iterator os.walkDir. By default onlyRegular=false. When onlyRegular=true, only regular files will be yielded by the iterator.

Note that there is no performance penalty as necessary info is already returned by stat/lstat and readdir syscalls.

Alternatives

No response

Standard Output Examples

No response

Backwards Compatibility

This feature is backward compatible.

Links

No response

@juancarlospaco
Copy link
Collaborator

"Regular file" is kinda blurry in Linux IMHO, maybe check how other langs do it (?),
in AUR https://aur.archlinux.org/packages you can find all kind of exotic stuff,
like mounting HTTP stuff like "regular files", so checking that can be expensive.

@a-mr
Copy link
Contributor Author

a-mr commented Sep 28, 2022

I mean just superficial check, i.e. check DT_REG field or S_ISREG(s.st_mode) in s=readdir(...) and S_IFREG for stat/lstat. No checking for mounted filesystem is expected.
It's true that one can implement e.g. some tricky Linux FUSE filesystem which can have weird generated "files" even without storage, but formally they still can be regular files. And files on NFS are (typically) regular. I think that "regular" only means that

  1. read/write cannot block indefinitely in normal conditions
  2. there is no extra special ioctl features associated with the file.

But anyway it's up to filesystem writers to ensure any sane semantics.

@Araq
Copy link
Member

Araq commented Oct 7, 2022

"Regular file" is kinda blurry in Linux IMHO

Well we mean "real" file by it. A thing that is stored persistently on a physical device, not UNIX's ridiculously broken bullshit excuse for "we don't understand programming, automation or how to expose sane APIs".

Araq pushed a commit that referenced this issue Oct 25, 2022
* Implement Unix file regularity check

* update std/dirs also
@ringabout
Copy link
Member

I suppose it was implemented by #20628, feel free to reopen it.

capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
* Implement Unix file regularity check

* update std/dirs also
bung87 pushed a commit to bung87/Nim that referenced this issue Jul 29, 2023
* Implement Unix file regularity check

* update std/dirs also
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants