Guest User

Untitled

a guest
Nov 18th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. #define FUSE_USE_VERSION 28
  2. #include <fuse.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <time.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <dirent.h>
  11. #include <errno.h>
  12. #include <sys/time.h>
  13.  
  14. /* get only file extension from a file path string */
  15. const char *getFileExtension (const char *path) {
  16. const char *ext = &path[strlen (path) - 1];
  17. while (*ext != '.') ext--;
  18. return ext + 1;
  19. }
  20.  
  21. /* get only filename from a file path string */
  22. const char *getFileName (const char *path) {
  23. const char *filename = &path[strlen (path) - 1];
  24. while (*filename != '/') filename--;
  25. return filename + 1;
  26. }
  27.  
  28. /* get only directory path from a file path string */
  29. char *getFileDir (const char *path) {
  30. char *dir = (char*) malloc (1024 * sizeof (const char));
  31. strcpy (dir, path);
  32. for (int i = strlen (dir)-1; i >= 0 && dir[i] != '/'; i--)
  33. dir[i] = 0;
  34. return dir;
  35. }
  36.  
  37. /* check if file is of forbidden extension */
  38. int isForbiddenExtension (const char *path) {
  39. const char *ext = getFileExtension (path);
  40. return !strcmp ("pdf", ext) || !strcmp ("doc", ext) || !strcmp ("txt", ext);
  41. }
  42.  
  43. /* parse a string into a unix permission formatting bits */
  44. int parse (char* perms) {
  45. int bits = 0;
  46. for (int i = 0; i < 9; i++)
  47. if (perms[i] != '-')
  48. bits |= (1 << (8-i));
  49. return bits;
  50. }
  51.  
  52. static int do_getattr (const char *path, struct stat *stbuf) {
  53. int res;
  54.  
  55. res = lstat (path, stbuf);
  56. if (res == -1) return -errno;
  57. return 0;
  58. }
  59.  
  60. static int do_readdir (const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) {
  61. DIR *dp;
  62. struct dirent *de;
  63.  
  64. (void) offset;
  65. (void) fi;
  66.  
  67. dp = opendir (path);
  68. if (dp == NULL)
  69. return -errno;
  70.  
  71. while ((de = readdir (dp)) != NULL) {
  72. struct stat st;
  73. memset (&st, 0, sizeof (st));
  74. st.st_ino = de->d_ino;
  75. st.st_mode = de->d_type << 12;
  76. if (filler (buf, de->d_name, &st, 0))
  77. break;
  78. }
  79.  
  80. closedir (dp);
  81. return 0;
  82. }
  83.  
  84. static int do_read (const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
  85. if (strstr (path, "Documents") && isForbiddenExtension (path)) {
  86. // menampilkan pesan error.
  87. system ("notify-send 'Terjadi kesalahan! File berisi konten berbahaya.'");
  88.  
  89. // mengubah nama file menjadi '<namafile>.<ekstensi>.ditandai'.
  90. char ditandai[1024];
  91. sprintf (ditandai, "%s.ditandai", path);
  92. rename (path, ditandai);
  93.  
  94. // menyiapkan folder rahasia.
  95. char rahasiaDir[1024];
  96. sprintf (rahasiaDir, "%srahasia", getFileDir (path));
  97. DIR *rahasia = opendir (rahasiaDir);
  98. if (!rahasia)
  99. // folder rahasia belum ada. Buat dulu.
  100. mkdir (rahasiaDir, 0700);
  101.  
  102. // memindahkan file ke folder rahasia.
  103. char rahasiaName[1024];
  104. sprintf (rahasiaName, "%s/%s", rahasiaDir, getFileName (ditandai));
  105. rename (ditandai, rahasiaName);
  106.  
  107. // men-set file permission menjadi '000' (tidak bisa read write execute)
  108. chmod (rahasiaName, parse ("---------"));
  109. return -1;
  110. }
  111. else {
  112. // normal implementation
  113. int fd, res;
  114.  
  115. (void) fi;
  116. fd = open (path, O_RDONLY);
  117. if (fd == -1)
  118. return -errno;
  119.  
  120. res = pread (fd, buf, size, offset);
  121. if (res == -1)
  122. res = -errno;
  123.  
  124. close (fd);
  125. return res;
  126. }
  127. }
  128.  
  129. static struct fuse_operations my_fuse = {
  130. .getattr = do_getattr,
  131. .readdir = do_readdir,
  132. .read = do_read,
  133. };
  134.  
  135. int main (int argc, char **argv) {
  136. return fuse_main (argc, argv, &my_fuse, NULL);
  137. }
Add Comment
Please, Sign In to add comment