kbd: use a better get_key method
[thunix.git] / fs / inode.c
blob10387d19c8793b4ff9914934bacbed5a91b5f2c4
1 #include <stdio.h>
2 #include <string.h>
3 #include <err.h>
4 #include <malloc.h>
5 #include <fs.h>
7 #define current_time 0
9 void free_inode(struct inode *inode)
11 /* Can not free current working directory */
12 if (inode == inode->i_fs->pwd)
13 return;
14 if(inode->i_data)
15 free(inode->i_data);
16 free(inode);
19 struct inode *new_inode(int mode, int data_size)
21 struct inode *inode;
23 inode = malloc(sizeof(*inode));
24 if (!inode)
25 return NULL;
26 memset(inode, 0, sizeof(*inode));
28 inode->i_data = malloc(data_size);
29 if (!inode->i_data) {
30 free(inode);
31 return NULL;
33 memset(inode->i_data, 0, data_size);
35 inode->i_mode = mode;
37 inode->i_atime = current_time;
38 inode->i_ctime = current_time;
39 inode->i_mtime = current_time;
41 inode->i_fs = root_fs();
43 return inode;
46 /*
47 * get the inode for dname under given dir
49 * Return NULL if not found, or error if errors happened,
50 * or return inode if found.
52 * The current implementation has no cache, it would be
53 * better to add one.
55 struct inode * iget(const char *dname, struct inode *dir)
57 struct inode * inode = ERR_PTR(-ENOSYS);
59 if (dir->i_op && dir->i_op->iget)
60 inode = dir->i_op->iget(dname, dir);
62 return inode;
66 int sys_mknod(const char *pathname, int mode)
68 struct inode *dir = namei_parent(pathname);
69 struct inode *inode;
70 int err;
72 if (IS_ERR(dir))
73 return PTR_ERR(dir);
75 err = -ENOSYS;
76 if (dir->i_op && dir->i_op->mknod) {
77 inode = dir->i_op->mknod(dir, get_base_name(pathname), mode);
78 if (IS_ERR(inode))
79 err = PTR_ERR(inode);
80 else
81 free_inode(inode);
84 free_inode(dir);
85 return err;