bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

fgetname function?


From: Bruno Haible
Subject: fgetname function?
Date: Mon, 12 Dec 2022 03:41:31 +0100

Paul Eggert wrote:
> https://lists.gnu.org/archive/html/bug-gnulib/2018-03/msg00079.html
> https://lists.gnu.org/archive/html/bug-gnulib/2018-03/msg00080.html

This thread contains a posting by Jim Meyering, with an attachment
getcwdat.c, that contains a function for finding the name of a directory
given as a file descriptor.

I'm wondering if we should add a generalization of this function:

  char * fgetname (int fd, char *buf, size_t size);

that would return the current file name of a file descriptor, if
available.

This function would have limitations:
  - For non-directories, it would only work on specific operating
    systems (in particular, not on OpenBSD).
  - For directories, it may not work when an ancestor directory
    is not readable (same restriction as for getcwd).

Also, it would be prone to races, and thus discouraged for functional
code. But it might be a useful addition for debugging code.

The implementation would proceed as described in [1][2]:
  - On Linux, use readlink("/proc/self/fd/<fd>").
  - On macOS and NetBSD, use fcntl F_GETPATH.[3][4]
  - On native Windows, use GetFileInformationByHandleEx with FileNameInfo.[5]
  - On FreeBSD, use kinfo_getfile or sysctl KERN_PROC_FILEDESC [6].
  - Otherwise, on directories, use a loop, like in Jim's code:
      fd = openat (fd, "..", O_RDONLY);

I think it would be worth it, because nowadays there are few OSes
where this would not work (e.g. OpenBSD).

What do think?

Bruno

[1] https://stackoverflow.com/questions/1188757/
[2] 
https://www.wikitechy.com/tutorials/linux/how-to-get-filename-from-file-descriptor-linux-in-c
[3] 
https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fcntl.2.html
[4] https://man.netbsd.org/fcntl.2
[5] 
https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getfileinformationbyhandleex
[6] 
https://www.freebsd.org/cgi/man.cgi?apropos=0&sektion=3&query=kinfo_getfile&manpath=FreeBSD+7.0-current&format=html






reply via email to

[Prev in Thread] Current Thread [Next in Thread]