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