Skip to content

Commit

Permalink
fs: Reimplement file_open to not depend on nx_open
Browse files Browse the repository at this point in the history
on the other hand, open/nx_open call file_open instead

Signed-off-by: Xiang Xiao <[email protected]>
Change-Id: I66990a77cdeb6ff18f7bf48a65bbc7b701dad552
  • Loading branch information
xiaoxiang781216 authored and Ouss4 committed Jan 11, 2021
1 parent 7a953bb commit 0032ddb
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 159 deletions.
2 changes: 1 addition & 1 deletion fs/driver/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ int find_blockdriver(FAR const char *pathname, int mountflags,
****************************************************************************/

#if !defined(CONFIG_DISABLE_MOUNTPOINT)
int block_proxy(FAR const char *blkdev, int oflags);
int block_proxy(FAR struct file *filep, FAR const char *blkdev, int oflags);
#endif

/****************************************************************************
Expand Down
14 changes: 6 additions & 8 deletions fs/driver/fs_blockproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ static FAR char *unique_chardev(void)

/* Make sure that file name is not in use */

ret = stat(devbuf, &statbuf);
ret = nx_stat(devbuf, &statbuf, 1);
if (ret < 0)
{
DEBUGASSERT(errno == ENOENT);
DEBUGASSERT(ret == -ENOENT);
return strdup(devbuf);
}

Expand Down Expand Up @@ -147,12 +147,11 @@ static FAR char *unique_chardev(void)
*
****************************************************************************/

int block_proxy(FAR const char *blkdev, int oflags)
int block_proxy(FAR struct file *filep, FAR const char *blkdev, int oflags)
{
FAR char *chardev;
bool readonly;
int ret;
int fd;

DEBUGASSERT(blkdev);

Expand Down Expand Up @@ -183,10 +182,9 @@ int block_proxy(FAR const char *blkdev, int oflags)
/* Open the newly created character driver */

oflags &= ~(O_CREAT | O_EXCL | O_APPEND | O_TRUNC);
fd = nx_open(chardev, oflags);
if (fd < 0)
ret = file_open(filep, chardev, oflags);
if (ret < 0)
{
ret = fd;
ferr("ERROR: Failed to open %s: %d\n", chardev, ret);
goto errout_with_bchdev;
}
Expand All @@ -209,7 +207,7 @@ int block_proxy(FAR const char *blkdev, int oflags)
*/

kmm_free(chardev);
return fd;
return OK;

errout_with_bchdev:
unlink(chardev);
Expand Down
2 changes: 1 addition & 1 deletion fs/inode/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
CSRCS += fs_files.c fs_foreachinode.c fs_inode.c fs_inodeaddref.c
CSRCS += fs_inodebasename.c fs_inodefind.c fs_inodefree.c fs_inoderelease.c
CSRCS += fs_inoderemove.c fs_inodereserve.c fs_inodesearch.c
CSRCS += fs_fileopen.c fs_filedetach.c
CSRCS += fs_filedetach.c

# Include inode/utils build support

Expand Down
110 changes: 0 additions & 110 deletions fs/inode/fs_fileopen.c

This file was deleted.

132 changes: 93 additions & 39 deletions fs/vfs/fs_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,31 +80,30 @@ int inode_checkflags(FAR struct inode *inode, int oflags)
}

/****************************************************************************
* Name: nx_vopen
* Name: file_vopen
*
* Description:
* nx_vopen() is identical to 'nx_open' except that it accepts a va_list
* file_vopen() is identical to 'file_open' except that it accepts va_list
* as an argument versus taking a variable length list of arguments.
*
* nx_vopen() is an internal NuttX interface and should not be called from
* applications.
* file_vopen() is an internal NuttX interface and should not be called
* from applications.
*
* Returned Value:
* The new file descriptor is returned on success; a negated errno value is
* returned on any failure.
* Zero (OK) is returned on success. On failure, a negated errno value is
* returned.
*
****************************************************************************/

int nx_vopen(FAR const char *path, int oflags, va_list ap)
int file_vopen(FAR struct file *filep,
FAR const char *path, int oflags, va_list ap)
{
struct inode_search_s desc;
FAR struct file *filep;
FAR struct inode *inode;
#if defined(CONFIG_FILE_MODE) || !defined(CONFIG_DISABLE_MOUNTPOINT)
mode_t mode = 0666;
#endif
int ret;
int fd;

if (path == NULL)
{
Expand Down Expand Up @@ -160,20 +159,11 @@ int nx_vopen(FAR const char *path, int oflags, va_list ap)
/* Release the inode reference */

inode_release(inode);
RELEASE_SEARCH(&desc);

/* Get the file descriptor of the opened character driver proxy */

fd = block_proxy(path, oflags);
if (fd < 0)
{
ret = fd;
goto errout_with_search;
}

/* Return the file descriptor */

RELEASE_SEARCH(&desc);
return fd;
return block_proxy(filep, path, oflags);
}
else
#endif
Expand Down Expand Up @@ -204,20 +194,10 @@ int nx_vopen(FAR const char *path, int oflags, va_list ap)

/* Associate the inode with a file structure */

fd = files_allocate(inode, oflags, 0, NULL, 0);
if (fd < 0)
{
ret = fd;
goto errout_with_inode;
}

/* Get the file structure corresponding to the file descriptor. */

ret = fs_getfilep(fd, &filep);
if (ret < 0)
{
goto errout_with_inode;
}
filep->f_oflags = oflags;
filep->f_pos = 0;
filep->f_inode = inode;
filep->f_priv = NULL;

/* Perform the driver open operation. NOTE that the open method may be
* called many times. The driver/mountpoint logic should handled this
Expand All @@ -241,23 +221,97 @@ int nx_vopen(FAR const char *path, int oflags, va_list ap)

if (ret < 0)
{
goto errout_with_fd;
goto errout_with_inode;
}

RELEASE_SEARCH(&desc);
return fd;

errout_with_fd:
files_release(fd);
return OK;

errout_with_inode:
filep->f_inode = NULL;
inode_release(inode);

errout_with_search:
RELEASE_SEARCH(&desc);
return ret;
}

/****************************************************************************
* Name: file_open
*
* Description:
* file_open() is similar to the standard 'open' interface except that it
* returns an instance of 'struct file' rather than a file descriptor. It
* also is not a cancellation point and does not modify the errno variable.
*
* Input Parameters:
* filep - The caller provided location in which to return the 'struct
* file' instance.
* path - The full path to the file to be open.
* oflags - open flags
* ... - Variable number of arguments, may include 'mode_t mode'
*
* Returned Value:
* Zero (OK) is returned on success. On failure, a negated errno value is
* returned.
*
****************************************************************************/

int file_open(FAR struct file *filep, FAR const char *path, int oflags, ...)
{
va_list ap;
int ret;

va_start(ap, oflags);
ret = file_vopen(filep, path, oflags, ap);
va_end(ap);

return ret;
}

/****************************************************************************
* Name: nx_vopen
*
* Description:
* nx_vopen() is identical to 'nx_open' except that it accepts a va_list
* as an argument versus taking a variable length list of arguments.
*
* nx_vopen() is an internal NuttX interface and should not be called from
* applications.
*
* Returned Value:
* The new file descriptor is returned on success; a negated errno value is
* returned on any failure.
*
****************************************************************************/

int nx_vopen(FAR const char *path, int oflags, va_list ap)
{
struct file filep;
int ret;
int fd;

/* Let file_vopen() do all of the work */

ret = file_vopen(&filep, path, oflags, ap);
if (ret < 0)
{
return ret;
}

/* Allocate a new file descriptor for the inode */

fd = files_allocate(filep.f_inode, filep.f_oflags,
filep.f_pos, filep.f_priv, 0);
if (fd < 0)
{
file_close(&filep);
return fd;
}

return fd;
}

/****************************************************************************
* Name: nx_open
*
Expand Down
2 changes: 2 additions & 0 deletions include/nuttx/fs/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,8 @@ int nx_dup2(int fd1, int fd2);
*
****************************************************************************/

int file_vopen(FAR struct file *filep,
FAR const char *path, int oflags, va_list ap);
int file_open(FAR struct file *filep, FAR const char *path, int oflags, ...);

/****************************************************************************
Expand Down

0 comments on commit 0032ddb

Please sign in to comment.