Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tmpfs_node *tmpfs_getnod(tmpfs_node *parent, char *path) {
- if (!path[0]) return parent;
- tmpfs_node *cnode = parent->sub.first; // sub directories.
- // Loop on all sub directories:
- while (cnode != (tmpfs_node *) NULLNODE) {
- // check name:
- unsigned int i = 0;
- char *name1 = &path[1]; // skip '/'.
- char *name2 = cnode->name;
- // loop on characters (compare name1 with name2)
- while (name1[i] && name1[i] != '/' && name2[i] && name1[i] == name2[i]) i++;
- // check comparison results:
- if (((!name1[i]) || name1[i] == '/') && (!name2[i])) {
- if (name1[i] == '/' && (cnode->mode & 0xF000) != MODE_TYPE_DIR)
- break; // not dir.
- return tmpfs_getnod(cnode, &name1[i]);
- }
- cnode = cnode->next;
- }
- // not found:
- return (tmpfs_node *) NULLNODE;
- }
- int tmpfs_mknod(unsigned int subid, char *path, unsigned int psize, mode_t mode, void *data) {
- tmpfs_node *rootdir = tmpfs_rootdir[subid];
- if (rootdir == (tmpfs_node *) 0) return -1;
- // 1- check if "path" already exists:
- if (tmpfs_getnod(rootdir, path) != (tmpfs_node *) NULLNODE)
- return -1; // file exists.
- // 2- Get parent:
- unsigned int i = psize;
- while(path[--i] != '/'); // make path[i] refers to the end of parent path.
- path[i] = 0;
- tmpfs_node *parent = tmpfs_getnod(rootdir, path);
- path[i] = '/';
- if (parent == (tmpfs_node *) NULLNODE) return -1; // invalid path.
- if ((parent->mode & 0xF000) != MODE_TYPE_DIR) return -1; // parent not directory.
- // 3- make node:
- tmpfs_node *nnode = (tmpfs_node *) kmalloc(sizeof(tmpfs_node));
- unsigned int namesize = psize - i; // with \0 in account.
- nnode->name = (char *) kmalloc(namesize);
- for (unsigned int j = 0; j < namesize; j++) nnode->name[j] = path[++i];
- nnode->mode = mode;
- nnode->size = 0;
- linkedlist_init((linkedlist *) &(nnode->sub));
- linkedlist_init((linkedlist *) &(nnode->blocks));
- // 4- add the node to the parent directory:
- linkedlist_add((linkedlist *) &(parent->sub), (linknode *) nnode);
- // 5- return:
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement