2011-04-29 05:05:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/nxffs/nxffs_unlink.c
|
|
|
|
*
|
2021-02-03 21:47:23 +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
|
2011-04-29 05:05:39 +08:00
|
|
|
*
|
2021-02-03 21:47:23 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-29 05:05:39 +08:00
|
|
|
*
|
2021-02-03 21:47:23 +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.
|
2011-04-29 05:05:39 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2020-12-05 13:10:23 +08:00
|
|
|
#include <stdint.h>
|
2011-04-29 05:05:39 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2012-03-22 02:01:07 +08:00
|
|
|
#include <nuttx/fs/fs.h>
|
2013-11-16 01:22:23 +08:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2011-04-29 05:05:39 +08:00
|
|
|
|
|
|
|
#include "nxffs.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_rminode
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Remove an inode from FLASH. This is the internal implementation of
|
2020-11-08 23:57:30 +08:00
|
|
|
* the file system unlink operation.
|
2011-04-29 05:05:39 +08:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* volume - Describes the NXFFS volume.
|
|
|
|
* name - the name of the inode to be deleted.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned if the inode is successfully deleted. Otherwise, a
|
|
|
|
* negated errno value is returned indicating the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_rminode(FAR struct nxffs_volume_s *volume, FAR const char *name)
|
|
|
|
{
|
|
|
|
FAR struct nxffs_ofile_s *ofile;
|
|
|
|
FAR struct nxffs_inode_s *inode;
|
|
|
|
struct nxffs_entry_s entry;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Check if the file is open */
|
|
|
|
|
2011-05-01 01:29:40 +08:00
|
|
|
ofile = nxffs_findofile(volume, name);
|
2011-04-29 05:05:39 +08:00
|
|
|
if (ofile)
|
|
|
|
{
|
|
|
|
/* We can't remove the inode if it is open */
|
|
|
|
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: Inode '%s' is open\n", name);
|
2011-05-07 00:07:27 +08:00
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout;
|
2011-04-29 05:05:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the NXFFS inode */
|
|
|
|
|
|
|
|
ret = nxffs_findinode(volume, name, &entry);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 05:50:49 +08:00
|
|
|
ferr("ERROR: Inode '%s' not found\n", name);
|
2011-05-07 00:07:27 +08:00
|
|
|
goto errout;
|
2011-04-29 05:05:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the position to the FLASH offset of the file header (nxffs_findinode
|
|
|
|
* should have left the block in the cache).
|
|
|
|
*/
|
|
|
|
|
|
|
|
nxffs_ioseek(volume, entry.hoffset);
|
|
|
|
|
2013-08-27 23:40:19 +08:00
|
|
|
/* Make sure that the block is in the cache */
|
2011-04-29 05:05:39 +08:00
|
|
|
|
2011-05-03 21:46:14 +08:00
|
|
|
ret = nxffs_rdcache(volume, volume->ioblock);
|
2011-04-29 05:05:39 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-12-05 13:10:23 +08:00
|
|
|
ferr("ERROR: Failed to read block %jd into cache: %d\n",
|
|
|
|
(intmax_t)volume->ioblock, ret);
|
2011-05-07 00:07:27 +08:00
|
|
|
goto errout_with_entry;
|
2011-04-29 05:05:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Change the file status... it is no longer valid */
|
|
|
|
|
|
|
|
inode = (FAR struct nxffs_inode_s *)&volume->cache[volume->iooffset];
|
|
|
|
inode->state = INODE_STATE_DELETED;
|
|
|
|
|
|
|
|
/* Then write the cached block back to FLASH */
|
|
|
|
|
2011-05-01 08:26:38 +08:00
|
|
|
ret = nxffs_wrcache(volume);
|
2011-04-29 05:05:39 +08:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-12-05 13:10:23 +08:00
|
|
|
ferr("ERROR: Failed to write block %jd: %d\n",
|
|
|
|
(intmax_t)volume->ioblock, ret);
|
2011-04-29 05:05:39 +08:00
|
|
|
}
|
|
|
|
|
2011-05-07 00:07:27 +08:00
|
|
|
errout_with_entry:
|
|
|
|
nxffs_freeentry(&entry);
|
|
|
|
errout:
|
|
|
|
return ret;
|
2011-04-29 05:05:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_unlink
|
|
|
|
*
|
|
|
|
* Description: Remove a file
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_unlink(FAR struct inode *mountpt, FAR const char *relpath)
|
|
|
|
{
|
|
|
|
FAR struct nxffs_volume_s *volume;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-12 01:59:51 +08:00
|
|
|
finfo("Entry\n");
|
2011-04-29 05:05:39 +08:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt && mountpt->i_private);
|
|
|
|
|
|
|
|
/* Get the mountpoint private data from the NuttX inode structure */
|
|
|
|
|
|
|
|
volume = mountpt->i_private;
|
2022-09-06 14:18:45 +08:00
|
|
|
ret = nxmutex_lock(&volume->lock);
|
2011-04-29 05:05:39 +08:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then remove the NXFFS inode */
|
|
|
|
|
|
|
|
ret = nxffs_rminode(volume, relpath);
|
2013-12-02 08:22:33 +08:00
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
nxmutex_unlock(&volume->lock);
|
2017-10-05 05:22:27 +08:00
|
|
|
|
2011-04-29 05:05:39 +08:00
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|