source: rtems/cpukit/libcsupport/src/open.c @ c625a641

4.115
Last change on this file since c625a641 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
RevLine 
[c9bb60a]1/**
2 *  @file
[07a3253d]3 *
[c9bb60a]4 *  @brief Open a File
5 *  @ingroup libcsupport
6 */
7
8/*
[4af849f]9 *  COPYRIGHT (c) 1989-2010.
[07a3253d]10 *  On-Line Applications Research Corporation (OAR).
11 *
[3b7c123]12 *  Modifications to support reference counting in the file system are
13 *  Copyright (c) 2012 embedded brains GmbH.
14 *
[07a3253d]15 *  The license and distribution terms for this file may be
16 *  found in the file LICENSE in this distribution or at
[c499856]17 *  http://www.rtems.org/license/LICENSE.
[07a3253d]18 */
19
[9c49db4]20#if HAVE_CONFIG_H
[3b7c123]21  #include "config.h"
[9c49db4]22#endif
23
[3b7c123]24#include <sys/stat.h>
[a02224e]25#include <fcntl.h>
[3b7c123]26#include <stdarg.h>
[07a3253d]27#include <unistd.h>
28
[3b7c123]29#include <rtems/libio_.h>
[07a3253d]30
[3b7c123]31static void create_regular_file(
32  rtems_filesystem_eval_path_context_t *ctx,
33  mode_t mode
[07a3253d]34)
35{
[3b7c123]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}
[07a3253d]60
[3b7c123]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;
[2563410]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);
[3b7c123]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 );
[07a3253d]87  }
88
[3b7c123]89  if ( write_access ) {
90    const rtems_filesystem_location_info_t *currentloc =
91      rtems_filesystem_eval_path_get_currentloc( &ctx );
[c625a641]92    mode_t type = rtems_filesystem_location_type( currentloc );
[07a3253d]93
[c625a641]94    if ( S_ISDIR( type ) ) {
[3b7c123]95      rtems_filesystem_eval_path_error( &ctx, EISDIR );
[07a3253d]96    }
[3b7c123]97  }
[07a3253d]98
[3b7c123]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 );
[07a3253d]102
[3b7c123]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      }
[07a3253d]111    }
112
[3b7c123]113    if ( rv == 0 ) {
114      rv = fd;
115    } else {
116      rv = -1;
[07a3253d]117    }
[3b7c123]118  }
[07a3253d]119
[3b7c123]120  if ( rv < 0 ) {
121    rtems_libio_free( iop );
[07a3253d]122  }
123
[3b7c123]124  return rv;
125}
126
[c9bb60a]127/**
128*  POSIX 1003.1 5.3.1 - Open a File
129*/
[3b7c123]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;
[efb5450]136
[3b7c123]137  va_start( ap, oflag );
[07a3253d]138
[3b7c123]139  mode = va_arg( ap, mode_t );
[07a3253d]140
[3b7c123]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;
[07a3253d]147  }
[50f32b11]148
[3b7c123]149  va_end( ap );
[d71fcab]150
[3b7c123]151  return rv;
[07a3253d]152}
153
[c9bb60a]154
[07a3253d]155
[a13c3df]156#if defined(RTEMS_NEWLIB) && !defined(HAVE__OPEN_R)
[07a3253d]157
158#include <reent.h>
159
[c9bb60a]160/**
161 *  This is the Newlib dependent reentrant version of open().
162 */
[07a3253d]163int _open_r(
[9e14ca2]164  struct _reent *ptr __attribute__((unused)),
[07a3253d]165  const char    *buf,
[3b7c123]166  int            oflag,
[07a3253d]167  int            mode
168)
169{
[3b7c123]170  return open( buf, oflag, mode );
[07a3253d]171}
172#endif
Note: See TracBrowser for help on using the repository browser.