为什么目录列表包含当前(。)和父(..)目录? [英] Why do directory listings contain the current (.) and parent (..) directory?

查看:116
本文介绍了为什么目录列表包含当前(。)和父(..)目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我用像readdir这样的函数列出一个目录的内容时,返回的文件名也包含。。和..。我怀疑这些只是文件系统中的正常链接,因此与实际文件无法区分,但我总是必须将其过滤掉,因为它们不是我列出的目录中的实际对象。像readdir这样的函数有一个很好的理由来包含它们吗?一些操作系统或文件系统是否包含更多或不同的虚拟文件名?除了用。进行字符串比较之外,还有更好的方法来滤除它们。和..?



更新:谢谢大家的回答。我想我一直认为像./和..这样的东西只不过是可以通过搜索和替换来处理的惯例。我觉得有点让人惊讶,虽然可能更高效和透明,让它们成为文件系统本身的一部分。



但是,有一个问题仍然存在:since。和..是这些链接的任意名称,是否有使用不同的文件系统的文件系统?

解决方案

.. 实际上是文件系统中的硬链接。他们是需要的,所以你可以指定相对路径,基于一些引用路径(考虑../sibling/file.txt)。由于这些硬链接实际上存在于文件系统中,所以 readdir 可以告诉你这些链接。 (实际上,术语 hard link 只是表示一些与实际目录无法区分的名称:它们都指向相同的 inode

最好的办法是只要 strcmp ,并忽略它们,要列出他们。

Whenever I list the contents of a directory with a function like readdir, the returned file names also include "." and "..". I have the suspicion that these are just normal links in the file system and therefore indistinguishable from actual files, but I always have to filter them out because they are not actual objects in the directory I am listing. Is there a good reason for functions like readdir to include them? Do some operating systems or file systems contain more or different virtual file names? Is there a better way to filter them out other than by doing string comparison with "." and ".."?

Update: thank you all for answering. I suppose I always thought that things like ./ and ../ were mere conventions that could be handled by searching and replacing. I find it a bit surprising, though probably more efficient and transparent, to have them be part of the file system itself.

One question remains, though: since . and .. are arbitrary names for these links, are there file systems that use different ones?

解决方案

. and .. are actually hard links in filesystems. They are needed so that you can specify relative paths, based on some reference path (consider "../sibling/file.txt"). Since these hard links are actually existing in the filesystem, it makes sense for readdir to tell you about them. (actually the term hard link just means some name that is indistinguishable from the actual directory referred to: they both point to the same inode in the filesystem).

Best way is to just strcmp and ignore them, if you don't want to list them.

这篇关于为什么目录列表包含当前(。)和父(..)目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆