Advertisement
mbah_bejo

tesm4.c

Jun 3rd, 2021
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.03 KB | None | 0 0
  1. #define FUSE_USE_VERSION 28
  2. #include <fuse.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <dirent.h>
  6. #include <errno.h>
  7. #include <sys/time.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11.  
  12. // #define MAX 256
  13. // char filelama[MAX][MAX];
  14.  
  15. static  const  char *dirpath = "/home/misdinar/Downloads";
  16. char alphabet[52] = {"ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba"};
  17.  
  18. void chyperEn(char *filename){
  19.     int count;
  20.     int start=0;
  21.     int length=strlen(filename);
  22.     int alphabetidx;
  23.     char *p;
  24.     for(count=length;count>=0;count--){
  25.         if(filename[count]=='/') break;
  26.         if(filename[count]=='.') length=count-1;
  27.     }
  28.     for(count=1;count<length;count++){
  29.         if(filename[count]=='/') start = count;
  30.     }
  31.     for(count=start;count<length;count++){
  32.         if(filename[count]=='/')continue;
  33.         p=strchr(alphabet,filename[count]);
  34.         if(p){
  35.             alphabetidx=p-alphabet;
  36.             if(alphabetidx >='A' && alphabetidx <= 'Z')
  37.             filename[count]=alphabet[alphabetidx-65];
  38.             else
  39.             filename[count]=alphabet[alphabetidx-97 + 25];
  40.         }
  41.     }
  42.   // printf("Encrypt --> %s\n",filename);
  43. }
  44.  
  45. void chyperDe(char *filename){
  46.  
  47.     int length = strlen(filename);
  48.     int start = 0;
  49.     int count,alphabetidx;
  50.     char *p;
  51.    
  52.     for (count = 1;count < length; count++){
  53.         if(filename[count] == '/' || filename[count+1] == '\0'){
  54.             start = count + 1;
  55.             break;
  56.         }
  57.     }
  58.  
  59.     for(count = strlen(filename); count >= 0; count--) {
  60.         if(filename[count] == '/') break;
  61.         else if(filename[count] == '.'){
  62.             if(count!=(strlen(filename)-1)){
  63.                 length=count-1;
  64.                 break;
  65.             }
  66.             else {
  67.                 length=strlen(filename);
  68.                 break;
  69.             }
  70.         }
  71.     }
  72.  
  73.     for(count = start;count < length;count++) {
  74.         if(filename[count] == '/') continue;
  75.         p = strchr(alphabet, filename[count]);
  76.         if(p){
  77.             alphabetidx = p-alphabet;
  78.             if(alphabetidx >='A' && alphabetidx <= 'Z')
  79.             filename[count]=alphabet[alphabetidx + 65];
  80.             else
  81.             filename[count]=alphabet[alphabetidx + 97 + 25];
  82.         }
  83.     }
  84. }
  85.  
  86. void logwriter(int info, char* cmd, char* fileawal, char* temp){
  87.     char x[1000];
  88.     char infox[1000];
  89.     char temp2[1000];
  90.  
  91.     FILE* fp;
  92.     time_t t = time(NULL);
  93.     struct tm tm = *localtime(&t);
  94.  
  95.     sprintf(infox, info?"WARNING":"INFO");
  96.     if(strcmp(temp,"")) sprintf(temp2, "::%s", temp);
  97.  
  98.     fp = fopen("/home/misdinar/fs.log","a");
  99.     sprintf(x, "%s::%d%d%d-%d:%d:%d::%s::%s%s\n", infox, tm.tm_mday, tm.tm_mon + 1,tm.tm_year + 1900, tm.tm_hour, tm.tm_min, tm.tm_sec, cmd, fileawal, temp2);
  100.  
  101.     if(fp==NULL){
  102.         printf("File gagal dibuat\n");
  103.         exit(EXIT_FAILURE);
  104.     }
  105.     fputs(x,fp);
  106.     fclose(fp);
  107. }
  108.  
  109. static  int  xmp_getattr(const char *path, struct stat *stbuf){
  110.  
  111.     int res;
  112.     char fpath[1000];
  113.   //char pathsementara[1000];
  114.     //printf("%s\n",pathsementara);
  115.     char temp[1000];
  116.     strcpy(temp, path);
  117.     if(strncmp(path,"/AtoZ_",6)==0) chyperDe(temp);
  118.     sprintf(fpath, "%s%s",dirpath,temp);
  119.   //printf("fpath getattr: %s\n",fpath);
  120.     res = lstat(fpath, stbuf);
  121.     if (res == -1)
  122.         return -errno;
  123.  
  124.     return 0;
  125. }
  126.  
  127. static int xmp_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi){
  128.     char fpath[1000];
  129.     if(strcmp(path,"/") == 0){
  130.         path=dirpath;
  131.         sprintf(fpath,"%s",path);
  132.     }
  133.     else {
  134.         char temp[1000];
  135.         if(strncmp(path,"/AtoZ_",6)==0) chyperDe(temp);
  136.         sprintf(fpath, "%s%s",dirpath,temp);
  137.     }
  138.   //printf("fpath read: %s\n",fpath);
  139.  
  140.     int res = 0;
  141.     int fd = 0;
  142.  
  143.     (void) fi;
  144.  
  145.     fd = open(fpath, O_RDONLY);
  146.     if (fd == -1)
  147.         return -errno;
  148.  
  149.     res = pread(fd, buf, size, offset);
  150.     if (res == -1)
  151.         res = -errno;
  152.  
  153.     close(fd);
  154.  
  155.     return res;
  156.  
  157. }
  158.  
  159. static int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
  160.     off_t offset, struct fuse_file_info *fi){
  161.     char fpath[1000];
  162.     char pathsementara[1000];
  163.   //sprintf(pathsementara,"%s%s",dirpath,path);
  164.     if((strcmp(fpath,"/") == 0))
  165.     {
  166.         path=dirpath;
  167.         sprintf(fpath,"%s",path);
  168.     }
  169.     else {
  170.         strcpy(pathsementara,path);
  171.         if(strncmp(path,"/AtoZ_",6)==0) chyperDe(pathsementara);
  172.         sprintf(fpath, "%s/%s",dirpath,pathsementara);
  173.     }
  174.     //printf("%s\n",fpath);
  175.     int res = 0;
  176.  
  177.     DIR *dp;
  178.     struct dirent *de;
  179.  
  180.     (void) offset;
  181.     (void) fi;
  182.  
  183.     dp = opendir(fpath);
  184.     if (dp == NULL)
  185.         return -errno;
  186.  
  187.     while ((de = readdir(dp)) != NULL) {
  188.         struct stat st;
  189.         memset(&st, 0, sizeof(st));
  190.         st.st_ino = de->d_ino;
  191.         st.st_mode = de->d_type << 12;
  192.  
  193.         if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
  194.  
  195.         char temp[1000];
  196.         strcpy(temp,de->d_name);
  197.         if(strncmp(path,"/AtoZ_",6)==0) chyperEn(temp);
  198.  
  199.         res = (filler(buf, temp, &st, 0));
  200.         if(res!=0) break;
  201.     }
  202.  
  203.     closedir(dp);
  204.  
  205.     return 0;
  206. }
  207.  
  208. static int xmp_rename(const char *from, const char *to){
  209.     char awal[1000], akhir[1000], temp[1000];
  210.     if (strcmp(from, "/") == 0){
  211.         from = dirpath;
  212.         sprintf(awal, "%s", from);
  213.     }
  214.     else {
  215.         strcpy(temp, from);
  216.         if(strncmp(from, "/AtoZ_", 6) == 0) chyperDe(temp);
  217.         sprintf(awal, "%s%s", dirpath, temp);
  218.     }
  219.  
  220.     if (strcmp(to, "/") == 0){
  221.         to = dirpath;
  222.         sprintf(akhir, "%s", to);
  223.     }
  224.     else {
  225.         char temp2[1000];
  226.         strcpy(temp2, to);
  227.         if(strncmp(to, "/AtoZ_", 6) == 0)chyperDe(temp2);
  228.  
  229.         sprintf(akhir, "%s%s", dirpath, temp2);
  230.     }
  231.    
  232.     int res;
  233.  
  234.     res = rename(awal, akhir);
  235.     if (res == -1)
  236.         return -errno;
  237.  
  238.     logwriter(0,"RENAME",awal,akhir);
  239.     return 0;
  240. }
  241.  
  242. static int xmp_truncate(const char *path, off_t size)
  243. {
  244.     char fpath[1000];
  245.     if (strcmp(path, "/") == 0){
  246.         path = dirpath;
  247.         sprintf(fpath, "%s", path);
  248.     }
  249.     else {
  250.         char temp[1000];
  251.         strcpy(temp, path);
  252.  
  253.         if(strncmp(path, "/AtoZ_", 6) == 0) chyperDe(temp);
  254.  
  255.         sprintf(fpath, "%s%s", dirpath, temp);
  256.     }
  257.  
  258.     int res;
  259.  
  260.     res = truncate(fpath, size);
  261.     if (res == -1)
  262.         return -errno;
  263.  
  264.     return 0;
  265. }
  266.  
  267. static int xmp_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
  268. {
  269.     char fpath[1000];
  270.     if (strcmp(path, "/") == 0){
  271.         path = dirpath;
  272.         sprintf(fpath, "%s", path);
  273.     }
  274.     else {
  275.         char temp[1000];
  276.         strcpy(temp, path);
  277.  
  278.         if(strncmp(path, "/AtoZ_", 6) == 0) chyperDe(temp);
  279.  
  280.         sprintf(fpath, "%s%s", dirpath, temp);
  281.     }
  282.  
  283.     int fd;
  284.     int res;
  285.  
  286.     (void) fi;
  287.     fd = open(fpath, O_WRONLY);
  288.     if (fd == -1)
  289.         return -errno;
  290.  
  291.     res = pwrite(fd, buf, size, offset);
  292.     if (res == -1)
  293.         res = -errno;
  294.  
  295.     close(fd);
  296.  
  297.     logwriter(0,"WRITE",fpath,"");
  298.  
  299.     return res;
  300. }
  301.  
  302. static int xmp_mkdir(const char *path, mode_t mode)
  303. {
  304.     char fpath[1000];
  305.     if (strcmp(path, "/") == 0)
  306.     {
  307.         path = dirpath;
  308.         sprintf(fpath, "%s", path);
  309.     }
  310.     else {
  311.         char temp[1000];
  312.         strcpy(temp, path);
  313.         if(strncmp(path, "/AtoZ_", 6) == 0) chyperDe(temp);
  314.         sprintf(fpath, "%s%s", dirpath, temp);
  315.     }
  316.  
  317.     int res;
  318.  
  319.     res = mkdir(fpath, 0750);
  320.     if (res == -1)
  321.         return -errno;
  322.  
  323.     logwriter(0,"MKDIR",fpath,"");
  324.  
  325.     return 0;
  326. }
  327.  
  328. static int xmp_unlink(const char *path)
  329. {
  330.     char fpath[1000];
  331.     if (strcmp(path, "/") == 0)
  332.     {
  333.         path = dirpath;
  334.         sprintf(fpath, "%s", path);
  335.     }
  336.     else {
  337.         char temp[1000];
  338.         strcpy(temp, path);
  339.  
  340.         if(strncmp(path, "/AtoZ_", 6) == 0) chyperDe(temp);
  341.  
  342.         sprintf(fpath, "%s%s", dirpath, temp);
  343.     }
  344.  
  345.     int res;
  346.  
  347.     res = unlink(fpath);
  348.     if (res == -1)
  349.         return -errno;
  350.  
  351.     logwriter(1,"UNLINK",fpath,"");
  352.     return 0;
  353. }
  354.  
  355. static int xmp_rmdir(const char *path)
  356. {
  357.     char fpath[1000], temp[1000];
  358.     if (strcmp(path, "/") == 0){
  359.         path = dirpath;
  360.         sprintf(fpath, "%s", path);
  361.     }
  362.     else {
  363.         strcpy(temp, path);
  364.         if(strncmp(path, "/AtoZ_", 6) == 0) chyperDe(temp);
  365.  
  366.         sprintf(fpath, "%s%s", dirpath, temp);
  367.     }
  368.  
  369.     int res;
  370.  
  371.     res = rmdir(fpath);
  372.     if (res == -1)
  373.         return -errno;
  374.  
  375.     logwriter(1,"RMDIR",fpath,"");
  376.  
  377.     return 0;
  378. }
  379.  
  380. static int xmp_mknod(const char *path, mode_t mode, dev_t rdev)
  381. {
  382.     char fpath[1000], temp[1000];
  383.     if (strcmp(path, "/") == 0){
  384.         path = dirpath;
  385.         sprintf(fpath, "%s", path);
  386.     }
  387.     else {
  388.         strcpy(temp, path);
  389.  
  390.         if(strncmp(path, "/AtoZ_", 6) == 0) chyperDe(temp);
  391.  
  392.         sprintf(fpath, "%s%s", dirpath, temp);
  393.     }
  394.  
  395.     int res;
  396.  
  397.     if (S_ISREG(mode)) {
  398.         res = open(fpath, O_CREAT | O_EXCL | O_WRONLY, mode);
  399.         if (res >= 0)
  400.             res = close(res);
  401.     } else if (S_ISFIFO(mode))
  402.     res = mkfifo(fpath, mode);
  403.     else
  404.         res = mknod(fpath, mode, rdev);
  405.     if (res == -1)
  406.         return -errno;
  407.  
  408.     logwriter(0,"CREATE",fpath,"");
  409.  
  410.     return 0;
  411. }
  412.  
  413. static int xmp_open(const char *path, struct fuse_file_info *fi)
  414. {
  415.     char fpath[1000];
  416.     if (strcmp(path, "/") == 0)
  417.     {
  418.         path = dirpath;
  419.         sprintf(fpath, "%s", path);
  420.     }
  421.     else {
  422.         char temp[1000];
  423.         strcpy(temp, path);
  424.  
  425.         if(strncmp(path, "/AtoZ_", 6) == 0)chyperDe(temp);
  426.  
  427.         sprintf(fpath, "%s%s", dirpath, temp);
  428.     }
  429.  
  430.     int res;
  431.  
  432.     res = open(fpath, fi->flags);
  433.     if (res == -1)
  434.         return -errno;
  435.  
  436.     close(res);
  437.    
  438.   //logwriter(0, "OPEN", fpath, "");
  439.  
  440.     return 0;
  441. }
  442.  
  443. static struct fuse_operations xmp_oper = {
  444.     .getattr = xmp_getattr,
  445.     .readdir = xmp_readdir,
  446.     .read = xmp_read,
  447.     .write = xmp_write,
  448.     .truncate = xmp_truncate,
  449.     .mkdir = xmp_mkdir,
  450.     .unlink = xmp_unlink,
  451.     .rmdir = xmp_rmdir,
  452.     .mknod = xmp_mknod,
  453.     .open = xmp_open,
  454.     .rename = xmp_rename
  455. };
  456.  
  457. int main(int  argc, char *argv[]){
  458.     umask(0);
  459.     return fuse_main(argc, argv, &xmp_oper, NULL);
  460. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement