Advertisement
JOEYLEE

Untitled

May 26th, 2025
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.67 KB | None | 0 0
  1. static struct inode *namex(char *path, int nameiparent, char *name)
  2. {
  3.     struct inode *ip, *next;
  4.     if (*path == '/')
  5.         ip = iget(ROOTDEV, ROOTINO);
  6.     else
  7.         ip = idup(myproc()->cwd);
  8.  
  9.     while ((path = skipelem(path, name)) != 0)
  10.     {
  11.         ilock(ip);
  12.         if (ip->type != T_DIR)
  13.         {
  14.             iunlockput(ip);
  15.             return 0;
  16.         }
  17.  
  18.         if (nameiparent == 1 && *path == '\0')
  19.         {
  20.             // Stop one level early.
  21.             iunlock(ip);
  22.             return ip;
  23.         }
  24.         if ((next = dirlookup(ip, name, 0)) == 0)
  25.         {
  26.             iunlockput(ip);
  27.             return 0;
  28.         }
  29.         if(nameiparent == 2 && *path == '\0'){
  30.             iunlockput(ip);
  31.             return next;
  32.         }  
  33.         if(ip->mode != M_READ && ip->mode != M_ALL){
  34.             iunlockput(ip);
  35.             return 0;
  36.         }
  37.         iunlockput(ip);
  38.         ilock(next);
  39.         char buf[MAXPATH];
  40.         while(next->type == T_SYMLINK){
  41.             int len = 0;
  42.             readi(next, 0, (uint64)&len, 0, sizeof(int));
  43.             readi(next, 0, (uint64)buf, sizeof(int), len + 1);
  44.             iunlockput(next);
  45.             if((next = namei(buf, 0)) == 0){
  46.                 end_op();
  47.                 return 0;
  48.             }
  49.             ilock(next);
  50.         }
  51.         iunlock(next);
  52.         ip = next;
  53.     }
  54.     if (nameiparent)
  55.     {
  56.         iput(ip);
  57.         return 0;
  58.     }
  59.     return ip;
  60. }
  61.  
  62. struct inode *namei(char *path, int trace)
  63. {
  64.     char name[DIRSIZ];
  65.     return namex(path, trace, name);
  66. }
  67.  
  68. struct inode *nameiparent(char *path, char *name)
  69. {
  70.     return namex(path, 1, name);
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement