使用 stat() 获取文件类型的替代方法? [英] Alternatives to using stat() to get file type?
问题描述
是否有任何可以确定文件类型的 stat(在大多数 Unix 系统上都可以找到)的替代方法?联机帮助页说调用 stat 很昂贵,我需要在我的应用中经常调用它.
Are there any alternatives to stat (which is found on most Unix systems) which can determine the file type? The manpage says that a call to stat is expensive, and I need to call it quite often in my app.
推荐答案
如果您已经打开了文件(因此您有文件描述符),则替代方案是 fstat()
.或者 lstat()
如果您想了解符号链接而不是符号链接指向的文件.
The alternative is fstat()
if you already have the file open (so you have a file descriptor for it). Or lstat()
if you want to find out about symbolic links rather than the file the symlink points to.
我认为手册页夸大了成本;它并不比任何其他必须将文件名解析为 inode 的系统调用差多少.它比 getpid()
成本更高;它比 open()
成本更低.
I think the man page is exaggerating the cost; it is not much worse than any other system call that has to resolve the name of the file into an inode. It is more costly than getpid()
; it is less costly than open()
.
这篇关于使用 stat() 获取文件类型的替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!