1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief IMFS Rename |
---|
5 | * @ingroup IMFS |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2010. |
---|
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 <string.h> |
---|
24 | #include <stdlib.h> |
---|
25 | |
---|
26 | int IMFS_rename( |
---|
27 | const rtems_filesystem_location_info_t *oldparentloc, |
---|
28 | const rtems_filesystem_location_info_t *oldloc, |
---|
29 | const rtems_filesystem_location_info_t *newparentloc, |
---|
30 | const char *name, |
---|
31 | size_t namelen |
---|
32 | ) |
---|
33 | { |
---|
34 | IMFS_jnode_t *node = oldloc->node_access; |
---|
35 | IMFS_jnode_t *new_parent = newparentloc->node_access; |
---|
36 | char *allocated_name; |
---|
37 | |
---|
38 | /* |
---|
39 | * FIXME: Due to insufficient checks we can create inaccessible nodes with |
---|
40 | * this operation. |
---|
41 | */ |
---|
42 | |
---|
43 | if ( node->Parent == NULL ) { |
---|
44 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
45 | } |
---|
46 | |
---|
47 | if ( namelen >= IMFS_NAME_MAX ) { |
---|
48 | rtems_set_errno_and_return_minus_one( ENAMETOOLONG ); |
---|
49 | } |
---|
50 | |
---|
51 | allocated_name = malloc( namelen ); |
---|
52 | if ( allocated_name == NULL ) { |
---|
53 | rtems_set_errno_and_return_minus_one( ENOMEM ); |
---|
54 | } |
---|
55 | |
---|
56 | memcpy( allocated_name, name, namelen ); |
---|
57 | |
---|
58 | if ( ( node->flags & IMFS_NODE_FLAG_NAME_ALLOCATED ) != 0 ) { |
---|
59 | free( node->name ); |
---|
60 | } |
---|
61 | |
---|
62 | node->name = allocated_name; |
---|
63 | node->namelen = namelen; |
---|
64 | node->flags |= IMFS_NODE_FLAG_NAME_ALLOCATED; |
---|
65 | |
---|
66 | IMFS_remove_from_directory( node ); |
---|
67 | IMFS_add_to_directory( new_parent, node ); |
---|
68 | IMFS_update_ctime( node ); |
---|
69 | |
---|
70 | return 0; |
---|
71 | } |
---|