Posted by : Unknown Sabtu, 28 November 2015

Halo sobat, kali ini naga akan memberi anda tutorial tentang FUSE. Tutorial disini akan menjelaskan source code dimana, dalam file system FUSE file yang kita baca bisa di copy, tetapi setelah di copy file Original menjadi tidak bisa di buka dan tidak bisa di copy lagi. Tetapi file hasil copy-an bisa di copy dan dibaca. Berikut Source Code nya

#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include <sys/statfs.h>

static const char *dirpath = "/home/kevin/Documents/";
char alamat[1000];
static int xmp_getattr(const char *path, struct stat *stbuf)
{
    int res;
    char fpath[1000];
    sprintf(fpath, "%s%s", dirpath, path);
    res = lstat(fpath, stbuf); 
    if (res == -1)
        return -errno;

    return 0;
}

static int xmp_getdir(const char *path, fuse_dirh_t h, fuse_dirfil_t filler)
{
    DIR *dp;
    struct dirent *de;
    int res = 0;
    char fpath[1000];
    sprintf(fpath, "%s%s", dirpath, path);
    dp = opendir(fpath);
    if(dp == NULL)
        return -errno;

    while((de = readdir(dp)) != NULL)
    {
        res = filler(h, de->d_name, de->d_type);
        if(res != 0)
            break;
    }

    closedir(dp);
    return res;
}

static int xmp_read(const char *path, char *buf, size_t size, off_t offset)
{
    int fd;
    int res;
    char fpath[1000];
    sprintf(fpath, "%s%s", dirpath, path);
    strcpy(alamat,path);
    fd = open(fpath, O_RDONLY);
    if(fd == -1)
        return -errno;
 
    res = pread(fd, buf, size, offset);
    if(res == -1)
        res = -errno;

    close(fd);
    return res;
}

static int xmp_mknod(const char *path, mode_t mode, dev_t rdev)
{
    int res;
    char fpath[1000];
    sprintf(fpath, "%s%s", dirpath, path);
    res = mknod(fpath, mode, rdev);
    if(res == -1)
        return -errno;

    return 0;
}

static int xmp_chmod(const char *path, mode_t mode)
{
    int res;

    char fpath[1000];
    sprintf(fpath, "%s%s", dirpath, path);
    res = chmod(fpath, mode);
    if(res == -1)
        return -errno;

    return 0;
}

static int xmp_write(const char *path, const char *buf, size_t size, off_t offset)                                                              
{
    int fd;
    int res;
    int res1;
    char fpath[1000],temp1[1000];

    sprintf(fpath, "%s%s", dirpath, path);
    fd = open(fpath, O_WRONLY);
 
    sprintf(temp1, "%s%s", dirpath, alamat);
    res1 = chmod(temp1, 0000);
    if(res1 == -1)
        res1 = -errno;

    if(fd == -1)
        return -errno;

    res = pwrite(fd, buf, size, offset);
    if(res == -1)
        res = -errno;

    close(fd);
    return res;
}

static int xmp_open(const char *path, int flags)
{
    int res;
    char fpath[1000];
    sprintf(fpath, "%s%s", dirpath, path);
    res = open(fpath, flags);
    if(res == -1)
        return -errno;

    close(res);
    return 0;
}

static struct fuse_operations xmp_oper =
{
    .getattr = xmp_getattr,
    //.readlink = xmp_readlink,
    .getdir = xmp_getdir,
    .mknod = xmp_mknod,
    //.mkdir = xmp_mkdir,
    //.symlink = xmp_symlink,
    //.unlink = xmp_unlink,
    //.rmdir = xmp_rmdir,
    //.rename = xmp_rename,
    //.link = xmp_link,
    .chmod = xmp_chmod,
    //.chown = xmp_chown,
    //.truncate = xmp_truncate,
    //.utime = xmp_utime,
    .open = xmp_open,
    .read = xmp_read,
    .write = xmp_write,
    //.release = xmp_release,
    //.fsync = xmp_fsync,
    //.readdir = hello_readdir
};

int main(int argc, char *argv[])
{
    return fuse_main(argc, argv, &xmp_oper);
}





Jangan lupa, ganti alamat yang berwarna merah dengan alamat yang ingin di mount. Untuk compile ketik di source code :

gcc -Wall fuse1.c `pkg-config fuse --cflags --libs` -o fuse1

Lalu jangan lupa membuat direktori dengan

mkdir /tmp/fuse

Lalu jalankan program dengan

./fuse1 /tmp/fuse


Jika sudah cek folder tmp/fuse pada partisi hardisk


/tmp/fuse
 
kevin/documents
Dapat dilihat isi dari documents sama dengan fuse. Lalu buka direktori fuse, dan lakukan copy dan paste terhadap sebuah file. Maka hasilnya seperti gambar dibawah



 

 


Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Naga Lapar -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -