1 | /* |
---|
2 | * _rename_r() - POSIX 1003.1b - 5.3.4 - Rename a file |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2007. |
---|
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 | #if defined(RTEMS_NEWLIB) && !defined(HAVE__RENAME_R) |
---|
19 | #include <sys/types.h> |
---|
20 | #include <sys/stat.h> |
---|
21 | #include <unistd.h> |
---|
22 | |
---|
23 | #include <rtems/libio_.h> |
---|
24 | #include <rtems/seterr.h> |
---|
25 | |
---|
26 | int _rename_r( |
---|
27 | struct _reent *ptr __attribute__((unused)), |
---|
28 | const char *old, |
---|
29 | const char *new |
---|
30 | ) |
---|
31 | { |
---|
32 | int old_parent_pathlen; |
---|
33 | rtems_filesystem_location_info_t old_loc; |
---|
34 | rtems_filesystem_location_info_t old_parent_loc; |
---|
35 | rtems_filesystem_location_info_t new_parent_loc; |
---|
36 | int i; |
---|
37 | int result; |
---|
38 | const char *name; |
---|
39 | bool free_old_parentloc = false; |
---|
40 | |
---|
41 | /* |
---|
42 | * Get the parent node of the old path to be renamed. Find the parent path. |
---|
43 | */ |
---|
44 | |
---|
45 | old_parent_pathlen = rtems_filesystem_dirname ( old ); |
---|
46 | |
---|
47 | if ( old_parent_pathlen == 0 ) |
---|
48 | rtems_filesystem_get_start_loc( old, &i, &old_parent_loc ); |
---|
49 | else { |
---|
50 | result = rtems_filesystem_evaluate_path( old, old_parent_pathlen, |
---|
51 | RTEMS_LIBIO_PERMS_WRITE, |
---|
52 | &old_parent_loc, |
---|
53 | false ); |
---|
54 | if ( result != 0 ) |
---|
55 | return -1; |
---|
56 | |
---|
57 | free_old_parentloc = true; |
---|
58 | } |
---|
59 | |
---|
60 | /* |
---|
61 | * Start from the parent to find the node that should be under it. |
---|
62 | */ |
---|
63 | |
---|
64 | old_loc = old_parent_loc; |
---|
65 | name = old + old_parent_pathlen; |
---|
66 | name += rtems_filesystem_prefix_separators( name, strlen( name ) ); |
---|
67 | |
---|
68 | result = rtems_filesystem_evaluate_relative_path( name , strlen( name ), |
---|
69 | 0, &old_loc, false ); |
---|
70 | if ( result != 0 ) { |
---|
71 | if ( free_old_parentloc ) |
---|
72 | rtems_filesystem_freenode( &old_parent_loc ); |
---|
73 | return -1; |
---|
74 | } |
---|
75 | |
---|
76 | /* |
---|
77 | * Get the parent of the new node we are renaming to. |
---|
78 | */ |
---|
79 | |
---|
80 | rtems_filesystem_get_start_loc( new, &i, &new_parent_loc ); |
---|
81 | |
---|
82 | result = (*new_parent_loc.ops->evalformake_h)( &new[i], &new_parent_loc, &name ); |
---|
83 | if ( result != 0 ) { |
---|
84 | rtems_filesystem_freenode( &new_parent_loc ); |
---|
85 | if ( free_old_parentloc ) |
---|
86 | rtems_filesystem_freenode( &old_parent_loc ); |
---|
87 | rtems_filesystem_freenode( &old_loc ); |
---|
88 | return -1; |
---|
89 | } |
---|
90 | |
---|
91 | /* |
---|
92 | * Check to see if the caller is trying to rename across file system |
---|
93 | * boundaries. |
---|
94 | */ |
---|
95 | |
---|
96 | if ( old_parent_loc.mt_entry != new_parent_loc.mt_entry ) { |
---|
97 | rtems_filesystem_freenode( &new_parent_loc ); |
---|
98 | if ( free_old_parentloc ) |
---|
99 | rtems_filesystem_freenode( &old_parent_loc ); |
---|
100 | rtems_filesystem_freenode( &old_loc ); |
---|
101 | rtems_set_errno_and_return_minus_one( EXDEV ); |
---|
102 | } |
---|
103 | |
---|
104 | result = (*new_parent_loc.ops->rename_h)( &old_parent_loc, &old_loc, &new_parent_loc, name ); |
---|
105 | |
---|
106 | rtems_filesystem_freenode( &new_parent_loc ); |
---|
107 | if ( free_old_parentloc ) |
---|
108 | rtems_filesystem_freenode( &old_parent_loc ); |
---|
109 | rtems_filesystem_freenode( &old_loc ); |
---|
110 | |
---|
111 | return result; |
---|
112 | } |
---|
113 | #endif |
---|