2011-04-28 23:05:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/nxffs/nxffs_unlink.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
|
2011-04-28 23:05:39 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-28 23:05:39 +02: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.
|
2011-04-28 23:05:39 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2020-12-05 06:10:23 +01:00
|
|
|
#include <stdint.h>
|
2011-04-28 23:05:39 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2012-03-21 19:01:07 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2013-11-15 18:22:23 +01:00
|
|
|
#include <nuttx/mtd/mtd.h>
|
2011-04-28 23:05:39 +02:00
|
|
|
|
|
|
|
#include "nxffs.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_rminode
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Remove an inode from FLASH. This is the internal implementation of
|
2020-11-08 16:57:30 +01:00
|
|
|
* the file system unlink operation.
|
2011-04-28 23:05:39 +02: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-04-30 19:29:40 +02:00
|
|
|
ofile = nxffs_findofile(volume, name);
|
2011-04-28 23:05:39 +02:00
|
|
|
if (ofile)
|
|
|
|
{
|
|
|
|
/* We can't remove the inode if it is open */
|
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Inode '%s' is open\n", name);
|
2011-05-06 18:07:27 +02:00
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout;
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the NXFFS inode */
|
|
|
|
|
|
|
|
ret = nxffs_findinode(volume, name, &entry);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Inode '%s' not found\n", name);
|
2011-05-06 18:07:27 +02:00
|
|
|
goto errout;
|
2011-04-28 23:05:39 +02: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 17:40:19 +02:00
|
|
|
/* Make sure that the block is in the cache */
|
2011-04-28 23:05:39 +02:00
|
|
|
|
2011-05-03 15:46:14 +02:00
|
|
|
ret = nxffs_rdcache(volume, volume->ioblock);
|
2011-04-28 23:05:39 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-12-05 06:10:23 +01:00
|
|
|
ferr("ERROR: Failed to read block %jd into cache: %d\n",
|
|
|
|
(intmax_t)volume->ioblock, ret);
|
2011-05-06 18:07:27 +02:00
|
|
|
goto errout_with_entry;
|
2011-04-28 23:05:39 +02: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 02:26:38 +02:00
|
|
|
ret = nxffs_wrcache(volume);
|
2011-04-28 23:05:39 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-12-05 06:10:23 +01:00
|
|
|
ferr("ERROR: Failed to write block %jd: %d\n",
|
|
|
|
(intmax_t)volume->ioblock, ret);
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
2011-05-06 18:07:27 +02:00
|
|
|
errout_with_entry:
|
|
|
|
nxffs_freeentry(&entry);
|
|
|
|
errout:
|
|
|
|
return ret;
|
2011-04-28 23:05:39 +02: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-11 19:59:51 +02:00
|
|
|
finfo("Entry\n");
|
2011-04-28 23:05:39 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt && mountpt->i_private);
|
|
|
|
|
|
|
|
/* Get the mountpoint private data from the NuttX inode structure */
|
|
|
|
|
|
|
|
volume = mountpt->i_private;
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&volume->exclsem);
|
2011-04-28 23:05:39 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then remove the NXFFS inode */
|
|
|
|
|
|
|
|
ret = nxffs_rminode(volume, relpath);
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&volume->exclsem);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
2011-04-28 23:05:39 +02:00
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|