2017-02-03 00:39:41 +08:00
|
|
|
/****************************************************************************
|
2020-07-01 13:37:39 +08:00
|
|
|
* fs/vfs/fs_symlink.c
|
2017-02-03 00:39:41 +08:00
|
|
|
*
|
2020-03-30 07:36:19 +08: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 00:39:41 +08:00
|
|
|
*
|
2020-03-30 07:36:19 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-02-03 00:39:41 +08:00
|
|
|
*
|
2020-03-30 07:36:19 +08: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 00:39:41 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2017-02-05 01:46:54 +08:00
|
|
|
#include <stdbool.h>
|
2017-02-03 07:11:08 +08:00
|
|
|
#include <unistd.h>
|
2017-02-03 05:24:39 +08:00
|
|
|
#include <string.h>
|
2021-05-18 14:59:14 +08:00
|
|
|
#include <assert.h>
|
2017-02-03 00:39:41 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
2023-08-08 06:11:20 +08:00
|
|
|
#include <nuttx/lib/lib.h>
|
2017-02-03 00:39:41 +08:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
|
|
|
#include "inode/inode.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_PSEUDOFS_SOFTLINKS
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-07-01 13:37:39 +08:00
|
|
|
* Name: symlink
|
2017-02-03 00:39:41 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2020-07-01 13:37:39 +08:00
|
|
|
* The symlink() function will create a new link (directory entry) for the
|
2017-02-03 07:11:08 +08:00
|
|
|
* existing file, path2. This implementation is simplied for use with
|
2017-02-03 00:39:41 +08: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 07:36:19 +08:00
|
|
|
* path2 - Points to a pathname naming the new directory entry to be
|
|
|
|
* created.
|
2017-02-03 00:39:41 +08:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, zero (OK) is returned. Otherwise, -1 (ERROR) is returned
|
2017-05-12 03:15:13 +08:00
|
|
|
* the errno variable is set appropriately.
|
2017-02-03 00:39:41 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-07-01 13:37:39 +08:00
|
|
|
int symlink(FAR const char *path1, FAR const char *path2)
|
2017-02-03 00:39:41 +08:00
|
|
|
{
|
2017-02-05 23:51:42 +08:00
|
|
|
struct inode_search_s desc;
|
2020-04-13 00:46:40 +08:00
|
|
|
FAR struct inode *inode = NULL;
|
2017-02-03 00:39:41 +08:00
|
|
|
int errcode;
|
|
|
|
int ret;
|
|
|
|
|
2020-09-14 01:15:05 +08:00
|
|
|
if (path1 == NULL)
|
2017-02-03 03:01:21 +08:00
|
|
|
{
|
2017-02-03 05:24:39 +08:00
|
|
|
errcode = EINVAL;
|
2017-02-03 03:01:21 +08:00
|
|
|
goto errout;
|
|
|
|
}
|
2017-02-03 00:39:41 +08:00
|
|
|
|
2020-09-17 02:01:42 +08:00
|
|
|
/* Check that no inode exists at the 'path2' and that the path up to
|
|
|
|
* 'path2' does not lie on a mounted volume.
|
2017-02-03 00:39:41 +08:00
|
|
|
*/
|
|
|
|
|
2017-02-06 04:25:45 +08:00
|
|
|
SETUP_SEARCH(&desc, path2, false);
|
2017-02-05 23:51:42 +08:00
|
|
|
|
|
|
|
ret = inode_find(&desc);
|
|
|
|
if (ret >= 0)
|
2017-02-03 00:39:41 +08:00
|
|
|
{
|
2017-02-05 23:51:42 +08:00
|
|
|
/* Something exists at the path2 where we are trying to create the
|
|
|
|
* link.
|
|
|
|
*/
|
|
|
|
|
2017-02-03 00:39:41 +08:00
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
/* Check if the inode is a mountpoint. */
|
|
|
|
|
2017-02-06 05:14:16 +08:00
|
|
|
DEBUGASSERT(desc.node != NULL);
|
|
|
|
if (INODE_IS_MOUNTPT(desc.node))
|
2017-02-03 00:39:41 +08:00
|
|
|
{
|
2017-02-03 05:24:39 +08:00
|
|
|
/* Symbolic links within the mounted volume are not supported */
|
2017-02-03 00:39:41 +08:00
|
|
|
|
|
|
|
errcode = ENOSYS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2017-02-03 07:11:08 +08:00
|
|
|
/* A node already exists in the pseudofs at 'path1' */
|
2017-02-03 00:39:41 +08:00
|
|
|
|
2017-02-03 05:24:39 +08:00
|
|
|
errcode = EEXIST;
|
2017-02-03 00:39:41 +08: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 07:11:08 +08:00
|
|
|
/* Copy path1 */
|
2017-02-03 00:39:41 +08:00
|
|
|
|
2017-02-03 07:11:08 +08:00
|
|
|
FAR char *newpath2 = strdup(path1);
|
2017-02-03 00:39:41 +08:00
|
|
|
if (newpath2 == NULL)
|
|
|
|
{
|
|
|
|
errcode = ENOMEM;
|
2017-02-06 04:25:45 +08:00
|
|
|
goto errout_with_search;
|
2017-02-03 00:39:41 +08: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.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
ret = inode_lock();
|
2020-03-30 07:36:19 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-08-08 06:11:20 +08:00
|
|
|
lib_free(newpath2);
|
2020-03-30 07:36:19 +08:00
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_search;
|
|
|
|
}
|
|
|
|
|
2021-07-09 13:43:26 +08:00
|
|
|
ret = inode_reserve(path2, 0777, &inode);
|
2022-09-06 14:18:45 +08:00
|
|
|
inode_unlock();
|
2017-02-03 00:39:41 +08:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2023-08-08 06:11:20 +08:00
|
|
|
lib_free(newpath2);
|
2017-02-03 00:39:41 +08:00
|
|
|
errcode = -ret;
|
2017-02-06 04:25:45 +08:00
|
|
|
goto errout_with_search;
|
2017-02-03 00:39:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the inode */
|
|
|
|
|
|
|
|
INODE_SET_SOFTLINK(inode);
|
|
|
|
inode->u.i_link = newpath2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Symbolic link successfully created */
|
|
|
|
|
2017-02-06 04:25:45 +08:00
|
|
|
RELEASE_SEARCH(&desc);
|
2017-02-03 00:39:41 +08:00
|
|
|
return OK;
|
|
|
|
|
|
|
|
errout_with_inode:
|
|
|
|
inode_release(inode);
|
2017-02-06 04:25:45 +08:00
|
|
|
|
|
|
|
errout_with_search:
|
|
|
|
RELEASE_SEARCH(&desc);
|
|
|
|
|
2017-02-03 00:39:41 +08:00
|
|
|
errout:
|
|
|
|
set_errno(errcode);
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_PSEUDOFS_SOFTLINKS */
|