Advertisement
VisualPaul

Untitled

Dec 24th, 2015
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.76 KB | None | 0 0
  1.  
  2. int
  3. sys_open(void)
  4. {
  5.   char *path;
  6.   int fd, omode;
  7.   struct file *f;
  8.   struct inode *ip;
  9.  
  10.   if(argstr(0, &path) < 0 || argint(1, &omode) < 0)
  11.     return -1;
  12.   ip = namei(path);
  13.   if (ip == 0) {
  14.     if(omode & O_CREATE){
  15.       int mode;
  16.       if (argint(2, &mode) < 0 || !check_permissions(mode))
  17.         return -1;
  18.       begin_trans();
  19.       ip = create(path, T_FILE, 0, 0, proc->euid, (uint)mode);
  20.       commit_trans();
  21.       if(ip == 0) {
  22.         return -1;
  23.       }
  24.     } else {
  25.       return -1;
  26.     }
  27.   } else {
  28.     ilock(ip);
  29.   }
  30.   // by this moment we hold ip
  31.   if (ip->type == T_MUTEX) {
  32.     iunlockput(ip);
  33.     return -1;
  34.   }
  35.    
  36.   if(ip->type == T_DIR && omode != O_RDONLY){
  37.     iunlockput(ip);
  38.     return -1;
  39.   }
  40.   if (((omode & O_WRONLY) || (omode & O_RDWR)) && !can_write(ip)) {
  41.     iunlockput(ip);
  42.     return -1;
  43.   }
  44.   if (!(omode & O_WRONLY) && !can_read(ip)) {
  45.     iunlockput(ip);
  46.     return -1;
  47.   }
  48.      
  49.   if (ip->type == T_FIFO) {
  50.     if (omode & O_RDWR)
  51.       goto fail_put;
  52.     if (ip->pipe == 0) {
  53.       ip->pipe = fifo_pipe_alloc(ip);
  54.       if (ip->pipe == 0)
  55.         goto fail_put;
  56.     }
  57.     struct pipe *pipe = ip->pipe;
  58.     /* the situation with locks and channels here is a bit messy, so
  59.        pipe->lock is used to protect pipe->readopen AND pipe->writeopen
  60.        &pipe->readopen is used to wait for the reader */
  61.  
  62.     acquire(&pipe->lock);
  63.     iunlock(ip);
  64.     if (omode == O_RDONLY) {
  65.       struct file *f = fifo_file_alloc(pipe, 0);
  66.       while (pipe->writeopen == 0) {
  67.         wakeup(&pipe->readopen);
  68.         sleep(&pipe->writeopen, &pipe->lock);
  69.       }
  70.       wakeup(&pipe->readopen);
  71.       release(&pipe->lock);
  72.       fd = fdalloc(f);
  73.     } else {
  74.       struct file *f = fifo_file_alloc(pipe, 1);
  75.       while (pipe->readopen == 0) {
  76.         wakeup(&pipe->writeopen);
  77.         sleep(&pipe->readopen, &pipe->lock);
  78.       }
  79.       wakeup(&pipe->writeopen);
  80.       release(&pipe->lock);
  81.       fd = fdalloc(f);
  82.     }
  83.     if (fd < 0)
  84.       goto fail;
  85.     return fd;
  86. fail_put:
  87.     iunlockput(ip);
  88.     return -1;
  89. fail:
  90.     iunlock(ip);
  91.     return -1;
  92.   }
  93.  
  94.   if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){
  95.     if(f)
  96.       fileclose(f);
  97.     iunlockput(ip);
  98.     return -1;
  99.   }
  100.   iunlock(ip);
  101.  
  102.   f->type = FD_INODE;
  103.   f->ip = ip;
  104.   f->off = 0;
  105.   f->readable = !(omode & O_WRONLY);
  106.   f->writable = (omode & O_WRONLY) || (omode & O_RDWR);
  107.   return fd;
  108. }
  109.  
  110. int
  111. sys_mkdir(void)
  112. {
  113.   char *path;
  114.   struct inode *ip;
  115.   int mode;
  116.  
  117.   begin_trans();
  118.   if(argstr(0, &path) < 0 || argint(1, &mode) < 0 || !check_permissions(mode) || (ip = create(path, T_DIR, 0, 0, proc->euid, (uint)mode)) == 0){
  119.     commit_trans();
  120.     return -1;
  121.   }
  122.   iunlockput(ip);
  123.   commit_trans();
  124.   return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement