Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2374 | konopa | 1 | /* This file contains the procedures for opening, closing, and seeking on files. */ |
2 | |||
3 | /* Methods: |
||
4 | * do_open: perform the OPEN system call |
||
5 | * do_close: perform the CLOSE system call |
||
6 | * do_lseek: perform the SEEK system call |
||
7 | */ |
||
8 | |||
9 | |||
10 | #include "fs.h" |
||
11 | #include "block.h" |
||
12 | #include "file.h" |
||
13 | #include "fproc.h" |
||
14 | #include "inode.h" |
||
15 | #include "param.h" |
||
16 | |||
17 | |||
18 | int do_open() |
||
19 | { |
||
20 | |||
21 | /* Perform the open(file_name) system call */ |
||
22 | |||
23 | int r; |
||
24 | register inode_t *rip; |
||
25 | filp_t *fil_ptr; |
||
26 | |||
27 | |||
28 | r = fetch_name(file_name, strlen(file_name)+1); |
||
29 | if (r != OK) |
||
30 | return err_code; /* name was bad */ |
||
31 | |||
32 | /* See if file descriptor and filp slots are available. */ |
||
33 | if ( (r = get_fd(0, &fd, &fil_ptr)) != OK) |
||
34 | return r; |
||
35 | |||
36 | /* Scan path name. */ |
||
37 | if ( (rip = eat_path(user_path)) == NIL_INODE) |
||
38 | return err_code; |
||
39 | |||
40 | /* Claim the file descriptor and filp slot and fill them in. */ |
||
41 | fp->fp_filp[fd] = fil_ptr; |
||
42 | fil_ptr->filp_count = 1; |
||
43 | fil_ptr->filp_ino = rip; |
||
44 | fil_ptr->filp_flags = R_BIT; |
||
45 | |||
46 | return fd; |
||
47 | } |
||
48 | |||
49 | int do_close() |
||
50 | { |
||
51 | |||
52 | /* Perform the close(fd) system call. */ |
||
53 | |||
54 | register filp_t *rfilp; |
||
55 | register inode_t *rip; |
||
56 | |||
57 | |||
58 | /* First locate the inode that belongs to the file descriptor */ |
||
59 | if ( (rfilp = get_filp(fd)) == NIL_FILP) |
||
60 | return err_code; |
||
61 | |||
62 | rip = rfilp->filp_ino; /* 'rip' points to the inode */ |
||
63 | |||
64 | if (--rfilp->filp_count == 0) { |
||
65 | put_inode(rip); |
||
66 | } |
||
67 | |||
68 | fp->fp_filp[fd] = NIL_FILP; |
||
69 | |||
70 | return OK; |
||
71 | } |
||
72 | |||
73 | int do_lseek() |
||
74 | { |
||
75 | |||
76 | /* Perform the seek(ls_fd, offset, whence) system call. */ |
||
77 | |||
78 | register filp_t *rfilp; |
||
79 | register offset_t pos, new_pos; |
||
80 | |||
81 | |||
82 | /* Check to see if the file descriptor is valid. */ |
||
83 | if ( (rfilp = get_filp(fd)) == NIL_FILP) |
||
84 | return err_code; |
||
85 | |||
86 | |||
87 | /* The value of 'whence' determines the start position to use. */ |
||
88 | switch(whence) { |
||
89 | case 0: pos = 0; |
||
90 | break; |
||
91 | case 1: pos = rfilp->filp_pos; |
||
92 | break; |
||
93 | case 2: pos = rfilp->filp_ino->i_size; |
||
94 | break; |
||
95 | default: return FS_EINVAL; |
||
96 | } |
||
97 | |||
98 | /* Check for overflow. */ |
||
99 | if (((long)offset > 0) && ((long)(pos + offset) < (long)pos)) |
||
100 | return FS_EINVAL; |
||
101 | if (((long)offset < 0) && ((long)(pos + offset) > (long)pos)) |
||
102 | return FS_EINVAL; |
||
103 | |||
104 | new_pos = pos + offset; |
||
105 | |||
106 | /* Check for setting behind the end. */ |
||
107 | if ((new_pos) > rfilp->filp_ino->i_size) { |
||
108 | pos = rfilp->filp_ino->i_size; |
||
109 | } |
||
110 | else if (new_pos < 0) { |
||
111 | pos = 0; |
||
112 | } |
||
113 | else { |
||
114 | pos = new_pos; |
||
115 | } |
||
116 | rfilp->filp_pos = pos; |
||
117 | |||
118 | return pos; |
||
119 | } |
||
120 |