文件类型指示符,sys/stat.h st_mode 常规文件代码值
我正在尝试识别目录条目的文件类型(Windows Unix 等..)。
在 sys/stat.h 中,st_mode 字的高位 nybble 具有编码值:
#define S_IFDIR 0x4000 /* directory */
#define S_IFIFO 0x1000 /* FIFO special */
#define S_IFCHR 0x2000 /* character special */
#define S_IFBLK 0x3000 /* block special */
#define S_IFREG 0x8000 /* or just 0x0000, regular */
从注释看来,nybble 可以是 0 或 8 来表示 一个“常规文件”。
那么这就引出了一个问题:在什么情况下它是 0 而不是 8? 如果我定义了这些代码,我会保留 0 来表示 未知/未定义/无效/不是文件或类似的东西。
事实上,S_ISREG 宏是:
#define S_ISREG(m) ((m) & S_IFREG)
在我看来,这表明常规文件应该 总是应该有代码 8(0 会是一种错误?)。
将 0 解释为未知或无效文件并忽略“或只是 0x0000”注释并始终期望 8 用于所有常规文件是否是一个有效的假设?
I am trying to identify file types for directory entries (Windows Unix etc..).
In sys/stat.h the high order nybble of the st_mode word have the coded values:
#define S_IFDIR 0x4000 /* directory */
#define S_IFIFO 0x1000 /* FIFO special */
#define S_IFCHR 0x2000 /* character special */
#define S_IFBLK 0x3000 /* block special */
#define S_IFREG 0x8000 /* or just 0x0000, regular */
From the comment it seems the nybble could be either 0 or 8 to represent
a 'regular file'.
So this begs the question: in what circumstances is it 0 and not 8?
If I had defined these codes, I would have reserved 0 to inidicate
unknown/undefined/invalid/not-a-file or something like that.
Indeed the S_ISREG macro is:
#define S_ISREG(m) ((m) & S_IFREG)
This would seem to me to indicate that a regular file should
always be expected to have the code 8 (and 0 would be an abberation?).
Would it be a valid assumption to interpret 0 as an unknown or invalid file and ignore the 'or just 0x0000' comment and always expect 8 to be used for all regular files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
大多数来源表明检查 S_ISREG 就足够了; 我不确定您什么时候会将 0x0000 视为“常规”文件。
我相信一些旧的实现使用了 0x0000(一个非常旧的 DJGPP 标头搜索会发现这一点),但它是我能找到的唯一真正的参考。 其他所有内容都指向 0x8000。
基本上,使用 S_ISREG 宏并希望您正在编译的任何内容的标头都能做正确的事情。
Most sources indicate that checking S_ISREG is enough; I'm not sure when you'd see 0x0000 as a "regular" file.
I believe some old implementations used 0x0000 (a really old DJGPP header search turns this up) but it's the only real reference I can find. Everything else points to 0x8000.
Basically, use the S_ISREG macro and hope that the header on whatever you're compiling against does the right thing.
我相信 S_IFREG 和 S_ISREG 的定义。 我从未使用过破坏这些宏的文件系统。
我的猜测是,常规文件的 0x0000 定义是为了处理可能使用不同文件类型信息编码的旧文件系统。 您使用什么操作系统和文件系统?
I would trust the definitions of S_IFREG and S_ISREG. I've never worked with a file system that broke those macros.
My guess is that the 0x0000 definition for a regular file is to handle legacy file systems that may have used a different encoding of file type information. What OS and file system are you using?