Subversion Repositories HelenOS

Rev

Rev 2251 | Go to most recent revision | 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.  
  20. #define CON_FS_ATTEMPTS     1000
  21.  
  22. static int fs_phone;
  23.  
  24. int main(int argc, char *argv[])
  25. {
  26.    
  27.     char fname[30];
  28.     unsigned int file_handle;
  29.     int retval, flags, entry;
  30.     unsigned short entries_num, inode_num;
  31.     size_t size;
  32.     void *share = NULL;
  33.  
  34.        
  35.     printf("Cat task\n");
  36.  
  37.     printf("Connecting to the SERVICE_FS...");
  38.     if (!connect_to_fs(&fs_phone, CON_FS_ATTEMPTS)) {
  39.         printf("Connection to SERVICE_FS was refused\n");
  40.         return -1;
  41.     }
  42.     printf("OK\n");
  43.    
  44.     printf("Creating address space area...");
  45.     size = ALIGN_UP(BLOCK_SIZE * sizeof(char), PAGE_SIZE);
  46.     share = mmap(share, size, AS_AREA_READ | AS_AREA_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0);
  47.     if ((int)share < 0){
  48.         printf("As_area_create error: %d\n", (int)share);
  49.         return -1;
  50.     }
  51.     printf("OK\n");
  52.  
  53.     printf("Connecting the task to FS...");
  54.     retval = async_req_2(fs_phone, FS_NEW_CONSUMER, task_get_id(), 0, NULL, NULL);
  55.     if (retval < 0) {
  56.         printf("FS_NEW_CONSUMER error: %d\n", retval);
  57.         return -1;
  58.     }
  59.     printf("OK\n");
  60.  
  61.     printf("Sending memory to FS_SERVICE...");
  62.     flags = 0;
  63.     flags = AS_AREA_READ | AS_AREA_WRITE;
  64.     retval = async_req_3(fs_phone, IPC_M_AS_AREA_SEND, (uintptr_t)share, size, flags, NULL, NULL, NULL);
  65.     if (retval < 0) {
  66.         printf("%d\n", retval);
  67.         return -1;
  68.     }
  69.     printf("OK\n");
  70.    
  71.     /* Allocating structure for extended message. */
  72.     message_params_t *params;
  73.     params = malloc(sizeof(message_params_t));
  74.     memset((void*)params, 0, sizeof(message_params_t));
  75.  
  76.     /* We want lookup our work directory. */
  77.     printf("Request for get number of entries...");
  78.     retval = send_request(fs_phone, FS_DSUM, params, share);
  79.     if (retval < 0) {
  80.         printf("%d\n", retval);
  81.         return -1;
  82.     }
  83.     printf("OK\n");
  84.     printf("Total number of entries: %d\n", retval);
  85.    
  86.     entries_num = retval;
  87.    
  88.  
  89.     /* File list in working directory. */
  90.     printf("File list:\n");
  91.    
  92.     for (entry = 0; entry < entries_num; entry++) {
  93.  
  94.         params->entry_number = entry;
  95.         retval = send_request(fs_phone, FS_READENTRY, params, share);
  96.         if (retval < 0) {
  97.             printf("%d\n", retval);
  98.             return -1;
  99.         }
  100.         /*
  101.         if (retval < sizeof(unsigned short))
  102.             continue;
  103.         */
  104.         memcpy(&inode_num, share, sizeof(unsigned short));
  105.         memcpy(fname, (void *)(share+sizeof(unsigned short)), retval-sizeof(unsigned short));
  106.  
  107.         /* Do not show empty entries. */
  108.         if (!inode_num)
  109.             continue;
  110.          
  111.         printf("Inode number: %u\t\t", inode_num);
  112.         printf("File name: %s\n", fname);
  113.        
  114.     }
  115.     printf("OK\n");
  116.    
  117.     /* We want to change working directory */
  118.     memcpy(fname, "uspace/fs", 10);
  119.     memcpy(params->fname, fname, 10);
  120.  
  121.     printf("Request for changing actual directory to %s...", fname);
  122.     retval = send_request(fs_phone, FS_CHDIR, params, share);
  123.     if (retval < 0) {
  124.         printf("%d\n", retval);
  125.         return -1;
  126.     }
  127.     printf("OK\n");
  128.  
  129.     /* We want to work with specified file. */
  130.     memcpy(fname, "fs.c", 10);
  131.     memcpy(params->fname, fname, 10);
  132.  
  133.     printf("Request for opening file %s...", fname);
  134.     retval = send_request(fs_phone, FS_OPEN, params, share);
  135.     if (retval < 0) {
  136.         printf("%d\n", retval);
  137.         return -1;
  138.     }
  139.     printf("OK\n");
  140.  
  141.     file_handle = retval;
  142.     printf("Returned file handle...%d\n", file_handle);
  143.    
  144.     memcpy(params->fname, fname, 10);
  145.     params->fd = file_handle;
  146.  
  147.     printf("Request for getting info about file %s[FSTAT called]...", fname);  
  148.     retval = send_request(fs_phone, FS_FSTAT, params, share);
  149.     if (retval < 0) {
  150.         printf("%d\n", retval);
  151.         return -1;
  152.     }
  153.     printf("OK\n");
  154.    
  155.     params->fd = file_handle;
  156.     params->offset = 100;
  157.     params->whence = 1; /* from actual position in the file */
  158.  
  159.     printf("Request for seeking after %d bytes inside file %s...", params->offset, fname);
  160.     retval = send_request(fs_phone, FS_SEEK, params, share);
  161.     if (retval < 0) {
  162.         printf("%d\n", retval);
  163.         return -1;
  164.     }
  165.     printf("OK\n");
  166.  
  167.     printf("New file position is: %d\n", retval);
  168.    
  169.     params->nbytes = 100;
  170.    
  171.     printf("Request for reading %d bytes from file %s...", params->nbytes, fname);
  172.     retval = send_request(fs_phone, FS_READ, params, share);
  173.     if (retval < 0) {
  174.         printf("%d\n", retval);
  175.         return -1;
  176.     }
  177.     printf("OK\n");
  178.    
  179.     printf("%d bytes was read into buffer\n", retval);
  180.     printf("\nContent of the buffer:\n");
  181.     printf(share);
  182.     printf("\n\n");
  183.    
  184.    
  185.     params->offset = 0;
  186.     params->whence = 0; /* from beginning of the file */
  187.  
  188.     printf("Request for seeking after %d bytes inside file %s...", params->offset, fname);
  189.     retval = send_request(fs_phone, FS_SEEK, params, share);
  190.     if (retval < 0) {
  191.         printf("%d\n", retval);
  192.         return -1;
  193.     }
  194.     printf("OK\n");
  195.  
  196.     printf("New file position is: %d\n", retval);
  197.     params->fd = file_handle;
  198.    
  199.     params->nbytes = 50;
  200.  
  201.     printf("Another request for reading %d bytes from file %s...", params->nbytes, fname);
  202.     retval = send_request(fs_phone, FS_READ, params, share);
  203.     if (retval < 0) {
  204.         printf("%d\n", retval);
  205.         return -1;
  206.     }
  207.     printf("OK\n");
  208.  
  209.     printf("%d bytes was read into buffer\n", retval);
  210.     printf("\nContent of the buffer:\n");
  211.     printf(share);
  212.     printf("\n\n");
  213.  
  214.  
  215.     printf("Request for closing file %s...", fname);
  216.     retval = send_request(fs_phone, FS_CLOSE, params, share);
  217.     if (retval < 0) {
  218.         printf("%d\n", retval);
  219.         return -1;
  220.     }
  221.     printf("OK\n");
  222.    
  223.     printf("Request for closing file %s once more...", fname);
  224.     retval = send_request(fs_phone, FS_CLOSE, params, share);
  225.     if (retval < 0) {
  226.         printf("%d\n", retval);
  227.         return -1;
  228.     }
  229.     printf("OK\n");
  230.  
  231.     return 0;
  232. }
  233.  
  234.