I am using libext2fs
to modify on disk inode but its not working.
I am doing this on mounted filesystem (EXT4). I tried running e2fsck
on a mounted file-system and it appears to work (although it give a warning).
Following is my code.
It runs without error but I don't see any changes when I do stat on testfile.txt.
Any idea ? or alternatives ?
#include <linux/fs.h>
#include <ext2fs/ext2fs.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "e2p/e2p.h"
int main(int argc, char **argv)
{
errcode_t ret;
int flags;
int superblock = 0;
int open_flags = EXT2_FLAG_RW | EXT2_FLAG_64BITS | EXT2_FLAG_SKIP_MMP | EXT2_FLAG_NOFREE_ON_ERROR | EXT2_FLAG_FORCE;
int blocksize = 0;
ext2_filsys fs = NULL;
struct ext2_inode inode;
ext2_ino_t root, cwd, inum;
int i, c;
struct stat fileStat;
const char * str = "This will be output to testfile.txt\n";
//Create some file for testing.
FILE * filedesc = fopen( "/home/test/testfile.txt", "w" );
fprintf( filedesc, str );
fclose( filedesc );
//open the device..( this is already mounted, e2fsck works on mounted devices. so I hope this will work. )
ret = ext2fs_open2( "/dev/sda1" , open_flags, superblock, blocksize, unix_io_manager, &fs );
if( ret )
{
fprintf(stderr, "failed to open filesystem %d.\n", ret);
return 1;
}
//Get the inode number of file using stat
ret = stat( "/home/test/testfile.txt", &fileStat );
if( ret )
{
fprintf(stderr, "failed to stat\n");
}
ret = ext2fs_read_inode(fs, fileStat.st_ino, &inode );
if( ret )
{
fprintf(stderr, "failed to open inode\n");
}
//do some changes.
inode.i_ctime += 1;
ext2fs_mark_changed( fs );
//write the modified inode.
ret = ext2fs_write_inode( fs, fileStat.st_ino, &inode );
if( ret )
{
fprintf(stderr, "failed to open inode\n");
}
//this didn't help
ret = ext2fs_flush(fs);
ret = ext2fs_close(fs);
if( ret )
{
fprintf(stderr, "error while closing filesystem\n");
}
return 0;
}