Archive for November 2015
Membuat File Tidak Dapat di Copy : FUSE
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
#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
/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
Membuat Game Benteng Takeshi pada Terminal Menggunakan Pipes
Buat game gak harus susah susah. Melalui bahasa C dan metode pipes kita sudah bisa membuat game sederhana dan memainkannya melalui terminal pada linux.
Oke diatas adalah contoh ketika game tersebut dijalankan. Game tersebut bertema kan benteng takeshi. Jadi setiap player akan menaruh ranjau pada lubang 1 - 16. Lalu player satunya akan menebak, dan jika tebakan benar maka poin akan menjadi miliknya jika tidak poin akan menjadi milik lawan. Easy? Right Then, berikut adalah source code nya.
Nah anda bisa copy kode diatas dan di paste kan ke file berbeda dan di compile di terminal berbeda sehingga bisa memainkannya bersama.Oke diatas adalah contoh ketika game tersebut dijalankan. Game tersebut bertema kan benteng takeshi. Jadi setiap player akan menaruh ranjau pada lubang 1 - 16. Lalu player satunya akan menebak, dan jika tebakan benar maka poin akan menjadi miliknya jika tidak poin akan menjadi milik lawan. Easy? Right Then, berikut adalah source code nya.
Judul Spoiler:
Player1
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(){
int fd,fs,fsc,ffe1,ffe2;
int fc,ranjau[5],tebak[4];
int entry[16];
int counterentry=0,counterentry2=0;;
int flagentry=0;
int flagentry2=0;
char *myfifo = "/tmp/fifo1";
char *fifo2="/tmp/fifo2";
char *fifoscore="/tmp/fifoscore";
int flag;
mkfifo(myfifo,0666);
mkfifo(fifoscore,0666);
fd = open(myfifo, O_WRONLY);
fs = open(fifo2,O_RDONLY);
fsc = open(fifoscore,O_RDWR);
int i,j,counter=0;
int scorePlayer[3];
scorePlayer[1]=0;
scorePlayer[2]=0;
//-----------------------------------------------------------------------------------------------
while(counterentry+counterentry2<32){
for(i=0;i<4;i++)
{
ranjau[i]=0;
}
if(counterentry<16){
system("clear");
for(i=0;i<4;i++)
{
ranjau[i]=0;
}
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
//printf("COUNTER = %d %d\n\n",counterentry,counterentry2);
for(i=0;i<4;i++)
{
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
while(ranjau[i]<0 || ranjau[i]>16)
{
printf("Lubang tersebut tidak tersedia\n");
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
}
for(j=0;j<counterentry;j++)
{
while(ranjau[i]==entry[j])
{
if(ranjau[i]==0) break;
printf("Anda telah memasukkan ranjau ke lubang tersebut\n");
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
}
}
if(ranjau[i]!=0)
{
entry[counterentry]=ranjau[i];
counterentry++;
}
if(counterentry>=16) break;
}
}
printf("%d %d %d %d = %d\n",ranjau[0],ranjau[1],ranjau[2],ranjau[3], counterentry);
ranjau[4]=counterentry;
write(fd,ranjau,sizeof(ranjau));
if(counterentry+counterentry2>=32) break;
//-----------------------------------------------------------------------------------------------
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
//printf("COUNTERINI = %d %d\n\n",counterentry,counterentry2);
read(fsc,scorePlayer,sizeof(scorePlayer));
read(fs, ranjau, sizeof(ranjau));
counterentry2=ranjau[4];
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
//printf("COUNTERTEBAK = %d %d\n\n",counterentry,counterentry2);
printf("READ : %d %d %d %d = %d\n",ranjau[0],ranjau[1],ranjau[2],ranjau[3],counter);
for(i=0;i<4;i++)
{
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
while(tebak[i]<=0 || tebak[i]>16)
{ printf("Lubang tersebut tidak tersedia\n");
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
}
for(j=0;j<i;j++)
{
while(tebak[i]==tebak[j])
{
printf("Anda telah menebak lubang tersebut\n");
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
}
}
}
for(j=0;j<4;j++){
flag=0;
for(i=0;i<4;i++)
{
if(ranjau[j]==tebak[i]) flag=1;
if(ranjau[j]==0) flag=2;
}
if(flag==1) scorePlayer[1]=scorePlayer[1]+1;
else if(flag==0) scorePlayer[2]=scorePlayer[2]+1;
}
write(fsc,scorePlayer,sizeof(scorePlayer));
if(counterentry+counterentry2>=32) break;
}
//-----------------------------------------------------------------------------------------------
read(fsc,scorePlayer,sizeof(scorePlayer));
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
if(scorePlayer[1]>scorePlayer[2])
system("zenity --error --title=GAMEOVER --text=PLAYER1WIN");
else if(scorePlayer[1]==scorePlayer[2])
system("zenity --error --title=GAMEOVER --text=DRAW!!");
else
system("zenity --error --title=GAMEOVER --text=PLAYER2WIN");
close (fd);
close (fc);
close (fs);
unlink(myfifo);
return 0;
}
Judul Spoiler:
Player 2
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(){
int fd,fc,fs,fsc,ffe1,ffe2;
char * myfifo = "/tmp/fifo1";
char *fifo2="/tmp/fifo2";
char *fifoscore="/tmp/fifoscore";
int entry[16];
int counterentry=0,flagentry=0;
int flagentry2=0;
int counterentry2=0;
mkfifo(fifo2,0666);
mkfifo(fifoscore,0666);
int ranjau[5];
int tebak[4];
int i,j;
int scorePlayer[3];
int counterplayer2=0;
int flag=0;
scorePlayer[1]=0;
scorePlayer[2]=0;
fd = open(myfifo, O_RDONLY);
fs = open(fifo2,O_WRONLY);
fsc = open(fifoscore,O_RDWR);
//-----------RRREEEEAAADDDD
while(counterentry+counterentry2<32){
read(fd, ranjau, sizeof(ranjau));
int flagcounter=0;
if(ranjau[0]==0 && ranjau[1]==0 && ranjau[2]==0 && ranjau[3]==0) flagcounter=1;
if(flagcounter==0)
{
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
//printf("COUNTER = %d %d\n\n",counterentry,counterentry2);
counterentry2=ranjau[4];
printf("READ : %d %d %d %d = %d\n",ranjau[0],ranjau[1],ranjau[2],ranjau[3],counterentry);
for(i=0;i<4;i++)
{
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
while(tebak[i]<=0 || tebak[i]>16)
{ printf("Lubang tersebut tidak tersedia\n");
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
}
for(j=0;j<i;j++)
{
while(tebak[i]==tebak[j])
{
printf("Anda telah menebak lubang tersebut\n");
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
}
}
}
for(j=0;j<4;j++){
flag=0;
for(i=0;i<4;i++)
{
if(ranjau[j]==tebak[i]) flag=1;
if(ranjau[j]==0) flag=2;
}
if(flag==1) scorePlayer[2]=scorePlayer[2]+1;
else if(flag==0) scorePlayer[1]=scorePlayer[1]+1;
}
}
write(fsc,scorePlayer,sizeof(scorePlayer));
//-------------------------------------------------------------------------------------
if(counterentry<16){
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
//printf("COUNTER = %d %d\n\n",counterentry,counterentry2);
for(i=0;i<4;i++)
{
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
while(ranjau[i]<0 || ranjau[i]>16)
{
printf("Lubang tersebut tidak tersedia\n");
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
}
for(j=0;j<counterentry;j++)
{
while(ranjau[i]==entry[j])
{
if(ranjau[i]==0) break;
printf("Anda telah memasukkan ranjau ke lubang tersebut\n");
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
}
}
if(ranjau[i]!=0)
{
entry[counterentry]=ranjau[i];
counterentry++;
}
if(counterentry>=16) break;
}
printf("%d %d %d %d %d= %d\n",ranjau[0],ranjau[1],ranjau[2],ranjau[3], ranjau[4], counterentry);
ranjau[4]=counterentry;
write(fs,ranjau,sizeof(ranjau));
read(fsc,scorePlayer,sizeof(scorePlayer));
}
}
write(fsc,scorePlayer,sizeof(scorePlayer));
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
close (fd);
close (fc);
close (fs);
unlink (fifo2);
return 0;
}