1 | /* |
---|
2 | * unlink() - POSIX 1003.1b - 5.5.1 - Remove an existing link |
---|
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 <errno.h> |
---|
19 | |
---|
20 | #include <rtems/libio_.h> |
---|
21 | #include <rtems/seterr.h> |
---|
22 | |
---|
23 | int unlink( |
---|
24 | const char *path |
---|
25 | ) |
---|
26 | { |
---|
27 | int parentpathlen; |
---|
28 | const char *name; |
---|
29 | rtems_filesystem_location_info_t parentloc; |
---|
30 | rtems_filesystem_location_info_t loc; |
---|
31 | int i; |
---|
32 | int result; |
---|
33 | bool free_parentloc = false; |
---|
34 | |
---|
35 | /* |
---|
36 | * Get the node to be unlinked. Find the parent path first. |
---|
37 | */ |
---|
38 | |
---|
39 | parentpathlen = rtems_filesystem_dirname ( path ); |
---|
40 | |
---|
41 | if ( parentpathlen == 0 ) |
---|
42 | rtems_filesystem_get_start_loc( path, &i, &parentloc ); |
---|
43 | else { |
---|
44 | result = rtems_filesystem_evaluate_path( path, parentpathlen, |
---|
45 | RTEMS_LIBIO_PERMS_WRITE, |
---|
46 | &parentloc, |
---|
47 | false ); |
---|
48 | if ( result != 0 ) |
---|
49 | return -1; |
---|
50 | |
---|
51 | free_parentloc = true; |
---|
52 | } |
---|
53 | |
---|
54 | /* |
---|
55 | * Start from the parent to find the node that should be under it. |
---|
56 | */ |
---|
57 | |
---|
58 | loc = parentloc; |
---|
59 | name = path + parentpathlen; |
---|
60 | name += rtems_filesystem_prefix_separators( name, strlen( name ) ); |
---|
61 | |
---|
62 | result = rtems_filesystem_evaluate_relative_path( name , strlen( name ), |
---|
63 | 0, &loc, false ); |
---|
64 | if ( result != 0 ) { |
---|
65 | if ( free_parentloc ) |
---|
66 | rtems_filesystem_freenode( &parentloc ); |
---|
67 | return -1; |
---|
68 | } |
---|
69 | |
---|
70 | if ( (*loc.ops->node_type_h)( &loc ) == RTEMS_FILESYSTEM_DIRECTORY ) { |
---|
71 | rtems_filesystem_freenode( &loc ); |
---|
72 | if ( free_parentloc ) |
---|
73 | rtems_filesystem_freenode( &parentloc ); |
---|
74 | rtems_set_errno_and_return_minus_one( EISDIR ); |
---|
75 | } |
---|
76 | |
---|
77 | result = (*loc.ops->unlink_h)( &parentloc, &loc ); |
---|
78 | |
---|
79 | rtems_filesystem_freenode( &loc ); |
---|
80 | if ( free_parentloc ) |
---|
81 | rtems_filesystem_freenode( &parentloc ); |
---|
82 | |
---|
83 | return result; |
---|
84 | } |
---|
85 | |
---|
86 | /* |
---|
87 | * _unlink_r |
---|
88 | * |
---|
89 | * This is the Newlib dependent reentrant version of unlink(). |
---|
90 | */ |
---|
91 | |
---|
92 | #if defined(RTEMS_NEWLIB) |
---|
93 | |
---|
94 | #include <reent.h> |
---|
95 | |
---|
96 | int _unlink_r( |
---|
97 | struct _reent *ptr __attribute__((unused)), |
---|
98 | const char *path |
---|
99 | ) |
---|
100 | { |
---|
101 | return unlink( path ); |
---|
102 | } |
---|
103 | #endif |
---|