0

我正在使用libext2fs在磁盘 inode 上进行修改,但它不起作用。我正在安装的文件系统(EXT4)上执行此操作。我尝试e2fsck在已安装的文件系统上运行,它似乎可以工作(尽管它会发出警告)。

以下是我的代码。

它运行没有错误,但是当我对 testfile.txt 进行统计时,我没有看到任何变化。

任何想法 ?或替代品?

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

0 回答 0