Advertisement
iocoder

tmpfs mknod

Nov 22nd, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.00 KB | None | 0 0
  1.  
  2. tmpfs_node *tmpfs_getnod(tmpfs_node *parent, char *path) {
  3.  
  4.     if (!path[0]) return parent;
  5.  
  6.     tmpfs_node *cnode = parent->sub.first; // sub directories.
  7.  
  8.     // Loop on all sub directories:
  9.     while (cnode != (tmpfs_node *) NULLNODE) {
  10.         // check name:
  11.         unsigned int i = 0;
  12.         char *name1 = &path[1]; // skip '/'.
  13.         char *name2 = cnode->name;
  14.  
  15.         // loop on characters (compare name1 with name2)
  16.         while (name1[i] && name1[i] != '/' && name2[i] && name1[i] == name2[i]) i++;
  17.  
  18.         // check comparison results:
  19.         if (((!name1[i]) || name1[i] == '/') && (!name2[i])) {
  20.             if (name1[i] == '/' && (cnode->mode & 0xF000) != MODE_TYPE_DIR)
  21.                     break; // not dir.
  22.             return tmpfs_getnod(cnode, &name1[i]);
  23.         }
  24.  
  25.         cnode = cnode->next;
  26.     }
  27.  
  28.     // not found:
  29.     return (tmpfs_node *) NULLNODE;
  30. }
  31.  
  32. int tmpfs_mknod(unsigned int subid, char *path, unsigned int psize, mode_t mode, void *data) {
  33.  
  34.     tmpfs_node *rootdir = tmpfs_rootdir[subid];
  35.     if (rootdir == (tmpfs_node *) 0) return -1;
  36.  
  37.     // 1- check if "path" already exists:
  38.     if (tmpfs_getnod(rootdir, path) != (tmpfs_node *) NULLNODE)
  39.         return -1; // file exists.
  40.  
  41.     // 2- Get parent:
  42.     unsigned int i = psize;
  43.     while(path[--i] != '/'); // make path[i] refers to the end of parent path.
  44.     path[i] = 0;
  45.     tmpfs_node *parent = tmpfs_getnod(rootdir, path);
  46.     path[i] = '/';
  47.     if (parent == (tmpfs_node *) NULLNODE) return -1; // invalid path.
  48.     if ((parent->mode & 0xF000) != MODE_TYPE_DIR) return -1; // parent not directory.
  49.  
  50.     // 3- make node:
  51.     tmpfs_node *nnode = (tmpfs_node *) kmalloc(sizeof(tmpfs_node));
  52.     unsigned int namesize = psize - i; // with \0 in account.
  53.     nnode->name = (char *) kmalloc(namesize);
  54.     for (unsigned int j = 0; j < namesize; j++) nnode->name[j] = path[++i];
  55.     nnode->mode = mode;
  56.     nnode->size = 0;
  57.     linkedlist_init((linkedlist *) &(nnode->sub));
  58.     linkedlist_init((linkedlist *) &(nnode->blocks));
  59.  
  60.     // 4- add the node to the parent directory:
  61.     linkedlist_add((linkedlist *) &(parent->sub), (linknode *) nnode);
  62.  
  63.     // 5- return:
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement