Skip to content

Instantly share code, notes, and snippets.

@dzwdz
Created September 11, 2022 12:53
Show Gist options
  • Select an option

  • Save dzwdz/c09a57f3b397482a61a971640fa628d2 to your computer and use it in GitHub Desktop.

Select an option

Save dzwdz/c09a57f3b397482a61a971640fa628d2 to your computer and use it in GitHub Desktop.
libext2fs basic usage
#include <stdio.h>
#include <sys/types.h> /* required by ext2fs.h */
#include <ext2fs/ext2fs.h>
#define die(...) do {fprintf(stderr, __VA_ARGS__); exit(1);} while (0)
static int dir_iter(struct ext2_dir_entry *dirent, int offset, int blocksize, char *buf, void *userdata) {
printf("dir_iter '%.*s', inode %u\n", dirent->name_len, dirent->name, dirent->inode);
return 0; /* the iteration stops when the return is negative */
}
int main() {
ext2_filsys fs;
errcode_t err;
/* io_manager - an abstraction for the storage device */
err = ext2fs_open("img.e2", 0 /* EXT2_FLAG_RW */, 0, 0, unix_io_manager, &fs);
if (err) die("ext2fs_open failed\n");
/*
ext2_ino_t inode;
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, "/", &inode);
if (err) die("ext2fs_namei failed\n");
*/
err = ext2fs_dir_iterate(fs, EXT2_ROOT_INO, 0, NULL, dir_iter, NULL);
if (err) die("ext2fs_dir_iterate failed\n");
err = ext2fs_close(fs);
if (err) die("ext2fs_close failed\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment