Glob 中的 "unsorted" 订单来自哪里?

Where does the "unsorted" order come from in Glob?

我一直在阅读 globan implementation 并使用 Ubuntu Bionic and Trusty 对其进行测试。

在高级别上,我使用 GLOB_NOSORT 标志针对两个目录测试了 /foo/*/bar 等模式:

是的,我知道我使用NOSORT,我不想排序。但是,订单是 100% 可重复的:

每次我在 Trusty 上 运行 这个我在 3 之前得到 A,而每次我在 Bionic 上 运行 这个我在 3 之前得到 3 A.

glob 手册描述的行为是:

Do not sort the returned list. By default, the list is sorted alphabetically. This flag causes the files to be returned in the order they were found in the directory.

但是,它们在目录中的顺序是什么? "natural" 命令从何而来,为什么它总是可重复的,为什么它在这两个 OS 版本之间发生变化?

自然顺序是,文件系统层 returns 从调用到 getdents()。那里的目录索引没有排序,而是在一些优化的数据结构中按 name 查找它。因此,理论上枚举的顺序会因时而异。

此外,对于 ext4,您不能假定任何特定的顺序,即使它在每次调用中都是稳定的。它通常稳定,只要没有写入任何内容,但即使这样也不能保证。

内核版本的差异也涉及对ext4代码库的大量修改;我想很难(但并非不可能)在某处发现代码中的差异,这就是您观察到的排序差异的原因。