Is there a POSIX syscall to resolve filesystem paths? I have the CWD for a path, as well as the path to a file from that CWD. I can't use chdir
to switch to the directory because I need to resolve paths from multiple threads simultaneously. I considered appending a /
in between the CWD and the path, but for some reason it feels like that's hacky. Is that the proper way to resolve relative paths?
views:
32answers:
2
+3
A:
I think appending the /
should be sufficient in pretty much all situations - even with .
,..
, extra /
s, or symlinks it should just do the right thing. If you really want to do this with the standard library, you can use realpath(3)
to do path normalization but I don't know of a way to do exactly what you want.
Steven Schlansker
2010-04-27 16:48:41
I'm pretty sure he wants realpath(), +1
Tim Post
2010-04-27 16:54:50
A:
Depending on what you want to do with the file, and if relatively recent additions in POSIX.1-2008 are acceptable for you, openat
and friends may be of interest:
int dirfd = open(desired_cwd_path, O_RDONLY);
int fd = openat(dirfd, file_relpath, O_RDONLY);
close(dirfd);
// ...use fd
(These system calls have existed on e.g. Solaris and Linux for quite some time.)
John Marshall
2010-04-27 22:36:24
Sigh... not available on Mac OS X, at least as of 10.5.7. Conversely, appropriately enough the Linux man page says "openat() and other similar system calls suffixed 'at' are supported for two reasons. [...] Second, openat() allows the implementation of a per-thread 'current working directory', via file descriptor(s) maintained by the application."
John Marshall
2010-04-27 23:00:13