1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief IMFS Create a New Symbolic Link Node |
---|
5 | * @ingroup IMFS |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2009. |
---|
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.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include "imfs.h" |
---|
22 | |
---|
23 | #include <stdlib.h> |
---|
24 | #include <string.h> |
---|
25 | |
---|
26 | static const IMFS_node_control IMFS_node_control_sym_link; |
---|
27 | |
---|
28 | int IMFS_symlink( |
---|
29 | const rtems_filesystem_location_info_t *parentloc, |
---|
30 | const char *name, |
---|
31 | size_t namelen, |
---|
32 | const char *target |
---|
33 | ) |
---|
34 | { |
---|
35 | char *dup_target; |
---|
36 | IMFS_jnode_t *new_node; |
---|
37 | |
---|
38 | /* |
---|
39 | * Duplicate link name |
---|
40 | */ |
---|
41 | dup_target = strdup(target); |
---|
42 | if (dup_target == NULL) { |
---|
43 | rtems_set_errno_and_return_minus_one(ENOMEM); |
---|
44 | } |
---|
45 | |
---|
46 | /* |
---|
47 | * Create a new link node. |
---|
48 | */ |
---|
49 | new_node = IMFS_create_node( |
---|
50 | parentloc, |
---|
51 | &IMFS_node_control_sym_link, |
---|
52 | sizeof( IMFS_sym_link_t ), |
---|
53 | name, |
---|
54 | namelen, |
---|
55 | ( S_IFLNK | ( S_IRWXU | S_IRWXG | S_IRWXO )), |
---|
56 | dup_target |
---|
57 | ); |
---|
58 | |
---|
59 | if (new_node == NULL) { |
---|
60 | free(dup_target); |
---|
61 | rtems_set_errno_and_return_minus_one(ENOMEM); |
---|
62 | } |
---|
63 | |
---|
64 | return 0; |
---|
65 | } |
---|
66 | |
---|
67 | ssize_t IMFS_readlink( |
---|
68 | const rtems_filesystem_location_info_t *loc, |
---|
69 | char *buf, |
---|
70 | size_t bufsize |
---|
71 | ) |
---|
72 | { |
---|
73 | IMFS_sym_link_t *sym_link; |
---|
74 | ssize_t i; |
---|
75 | |
---|
76 | sym_link = loc->node_access; |
---|
77 | |
---|
78 | for( i=0; ((i<bufsize) && (sym_link->name[i] != '\0')); i++ ) |
---|
79 | buf[i] = sym_link->name[i]; |
---|
80 | |
---|
81 | return i; |
---|
82 | } |
---|
83 | |
---|
84 | static int IMFS_stat_sym_link( |
---|
85 | const rtems_filesystem_location_info_t *loc, |
---|
86 | struct stat *buf |
---|
87 | ) |
---|
88 | { |
---|
89 | const IMFS_sym_link_t *sym_link = loc->node_access; |
---|
90 | |
---|
91 | buf->st_size = strlen( sym_link->name ); |
---|
92 | |
---|
93 | return IMFS_stat( loc, buf ); |
---|
94 | } |
---|
95 | |
---|
96 | static const rtems_filesystem_file_handlers_r IMFS_link_handlers = { |
---|
97 | .open_h = rtems_filesystem_default_open, |
---|
98 | .close_h = rtems_filesystem_default_close, |
---|
99 | .read_h = rtems_filesystem_default_read, |
---|
100 | .write_h = rtems_filesystem_default_write, |
---|
101 | .ioctl_h = rtems_filesystem_default_ioctl, |
---|
102 | .lseek_h = rtems_filesystem_default_lseek, |
---|
103 | .fstat_h = IMFS_stat_sym_link, |
---|
104 | .ftruncate_h = rtems_filesystem_default_ftruncate, |
---|
105 | .fsync_h = rtems_filesystem_default_fsync_or_fdatasync, |
---|
106 | .fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync, |
---|
107 | .fcntl_h = rtems_filesystem_default_fcntl, |
---|
108 | .kqfilter_h = rtems_filesystem_default_kqfilter, |
---|
109 | .poll_h = rtems_filesystem_default_poll, |
---|
110 | .readv_h = rtems_filesystem_default_readv, |
---|
111 | .writev_h = rtems_filesystem_default_writev |
---|
112 | }; |
---|
113 | |
---|
114 | static IMFS_jnode_t *IMFS_node_initialize_sym_link( |
---|
115 | IMFS_jnode_t *node, |
---|
116 | void *arg |
---|
117 | ) |
---|
118 | { |
---|
119 | IMFS_sym_link_t *sym_link = (IMFS_sym_link_t *) node; |
---|
120 | |
---|
121 | sym_link->name = arg; |
---|
122 | |
---|
123 | return node; |
---|
124 | } |
---|
125 | |
---|
126 | static void IMFS_node_destroy_sym_link( IMFS_jnode_t *node ) |
---|
127 | { |
---|
128 | IMFS_sym_link_t *sym_link = (IMFS_sym_link_t *) node; |
---|
129 | |
---|
130 | free( sym_link->name ); |
---|
131 | |
---|
132 | IMFS_node_destroy_default( node ); |
---|
133 | } |
---|
134 | |
---|
135 | static const IMFS_node_control IMFS_node_control_sym_link = { |
---|
136 | .handlers = &IMFS_link_handlers, |
---|
137 | .node_initialize = IMFS_node_initialize_sym_link, |
---|
138 | .node_remove = IMFS_node_remove_default, |
---|
139 | .node_destroy = IMFS_node_destroy_sym_link |
---|
140 | }; |
---|