2017-02-02 17:39:41 +01:00
|
|
|
/****************************************************************************
|
2020-07-01 07:37:39 +02:00
|
|
|
* fs/vfs/fs_symlink.c
|
2017-02-02 17:39:41 +01:00
|
|
|
*
|
2020-03-30 01:36:19 +02: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-02 17:39:41 +01:00
|
|
|
*
|
2020-03-30 01:36:19 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-02-02 17:39:41 +01:00
|
|
|
*
|
2020-03-30 01:36:19 +02: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-02 17:39:41 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2017-02-04 18:46:54 +01:00
|
|
|
#include <stdbool.h>
|
2017-02-03 00:11:08 +01:00
|
|
|
#include <unistd.h>
|
2017-02-02 22:24:39 +01:00
|
|
|
#include <string.h>
|
2017-02-02 17:39:41 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
2017-02-02 22:24:39 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2017-02-02 17:39:41 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
|
|
|
#include "inode/inode.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-07-01 07:37:39 +02:00
|
|
|
* Name: symlink
|
2017-02-02 17:39:41 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2020-07-01 07:37:39 +02:00
|
|
|
* The symlink() function will create a new link (directory entry) for the
|
2017-02-03 00:11:08 +01:00
|
|
|
* existing file, path2. This implementation is simplied for use with
|
2017-02-02 17:39:41 +01:00
|
|
|
* NuttX in these ways:
|
|
|
|
*
|
|
|
|
* - Links may be created only within the NuttX top-level, pseudo file
|
|
|
|
* system. No file system currently supported by NuttX provides
|
|
|
|
* symbolic links.
|
|
|
|
* - For the same reason, only soft links are implemented.
|
|
|
|
* - File privileges are ignored.
|
|
|
|
* - c_time is not updated.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* path1 - Points to a pathname naming an existing file.
|
2020-03-30 01:36:19 +02:00
|
|
|
* path2 - Points to a pathname naming the new directory entry to be
|
|
|
|
* created.
|
2017-02-02 17:39:41 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, zero (OK) is returned. Otherwise, -1 (ERROR) is returned
|
2017-05-11 21:15:13 +02:00
|
|
|
* the errno variable is set appropriately.
|
2017-02-02 17:39:41 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-07-01 07:37:39 +02:00
|
|
|
int symlink(FAR const char *path1, FAR const char *path2)
|
2017-02-02 17:39:41 +01:00
|
|
|
{
|
2017-02-05 16:51:42 +01:00
|
|
|
struct inode_search_s desc;
|
2020-04-12 18:46:40 +02:00
|
|
|
FAR struct inode *inode = NULL;
|
2017-02-02 17:39:41 +01:00
|
|
|
int errcode;
|
|
|
|
int ret;
|
|
|
|
|
2017-02-03 00:11:08 +01:00
|
|
|
/* Both paths must be absolute. We need only check path1 here. path2 will
|
2017-02-02 20:01:21 +01:00
|
|
|
* be checked by inode find.
|
|
|
|
*/
|
|
|
|
|
2017-02-03 00:11:08 +01:00
|
|
|
if (path1 == NULL || *path1 != '/')
|
2017-02-02 20:01:21 +01:00
|
|
|
{
|
2017-02-02 22:24:39 +01:00
|
|
|
errcode = EINVAL;
|
2017-02-02 20:01:21 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
2017-02-02 17:39:41 +01:00
|
|
|
|
2020-03-30 01:36:19 +02:00
|
|
|
/* Check that no inode exists at the 'path1' and that the path up to
|
|
|
|
* 'path1' does not lie on a mounted volume.
|
2017-02-02 17:39:41 +01:00
|
|
|
*/
|
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
SETUP_SEARCH(&desc, path2, false);
|
2017-02-05 16:51:42 +01:00
|
|
|
|
|
|
|
ret = inode_find(&desc);
|
|
|
|
if (ret >= 0)
|
2017-02-02 17:39:41 +01:00
|
|
|
{
|
2017-02-05 16:51:42 +01:00
|
|
|
/* Something exists at the path2 where we are trying to create the
|
|
|
|
* link.
|
|
|
|
*/
|
|
|
|
|
2017-02-02 17:39:41 +01:00
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
/* Check if the inode is a mountpoint. */
|
|
|
|
|
2017-02-05 22:14:16 +01:00
|
|
|
DEBUGASSERT(desc.node != NULL);
|
|
|
|
if (INODE_IS_MOUNTPT(desc.node))
|
2017-02-02 17:39:41 +01:00
|
|
|
{
|
2017-02-02 22:24:39 +01:00
|
|
|
/* Symbolic links within the mounted volume are not supported */
|
2017-02-02 17:39:41 +01:00
|
|
|
|
|
|
|
errcode = ENOSYS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2017-02-03 00:11:08 +01:00
|
|
|
/* A node already exists in the pseudofs at 'path1' */
|
2017-02-02 17:39:41 +01:00
|
|
|
|
2017-02-02 22:24:39 +01:00
|
|
|
errcode = EEXIST;
|
2017-02-02 17:39:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No inode exists that contains this path. Create a new inode in the
|
|
|
|
* pseudo-filesystem at this location.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2017-02-03 00:11:08 +01:00
|
|
|
/* Copy path1 */
|
2017-02-02 17:39:41 +01:00
|
|
|
|
2017-02-03 00:11:08 +01:00
|
|
|
FAR char *newpath2 = strdup(path1);
|
2017-02-02 17:39:41 +01:00
|
|
|
if (newpath2 == NULL)
|
|
|
|
{
|
|
|
|
errcode = ENOMEM;
|
2017-02-05 21:25:45 +01:00
|
|
|
goto errout_with_search;
|
2017-02-02 17:39:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create an inode in the pseudo-filesystem at this path.
|
|
|
|
* NOTE that the new inode will be created with a reference
|
|
|
|
* count of zero.
|
|
|
|
*/
|
|
|
|
|
2020-03-30 01:36:19 +02:00
|
|
|
ret = inode_semtake();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
kmm_free(newpath2);
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_search;
|
|
|
|
}
|
|
|
|
|
2017-02-03 00:11:08 +01:00
|
|
|
ret = inode_reserve(path2, &inode);
|
2017-02-02 17:39:41 +01:00
|
|
|
inode_semgive();
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2017-02-02 22:24:39 +01:00
|
|
|
kmm_free(newpath2);
|
2017-02-02 17:39:41 +01:00
|
|
|
errcode = -ret;
|
2017-02-05 21:25:45 +01:00
|
|
|
goto errout_with_search;
|
2017-02-02 17:39:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the inode */
|
|
|
|
|
|
|
|
INODE_SET_SOFTLINK(inode);
|
|
|
|
inode->u.i_link = newpath2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Symbolic link successfully created */
|
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
RELEASE_SEARCH(&desc);
|
2017-02-02 17:39:41 +01:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_inode:
|
|
|
|
inode_release(inode);
|
2017-02-05 21:25:45 +01:00
|
|
|
|
|
|
|
errout_with_search:
|
|
|
|
RELEASE_SEARCH(&desc);
|
|
|
|
|
2017-02-02 17:39:41 +01:00
|
|
|
errout:
|
|
|
|
set_errno(errcode);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_PSEUDOFS_SOFTLINKS */
|