2017-02-03 20:57:12 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/vfs/fs_readlink.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 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 will 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2017-02-04 18:46:54 +01:00
|
|
|
#include <stdbool.h>
|
2017-02-03 20:57:12 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
|
|
|
#include "inode/inode.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mkdir
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The readlink() function will place the contents of the symbolic link
|
|
|
|
* referred to by 'path' in the buffer 'buf' which has size 'bufsize'. If
|
|
|
|
* the number of bytes in the symbolic link is less than bufsize, the
|
|
|
|
* contents of the remainder of 'buf' are unspecified. If the buf argument
|
|
|
|
* is not large enough to contain the link content, the first bufsize bytes
|
|
|
|
* will be placed in buf. If the value of bufsize is greater than
|
|
|
|
* {SSIZE_MAX}, the result is implementation-defined. *
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* path - The full path to the symbolic link
|
|
|
|
* buf - The user-provided buffer in which to return the path to the
|
|
|
|
* link target.
|
|
|
|
* bufixe - The size of 'buf'
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Upon successful completion, readlink() will return the count of bytes
|
|
|
|
* placed in the buffer. Otherwise, it will return a value of -1, leave the
|
|
|
|
* buffer unchanged, and set errno to indicate the error.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
ssize_t readlink(FAR const char *path, FAR char *buf, size_t bufsize)
|
|
|
|
{
|
2017-02-05 16:51:42 +01:00
|
|
|
struct inode_search_s desc;
|
2017-02-03 20:57:12 +01:00
|
|
|
FAR struct inode *node;
|
|
|
|
int errcode;
|
2017-02-05 16:51:42 +01:00
|
|
|
int ret;
|
2017-02-03 20:57:12 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(path != NULL && buf != NULL && bufsize > 0);
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Find the inode that includes this path (without dereferencing the final)
|
2017-02-03 20:57:12 +01:00
|
|
|
* symbolic link node.
|
|
|
|
*/
|
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
SETUP_SEARCH(&desc, path, true);
|
2017-02-05 16:51:42 +01:00
|
|
|
|
|
|
|
ret = inode_find(&desc);
|
|
|
|
if (ret < 0)
|
2017-02-03 20:57:12 +01:00
|
|
|
{
|
2017-02-05 16:51:42 +01:00
|
|
|
errcode = -ret;
|
2017-02-05 21:25:45 +01:00
|
|
|
goto errout_with_search;
|
2017-02-03 20:57:12 +01:00
|
|
|
}
|
|
|
|
|
2017-02-05 16:51:42 +01:00
|
|
|
/* Get the search results */
|
|
|
|
|
|
|
|
node = desc.node;
|
|
|
|
DEBUGASSERT(node != NULL);
|
|
|
|
|
2017-02-03 20:57:12 +01:00
|
|
|
/* An inode was found that includes this path and possibly refers to a
|
|
|
|
* symbolic link.
|
|
|
|
*
|
|
|
|
* Check if the inode is a valid symbolic link.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!INODE_IS_SOFTLINK(node))
|
|
|
|
{
|
|
|
|
errcode = EINVAL;
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the link target pathto the user-provided buffer. */
|
|
|
|
|
|
|
|
*buf = '\0';
|
2020-01-02 17:49:34 +01:00
|
|
|
strncpy(buf, node->u.i_link, bufsize);
|
2017-02-03 20:57:12 +01:00
|
|
|
|
|
|
|
/* Release our reference on the inode and return the length */
|
|
|
|
|
|
|
|
inode_release(node);
|
2017-02-05 21:25:45 +01:00
|
|
|
RELEASE_SEARCH(&desc);
|
2017-02-03 20:57:12 +01:00
|
|
|
return strlen(buf);
|
|
|
|
|
|
|
|
errout_with_inode:
|
|
|
|
inode_release(node);
|
2017-02-05 21:25:45 +01:00
|
|
|
|
|
|
|
errout_with_search:
|
|
|
|
RELEASE_SEARCH(&desc);
|
2017-02-03 20:57:12 +01:00
|
|
|
set_errno(errcode);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_PSEUDOFS_SOFTLINKS */
|