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

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

问题描述

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

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?

推荐答案

... 实际上是文件系统中的硬链接.它们是必需的,以便您可以根据一些参考路径指定相对路径(考虑 "../sibling/file.txt").由于这些硬链接实际上存在于文件系统中,因此 readdir 告诉您它们是有意义的.(实际上,hard link 一词只是指一些与实际引用的目录无法区分的名称:它们都指向文件系统中的相同 inode.

. 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).

如果您不想列出它们,最好的方法是只 strcmp 并忽略它们.

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

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

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