文件类型指示符,sys / stat.h st_mode常规文件代码值

时间:2022-09-24 20:26:38

I am trying to identify file types for directory entries (Windows Unix etc..).

我正在尝试识别目录条目的文件类型(Windows Unix等...)。

In sys/stat.h the high order nybble of the st_mode word have the coded values:

在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 */

From the comment it seems the nybble could be either 0 or 8 to represent a 'regular file'.

从评论看来,似乎nybble可以是0或8来表示“常规文件”。

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.

所以这引出了一个问题:它在什么情况下是0而不是8?如果我已经定义了这些代码,我会保留0来代替未知/未定义/无效/非文件或类似的东西。

Indeed the S_ISREG macro is:

实际上S_ISREG宏是:

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

在我看来,这似乎表明常规文件应始终具有代码8(0将是一个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?

将0解释为未知或无效文件并忽略'或仅0x0000'注释并始终期望8用于所有常规文件是否是一个有效的假设?

2 个解决方案

#1


Most sources indicate that checking S_ISREG is enough; I'm not sure when you'd see 0x0000 as a "regular" file.

大多数消息来源表明检查S_ISREG就足够了;我不确定你什么时候会看到0x0000作为“常规”文件。

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.

我相信一些旧的实现使用了0x0000(一个非常古老的DJGPP标头搜索把它转过来),但它是我能找到的唯一真正的引用。其他一切都指向0x8000。

Basically, use the S_ISREG macro and hope that the header on whatever you're compiling against does the right thing.

基本上,使用S_ISREG宏并希望你正在编译的任何标题做正确的事情。

#2


I would trust the definitions of S_IFREG and S_ISREG. I've never worked with a file system that broke those macros.

我相信S_IFREG和S_ISREG的定义。我从未使用破坏这些宏的文件系统。

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?

我的猜测是,常规文件的0x0000定义是处理可能使用不同文件类型信息编码的旧文件系统。您使用的操作系统和文件系统是什么?

#1


Most sources indicate that checking S_ISREG is enough; I'm not sure when you'd see 0x0000 as a "regular" file.

大多数消息来源表明检查S_ISREG就足够了;我不确定你什么时候会看到0x0000作为“常规”文件。

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.

我相信一些旧的实现使用了0x0000(一个非常古老的DJGPP标头搜索把它转过来),但它是我能找到的唯一真正的引用。其他一切都指向0x8000。

Basically, use the S_ISREG macro and hope that the header on whatever you're compiling against does the right thing.

基本上,使用S_ISREG宏并希望你正在编译的任何标题做正确的事情。

#2


I would trust the definitions of S_IFREG and S_ISREG. I've never worked with a file system that broke those macros.

我相信S_IFREG和S_ISREG的定义。我从未使用破坏这些宏的文件系统。

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?

我的猜测是,常规文件的0x0000定义是处理可能使用不同文件类型信息编码的旧文件系统。您使用的操作系统和文件系统是什么?