C 如何将nlink\u t与int进行比较

C 如何将nlink\u t与int进行比较,c,file,parsing,variables,C,File,Parsing,Variables,我在Linux上使用stat系统调用并检索文件信息 char *parent_dir; // for example: /run/atd.pid/ struct stat buf; stat(parent_dir, &buf); buf结构类型: struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino;

我在Linux上使用
stat
系统调用并检索文件信息

char *parent_dir; // for example: /run/atd.pid/
struct stat buf;
stat(parent_dir, &buf);
buf结构类型:

struct stat {
               dev_t     st_dev;     /* ID of device containing file */
               ino_t     st_ino;     /* inode number */
               mode_t    st_mode;    /* protection */
               nlink_t   st_nlink;   /* number of hard links */
               uid_t     st_uid;     /* user ID of owner */
               gid_t     st_gid;     /* group ID of owner */
               dev_t     st_rdev;    /* device ID (if special file) */
               off_t     st_size;    /* total size, in bytes */
               blksize_t st_blksize; /* blocksize for file system I/O */
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
               time_t    st_atime;   /* time of last access */
               time_t    st_mtime;   /* time of last modification */
               time_t    st_ctime;   /* time of last status change */
           };
我得到了很多这样的硬链接:
buf.st\nlink

我的问题是,我无法将硬链接的数量与整数值进行比较。我尝试初始化另一个nlink\u t,然后将我的变量与
stat
变量进行比较,但它不起作用。我也试过这个

nlink\u t转换为int的替代方法,但不起作用。始终返回相同的数字

int
parse_to_int(nlink_t *source)
{
  int buffer_size = sizeof(*source);
  char buffer[buffer_size];
  snprintf(&buffer[0], buffer_size, "%lu", (unsigned long)source);
  int val = atoi(buffer);
  return val;
}
有什么想法吗

使用
parse_to_int
函数时的程序输出:

get stat for: /run/nm-dhclient-wlan0.conf/ 
nlink_t: 321
get stat for: /run/wpa_supplicant/ 
nlink_t: 321
get stat for: /run/udisks2/ 
nlink_t: 321
get stat for: /run/nm-dns-dnsmasq.conf/ 
nlink_t: 321
...

nlink\u t
类型定义为整数类型(例如,
unsigned short
unsigned int
),因此您应该能够将
stat.st\u nlink
强制转换为
unsigned
unsigned long
,而无需编译器抱怨

您的
parse_to_int()
函数不正确,因为您将指针(
nlink_t*
)强制转换为
无符号int
,而不是
nlink_t
变量的值。但是你不需要这个函数,只要正确地使用cast就可以了

附录


还要确保您没有将无符号类型与
-1
进行比较,这将给您带来意外的结果。

您应该能够与uint进行比较。如果你有超过64k的硬链接,我会很惊讶

试图根据nlink的长度猜测打印nlink的字符串表示形式所需的大小是一个bug。nlink_t可以是8字节,但4000000000大于打印出的8字节数字

找到正确大小的正确方法是检查snprintf的返回值,它会告诉您实际需要多大的缓冲区(或者您可以下注并分配大于8的缓冲区。sizeof(*source)不是一个固定的解决方案

int size = snprintf(NULL, 0, "%lu", (unsigned long)source);
buffer = new chara[size + 1]; //+1 for a NULL
int size = snprintf(buffer, size, "%lu", (unsigned long)source);    
此外,除非您的文件系统正在更改,否则硬链接的数量应该保持一致

tmp]$ ls -ld /home/
drwxr-xr-x. 5 root root 4096 Feb  3  2012 /home/

所有者之前的数字是硬链接的数量,在本例中为5。

因此我遇到了几个问题:

  • 如果路径以
    /
    结尾,则无法从文件中获取
    stat
  • 在您计算机的文件夹列表中,有一些文件夹是由您的操作系统创建的。内存中有一些类型的数据,解释为文件夹和文件(称为)。因此,如果您试图从此类文件中获取
    i-node
    编号,则会得到一些垃圾值

  • 您不能将nlink\u t类型转换为int并使用它吗?或者是否有其他东西妨碍了您?失败案例的源代码可能有助于解决问题。代码没有失败。如果我尝试按本文所述获取值,它总是返回一个数字。@gkiko:我不明白这种行为。请显示一个显示问题的最小程序。我编辑了问题。问题是我调用了以/结尾的文件的
    stat
    。但我仍然存在溢出问题。