Subversion Repositories HelenOS

Rev

Rev 2734 | Rev 2741 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2008 Jakub Jermar
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup libc
  30.  * @{
  31.  */
  32. /** @file
  33.  */
  34.  
  35. #include <vfs.h>
  36. #include <stdlib.h>
  37. #include <unistd.h>
  38. #include <dirent.h>
  39. #include <fcntl.h>
  40. #include <sys/stat.h>
  41. #include <sys/types.h>
  42. #include <ipc/ipc.h>
  43. #include <ipc/services.h>
  44. #include <async.h>
  45. #include <atomic.h>
  46. #include <futex.h>
  47. #include <errno.h>
  48. #include <string.h>
  49. #include "../../srv/vfs/vfs.h"
  50.  
  51. int vfs_phone = -1;
  52. atomic_t vfs_phone_futex = FUTEX_INITIALIZER;
  53.  
  54. static int vfs_connect(void)
  55. {
  56.     if (vfs_phone < 0)
  57.         vfs_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VFS, 0, 0);
  58.     return vfs_phone;
  59. }
  60.  
  61. int mount(const char *fs_name, const char *mp, const char *dev)
  62. {
  63.     int res;
  64.     ipcarg_t rc;
  65.     aid_t req;
  66.  
  67.     int dev_handle = 0; /* TODO */
  68.  
  69.     futex_down(&vfs_phone_futex);
  70.     async_serialize_start();
  71.     if (vfs_phone < 0) {
  72.         res = vfs_connect();
  73.         if (res < 0) {
  74.             async_serialize_end();
  75.             futex_up(&vfs_phone_futex);
  76.             return res;
  77.         }
  78.     }
  79.     req = async_send_1(vfs_phone, VFS_MOUNT, dev_handle, NULL);
  80.     rc = ipc_data_write_start(vfs_phone, (void *)fs_name, strlen(fs_name));
  81.     if (rc != EOK) {
  82.         async_wait_for(req, NULL);
  83.         async_serialize_end();
  84.         futex_up(&vfs_phone_futex);
  85.         return (int) rc;
  86.     }
  87.     rc = ipc_data_write_start(vfs_phone, (void *)mp, strlen(mp));
  88.     if (rc != EOK) {
  89.         async_wait_for(req, NULL);
  90.         async_serialize_end();
  91.         futex_up(&vfs_phone_futex);
  92.         return (int) rc;
  93.     }
  94.     async_wait_for(req, &rc);
  95.     async_serialize_end();
  96.     futex_up(&vfs_phone_futex);
  97.     return (int) rc;
  98. }
  99.  
  100. static int _open(const char *path, int lflag, int oflag, ...)
  101. {
  102.     int res;
  103.     ipcarg_t rc;
  104.     ipc_call_t answer;
  105.     aid_t req;
  106.    
  107.     futex_down(&vfs_phone_futex);
  108.     async_serialize_start();
  109.     if (vfs_phone < 0) {
  110.         res = vfs_connect();
  111.         if (res < 0) {
  112.             async_serialize_end();
  113.             futex_up(&vfs_phone_futex);
  114.             return res;
  115.         }
  116.     }
  117.     req = async_send_3(vfs_phone, VFS_OPEN, lflag, oflag, 0, &answer);
  118.     rc = ipc_data_write_start(vfs_phone, path, strlen(path));
  119.     if (rc != EOK) {
  120.         async_wait_for(req, NULL);
  121.         async_serialize_end();
  122.         futex_up(&vfs_phone_futex);
  123.         return (int) rc;
  124.     }
  125.     async_wait_for(req, &rc);
  126.     async_serialize_end();
  127.     futex_up(&vfs_phone_futex);
  128.     return (int) IPC_GET_ARG1(answer);
  129. }
  130.  
  131. int open(const char *path, int oflag, ...)
  132. {
  133.     return _open(path, L_FILE, oflag);
  134. }
  135.  
  136. int close(int fildes)
  137. {
  138.     int res;
  139.     ipcarg_t rc;
  140.  
  141.     futex_down(&vfs_phone_futex);
  142.     async_serialize_start();
  143.     if (vfs_phone < 0) {
  144.         res = vfs_connect();
  145.         if (res < 0) {
  146.             async_serialize_end();
  147.             futex_up(&vfs_phone_futex);
  148.             return res;
  149.         }
  150.     }
  151.        
  152.     rc = async_req_1_0(vfs_phone, VFS_CLOSE, fildes);
  153.  
  154.     async_serialize_end();
  155.     futex_up(&vfs_phone_futex);
  156.    
  157.     return (int)rc;
  158. }
  159.  
  160. ssize_t read(int fildes, void *buf, size_t nbyte)
  161. {
  162.     int res;
  163.     ipcarg_t rc;
  164.     ipc_call_t answer;
  165.     aid_t req;
  166.  
  167.     futex_down(&vfs_phone_futex);
  168.     async_serialize_start();
  169.     if (vfs_phone < 0) {
  170.         res = vfs_connect();
  171.         if (res < 0) {
  172.             async_serialize_end();
  173.             futex_up(&vfs_phone_futex);
  174.             return res;
  175.         }
  176.     }
  177.     req = async_send_1(vfs_phone, VFS_READ, fildes, &answer);
  178.     if (ipc_data_read_start(vfs_phone, (void *)buf, nbyte) != EOK) {
  179.         async_wait_for(req, NULL);
  180.         async_serialize_end();
  181.         futex_up(&vfs_phone_futex);
  182.         return (ssize_t) rc;
  183.     }
  184.     async_wait_for(req, &rc);
  185.     async_serialize_end();
  186.     futex_up(&vfs_phone_futex);
  187.     if (rc == EOK)
  188.         return (ssize_t) IPC_GET_ARG1(answer);
  189.     else
  190.         return -1;
  191. }
  192.  
  193. ssize_t write(int fildes, const void *buf, size_t nbyte)
  194. {
  195.     int res;
  196.     ipcarg_t rc;
  197.     ipc_call_t answer;
  198.     aid_t req;
  199.  
  200.     futex_down(&vfs_phone_futex);
  201.     async_serialize_start();
  202.     if (vfs_phone < 0) {
  203.         res = vfs_connect();
  204.         if (res < 0) {
  205.             async_serialize_end();
  206.             futex_up(&vfs_phone_futex);
  207.             return res;
  208.         }
  209.     }
  210.     req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer);
  211.     if (ipc_data_write_start(vfs_phone, (void *)buf, nbyte) != EOK) {
  212.         async_wait_for(req, NULL);
  213.         async_serialize_end();
  214.         futex_up(&vfs_phone_futex);
  215.         return (ssize_t) rc;
  216.     }
  217.     async_wait_for(req, &rc);
  218.     async_serialize_end();
  219.     futex_up(&vfs_phone_futex);
  220.     if (rc == EOK)
  221.         return (ssize_t) IPC_GET_ARG1(answer);
  222.     else
  223.         return -1;
  224. }
  225.  
  226. off_t lseek(int fildes, off_t offset, int whence)
  227. {
  228.     int res;
  229.     ipcarg_t rc;
  230.  
  231.     futex_down(&vfs_phone_futex);
  232.     async_serialize_start();
  233.     if (vfs_phone < 0) {
  234.         res = vfs_connect();
  235.         if (res < 0) {
  236.             async_serialize_end();
  237.             futex_up(&vfs_phone_futex);
  238.             return res;
  239.         }
  240.     }
  241.        
  242.     off_t newoffs;
  243.     rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence,
  244.         (ipcarg_t)&newoffs);
  245.  
  246.     async_serialize_end();
  247.     futex_up(&vfs_phone_futex);
  248.  
  249.     if (rc != EOK)
  250.         return (off_t) -1;
  251.    
  252.     return newoffs;
  253. }
  254.  
  255. int ftruncate(int fildes, off_t length)
  256. {
  257.     int res;
  258.     ipcarg_t rc;
  259.    
  260.     futex_down(&vfs_phone_futex);
  261.     async_serialize_start();
  262.     if (vfs_phone < 0) {
  263.         res = vfs_connect();
  264.         if (res < 0) {
  265.             async_serialize_end();
  266.             futex_up(&vfs_phone_futex);
  267.             return res;
  268.         }
  269.     }
  270.     rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length);
  271.     async_serialize_end();
  272.     futex_up(&vfs_phone_futex);
  273.     return (int) rc;
  274. }
  275.  
  276. DIR *opendir(const char *dirname)
  277. {
  278.     DIR *dirp = malloc(sizeof(DIR));
  279.     if (!dirp)
  280.         return NULL;
  281.     dirp->fd = _open(dirname, L_DIRECTORY, 0);
  282.     if (dirp->fd < 0) {
  283.         free(dirp);
  284.         return NULL;
  285.     }
  286.     return dirp;
  287. }
  288.  
  289. struct dirent *readdir(DIR *dirp)
  290. {
  291.     ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1);
  292.     if (len <= 0)
  293.         return NULL;
  294.     return &dirp->res;
  295. }
  296.  
  297. void rewinddir(DIR *dirp)
  298. {
  299.     (void) lseek(dirp->fd, 0, SEEK_SET);
  300. }
  301.  
  302. int closedir(DIR *dirp)
  303. {
  304.     (void) close(dirp->fd);
  305.     free(dirp);
  306.     return 0;
  307. }
  308.  
  309. int mkdir(const char *path, mode_t mode)
  310. {
  311.     int res;
  312.     ipcarg_t rc;
  313.     aid_t req;
  314.    
  315.     futex_down(&vfs_phone_futex);
  316.     async_serialize_start();
  317.     if (vfs_phone < 0) {
  318.         res = vfs_connect();
  319.         if (res < 0) {
  320.             async_serialize_end();
  321.             futex_up(&vfs_phone_futex);
  322.             return res;
  323.         }
  324.     }
  325.     req = async_send_1(vfs_phone, VFS_MKDIR, mode, NULL);
  326.     rc = ipc_data_write_start(vfs_phone, path, strlen(path));
  327.     if (rc != EOK) {
  328.         async_wait_for(req, NULL);
  329.         async_serialize_end();
  330.         futex_up(&vfs_phone_futex);
  331.         return (int) rc;
  332.     }
  333.     async_wait_for(req, &rc);
  334.     async_serialize_end();
  335.     futex_up(&vfs_phone_futex);
  336.     return EOK;
  337. }
  338.  
  339. static int _unlink(const char *path, int lflag)
  340. {
  341.     int res;
  342.     ipcarg_t rc;
  343.     aid_t req;
  344.    
  345.     futex_down(&vfs_phone_futex);
  346.     async_serialize_start();
  347.     if (vfs_phone < 0) {
  348.         res = vfs_connect();
  349.         if (res < 0) {
  350.             async_serialize_end();
  351.             futex_up(&vfs_phone_futex);
  352.             return res;
  353.         }
  354.     }
  355.     req = async_send_0(vfs_phone, VFS_UNLINK, NULL);
  356.     rc = ipc_data_write_start(vfs_phone, path, strlen(path));
  357.     if (rc != EOK) {
  358.         async_wait_for(req, NULL);
  359.         async_serialize_end();
  360.         futex_up(&vfs_phone_futex);
  361.         return (int) rc;
  362.     }
  363.     async_wait_for(req, &rc);
  364.     async_serialize_end();
  365.     futex_up(&vfs_phone_futex);
  366.     return EOK;
  367. }
  368.  
  369. int unlink(const char *path)
  370. {
  371.     return _unlink(path, L_NONE);
  372. }
  373.  
  374. int rmdir(const char *path)
  375. {
  376.     return _unlink(path, L_DIRECTORY);
  377. }
  378.  
  379. /** @}
  380.  */
  381.