0

libext2fsディスクiノードを変更するために使用していますが、機能しません。マウントされたファイルシステム(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;
}
4

0 に答える 0