Subversion Repositories HelenOS

Rev

Rev 2404 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 1987,1997, Prentice Hall
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use of the MINIX operating system in source and
  6.  * binary forms, with or without modification, are permitted provided
  7.  * that the following conditions are met:
  8.  
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  
  12.  * - Redistributions in binary form must reproduce the above
  13.  *   copyright notice, this list of conditions and the following
  14.  *   disclaimer in the documentation and/or other materials provided
  15.  *   with the distribution.
  16.  
  17.  * - Neither the name of Prentice Hall nor the names of the software
  18.  *   authors or contributors may be used to endorse or promote
  19.  *   products derived from this software without specific prior
  20.  *   written permission.
  21.  
  22.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
  23.  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  24.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  26.  * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
  27.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30.  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  32.  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  33.  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34.  */
  35.  
  36. /** @addtogroup FileSystemImpl
  37. * @{
  38. */
  39.  
  40. /**
  41.  * @file    stadir.c
  42.  * @brief   This file contains the code for performing four system calls relating to
  43.  *        status and directories.
  44.  */
  45.  
  46.  
  47. #include "fs.h"
  48. #include "stat.h"
  49. #include "file.h"
  50. #include "fproc.h"
  51. #include "inode.h"
  52. #include "param.h"
  53.  
  54.  
  55. static int change(inode_t **iip, char *name_ptr, int len);
  56. static int stat_inode(inode_t *rip, filp_t *fil_ptr, char *user_addr);
  57.  
  58. /**
  59.  * Perform the CHDIR system call
  60.  */
  61. int do_chdir()
  62. {
  63.      
  64.     /* Perform the chdir(file_name) system call */
  65.     int r;
  66.  
  67.     r = change(&fp->fp_workdir, file_name, strlen(file_name)+1);
  68.    
  69.     return r;
  70. }
  71.  
  72. /**
  73.  * Execute changing the current directory
  74.  */    
  75. int change(inode_t **iip, char *name_ptr, int len)
  76. {
  77.    
  78.     /*
  79.      * iip:     pointer to the inode pointer for the dir
  80.      * name_ptr:    pointer to the directory name to change to
  81.      * len:     length of the directory name string
  82.      */
  83.  
  84.     /* Do the actual work for chdir() and chroot(). */
  85.    
  86.     inode_t *rip;
  87.     register int r;
  88.      
  89.     r = OK;
  90.  
  91.     /* Try to open the new directory. */
  92.     if (fetch_name(name_ptr, len) != OK)
  93.         return err_code;
  94.      
  95.     if ((rip = eat_path(user_path)) == NIL_INODE)
  96.         return err_code;
  97.      
  98.     /* It must be a directory and also be searchable. */
  99.     if ((rip->i_mode & I_TYPE) != I_DIRECTORY)
  100.         r = FS_ENOTDIR;
  101.  
  102.     /* If error, return inode. */
  103.     if (r != OK) {
  104.         put_inode(rip);
  105.         return r;
  106.     }
  107.        
  108.     /* Everything is OK.  Make the change. */
  109.     put_inode(*iip);    /* release the old directory */
  110.     *iip = rip;     /* acquire the new one */
  111.  
  112.     return OK;
  113. }
  114.  
  115. /**
  116.  * Perform the STAT system call
  117.  */
  118. int do_stat()
  119. {
  120.    
  121.     /* Perform the stat(name, buf) system call. */
  122.    
  123.     register inode_t *rip;
  124.     register int r;
  125.    
  126.     /* Both stat() and fstat() use the same routine to do the real work.  That
  127.      * routine expects an inode, so acquire it temporarily.
  128.      */
  129.      
  130.     if (fetch_name(file_name, strlen(file_name)+1) != OK)
  131.         return err_code;
  132.        
  133.     if ((rip = eat_path(user_path)) == NIL_INODE)
  134.         return err_code;
  135.        
  136.     r = stat_inode(rip, NIL_FILP, fp->buffer); /* actually do the work.*/
  137.     put_inode(rip);     /* release the inode */
  138.      
  139.     return r;
  140. }
  141.  
  142. /**
  143.  * Perform the FSTAT system call
  144.  */
  145. int do_fstat()
  146. {
  147.    
  148.     /* Perform the fstat(fd, buf) system call. */
  149.    
  150.     register filp_t *rfilp;
  151.      
  152.  
  153.     /* Is the file descriptor valid? */
  154.     if ((rfilp = get_filp(fd)) == NIL_FILP)
  155.         return err_code;
  156.      
  157.     if (rfilp->filp_ino == NIL_INODE)
  158.         return FS_ENOENT;
  159.      
  160.     return(stat_inode(rfilp->filp_ino, NIL_FILP, fp->buffer));
  161. }
  162.  
  163. /**
  164.  * Execute the actual STAT operation
  165.  */
  166. int stat_inode(register inode_t *rip, filp_t *fil_ptr, char *user_addr)
  167. {  
  168.    
  169.     /*
  170.      * rip:         pointer to inode to stat
  171.      * fil_ptr:     filp pointer, supplied by 'fstat'
  172.      * user_addr:   user space address where stat buf goes
  173.      */
  174.  
  175.     /* Common code for stat and fstat system calls. */
  176.    
  177.     stat_t statbuf;
  178.    
  179.     /* Fill in the statbuf struct. */
  180.     statbuf.st_ino = rip->i_num;
  181.     statbuf.st_mode = rip->i_mode;
  182.     statbuf.st_nlink = rip->i_nlinks & BYTE;
  183.     statbuf.st_size = rip->i_size;
  184.    
  185.     /* Copy infomations to userspace. */
  186.     memcpy(user_addr, &statbuf, sizeof(statbuf));
  187.      
  188.     return OK;
  189. }
  190.  
  191.  
  192. /**
  193.  * }
  194.  */
  195.  
  196.