2017-02-03 20:57:12 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/vfs/fs_readlink.c
|
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* 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
|
2017-02-03 20:57:12 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-02-03 20:57:12 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* 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.
|
2017-02-03 20:57:12 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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 */
|