source: rtems/cpukit/libcsupport/src/open.c @ 183af89

4.11
Last change on this file since 183af89 was 2563410, checked in by Sebastian Huber <sebastian.huber@…>, on Mar 13, 2012 at 8:22:11 AM

Filesystem: Rename defines

o Removed RTEMS_LIBIO_PERMS_SEARCH.
o Renamed RTEMS_LIBIO_PERMS_READ in RTEMS_FS_PERMS_READ.
o Renamed RTEMS_LIBIO_PERMS_WRITE in RTEMS_FS_PERMS_WRITE.
o Renamed RTEMS_LIBIO_PERMS_EXEC in RTEMS_FS_PERMS_EXEC.
o Renamed RTEMS_LIBIO_FOLLOW_HARD_LINK in RTEMS_FS_FOLLOW_HARD_LINK.
o Renamed RTEMS_LIBIO_FOLLOW_SYM_LINK in RTEMS_FS_FOLLOW_SYM_LINK.
o Renamed RTEMS_LIBIO_MAKE in RTEMS_FS_MAKE.
o Renamed RTEMS_LIBIO_EXCLUSIVE in RTEMS_FS_EXCLUSIVE.
o Renamed RTEMS_LIBIO_ACCEPT_RESIDUAL_DELIMITERS in

RTEMS_FS_ACCEPT_RESIDUAL_DELIMITERS.

o Renamed RTEMS_LIBIO_REJECT_TERMINAL_DOT in
RTEMS_FS_REJECT_TERMINAL_DOT.

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