How to compare nlink_t with int

I use the stat system call on Linux and retrieve the file information.

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

buf structure type:

 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 */ }; 

I get a number of hard links like: buf.st_nlink .

My problem is that I cannot compare the number of hard links with an integer value. I tried to initialize another nlink_t and then compare my variable with stat variable, but it does not work. I also tried this link .

An alternative way to cast nlink_t to int , but it does not work. always returns the same number.

 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; } 

Any idea?

Program output when I use the parse_to_int function:

 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 ... 
+4
source share
3 answers

nlink_t is typedef'd as an integer type (e.g. unsigned short or unsigned int ), so you can use stat.st_nlink for unsigned or unsigned long without a compiler complaint.

Your parse_to_int() function is incorrect because you are casting a pointer ( nlink_t* ) to an unsigned int instead of the value of the nlink_t variable. But you do not need a function, just use casting correctly.

Adding

Also make sure that you are not comparing an unsigned type with -1 , which will give you unexpected results.

+2
source

So, I had a few problems:

  • You cannot get stat from file if the path ends with /
  • There are several folders in the list of computer directories that are created by your operating system. Some data in memory interpreted as folders and files (called a virtual file system ). Therefore, if you try to get an i-node number from this type of file, you will get some garbage.
+1
source

You should be able to compare with uint. If you have more than 64k hard links, I would be surprised.

Trying to guess the size it takes to print the string representation of nlink_t from how many bytes the length of nlink_t is an error. nlink_t can be 8 bytes, but 4,000,000,000 is more than 8 bytes of digits.

The correct way to find the size you need is to check the return value of snprintf, it will tell you how you really need the buffer (or you could lay out and allocate something larger than 8. sizeof (* source) is not a hard decision.

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

In addition, the number of hard links should be consistent if your file system does not change. Take a look at the conclusion

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

This number in front of the owner is the number of hard links, in this case 5.

0
source

All Articles