Advertisement
JOEYLEE

Untitled

May 25th, 2025
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.67 KB | None | 0 0
  1. uint64 sys_open(void)
  2. {
  3.     char path[MAXPATH];
  4.     int fd, omode;
  5.     struct file *f;
  6.     struct inode *ip;
  7.     int n;
  8.     if ((n = argstr(0, path, MAXPATH)) < 0 || argint(1, &omode) < 0)
  9.         return -1;
  10.  
  11.     begin_op();
  12.  
  13.     if (omode & O_CREATE)
  14.     {
  15.         ip = create(path, T_FILE, 0, 0);
  16.         if (ip == 0)
  17.         {
  18.             end_op();
  19.             return -1;
  20.         }
  21.     }
  22.     else
  23.     {
  24.         if(omode & O_NOACCESS){
  25.             ip = namei(path, 2);
  26.         }
  27.         else if ((ip = namei(path, 0)) == 0) //file doesn't exist(may be a symlink)
  28.         {
  29.             end_op();
  30.             return -1;
  31.         }
  32.         ilock(ip);
  33.  
  34.         if (ip->type == T_DIR && omode != O_RDONLY && omode != O_NOACCESS)
  35.         {
  36.             iunlockput(ip);
  37.             end_op();
  38.             return -1;
  39.         }
  40.         if ((ip->type == T_SYMLINK) && !(omode & O_NOACCESS)){  //trace symlink
  41.             while (ip->type == T_SYMLINK) {
  42.                 int len = 0;
  43.                 readi(ip, 0, (uint64)&len, 0, sizeof(int));
  44.                 readi(ip, 0, (uint64)path, sizeof(int), len + 1);
  45.                 iunlockput(ip);
  46.                 if((ip = namei(path, 0)) == 0){
  47.                     end_op();
  48.                     return -1;
  49.                 }
  50.                 ilock(ip);
  51.             }
  52.         }
  53.  
  54.         if(ip->mode == M_READ && ((omode == O_WRONLY) || (omode == O_RDWR))){
  55.             iunlockput(ip);
  56.             end_op();
  57.             return -1;
  58.         }
  59.            
  60.         if(ip->mode == M_WRITE && ((omode == O_RDONLY) || (omode == O_RDWR))) {
  61.             iunlockput(ip);
  62.             end_op();
  63.             return -1;
  64.         }
  65.            
  66.         if(ip->mode == 0 && omode != O_NOACCESS){
  67.             iunlockput(ip);
  68.             end_op();
  69.             return -1;
  70.         }
  71.            
  72.     }
  73.  
  74.     if (ip->type == T_DEVICE && (ip->major < 0 || ip->major >= NDEV))
  75.     {
  76.         iunlockput(ip);
  77.         end_op();
  78.         return -1;
  79.     }
  80.  
  81.     if ((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0)
  82.     {
  83.         if (f)
  84.             fileclose(f);
  85.         iunlockput(ip);
  86.         end_op();
  87.         return -1;
  88.     }
  89.  
  90.     if (ip->type == T_DEVICE)
  91.     {
  92.         f->type = FD_DEVICE;
  93.         f->major = ip->major;
  94.     }
  95.     else
  96.     {
  97.         f->type = FD_INODE;
  98.         f->off = 0;
  99.     }
  100.     f->ip = ip;
  101.     f->readable = !(omode & O_WRONLY);
  102.     f->writable = (omode & O_WRONLY) || (omode & O_RDWR);
  103.     if(omode & O_NOACCESS){
  104.         f->readable = 0;
  105.         f->writable = 0;
  106.     }
  107.     if ((omode & O_TRUNC) && ip->type == T_FILE)
  108.     {
  109.         itrunc(ip);
  110.     }
  111.  
  112.     iunlock(ip);
  113.     end_op();
  114.  
  115.     return fd;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement