Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. #define FUSE_USE_VERSION 26
  2.  
  3. #include <fuse.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <sys/stat.h>
  9. #include <sys/types.h>
  10. #include <unistd.h>
  11. #include <dirent.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14.  
  15. #include <sys/types.h>
  16. #include <sys/socket.h>
  17.  
  18. #include <netinet/in.h>
  19.  
  20. static const char *hello_str = "Hello World!\n";
  21. static const char *hello_path = "/hello";
  22.  
  23. static int dfs_open(const char *path, struct fuse_file_info * info){
  24. if (strcmp(path, hello_path) != 0)
  25. return -ENOENT;
  26.  
  27. if ((info->flags & 3) != O_RDONLY)
  28. return -EACCES;
  29.  
  30. return 0;
  31. }
  32.  
  33. static int dfs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info * info) {
  34. size_t len;
  35. (void) info;
  36. if(strcmp(path, hello_path) != 0)
  37. return -ENOENT;
  38.  
  39. len = strlen(hello_str);
  40. if (offset < len) {
  41. if (offset + size > len)
  42. size = len - offset;
  43. memcpy(buf, hello_str + offset, size);
  44. } else
  45. size = 0;
  46.  
  47. return size;
  48. }
  49.  
  50. static int dfs_write (const char * a, const char * b, size_t count, off_t d, struct fuse_file_info * info){
  51. return 0;
  52. }
  53.  
  54. static int dfs_release (const char * a, struct fuse_file_info * info){
  55. return 0;
  56. }
  57.  
  58. static int dfs_rename (const char * oldpath, const char * newpath){
  59. return rename(oldpath, newpath);
  60. }
  61.  
  62.  
  63. static int dfs_unlink (const char * pathname){
  64. return unlink(pathname);
  65. }
  66.  
  67. static int dfs_rmdir (const char * pathname){
  68. printf("Removing Directory With Name: %s\n", pathname);
  69. return rmdir(pathname);
  70. }
  71.  
  72. typedef struct{
  73. int mode;
  74. char dir_name[100];
  75.  
  76. } socket_struct;
  77.  
  78.  
  79. void my_mkdir(int network_socket, struct sockaddr_in sin, socket_struct a){
  80.  
  81.  
  82.  
  83. printf("sheyvanili teqsti: %s\n", a.dir_name);
  84. printf("Sending You Peace of shit\n");
  85. sendto(network_socket, &a, sizeof(socket_struct), 0, (struct sockaddr*) &sin, sizeof(sin));
  86.  
  87.  
  88. // send(network_socket, server_message, sizeof(server_message), 0);
  89. }
  90.  
  91. static int dfs_mkdir (const char * pathname, mode_t mode){
  92.  
  93. int network_socket;
  94. network_socket = socket(AF_INET, SOCK_STREAM, 0);
  95.  
  96. struct sockaddr_in server_address;
  97. server_address.sin_family = AF_INET;
  98. server_address.sin_port = htons(10018);
  99. server_address.sin_addr.s_addr = INADDR_ANY;
  100.  
  101. printf("HERE BROOOO!!!!\n");
  102.  
  103. int connection_status = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
  104.  
  105.  
  106. socket_struct bla;
  107.  
  108. strcpy(bla.dir_name, pathname);
  109. bla.mode = mode;
  110.  
  111.  
  112. printf("connection_status: %d\n", connection_status);
  113. if (connection_status == -1) {
  114. printf("There was an error making a connection.\n");
  115. }
  116.  
  117.  
  118.  
  119. my_mkdir(network_socket, server_address, bla);
  120.  
  121.  
  122. close(network_socket);
  123.  
  124. return 0;
  125.  
  126. // printf("Creating Directory With Name: %s\n", pathname);
  127. // return mkdir(pathname, mode);
  128. }
  129.  
  130. static int dfs_opendir (const char * name, struct fuse_file_info * info){
  131. printf("Opening Directory With Name: %s\n", name);
  132. return opendir(name);
  133. }
  134.  
  135. static int dfs_releasedir (const char * name, struct fuse_file_info * info){
  136. printf("Closing Directory With Name: %s\n", name);
  137. DIR * dirp = opendir(name);
  138. return closedir(dirp);
  139. }
  140.  
  141. static int dfs_truncate (const char * path, off_t length){
  142. return truncate(path, length);
  143. }
  144.  
  145. static int dfs_create (const char * pathname, mode_t mode, struct fuse_file_info * info){
  146. return creat(pathname, mode);
  147. }
  148.  
  149. static int dfs_getattr(const char * path, struct stat * stbuf){
  150. int res = 0;
  151.  
  152. memset(stbuf, 0, sizeof(struct stat));
  153. if (strcmp(path, "/") == 0) {
  154. stbuf->st_mode = S_IFDIR | 0755;
  155. stbuf->st_nlink = 2;
  156. } else if (strcmp(path, hello_path) == 0) {
  157. stbuf->st_mode = S_IFREG | 0444;
  158. stbuf->st_nlink = 1;
  159. stbuf->st_size = strlen(hello_str);
  160. } else
  161. res = -ENOENT;
  162.  
  163. return res;
  164. }
  165.  
  166. static struct fuse_operations dfs_oper = {
  167. .open = dfs_open,
  168. .read = dfs_read,
  169. .write = dfs_write,
  170. .release = dfs_release,
  171. .rename = dfs_rename,
  172. .unlink = dfs_unlink,
  173. .rmdir = dfs_rmdir,
  174. .mkdir = dfs_mkdir,
  175. .opendir = dfs_opendir,
  176. .releasedir = dfs_releasedir,
  177. .create = dfs_create,
  178. .getattr = dfs_getattr,
  179. .truncate = dfs_truncate,
  180. };
  181.  
  182. int main(int argc, char *argv[])
  183. {
  184. return fuse_main(argc, argv, &dfs_oper, NULL);
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement