1 | /* |
---|
2 | * rmdir() - POSIX 1003.1b - 5.2.2 - Remove a Directory |
---|
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 <sys/types.h> |
---|
19 | #include <fcntl.h> |
---|
20 | #include <unistd.h> |
---|
21 | #include <errno.h> |
---|
22 | #include <stdlib.h> |
---|
23 | |
---|
24 | #include <rtems/libio_.h> |
---|
25 | #include <rtems/seterr.h> |
---|
26 | |
---|
27 | int rmdir( |
---|
28 | const char *pathname |
---|
29 | ) |
---|
30 | { |
---|
31 | int parentpathlen; |
---|
32 | const char *name; |
---|
33 | rtems_filesystem_location_info_t parentloc; |
---|
34 | rtems_filesystem_location_info_t loc; |
---|
35 | int i; |
---|
36 | int result; |
---|
37 | |
---|
38 | /* |
---|
39 | * Get the parent node of the node we wish to remove. Find the parent path. |
---|
40 | */ |
---|
41 | |
---|
42 | parentpathlen = rtems_filesystem_dirname ( pathname ); |
---|
43 | |
---|
44 | if ( parentpathlen == 0 ) |
---|
45 | rtems_filesystem_get_start_loc( pathname, &i, &parentloc ); |
---|
46 | else { |
---|
47 | result = rtems_filesystem_evaluate_path(pathname, parentpathlen, |
---|
48 | RTEMS_LIBIO_PERMS_WRITE, |
---|
49 | &parentloc, |
---|
50 | false ); |
---|
51 | if ( result != 0 ) |
---|
52 | return -1; |
---|
53 | } |
---|
54 | |
---|
55 | /* |
---|
56 | * Start from the parent to find the node that should be under it. |
---|
57 | */ |
---|
58 | |
---|
59 | loc = parentloc; |
---|
60 | name = pathname + parentpathlen; |
---|
61 | name += rtems_filesystem_prefix_separators( name, strlen( name ) ); |
---|
62 | |
---|
63 | result = rtems_filesystem_evaluate_relative_path( name , strlen( name ), |
---|
64 | 0, &loc, false ); |
---|
65 | if ( result != 0 ) { |
---|
66 | rtems_filesystem_freenode( &parentloc ); |
---|
67 | return -1; |
---|
68 | } |
---|
69 | |
---|
70 | /* |
---|
71 | * Verify you can remove this node as a directory. |
---|
72 | */ |
---|
73 | |
---|
74 | if ( !loc.ops->node_type_h ){ |
---|
75 | rtems_filesystem_freenode( &loc ); |
---|
76 | rtems_filesystem_freenode( &parentloc ); |
---|
77 | rtems_set_errno_and_return_minus_one( ENOTSUP ); |
---|
78 | } |
---|
79 | |
---|
80 | if ( (*loc.ops->node_type_h)( &loc ) != RTEMS_FILESYSTEM_DIRECTORY ){ |
---|
81 | rtems_filesystem_freenode( &loc ); |
---|
82 | rtems_filesystem_freenode( &parentloc ); |
---|
83 | rtems_set_errno_and_return_minus_one( ENOTDIR ); |
---|
84 | } |
---|
85 | |
---|
86 | /* |
---|
87 | * Use the filesystems rmnod to remove the node. |
---|
88 | */ |
---|
89 | |
---|
90 | if ( !loc.handlers->rmnod_h ){ |
---|
91 | rtems_filesystem_freenode( &loc ); |
---|
92 | rtems_filesystem_freenode( &parentloc ); |
---|
93 | rtems_set_errno_and_return_minus_one( ENOTSUP ); |
---|
94 | } |
---|
95 | |
---|
96 | result = (*loc.handlers->rmnod_h)( &parentloc, &loc ); |
---|
97 | |
---|
98 | rtems_filesystem_freenode( &loc ); |
---|
99 | rtems_filesystem_freenode( &parentloc ); |
---|
100 | |
---|
101 | return result; |
---|
102 | } |
---|