2007-05-21 16:36:00 +02:00
|
|
|
/****************************************************************************
|
2014-09-28 19:46:11 +02:00
|
|
|
* fs/vfs/fs_mkdir.c
|
2007-05-21 16:36:00 +02:00
|
|
|
*
|
2020-03-21 17:48:14 +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
|
2007-05-21 16:36:00 +02:00
|
|
|
*
|
2020-03-21 17:48:14 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-05-21 16:36:00 +02:00
|
|
|
*
|
2020-03-21 17:48:14 +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.
|
2007-05-21 16:36:00 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2017-02-04 18:46:54 +01:00
|
|
|
|
2007-05-21 16:36:00 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2017-02-04 18:46:54 +01:00
|
|
|
#include <stdbool.h>
|
2017-02-05 16:51:42 +01:00
|
|
|
#include <assert.h>
|
2007-05-21 16:36:00 +02:00
|
|
|
#include <errno.h>
|
2017-02-04 18:46:54 +01:00
|
|
|
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2014-09-29 15:14:38 +02:00
|
|
|
#include "inode/inode.h"
|
2007-05-21 16:36:00 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2014-02-21 01:14:02 +01:00
|
|
|
* Pre-processor Definitions
|
2007-05-21 16:36:00 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-02-21 01:14:02 +01:00
|
|
|
#undef FS_HAVE_MKDIR
|
2020-08-13 15:53:59 +02:00
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) || !defined(CONFIG_DISABLE_PSEUDOFS_OPERATIONS)
|
2014-02-21 01:14:02 +01:00
|
|
|
# define FS_HAVE_MKDIR 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef FS_HAVE_MKDIR
|
|
|
|
|
2007-05-21 16:36:00 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mkdir
|
|
|
|
*
|
|
|
|
* Description: Create a directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int mkdir(const char *pathname, mode_t mode)
|
|
|
|
{
|
2017-02-05 16:51:42 +01:00
|
|
|
struct inode_search_s desc;
|
2007-05-21 16:36:00 +02:00
|
|
|
FAR struct inode *inode;
|
2017-02-05 21:25:45 +01:00
|
|
|
int errcode;
|
|
|
|
int ret;
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2014-02-19 17:30:50 +01:00
|
|
|
/* Find the inode that includes this path */
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
SETUP_SEARCH(&desc, pathname, false);
|
2017-02-05 16:51:42 +01:00
|
|
|
|
|
|
|
ret = inode_find(&desc);
|
|
|
|
if (ret >= 0)
|
2007-05-21 16:36:00 +02:00
|
|
|
{
|
2014-02-19 17:30:50 +01:00
|
|
|
/* An inode was found that includes this path and possibly refers to a
|
|
|
|
* mountpoint.
|
|
|
|
*/
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2017-02-05 16:51:42 +01:00
|
|
|
inode = desc.node;
|
|
|
|
DEBUGASSERT(inode != NULL);
|
|
|
|
|
2014-02-19 17:30:50 +01:00
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
/* Check if the inode is a valid mountpoint. */
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2014-02-19 17:30:50 +01:00
|
|
|
if (!INODE_IS_MOUNTPT(inode) || !inode->u.i_mops)
|
|
|
|
{
|
|
|
|
/* The inode is not a mountpoint */
|
2007-05-21 16:36:00 +02:00
|
|
|
|
2014-02-19 17:30:50 +01:00
|
|
|
errcode = ENXIO;
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the mkdir operation using the relative path
|
|
|
|
* at the mountpoint.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (inode->u.i_mops->mkdir)
|
|
|
|
{
|
2017-02-05 16:51:42 +01:00
|
|
|
ret = inode->u.i_mops->mkdir(inode, desc.relpath, mode);
|
2014-02-19 17:30:50 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2014-02-21 01:14:02 +01:00
|
|
|
{
|
2014-02-19 17:30:50 +01:00
|
|
|
errcode = ENOSYS;
|
|
|
|
goto errout_with_inode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release our reference on the inode */
|
|
|
|
|
|
|
|
inode_release(inode);
|
|
|
|
#else
|
|
|
|
/* But mountpoints are not supported in this configuration */
|
|
|
|
|
2014-02-20 14:39:36 +01:00
|
|
|
errcode = EEXIST;
|
2014-02-21 01:14:02 +01:00
|
|
|
goto errout_with_inode;
|
2014-02-19 17:30:50 +01:00
|
|
|
#endif
|
2007-05-21 16:36:00 +02:00
|
|
|
}
|
|
|
|
|
2014-02-21 01:14:02 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2014-02-19 17:30:50 +01:00
|
|
|
/* No inode exists that contains this path. Create a new inode in the
|
|
|
|
* pseudo-filesystem at this location.
|
2007-05-21 16:36:00 +02:00
|
|
|
*/
|
|
|
|
|
2014-02-19 17:30:50 +01:00
|
|
|
else
|
2007-05-21 16:36:00 +02:00
|
|
|
{
|
2015-09-23 18:34:08 +02: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.
|
|
|
|
*/
|
2014-02-19 17:30:50 +01:00
|
|
|
|
2020-03-30 01:36:19 +02:00
|
|
|
ret = inode_semtake();
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
errcode = -ret;
|
|
|
|
goto errout_with_search;
|
|
|
|
}
|
|
|
|
|
2014-02-19 17:30:50 +01:00
|
|
|
ret = inode_reserve(pathname, &inode);
|
|
|
|
inode_semgive();
|
|
|
|
|
2007-05-21 16:36:00 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-02-19 17:30:50 +01:00
|
|
|
errcode = -ret;
|
2017-02-05 21:25:45 +01:00
|
|
|
goto errout_with_search;
|
2007-05-21 16:36:00 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-21 01:14:02 +01:00
|
|
|
#else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errcode = ENXIO;
|
2017-02-05 21:25:45 +01:00
|
|
|
goto errout_with_search;
|
2014-02-21 01:14:02 +01:00
|
|
|
}
|
|
|
|
#endif
|
2007-05-21 16:36:00 +02:00
|
|
|
|
|
|
|
/* Directory successfully created */
|
|
|
|
|
2017-02-05 21:25:45 +01:00
|
|
|
RELEASE_SEARCH(&desc);
|
2007-05-21 16:36:00 +02:00
|
|
|
return OK;
|
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
errout_with_inode:
|
2007-05-21 16:36:00 +02:00
|
|
|
inode_release(inode);
|
2017-02-05 21:25:45 +01:00
|
|
|
|
|
|
|
errout_with_search:
|
|
|
|
RELEASE_SEARCH(&desc);
|
2014-02-19 17:30:50 +01:00
|
|
|
set_errno(errcode);
|
2007-05-21 16:36:00 +02:00
|
|
|
return ERROR;
|
|
|
|
}
|
2014-02-21 01:14:02 +01:00
|
|
|
|
|
|
|
#endif /* FS_HAVE_MKDIR */
|