Created
October 15, 2020 02:48
-
-
Save cosven/de6c4a3edc2e94f30471b21279115338 to your computer and use it in GitHub Desktop.
get file inode number and inode generation number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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