mirror of
https://github.com/apache/nuttx.git
synced 2026-06-01 07:45:16 +08:00
fs/vfs/fd_open.c: fs_fdopen() must not set errno
Functions within the OS must never set the errno value. fs_fdopen() was setting the errno value. Now, after some parameter changes, it reports errors via a negated errno integer return value as do most all other internal OS functions.
This commit is contained in:
+30
-38
@@ -1,35 +1,20 @@
|
||||
/****************************************************************************
|
||||
* fs/vfs/fs_fdopen.c
|
||||
*
|
||||
* Copyright (C) 2007-2014, 2017 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership. The
|
||||
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance with the
|
||||
* License. You may obtain a copy of the License at
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
@@ -124,11 +109,11 @@ static inline int fs_checkfd(FAR struct tcb_s *tcb, int fd, int oflags)
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR struct file_struct *fs_fdopen(int fd, int oflags, FAR struct tcb_s *tcb)
|
||||
int fs_fdopen(int fd, int oflags, FAR struct tcb_s *tcb,
|
||||
FAR struct file_struct **filep)
|
||||
{
|
||||
FAR struct streamlist *slist;
|
||||
FAR FILE *stream;
|
||||
int errcode = OK;
|
||||
int ret;
|
||||
int i;
|
||||
|
||||
@@ -136,7 +121,7 @@ FAR struct file_struct *fs_fdopen(int fd, int oflags, FAR struct tcb_s *tcb)
|
||||
|
||||
if (fd < 0)
|
||||
{
|
||||
errcode = EBADF;
|
||||
ret = -EBADF;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
@@ -170,7 +155,7 @@ FAR struct file_struct *fs_fdopen(int fd, int oflags, FAR struct tcb_s *tcb)
|
||||
#else
|
||||
/* No networking... it is just a bad descriptor */
|
||||
|
||||
errcode = EBADF;
|
||||
ret = -EBADF;
|
||||
goto errout;
|
||||
#endif
|
||||
}
|
||||
@@ -190,7 +175,6 @@ FAR struct file_struct *fs_fdopen(int fd, int oflags, FAR struct tcb_s *tcb)
|
||||
{
|
||||
/* No... return the reported error */
|
||||
|
||||
errcode = -ret;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
@@ -207,7 +191,6 @@ FAR struct file_struct *fs_fdopen(int fd, int oflags, FAR struct tcb_s *tcb)
|
||||
ret = nxsem_wait(&slist->sl_sem);
|
||||
if (ret < 0)
|
||||
{
|
||||
errcode = -ret;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
@@ -235,7 +218,7 @@ FAR struct file_struct *fs_fdopen(int fd, int oflags, FAR struct tcb_s *tcb)
|
||||
|
||||
if (!stream->fs_bufstart)
|
||||
{
|
||||
errcode = ENOMEM;
|
||||
ret = -ENOMEM;
|
||||
goto errout_with_sem;
|
||||
}
|
||||
|
||||
@@ -262,13 +245,18 @@ FAR struct file_struct *fs_fdopen(int fd, int oflags, FAR struct tcb_s *tcb)
|
||||
stream->fs_oflags = (uint16_t)oflags;
|
||||
|
||||
nxsem_post(&slist->sl_sem);
|
||||
return stream;
|
||||
if (filep != NULL)
|
||||
{
|
||||
*filep = stream;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
|
||||
/* No free stream available.. report ENFILE */
|
||||
|
||||
errcode = ENFILE;
|
||||
ret = -ENFILE;
|
||||
|
||||
#if !defined(CONFIG_STDIO_DISABLE_BUFFERING) && CONFIG_STDIO_BUFFER_SIZE > 0
|
||||
errout_with_sem:
|
||||
@@ -276,6 +264,10 @@ errout_with_sem:
|
||||
nxsem_post(&slist->sl_sem);
|
||||
|
||||
errout:
|
||||
set_errno(errcode);
|
||||
return NULL;
|
||||
if (filep != NULL)
|
||||
{
|
||||
*filep = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user