Subversion Repositories HelenOS

Rev

Rev 2389 | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Cat
  3.  */
  4.  
  5. #include <err.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <async.h>
  11. #include <align.h>
  12. #include <as.h>
  13. #include <ipc/ipc.h>
  14. #include <ipc/services.h>
  15. #include <sys/mman.h>
  16. #include "../fs/fs.h"
  17. #include "../share/shared_proto.h"
  18. #include "../console/console.h"
  19. #include "../fs/stat.h"
  20.  
  21. #define CON_FS_ATTEMPTS     1000
  22.  
  23. static int fs_phone;
  24.  
  25. int main(int argc, char *argv[])
  26. {
  27.    
  28.     char fname[30];
  29.     unsigned int file_handle;
  30.     int retval, flags, entry;
  31.     unsigned short entries_num, inode_num;
  32.     size_t size;
  33.     void *share = NULL;
  34.  
  35.        
  36.     printf("Cat task\n");
  37.  
  38.     printf("Connecting to the SERVICE_FS...");
  39.     if (!connect_to_fs(&fs_phone, CON_FS_ATTEMPTS)) {
  40.         printf("Connection to SERVICE_FS was refused\n");
  41.         return -1;
  42.     }
  43.     printf("OK\n");
  44.    
  45.     printf("Creating address space area...");
  46.     size = ALIGN_UP(BLOCK_SIZE * sizeof(char), PAGE_SIZE);
  47.     share = mmap(share, size, AS_AREA_READ | AS_AREA_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
  48.     if ((int)share < 0){
  49.         printf("As_area_create error: %d\n", (int)share);
  50.         return -1;
  51.     }
  52.     printf("OK\n");
  53.  
  54.     printf("Connecting the task to FS...");
  55.     retval = async_req_2(fs_phone, FS_NEW_CONSUMER, task_get_id(), 0, NULL, NULL);
  56.     if (retval < 0) {
  57.         printf("%d\n", retval);
  58.         return -1;
  59.     }
  60.     printf("OK\n");
  61.  
  62.     printf("Sending memory to FS_SERVICE...");
  63.     flags = 0;
  64.     flags = AS_AREA_READ | AS_AREA_WRITE;
  65.     retval = async_req_3(fs_phone, IPC_M_AS_AREA_SEND, (uintptr_t)share, size, flags, NULL, NULL, NULL);
  66.     if (retval < 0) {
  67.         printf("%d\n", retval);
  68.         return -1;
  69.     }
  70.     printf("OK\n");
  71.    
  72.     /* Allocating structure for extended message. */
  73.     message_params_t *params;
  74.     params = malloc(sizeof(message_params_t));
  75.     memset((void*)params, 0, sizeof(message_params_t));
  76.  
  77.     /* We want lookup our work directory. */
  78.     printf("Request for get number of entries...");
  79.     retval = send_request(fs_phone, FS_DSUM, params, share);
  80.     if (retval < 0) {
  81.         printf("%d\n", retval);
  82.         return -1;
  83.     }
  84.     printf("OK\n");
  85.     printf("Total number of entries: %d\n", retval);
  86.    
  87.     entries_num = retval;
  88.    
  89.  
  90.     /* File list in working directory. */
  91.     printf("File list:\n");
  92.    
  93.     for (entry = 0; entry < entries_num; entry++) {
  94.  
  95.         params->entry_number = entry;
  96.         retval = send_request(fs_phone, FS_READENTRY, params, share);
  97.         if (retval < 0) {
  98.             printf("%d\n", retval);
  99.             return -1;
  100.         }
  101.         /*
  102.         if (retval < sizeof(unsigned short))
  103.             continue;
  104.         */
  105.         memcpy(&inode_num, share, sizeof(unsigned short));
  106.         memcpy(fname, (void *)(share+sizeof(unsigned short)), retval-sizeof(unsigned short));
  107.  
  108.         /* Do not show empty entries. */
  109.         if (!inode_num)
  110.             continue;
  111.          
  112.         printf("Inode number: %u\t\t", inode_num);
  113.         printf("File name: %s\n", fname);
  114.        
  115.     }
  116.     printf("OK\n");
  117.    
  118.     /* We want to change working directory */
  119.     memcpy(fname, "uspace/fs", 10);
  120.     memcpy(params->fname, fname, 10);
  121.  
  122.     printf("Request for changing actual directory to %s...", fname);
  123.     retval = send_request(fs_phone, FS_CHDIR, params, share);
  124.     if (retval < 0) {
  125.         printf("%d\n", retval);
  126.         return -1;
  127.     }
  128.     printf("OK\n");
  129.  
  130.     /* We want to work with specified file. */
  131.     memcpy(fname, "fs.c", 10);
  132.     memcpy(params->fname, fname, 10);
  133.  
  134.     printf("Request for opening file %s...", fname);
  135.     retval = send_request(fs_phone, FS_OPEN, params, share);
  136.     if (retval < 0) {
  137.         printf("%d\n", retval);
  138.         return -1;
  139.     }
  140.     printf("OK\n");
  141.  
  142.     file_handle = retval;
  143.     printf("Returned file handle...%d\n", file_handle);
  144.    
  145.     memcpy(params->fname, fname, 10);
  146.     params->fd = file_handle;
  147.  
  148.     printf("Request for getting info about file %s[FSTAT called]...", fname);  
  149.     retval = send_request(fs_phone, FS_FSTAT, params, share);
  150.     if (retval < 0) {
  151.         printf("%d\n", retval);
  152.         return -1;
  153.     }
  154.     printf("OK\n");
  155.    
  156.     params->fd = file_handle;
  157.     params->offset = 100;
  158.     params->whence = 1; /* from actual position in the file */
  159.  
  160.     printf("Request for seeking after %d bytes inside file %s...", params->offset, fname);
  161.     retval = send_request(fs_phone, FS_SEEK, params, share);
  162.     if (retval < 0) {
  163.         printf("%d\n", retval);
  164.         return -1;
  165.     }
  166.     printf("OK\n");
  167.  
  168.     printf("New file position is: %d\n", retval);
  169.    
  170.     params->nbytes = 100;
  171.    
  172.     printf("Request for reading %d bytes from file %s...", params->nbytes, fname);
  173.     retval = send_request(fs_phone, FS_READ, params, share);
  174.     if (retval < 0) {
  175.         printf("%d\n", retval);
  176.         return -1;
  177.     }
  178.     printf("OK\n");
  179.    
  180.     printf("%d bytes was read into buffer\n", retval);
  181.     printf("\nContent of the buffer:\n");
  182.     printf(share);
  183.     printf("\n\n");
  184.    
  185.    
  186.     params->offset = 0;
  187.     params->whence = 0; /* from beginning of the file */
  188.  
  189.     printf("Request for seeking after %d bytes inside file %s...", params->offset, fname);
  190.     retval = send_request(fs_phone, FS_SEEK, params, share);
  191.     if (retval < 0) {
  192.         printf("%d\n", retval);
  193.         return -1;
  194.     }
  195.     printf("OK\n");
  196.  
  197.     printf("New file position is: %d\n", retval);
  198.     params->fd = file_handle;
  199.    
  200.     params->nbytes = 50;
  201.  
  202.     printf("Another request for reading %d bytes from file %s...", params->nbytes, fname);
  203.     retval = send_request(fs_phone, FS_READ, params, share);
  204.     if (retval < 0) {
  205.         printf("%d\n", retval);
  206.         return -1;
  207.     }
  208.     printf("OK\n");
  209.  
  210.     printf("%d bytes was read into buffer\n", retval);
  211.     printf("\nContent of the buffer:\n");
  212.     printf(share);
  213.     printf("\n\n");
  214.  
  215.  
  216.     printf("Request for closing file %s...", fname);
  217.     retval = send_request(fs_phone, FS_CLOSE, params, share);
  218.     if (retval < 0) {
  219.         printf("%d\n", retval);
  220.         return -1;
  221.     }
  222.     printf("OK\n");
  223.    
  224.     /*
  225.     printf("Request for closing file %s once more...", fname);
  226.     retval = send_request(fs_phone, FS_CLOSE, params, share);
  227.     if (retval < 0) {
  228.         printf("%d\n", retval);
  229.         return -1;
  230.     }
  231.     printf("OK\n");
  232.     */
  233.  
  234.     printf("Request for dropping the connection to the FS...");
  235.     retval = send_request(fs_phone, FS_DROP_CONSUMER, params, share);
  236.     if (retval < 0) {
  237.         printf("%d\n", retval);
  238.         return -1;
  239.     }
  240.     printf("OK\n");
  241.    
  242.    
  243.     /* Unmapping share area. */
  244.     printf("Unmapping share area...");
  245.     retval = munmap(share, size);
  246.     if (retval < 0) {
  247.         printf("%d\n", retval);
  248.         return -1;
  249.     }
  250.     printf("OK\n");
  251.    
  252.     printf("Creating address space area...");
  253.     size = ALIGN_UP(BLOCK_SIZE * sizeof(char), PAGE_SIZE);
  254.     share = mmap(share, size, AS_AREA_READ | AS_AREA_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
  255.     if ((int)share < 0){
  256.         printf("As_area_create error: %d\n", (int)share);
  257.         return -1;
  258.     }
  259.     printf("OK\n");
  260.  
  261.     /* Next connection to the FS. */
  262.     printf("Connecting the task to FS...");
  263.     retval = async_req_2(fs_phone, FS_NEW_CONSUMER, task_get_id(), 0, NULL, NULL);
  264.     if (retval < 0) {
  265.         printf("FS_NEW_CONSUMER error: %d\n", retval);
  266.         return -1;
  267.     }
  268.     printf("OK\n");
  269.    
  270.     printf("Sending memory to FS_SERVICE...");
  271.     flags = 0;
  272.     flags = AS_AREA_READ | AS_AREA_WRITE;
  273.     retval = async_req_3(fs_phone, IPC_M_AS_AREA_SEND, (uintptr_t)share, size, flags, NULL, NULL, NULL);
  274.     if (retval < 0) {
  275.         printf("%d\n", retval);
  276.         return -1;
  277.     }
  278.     printf("OK\n");
  279.  
  280.     /* We want to work with specified file. */
  281.     memcpy(fname, "/kernel/arch", 20);
  282.     memcpy(params->fname, fname, 20);
  283.  
  284.     printf("Request for getting info about file %s[STAT called]...", fname);   
  285.     retval = send_request(fs_phone, FS_STAT, params, share);
  286.     if (retval < 0) {
  287.         printf("%d\n", retval);
  288.         return -1;
  289.     }
  290.     printf("OK\n");
  291.    
  292.     stat_t file_info;
  293.     memcpy((void *)(&file_info), share, sizeof(stat_t));
  294.  
  295.     printf("Info about file: %s\n", fname);
  296.     printf("Inode number: %d\n", file_info.st_ino);
  297.     printf("Mode: %d\n", file_info.st_mode);
  298.     printf("Links: %d\n", file_info.st_nlink);
  299.     printf("Size: %d\n", file_info.st_size);
  300.    
  301.     return 0;
  302. }
  303.  
  304.