扫描目录并获取最新匹配的创建时间
在 Linux 下,我可以使用 opendir 打开目录,然后使用 readdir 来获取文件名。
我一直在尝试使用 scandir 并认为“太好了,我可以通过传入自定义过滤器来搜索此目录中我想要的文件”,并使用自定义排序进行排序,我想按创建日期进行排序。但后来我意识到当前的结构是多么有限。它只包含最少的信息。
这是唯一可能的 API 吗?即我是否必须统计每个文件才能获取其大小以进行排序?这就是 ls -t 的工作原理吗?
Under Linux I can open a directory using opendir and then use readdir to get the filenames.
I have been experimenting with scandir and thought "great I can search for the files in this directory that I want by passing in a custom filter", and sort using a custom sort where I want to sort by creation date. But then I realised how limited the dirent structure is. It contains only minimal information.
Is this the only API possible? i.e. do I have to stat every single file to get it's size for sorting? Is this how ls -t works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这确实是 ls -t 的工作原理,正如“strace ls -t”将证实的那样。从历史上看,UNIX 目录只是一个包含文件名列表的特殊文件,应用程序应该自己读取和解析该“文件”。当然,当开发出扩展文件名固定长度的新文件系统时,这会导致问题,因此开发了 opendir/readdir/filledir 接口来抽象文件系统目录实现。但目录列表中直接可用内容的限制仍然存在。
That is, indeed, how ls -t works, as 'strace ls -t' will confirm. Historically, a UNIX directory was just a special file containing a list of file names, and applications were expected to read and parse that "file" themselves. Naturally, that led to problems when newer file systems were developed that expanded the fixed length of file names, so the opendir/readdir/closedir interface was developed to abstract away the filesystem directory implementation. But the limitation on what is directly available in a directory listing remains.
POSIX 没有任何存储创建时间的工具,更不用说检索它了。
POSIX does not have any facility for storing creation time, much less retrieving it.