在ext4文件系统上更改ondisk inode

时间:2013-03-21 18:17:59

标签: linux inode ext3 ext4

我正在使用libext2fs修改磁盘inode,但它无法正常工作。 我在挂载文件系统(EXT4)上执行此操作。我尝试在已安装的文件系统上运行e2fsck,它似乎有效(尽管它会发出警告)。

以下是我的代码。

它运行时没有错误,但是当我在testfile.txt上执行stat时,我没有看到任何更改。

有什么想法吗?还是替代品?

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

0 个答案:

没有答案