source: rtems/cpukit/libcsupport/src/open.c @ 50a50313

4.115
Last change on this file since 50a50313 was c625a641, checked in by Sebastian Huber <sebastian.huber@…>, on 12/21/14 at 19:12:28

Filesystem: Delete node type operation

Use the fstat handler instead.

  • Property mode set to 100644
File size: 3.7 KB
Line 
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
31static 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
61static 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 = iop - rtems_libio_iops;
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  int eval_flags = RTEMS_FS_FOLLOW_LINK
77    | (read_access ? RTEMS_FS_PERMS_READ : 0)
78    | (write_access ? RTEMS_FS_PERMS_WRITE : 0)
79    | (make ? RTEMS_FS_MAKE : 0)
80    | (exclusive ?  RTEMS_FS_EXCLUSIVE : 0);
81  rtems_filesystem_eval_path_context_t ctx;
82
83  rtems_filesystem_eval_path_start( &ctx, path, eval_flags );
84
85  if ( rtems_filesystem_eval_path_has_token( &ctx ) ) {
86    create_regular_file( &ctx, mode );
87  }
88
89  if ( write_access ) {
90    const rtems_filesystem_location_info_t *currentloc =
91      rtems_filesystem_eval_path_get_currentloc( &ctx );
92    mode_t type = rtems_filesystem_location_type( currentloc );
93
94    if ( S_ISDIR( type ) ) {
95      rtems_filesystem_eval_path_error( &ctx, EISDIR );
96    }
97  }
98
99  iop->flags |= rtems_libio_fcntl_flags( oflag );
100  rtems_filesystem_eval_path_extract_currentloc( &ctx, &iop->pathinfo );
101  rtems_filesystem_eval_path_cleanup( &ctx );
102
103  rv = (*iop->pathinfo.handlers->open_h)( iop, path, oflag, mode );
104
105  if ( rv == 0 ) {
106    if ( truncate ) {
107      rv = ftruncate( fd, 0 );
108      if ( rv != 0 ) {
109        (*iop->pathinfo.handlers->close_h)( iop );
110      }
111    }
112
113    if ( rv == 0 ) {
114      rv = fd;
115    } else {
116      rv = -1;
117    }
118  }
119
120  if ( rv < 0 ) {
121    rtems_libio_free( iop );
122  }
123
124  return rv;
125}
126
127/**
128*  POSIX 1003.1 5.3.1 - Open a File
129*/
130int open( const char *path, int oflag, ... )
131{
132  int rv = 0;
133  va_list ap;
134  mode_t mode = 0;
135  rtems_libio_t *iop = NULL;
136
137  va_start( ap, oflag );
138
139  mode = va_arg( ap, mode_t );
140
141  iop = rtems_libio_allocate();
142  if ( iop != NULL ) {
143    rv = do_open( iop, path, oflag, mode );
144  } else {
145    errno = ENFILE;
146    rv = -1;
147  }
148
149  va_end( ap );
150
151  return rv;
152}
153
154
155
156#if defined(RTEMS_NEWLIB) && !defined(HAVE__OPEN_R)
157
158#include <reent.h>
159
160/**
161 *  This is the Newlib dependent reentrant version of open().
162 */
163int _open_r(
164  struct _reent *ptr __attribute__((unused)),
165  const char    *buf,
166  int            oflag,
167  int            mode
168)
169{
170  return open( buf, oflag, mode );
171}
172#endif
Note: See TracBrowser for help on using the repository browser.