1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Open a File |
---|
5 | * @ingroup libcsupport |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2010. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * Modifications to support reference counting in the file system are |
---|
13 | * Copyright (c) 2012 embedded brains GmbH. |
---|
14 | * |
---|
15 | * The license and distribution terms for this file may be |
---|
16 | * found in the file LICENSE in this distribution or at |
---|
17 | * http://www.rtems.org/license/LICENSE. |
---|
18 | */ |
---|
19 | |
---|
20 | #if HAVE_CONFIG_H |
---|
21 | #include "config.h" |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <sys/stat.h> |
---|
25 | #include <fcntl.h> |
---|
26 | #include <stdarg.h> |
---|
27 | #include <unistd.h> |
---|
28 | |
---|
29 | #include <rtems/libio_.h> |
---|
30 | |
---|
31 | static void create_regular_file( |
---|
32 | rtems_filesystem_eval_path_context_t *ctx, |
---|
33 | mode_t mode |
---|
34 | ) |
---|
35 | { |
---|
36 | int rv = 0; |
---|
37 | const rtems_filesystem_location_info_t *currentloc = |
---|
38 | rtems_filesystem_eval_path_get_currentloc( ctx ); |
---|
39 | const char *token = rtems_filesystem_eval_path_get_token( ctx ); |
---|
40 | size_t tokenlen = rtems_filesystem_eval_path_get_tokenlen( ctx ); |
---|
41 | |
---|
42 | rv = rtems_filesystem_mknod( |
---|
43 | currentloc, |
---|
44 | token, |
---|
45 | tokenlen, |
---|
46 | S_IFREG | mode, |
---|
47 | 0 |
---|
48 | ); |
---|
49 | |
---|
50 | if ( rv == 0 ) { |
---|
51 | /* The mode only applies to future accesses of the newly created file */ |
---|
52 | rtems_filesystem_eval_path_set_flags( ctx, 0 ); |
---|
53 | |
---|
54 | rtems_filesystem_eval_path_set_path( ctx, token, tokenlen ); |
---|
55 | rtems_filesystem_eval_path_continue( ctx ); |
---|
56 | } else { |
---|
57 | rtems_filesystem_eval_path_error( ctx, 0 ); |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | static int do_open( |
---|
62 | rtems_libio_t *iop, |
---|
63 | const char *path, |
---|
64 | int oflag, |
---|
65 | mode_t mode |
---|
66 | ) |
---|
67 | { |
---|
68 | int rv = 0; |
---|
69 | int fd = rtems_libio_iop_to_descriptor( iop ); |
---|
70 | int rwflag = oflag + 1; |
---|
71 | bool read_access = (rwflag & _FREAD) == _FREAD; |
---|
72 | bool write_access = (rwflag & _FWRITE) == _FWRITE; |
---|
73 | bool make = (oflag & O_CREAT) == O_CREAT; |
---|
74 | bool exclusive = (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL); |
---|
75 | bool truncate = (oflag & O_TRUNC) == O_TRUNC; |
---|
76 | bool open_dir; |
---|
77 | int eval_flags = RTEMS_FS_FOLLOW_LINK |
---|
78 | | (read_access ? RTEMS_FS_PERMS_READ : 0) |
---|
79 | | (write_access ? RTEMS_FS_PERMS_WRITE : 0) |
---|
80 | | (make ? RTEMS_FS_MAKE : 0) |
---|
81 | | (exclusive ? RTEMS_FS_EXCLUSIVE : 0); |
---|
82 | rtems_filesystem_eval_path_context_t ctx; |
---|
83 | |
---|
84 | rtems_filesystem_eval_path_start( &ctx, path, eval_flags ); |
---|
85 | |
---|
86 | if ( rtems_filesystem_eval_path_has_token( &ctx ) ) { |
---|
87 | create_regular_file( &ctx, mode ); |
---|
88 | } |
---|
89 | |
---|
90 | #ifdef O_DIRECTORY |
---|
91 | open_dir = ( oflag & O_DIRECTORY ) == O_DIRECTORY; |
---|
92 | #else |
---|
93 | open_dir = false; |
---|
94 | #endif |
---|
95 | |
---|
96 | if ( write_access || open_dir ) { |
---|
97 | const rtems_filesystem_location_info_t *currentloc = |
---|
98 | rtems_filesystem_eval_path_get_currentloc( &ctx ); |
---|
99 | mode_t type = rtems_filesystem_location_type( currentloc ); |
---|
100 | |
---|
101 | if ( write_access && S_ISDIR( type ) ) { |
---|
102 | rtems_filesystem_eval_path_error( &ctx, EISDIR ); |
---|
103 | } |
---|
104 | |
---|
105 | if ( open_dir && !S_ISDIR( type ) ) { |
---|
106 | rtems_filesystem_eval_path_error( &ctx, ENOTDIR ); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | rtems_filesystem_eval_path_extract_currentloc( &ctx, &iop->pathinfo ); |
---|
111 | rtems_filesystem_eval_path_cleanup( &ctx ); |
---|
112 | |
---|
113 | _Atomic_Store_uint( |
---|
114 | &iop->flags, |
---|
115 | rtems_libio_fcntl_flags( oflag ), |
---|
116 | ATOMIC_ORDER_RELAXED |
---|
117 | ); |
---|
118 | |
---|
119 | rv = (*iop->pathinfo.handlers->open_h)( iop, path, oflag, mode ); |
---|
120 | |
---|
121 | if ( rv == 0 ) { |
---|
122 | rtems_libio_iop_flags_set( iop, LIBIO_FLAGS_OPEN ); |
---|
123 | |
---|
124 | if ( truncate ) { |
---|
125 | rv = ftruncate( fd, 0 ); |
---|
126 | if ( rv != 0 ) { |
---|
127 | (*iop->pathinfo.handlers->close_h)( iop ); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | if ( rv == 0 ) { |
---|
132 | rv = fd; |
---|
133 | } else { |
---|
134 | rv = -1; |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | if ( rv < 0 ) { |
---|
139 | rtems_libio_free( iop ); |
---|
140 | } |
---|
141 | |
---|
142 | return rv; |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * POSIX 1003.1 5.3.1 - Open a File |
---|
147 | */ |
---|
148 | int open( const char *path, int oflag, ... ) |
---|
149 | { |
---|
150 | int rv = 0; |
---|
151 | va_list ap; |
---|
152 | mode_t mode = 0; |
---|
153 | rtems_libio_t *iop = NULL; |
---|
154 | |
---|
155 | va_start( ap, oflag ); |
---|
156 | |
---|
157 | mode = va_arg( ap, mode_t ); |
---|
158 | |
---|
159 | iop = rtems_libio_allocate(); |
---|
160 | if ( iop != NULL ) { |
---|
161 | rv = do_open( iop, path, oflag, mode ); |
---|
162 | } else { |
---|
163 | errno = ENFILE; |
---|
164 | rv = -1; |
---|
165 | } |
---|
166 | |
---|
167 | va_end( ap ); |
---|
168 | |
---|
169 | return rv; |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | |
---|
174 | #if defined(RTEMS_NEWLIB) && !defined(HAVE__OPEN_R) |
---|
175 | |
---|
176 | #include <reent.h> |
---|
177 | |
---|
178 | /** |
---|
179 | * This is the Newlib dependent reentrant version of open(). |
---|
180 | */ |
---|
181 | int _open_r( |
---|
182 | struct _reent *ptr RTEMS_UNUSED, |
---|
183 | const char *buf, |
---|
184 | int oflag, |
---|
185 | int mode |
---|
186 | ) |
---|
187 | { |
---|
188 | return open( buf, oflag, mode ); |
---|
189 | } |
---|
190 | #endif |
---|