Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- uint64 sys_open(void)
- {
- char path[MAXPATH];
- int fd, omode;
- struct file *f;
- struct inode *ip;
- int n;
- if ((n = argstr(0, path, MAXPATH)) < 0 || argint(1, &omode) < 0)
- return -1;
- begin_op();
- if (omode & O_CREATE)
- {
- ip = create(path, T_FILE, 0, 0);
- if (ip == 0)
- {
- end_op();
- return -1;
- }
- }
- else
- {
- if(omode & O_NOACCESS){
- ip = namei(path, 2);
- }
- else if ((ip = namei(path, 0)) == 0) //file doesn't exist(may be a symlink)
- {
- end_op();
- return -1;
- }
- ilock(ip);
- if (ip->type == T_DIR && omode != O_RDONLY && omode != O_NOACCESS)
- {
- iunlockput(ip);
- end_op();
- return -1;
- }
- if ((ip->type == T_SYMLINK) && !(omode & O_NOACCESS)){ //trace symlink
- while (ip->type == T_SYMLINK) {
- int len = 0;
- readi(ip, 0, (uint64)&len, 0, sizeof(int));
- readi(ip, 0, (uint64)path, sizeof(int), len + 1);
- iunlockput(ip);
- if((ip = namei(path, 0)) == 0){
- end_op();
- return -1;
- }
- ilock(ip);
- }
- }
- if(ip->mode == M_READ && ((omode == O_WRONLY) || (omode == O_RDWR))){
- iunlockput(ip);
- end_op();
- return -1;
- }
- if(ip->mode == M_WRITE && ((omode == O_RDONLY) || (omode == O_RDWR))) {
- iunlockput(ip);
- end_op();
- return -1;
- }
- if(ip->mode == 0 && omode != O_NOACCESS){
- iunlockput(ip);
- end_op();
- return -1;
- }
- }
- if (ip->type == T_DEVICE && (ip->major < 0 || ip->major >= NDEV))
- {
- iunlockput(ip);
- end_op();
- return -1;
- }
- if ((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0)
- {
- if (f)
- fileclose(f);
- iunlockput(ip);
- end_op();
- return -1;
- }
- if (ip->type == T_DEVICE)
- {
- f->type = FD_DEVICE;
- f->major = ip->major;
- }
- else
- {
- f->type = FD_INODE;
- f->off = 0;
- }
- f->ip = ip;
- f->readable = !(omode & O_WRONLY);
- f->writable = (omode & O_WRONLY) || (omode & O_RDWR);
- if(omode & O_NOACCESS){
- f->readable = 0;
- f->writable = 0;
- }
- if ((omode & O_TRUNC) && ip->type == T_FILE)
- {
- itrunc(ip);
- }
- iunlock(ip);
- end_op();
- return fd;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement