Skip to content

Instantly share code, notes, and snippets.

@cosven
Created October 15, 2020 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosven/de6c4a3edc2e94f30471b21279115338 to your computer and use it in GitHub Desktop.
Save cosven/de6c4a3edc2e94f30471b21279115338 to your computer and use it in GitHub Desktop.
get file inode number and inode generation number
#include <stdint.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <linux/fs.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
int main (int argc, char **argv) {
if (argc < 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}
const char *filename = argv[1];
uint32_t generation = 0;
struct stat file_stat;
int fileno = open(filename, O_RDONLY);
if (fileno < 0) {
printf("Open file %s error", filename);
return 1;
}
int ret = fstat(fileno, &file_stat);
if (ret < 0) {
printf("Stat file %s error", filename);
return 1;
}
// get inode number and generation number
if (ioctl(fileno, FS_IOC_GETVERSION, &generation)) {
printf("errno: %d\n", errno);
}
printf("inode number: %lu\n", file_stat.st_ino);
printf("inode generation: %u\n", generation);
close(fileno);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment