1 | /* |
---|
2 | * IMFS Directory Access Routines |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-1999. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | * |
---|
11 | * $Id$ |
---|
12 | */ |
---|
13 | |
---|
14 | #if HAVE_CONFIG_H |
---|
15 | #include "config.h" |
---|
16 | #endif |
---|
17 | |
---|
18 | #include <sys/types.h> |
---|
19 | #include <sys/stat.h> |
---|
20 | #include <rtems/chain.h> |
---|
21 | #include <fcntl.h> |
---|
22 | #include <errno.h> |
---|
23 | #include <stdlib.h> |
---|
24 | #include <stdio.h> |
---|
25 | #include <string.h> |
---|
26 | #include <dirent.h> |
---|
27 | |
---|
28 | #include "imfs.h" |
---|
29 | #include <rtems/libio_.h> |
---|
30 | #include <rtems/seterr.h> |
---|
31 | |
---|
32 | /* |
---|
33 | * imfs_dir_open |
---|
34 | * |
---|
35 | * This rountine will verify that the node being opened as a directory is |
---|
36 | * in fact a directory node. If it is then the offset into the directory |
---|
37 | * will be set to 0 to position to the first directory entry. |
---|
38 | */ |
---|
39 | |
---|
40 | int imfs_dir_open( |
---|
41 | rtems_libio_t *iop, |
---|
42 | const char *pathname, |
---|
43 | uint32_t flag, |
---|
44 | uint32_t mode |
---|
45 | ) |
---|
46 | { |
---|
47 | IMFS_jnode_t *the_jnode; |
---|
48 | |
---|
49 | /* Is the node a directory ? */ |
---|
50 | the_jnode = (IMFS_jnode_t *) iop->pathinfo.node_access; |
---|
51 | |
---|
52 | if ( the_jnode->type != IMFS_DIRECTORY ) |
---|
53 | return -1; /* It wasn't a directory --> return error */ |
---|
54 | |
---|
55 | iop->offset = 0; |
---|
56 | return 0; |
---|
57 | } |
---|
58 | |
---|
59 | /* |
---|
60 | * imfs_dir_read |
---|
61 | * |
---|
62 | * This routine will read the next directory entry based on the directory |
---|
63 | * offset. The offset should be equal to -n- time the size of an individual |
---|
64 | * dirent structure. If n is not an integer multiple of the sizeof a |
---|
65 | * dirent structure, an integer division will be performed to determine |
---|
66 | * directory entry that will be returned in the buffer. Count should reflect |
---|
67 | * -m- times the sizeof dirent bytes to be placed in the buffer. |
---|
68 | * If there are not -m- dirent elements from the current directory position |
---|
69 | * to the end of the exisiting file, the remaining entries will be placed in |
---|
70 | * the buffer and the returned value will be equal to -m actual- times the |
---|
71 | * size of a directory entry. |
---|
72 | */ |
---|
73 | |
---|
74 | ssize_t imfs_dir_read( |
---|
75 | rtems_libio_t *iop, |
---|
76 | void *buffer, |
---|
77 | size_t count |
---|
78 | ) |
---|
79 | { |
---|
80 | /* |
---|
81 | * Read up to element iop->offset in the directory chain of the |
---|
82 | * imfs_jnode_t struct for this file descriptor. |
---|
83 | */ |
---|
84 | rtems_chain_node *the_node; |
---|
85 | rtems_chain_control *the_chain; |
---|
86 | IMFS_jnode_t *the_jnode; |
---|
87 | int bytes_transferred; |
---|
88 | int current_entry; |
---|
89 | int first_entry; |
---|
90 | int last_entry; |
---|
91 | struct dirent tmp_dirent; |
---|
92 | |
---|
93 | the_jnode = (IMFS_jnode_t *)iop->pathinfo.node_access; |
---|
94 | the_chain = &the_jnode->info.directory.Entries; |
---|
95 | |
---|
96 | if ( rtems_chain_is_empty( the_chain ) ) |
---|
97 | return 0; |
---|
98 | |
---|
99 | /* Move to the first of the desired directory entries */ |
---|
100 | the_node = the_chain->first; |
---|
101 | |
---|
102 | bytes_transferred = 0; |
---|
103 | first_entry = iop->offset; |
---|
104 | /* protect against using sizes that are not exact multiples of the */ |
---|
105 | /* -dirent- size. These could result in unexpected results */ |
---|
106 | last_entry = first_entry + (count/sizeof(struct dirent)) * sizeof(struct dirent); |
---|
107 | |
---|
108 | /* The directory was not empty so try to move to the desired entry in chain*/ |
---|
109 | for ( |
---|
110 | current_entry = 0; |
---|
111 | current_entry < last_entry; |
---|
112 | current_entry = current_entry + sizeof(struct dirent) ){ |
---|
113 | |
---|
114 | if ( rtems_chain_is_tail( the_chain, the_node ) ){ |
---|
115 | /* We hit the tail of the chain while trying to move to the first */ |
---|
116 | /* entry in the read */ |
---|
117 | return bytes_transferred; /* Indicate that there are no more */ |
---|
118 | /* entries to return */ |
---|
119 | } |
---|
120 | |
---|
121 | if( current_entry >= first_entry ) { |
---|
122 | /* Move the entry to the return buffer */ |
---|
123 | tmp_dirent.d_off = current_entry; |
---|
124 | tmp_dirent.d_reclen = sizeof( struct dirent ); |
---|
125 | the_jnode = (IMFS_jnode_t *) the_node; |
---|
126 | tmp_dirent.d_ino = the_jnode->st_ino; |
---|
127 | tmp_dirent.d_namlen = strlen( the_jnode->name ); |
---|
128 | strcpy( tmp_dirent.d_name, the_jnode->name ); |
---|
129 | memcpy( |
---|
130 | buffer + bytes_transferred, |
---|
131 | (void *)&tmp_dirent, |
---|
132 | sizeof( struct dirent ) |
---|
133 | ); |
---|
134 | iop->offset = iop->offset + sizeof(struct dirent); |
---|
135 | bytes_transferred = bytes_transferred + sizeof( struct dirent ); |
---|
136 | } |
---|
137 | |
---|
138 | the_node = the_node->next; |
---|
139 | } |
---|
140 | |
---|
141 | /* Success */ |
---|
142 | return bytes_transferred; |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | |
---|
147 | /* |
---|
148 | * imfs_dir_close |
---|
149 | * |
---|
150 | * This routine will be called by the generic close routine to cleanup any |
---|
151 | * resources that have been allocated for the management of the file |
---|
152 | */ |
---|
153 | |
---|
154 | int imfs_dir_close( |
---|
155 | rtems_libio_t *iop |
---|
156 | ) |
---|
157 | { |
---|
158 | /* |
---|
159 | * The generic close routine handles the deallocation of the file control |
---|
160 | * and associated memory. At present the imfs_dir_close simply |
---|
161 | * returns a successful completion status. |
---|
162 | */ |
---|
163 | |
---|
164 | return 0; |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | |
---|
169 | /* |
---|
170 | * imfs_dir_lseek |
---|
171 | * |
---|
172 | * This routine will behave in one of three ways based on the state of |
---|
173 | * argument whence. Based on the state of its value the offset argument will |
---|
174 | * be interpreted using one of the following methods: |
---|
175 | * |
---|
176 | * SEEK_SET - offset is the absolute byte offset from the start of the |
---|
177 | * logical start of the dirent sequence that represents the |
---|
178 | * directory |
---|
179 | * SEEK_CUR - offset is used as the relative byte offset from the current |
---|
180 | * directory position index held in the iop structure |
---|
181 | * SEEK_END - N/A --> This will cause an EINVAL to be returned. |
---|
182 | */ |
---|
183 | |
---|
184 | rtems_off64_t imfs_dir_lseek( |
---|
185 | rtems_libio_t *iop, |
---|
186 | rtems_off64_t offset, |
---|
187 | int whence |
---|
188 | ) |
---|
189 | { |
---|
190 | switch( whence ) { |
---|
191 | case SEEK_SET: /* absolute move from the start of the file */ |
---|
192 | case SEEK_CUR: /* relative move */ |
---|
193 | iop->offset = (iop->offset/sizeof(struct dirent)) * |
---|
194 | sizeof(struct dirent); |
---|
195 | break; |
---|
196 | |
---|
197 | case SEEK_END: /* Movement past the end of the directory via lseek */ |
---|
198 | /* is not a permitted operation */ |
---|
199 | default: |
---|
200 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
201 | break; |
---|
202 | } |
---|
203 | |
---|
204 | return 0; |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
208 | |
---|
209 | /* |
---|
210 | * imfs_dir_fstat |
---|
211 | * |
---|
212 | * This routine will obtain the following information concerning the current |
---|
213 | * directory: |
---|
214 | * st_dev 0ll |
---|
215 | * st_ino 1 |
---|
216 | * st_mode mode extracted from the jnode |
---|
217 | * st_nlink number of links to this node |
---|
218 | * st_uid uid extracted from the jnode |
---|
219 | * st_gid gid extracted from the jnode |
---|
220 | * st_rdev 0ll |
---|
221 | * st_size the number of bytes in the directory |
---|
222 | * This is calculated by taking the number of entries |
---|
223 | * in the directory and multiplying by the size of a |
---|
224 | * dirent structure |
---|
225 | * st_blksize 0 |
---|
226 | * st_blocks 0 |
---|
227 | * stat_atime time of last access |
---|
228 | * stat_mtime time of last modification |
---|
229 | * stat_ctime time of the last change |
---|
230 | * |
---|
231 | * This information will be returned to the calling function in a -stat- struct |
---|
232 | * |
---|
233 | */ |
---|
234 | |
---|
235 | int imfs_dir_fstat( |
---|
236 | rtems_filesystem_location_info_t *loc, |
---|
237 | struct stat *buf |
---|
238 | ) |
---|
239 | { |
---|
240 | rtems_chain_node *the_node; |
---|
241 | rtems_chain_control *the_chain; |
---|
242 | IMFS_jnode_t *the_jnode; |
---|
243 | |
---|
244 | |
---|
245 | the_jnode = (IMFS_jnode_t *) loc->node_access; |
---|
246 | |
---|
247 | buf->st_dev = 0ll; |
---|
248 | buf->st_ino = the_jnode->st_ino; |
---|
249 | buf->st_mode = the_jnode->st_mode; |
---|
250 | buf->st_nlink = the_jnode->st_nlink; |
---|
251 | buf->st_uid = the_jnode->st_uid; |
---|
252 | buf->st_gid = the_jnode->st_gid; |
---|
253 | buf->st_rdev = 0ll; |
---|
254 | buf->st_blksize = 0; |
---|
255 | buf->st_blocks = 0; |
---|
256 | buf->st_atime = the_jnode->stat_atime; |
---|
257 | buf->st_mtime = the_jnode->stat_mtime; |
---|
258 | buf->st_ctime = the_jnode->stat_ctime; |
---|
259 | |
---|
260 | buf->st_size = 0; |
---|
261 | |
---|
262 | the_chain = &the_jnode->info.directory.Entries; |
---|
263 | |
---|
264 | /* Run through the chain and count the number of directory entries */ |
---|
265 | /* that are subordinate to this directory node */ |
---|
266 | for ( the_node = the_chain->first ; |
---|
267 | !rtems_chain_is_tail( the_chain, the_node ) ; |
---|
268 | the_node = the_node->next ) { |
---|
269 | |
---|
270 | buf->st_size = buf->st_size + sizeof( struct dirent ); |
---|
271 | } |
---|
272 | |
---|
273 | return 0; |
---|
274 | } |
---|
275 | |
---|
276 | /* |
---|
277 | * IMFS_dir_rmnod |
---|
278 | * |
---|
279 | * This routine is available from the optable to remove a node |
---|
280 | * from the IMFS file system. |
---|
281 | */ |
---|
282 | |
---|
283 | int imfs_dir_rmnod( |
---|
284 | rtems_filesystem_location_info_t *parent_pathloc, /* IN */ |
---|
285 | rtems_filesystem_location_info_t *pathloc /* IN */ |
---|
286 | ) |
---|
287 | { |
---|
288 | IMFS_jnode_t *the_jnode; |
---|
289 | |
---|
290 | the_jnode = (IMFS_jnode_t *) pathloc->node_access; |
---|
291 | |
---|
292 | /* |
---|
293 | * You cannot remove a node that still has children |
---|
294 | */ |
---|
295 | |
---|
296 | if ( ! rtems_chain_is_empty( &the_jnode->info.directory.Entries ) ) |
---|
297 | rtems_set_errno_and_return_minus_one( ENOTEMPTY ); |
---|
298 | |
---|
299 | /* |
---|
300 | * You cannot remove the file system root node. |
---|
301 | */ |
---|
302 | |
---|
303 | if ( pathloc->mt_entry->mt_fs_root.node_access == pathloc->node_access ) |
---|
304 | rtems_set_errno_and_return_minus_one( EBUSY ); |
---|
305 | |
---|
306 | /* |
---|
307 | * You cannot remove a mountpoint. |
---|
308 | */ |
---|
309 | |
---|
310 | if ( the_jnode->info.directory.mt_fs != NULL ) |
---|
311 | rtems_set_errno_and_return_minus_one( EBUSY ); |
---|
312 | |
---|
313 | IMFS_create_orphan( the_jnode ); |
---|
314 | IMFS_check_node_remove( the_jnode ); |
---|
315 | |
---|
316 | return 0; |
---|
317 | } |
---|