1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Operations Table for Directories |
---|
5 | * @ingroup IMFS |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-1999. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.com/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include "imfs.h" |
---|
22 | |
---|
23 | #include <dirent.h> |
---|
24 | |
---|
25 | static size_t IMFS_directory_size( const IMFS_jnode_t *node ) |
---|
26 | { |
---|
27 | size_t size = 0; |
---|
28 | const rtems_chain_control *chain = &node->info.directory.Entries; |
---|
29 | const rtems_chain_node *current = rtems_chain_immutable_first( chain ); |
---|
30 | const rtems_chain_node *tail = rtems_chain_immutable_tail( chain ); |
---|
31 | |
---|
32 | while ( current != tail ) { |
---|
33 | size += sizeof( struct dirent ); |
---|
34 | current = rtems_chain_immutable_next( current ); |
---|
35 | } |
---|
36 | |
---|
37 | return size; |
---|
38 | } |
---|
39 | |
---|
40 | static int IMFS_stat_directory( |
---|
41 | const rtems_filesystem_location_info_t *loc, |
---|
42 | struct stat *buf |
---|
43 | ) |
---|
44 | { |
---|
45 | const IMFS_jnode_t *node = loc->node_access; |
---|
46 | |
---|
47 | buf->st_size = IMFS_directory_size( node ); |
---|
48 | |
---|
49 | return IMFS_stat( loc, buf ); |
---|
50 | } |
---|
51 | |
---|
52 | static const rtems_filesystem_file_handlers_r IMFS_directory_handlers = { |
---|
53 | rtems_filesystem_default_open, |
---|
54 | rtems_filesystem_default_close, |
---|
55 | imfs_dir_read, |
---|
56 | rtems_filesystem_default_write, |
---|
57 | rtems_filesystem_default_ioctl, |
---|
58 | rtems_filesystem_default_lseek_directory, |
---|
59 | IMFS_stat_directory, |
---|
60 | rtems_filesystem_default_ftruncate_directory, |
---|
61 | rtems_filesystem_default_fsync_or_fdatasync_success, |
---|
62 | rtems_filesystem_default_fsync_or_fdatasync_success, |
---|
63 | rtems_filesystem_default_fcntl |
---|
64 | }; |
---|
65 | |
---|
66 | static IMFS_jnode_t *IMFS_node_initialize_directory( |
---|
67 | IMFS_jnode_t *node, |
---|
68 | const IMFS_types_union *info |
---|
69 | ) |
---|
70 | { |
---|
71 | rtems_chain_initialize_empty( &node->info.directory.Entries ); |
---|
72 | |
---|
73 | return node; |
---|
74 | } |
---|
75 | |
---|
76 | static bool IMFS_is_mount_point( const IMFS_jnode_t *node ) |
---|
77 | { |
---|
78 | return node->info.directory.mt_fs != NULL; |
---|
79 | } |
---|
80 | |
---|
81 | static IMFS_jnode_t *IMFS_node_remove_directory( |
---|
82 | IMFS_jnode_t *node |
---|
83 | ) |
---|
84 | { |
---|
85 | if ( !rtems_chain_is_empty( &node->info.directory.Entries ) ) { |
---|
86 | errno = ENOTEMPTY; |
---|
87 | node = NULL; |
---|
88 | } else if ( IMFS_is_mount_point( node ) ) { |
---|
89 | errno = EBUSY; |
---|
90 | node = NULL; |
---|
91 | } |
---|
92 | |
---|
93 | return node; |
---|
94 | } |
---|
95 | |
---|
96 | const IMFS_node_control IMFS_node_control_directory = { |
---|
97 | .imfs_type = IMFS_DIRECTORY, |
---|
98 | .handlers = &IMFS_directory_handlers, |
---|
99 | .node_initialize = IMFS_node_initialize_directory, |
---|
100 | .node_remove = IMFS_node_remove_directory, |
---|
101 | .node_destroy = IMFS_node_destroy_default |
---|
102 | }; |
---|