2011-04-28 23:05:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/nxffs/nxffs_open.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:06:25 +01:00
|
|
|
#include <stdint.h>
|
2011-04-28 23:05:39 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
2011-04-30 19:29:40 +02:00
|
|
|
#include <time.h>
|
2011-04-28 23:05:39 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2022-08-12 08:13:41 +02:00
|
|
|
#include <nuttx/crc32.h>
|
2023-08-08 00:11:20 +02:00
|
|
|
#include <nuttx/lib/lib.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"
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-04-30 22:06:23 +02:00
|
|
|
* Private Data
|
2011-04-28 23:05:39 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2011-04-30 22:06:23 +02:00
|
|
|
/* Since we are limited to a single file opened for writing, it makes sense
|
|
|
|
* to pre-allocate the write state structure.
|
|
|
|
*/
|
|
|
|
|
2013-04-30 23:54:02 +02:00
|
|
|
#ifdef CONFIG_NXFFS_PREALLOCATED
|
2011-04-30 22:06:23 +02:00
|
|
|
static struct nxffs_wrfile_s g_wrfile;
|
|
|
|
#endif
|
|
|
|
|
2011-04-28 23:05:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-04-30 19:29:40 +02:00
|
|
|
* Name: nxffs_hdrpos
|
2011-04-28 23:05:39 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2011-04-30 19:29:40 +02:00
|
|
|
* Find a valid location for the inode header. A valid location will have
|
|
|
|
* these properties:
|
|
|
|
*
|
|
|
|
* 1. It will lie in the free flash region.
|
|
|
|
* 2. It will have enough contiguous memory to hold the entire header
|
|
|
|
* (excluding the file name which may lie in the next block).
|
|
|
|
* 3. The memory at this location will be fully erased.
|
|
|
|
*
|
|
|
|
* This function will only perform the checks of 1) and 2).
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* volume - Describes the NXFFS volume
|
|
|
|
* wrfile - Contains the current guess for the header position. On
|
|
|
|
* successful return, this field will hold the selected header
|
|
|
|
* position.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success. Otherwise, a negated errno value is
|
|
|
|
* returned indicating the nature of the failure. Of special interest
|
|
|
|
* the return error of -ENOSPC which means that the FLASH volume is
|
|
|
|
* full and should be repacked.
|
|
|
|
*
|
|
|
|
* On successful return the following are also valid:
|
|
|
|
*
|
2020-11-22 01:43:25 +01:00
|
|
|
* wrfile->ofile.entry.hoffset - FLASH offset to candidate header
|
|
|
|
* position
|
2011-04-30 19:29:40 +02:00
|
|
|
* volume->ioblock - Read/write block number of the block containing the
|
|
|
|
* header position
|
|
|
|
* volume->iooffset - The offset in the block to the candidate header
|
|
|
|
* position.
|
|
|
|
* volume->froffset - Updated offset to the first free FLASH block.
|
2011-04-28 23:05:39 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
static inline int nxffs_hdrpos(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR struct nxffs_wrfile_s *wrfile)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Reserve memory for the object */
|
|
|
|
|
|
|
|
ret = nxffs_wrreserve(volume, SIZEOF_NXFFS_INODE_HDR);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Save the offset to the FLASH region reserved for the inode header */
|
|
|
|
|
2011-04-30 22:06:23 +02:00
|
|
|
wrfile->ofile.entry.hoffset = nxffs_iotell(volume);
|
2011-04-30 19:29:40 +02:00
|
|
|
}
|
2019-10-27 18:48:14 +01:00
|
|
|
|
2011-05-06 17:19:07 +02:00
|
|
|
return ret;
|
2011-04-30 19:29:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_nampos
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a valid location for the inode name. A valid location will have
|
|
|
|
* these properties:
|
|
|
|
*
|
|
|
|
* 1. It will lie in the free flash region.
|
|
|
|
* 2. It will have enough contiguous memory to hold the entire name
|
|
|
|
* 3. The memory at this location will be fully erased.
|
|
|
|
*
|
|
|
|
* This function will only perform the checks of 1) and 2).
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* volume - Describes the NXFFS volume
|
|
|
|
* wrfile - Contains the current guess for the name position. On
|
|
|
|
* successful return, this field will hold the selected name
|
|
|
|
* position.
|
|
|
|
* namlen - The length of the name.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success. Otherwise, a negated errno value is
|
|
|
|
* returned indicating the nature of the failure. Of special interest
|
|
|
|
* the return error of -ENOSPC which means that the FLASH volume is
|
|
|
|
* full and should be repacked.
|
|
|
|
*
|
|
|
|
* On successful return the following are also valid:
|
|
|
|
*
|
2011-05-04 16:20:52 +02:00
|
|
|
* wrfile->ofile.entry.noffset - FLASH offset to candidate name position
|
2011-04-30 19:29:40 +02:00
|
|
|
* volume->ioblock - Read/write block number of the block containing the
|
|
|
|
* name position
|
|
|
|
* volume->iooffset - The offset in the block to the candidate name
|
|
|
|
* position.
|
|
|
|
* volume->froffset - Updated offset to the first free FLASH block.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int nxffs_nampos(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR struct nxffs_wrfile_s *wrfile,
|
|
|
|
int namlen)
|
2011-04-28 23:05:39 +02:00
|
|
|
{
|
2011-04-30 19:29:40 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Reserve memory for the object */
|
|
|
|
|
|
|
|
ret = nxffs_wrreserve(volume, namlen);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Save the offset to the FLASH region reserved for the inode name */
|
|
|
|
|
2011-04-30 22:06:23 +02:00
|
|
|
wrfile->ofile.entry.noffset = nxffs_iotell(volume);
|
2011-04-30 19:29:40 +02:00
|
|
|
}
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-05-06 17:19:07 +02:00
|
|
|
return ret;
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_hdrerased
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a valid location for the inode header. A valid location will have
|
|
|
|
* these properties:
|
|
|
|
*
|
|
|
|
* 1. It will lie in the free flash region.
|
|
|
|
* 2. It will have enough contiguous memory to hold the entire header
|
|
|
|
* (excluding the file name which may lie in the next block).
|
|
|
|
* 3. The memory at this location will be fully erased.
|
|
|
|
*
|
|
|
|
* This function will only perform the check 3).
|
|
|
|
*
|
|
|
|
* On entry it assumes:
|
|
|
|
*
|
|
|
|
* volume->ioblock - Read/write block number of the block containing the
|
|
|
|
* header position
|
|
|
|
* volume->iooffset - The offset in the block to the candidate header
|
|
|
|
* position.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* volume - Describes the NXFFS volume
|
|
|
|
* wrfile - Contains the current guess for the header position. On
|
|
|
|
* successful return, this field will hold the selected header
|
|
|
|
* position.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success. Otherwise, a negated errno value is
|
|
|
|
* returned indicating the nature of the failure. Of special interest
|
|
|
|
* the return error of -ENOSPC which means that the FLASH volume is
|
|
|
|
* full and should be repacked.
|
|
|
|
*
|
|
|
|
* On successful return the following are also valid:
|
|
|
|
*
|
2020-11-22 01:43:25 +01:00
|
|
|
* wrfile->ofile.entry.hoffset - FLASH offset to candidate header
|
|
|
|
* position
|
2011-04-30 19:29:40 +02:00
|
|
|
* volume->ioblock - Read/write block number of the block containing the
|
|
|
|
* header position
|
|
|
|
* volume->iooffset - The offset in the block to the candidate header
|
|
|
|
* position.
|
|
|
|
* volume->froffset - Updated offset to the first free FLASH block.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int nxffs_hdrerased(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR struct nxffs_wrfile_s *wrfile)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Find a valid location to save the inode header */
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
ret = nxffs_wrverify(volume, SIZEOF_NXFFS_INODE_HDR);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* This is where we will put the header */
|
|
|
|
|
2011-04-30 22:06:23 +02:00
|
|
|
wrfile->ofile.entry.hoffset = nxffs_iotell(volume);
|
2011-04-30 19:29:40 +02:00
|
|
|
}
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_namerased
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find a valid location for the inode name. A valid location will have
|
|
|
|
* these properties:
|
|
|
|
*
|
|
|
|
* 1. It will lie in the free flash region.
|
|
|
|
* 2. It will have enough contiguous memory to hold the entire name
|
|
|
|
* (excluding the file name which may lie in the next block).
|
|
|
|
* 3. The memory at this location will be fully erased.
|
|
|
|
*
|
|
|
|
* This function will only perform the check 3).
|
|
|
|
*
|
|
|
|
* On entry it assumes:
|
|
|
|
*
|
|
|
|
* volume->ioblock - Read/write block number of the block containing the
|
|
|
|
* name position
|
|
|
|
* volume->iooffset - The offset in the block to the candidate name
|
|
|
|
* position.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* volume - Describes the NXFFS volume
|
|
|
|
* wrfile - Contains the current guess for the name position. On
|
|
|
|
* successful return, this field will hold the selected name
|
|
|
|
* position.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success. Otherwise, a negated errno value is
|
|
|
|
* returned indicating the nature of the failure. Of special interest
|
|
|
|
* the return error of -ENOSPC which means that the FLASH volume is
|
|
|
|
* full and should be repacked.
|
|
|
|
*
|
|
|
|
* On successful return the following are also valid:
|
|
|
|
*
|
2011-05-04 16:20:52 +02:00
|
|
|
* wrfile->ofile.entry.noffset - FLASH offset to candidate name position
|
2011-04-30 19:29:40 +02:00
|
|
|
* volume->ioblock - Read/write block number of the block containing the
|
|
|
|
* name position
|
|
|
|
* volume->iooffset - The offset in the block to the candidate name
|
|
|
|
* position.
|
|
|
|
* volume->froffset - Updated offset to the first free FLASH block.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int nxffs_namerased(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR struct nxffs_wrfile_s *wrfile,
|
|
|
|
int namlen)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Find a valid location to save the inode name */
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
ret = nxffs_wrverify(volume, namlen);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* This is where we will put the name */
|
|
|
|
|
2011-05-01 01:24:48 +02:00
|
|
|
wrfile->ofile.entry.noffset = nxffs_iotell(volume);
|
2011-04-30 19:29:40 +02:00
|
|
|
}
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-04 16:20:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_wrname
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write the inode name to cache at the position verified by
|
|
|
|
* nxffs_namerased().
|
|
|
|
*
|
|
|
|
* On entry it assumes:
|
|
|
|
*
|
|
|
|
* entry->noffset - FLASH offset to final name position
|
|
|
|
* volume->ioblock - Read/write block number of the block containing the
|
|
|
|
* name position
|
|
|
|
* volume->iooffset - The offset in the block to the candidate name
|
|
|
|
* position.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* volume - Describes the NXFFS volume
|
2013-12-02 01:22:33 +01:00
|
|
|
* entry - Describes the entry to be written.
|
2011-05-04 16:20:52 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero is returned on success. Otherwise, a negated errno value is
|
|
|
|
* returned indicating the nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int nxffs_wrname(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR struct nxffs_entry_s *entry,
|
|
|
|
int namlen)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Seek to the inode name position and assure that it is in the volume
|
|
|
|
* cache.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nxffs_ioseek(volume, entry->noffset);
|
|
|
|
ret = nxffs_rdcache(volume, volume->ioblock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-12-05 06:06:25 +01:00
|
|
|
ferr("ERROR: Failed to read inode name block %jd: %d\n",
|
|
|
|
(intmax_t)volume->ioblock, -ret);
|
2011-05-04 16:20:52 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the inode name to the volume cache and write the inode name block */
|
|
|
|
|
|
|
|
memcpy(&volume->cache[volume->iooffset], entry->name, namlen);
|
|
|
|
ret = nxffs_wrcache(volume);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-12-05 06:06:25 +01:00
|
|
|
ferr("ERROR: Failed to write inode header block %jd: %d\n",
|
|
|
|
(intmax_t)volume->ioblock, -ret);
|
2011-05-04 16:20:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-04-28 23:05:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_wropen
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Handle opening for writing. Only a single writer is permitted and only
|
|
|
|
* file creation is supported.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int nxffs_wropen(FAR struct nxffs_volume_s *volume,
|
2011-05-02 18:14:20 +02:00
|
|
|
FAR const char *name, mode_t oflags,
|
2011-04-28 23:05:39 +02:00
|
|
|
FAR struct nxffs_ofile_s **ppofile)
|
|
|
|
{
|
|
|
|
FAR struct nxffs_wrfile_s *wrfile;
|
|
|
|
FAR struct nxffs_entry_s entry;
|
2011-04-30 19:29:40 +02:00
|
|
|
bool packed;
|
|
|
|
bool truncate = false;
|
|
|
|
int namlen;
|
2011-04-28 23:05:39 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Limitation: Only a single writer is permitted. Writing may involve
|
|
|
|
* extension of the file system in FLASH. Since files are contiguous
|
|
|
|
* in FLASH, only a single file may be extending the FLASH region.
|
|
|
|
*/
|
|
|
|
|
2017-10-04 23:22:27 +02:00
|
|
|
ret = nxsem_wait(&volume->wrsem);
|
|
|
|
if (ret < 0)
|
2011-04-28 23:05:39 +02:00
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
ferr("ERROR: nxsem_wait failed: %d\n", ret);
|
2011-04-30 19:29:40 +02:00
|
|
|
goto errout;
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Get exclusive access to the volume. Note that the volume lock
|
|
|
|
* protects the open file list. Note that lock is ALWAYS taken
|
2011-05-01 16:48:27 +02:00
|
|
|
* after wrsem to avoid deadlocks.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&volume->lock);
|
2017-10-04 23:22:27 +02:00
|
|
|
if (ret < 0)
|
2011-05-01 16:48:27 +02:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ferr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2011-05-01 16:48:27 +02:00
|
|
|
goto errout_with_wrsem;
|
|
|
|
}
|
|
|
|
|
2011-04-28 23:05:39 +02:00
|
|
|
/* Check if the file exists */
|
|
|
|
|
|
|
|
ret = nxffs_findinode(volume, name, &entry);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
2011-10-05 00:07:45 +02:00
|
|
|
FAR struct nxffs_ofile_s *ofile;
|
|
|
|
|
2014-03-06 15:01:56 +01:00
|
|
|
/* It exists. Release the entry. */
|
|
|
|
|
|
|
|
nxffs_freeentry(&entry);
|
|
|
|
|
|
|
|
/* Is the file already open for reading? */
|
2011-10-05 00:07:45 +02:00
|
|
|
|
|
|
|
ofile = nxffs_findofile(volume, name);
|
|
|
|
if (ofile)
|
|
|
|
{
|
|
|
|
/* The file is already open.
|
|
|
|
* Limitation: Files cannot be open both for reading and writing.
|
|
|
|
*/
|
|
|
|
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: File is open for reading\n");
|
2011-10-05 00:07:45 +02:00
|
|
|
ret = -ENOSYS;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2011-10-05 00:07:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* It would be an error if we are asked to create the file
|
2011-04-28 23:05:39 +02:00
|
|
|
* exclusively.
|
|
|
|
*/
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
else if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
|
2011-04-28 23:05:39 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: File exists, can't create O_EXCL\n");
|
2011-04-30 19:29:40 +02:00
|
|
|
ret = -EEXIST;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Were we asked to truncate the file? NOTE: Don't truncate the
|
|
|
|
* file if we were not also asked to created it. See below...
|
|
|
|
* we will not re-create the file unless O_CREAT is also specified.
|
|
|
|
*/
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
|
2011-04-28 23:05:39 +02:00
|
|
|
{
|
2020-11-22 01:43:25 +01:00
|
|
|
/* Just schedule the removal the file and fall through to re-create
|
|
|
|
* it.
|
|
|
|
* Note that the old file of the same name will not actually be
|
|
|
|
* removed until the new file is successfully written.
|
2011-04-30 19:29:40 +02:00
|
|
|
*/
|
2011-04-28 23:05:39 +02:00
|
|
|
|
2013-12-02 01:22:33 +01:00
|
|
|
truncate = true;
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The file exists and we were not asked to truncate (and recreate) it.
|
|
|
|
* Limitation: Cannot write to existing files.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2020-11-22 01:43:25 +01:00
|
|
|
ferr("ERROR: File %s exists and we were not asked to "
|
2020-11-22 01:44:18 +01:00
|
|
|
"truncate it\n", name);
|
2011-04-30 19:29:40 +02:00
|
|
|
ret = -ENOSYS;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Okay, the file is not open and does not exists (maybe because we deleted
|
|
|
|
* it). Now, make sure that we were asked to created it.
|
|
|
|
*/
|
|
|
|
|
2011-05-02 18:14:20 +02:00
|
|
|
if ((oflags & O_CREAT) == 0)
|
2011-04-28 23:05:39 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Not asked to create the file\n");
|
2011-04-30 19:29:40 +02:00
|
|
|
ret = -ENOENT;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2011-04-30 19:29:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that the length of the file name will fit in a uint8_t */
|
|
|
|
|
|
|
|
namlen = strlen(name);
|
2011-05-04 00:10:52 +02:00
|
|
|
if (namlen > CONFIG_NXFFS_MAXNAMLEN)
|
2011-04-30 19:29:40 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Name is too long: %d\n", namlen);
|
2011-04-30 19:29:40 +02:00
|
|
|
ret = -EINVAL;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes.. Create a new structure that will describe the state of this open
|
|
|
|
* file. NOTE that a special variant of the open file structure is used
|
|
|
|
* that includes additional information to support the write operation.
|
|
|
|
*/
|
|
|
|
|
2013-04-30 23:54:02 +02:00
|
|
|
#ifdef CONFIG_NXFFS_PREALLOCATED
|
2011-04-30 22:06:23 +02:00
|
|
|
wrfile = &g_wrfile;
|
|
|
|
memset(wrfile, 0, sizeof(struct nxffs_wrfile_s));
|
|
|
|
#else
|
2020-11-22 01:43:25 +01:00
|
|
|
wrfile = (FAR struct nxffs_wrfile_s *)
|
|
|
|
kmm_zalloc(sizeof(struct nxffs_wrfile_s));
|
2011-04-28 23:05:39 +02:00
|
|
|
if (!wrfile)
|
|
|
|
{
|
2011-04-30 19:29:40 +02:00
|
|
|
ret = -ENOMEM;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
2011-04-30 22:06:23 +02:00
|
|
|
#endif
|
2011-04-28 23:05:39 +02:00
|
|
|
|
|
|
|
/* Initialize the open file state structure */
|
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
wrfile->ofile.crefs = 1;
|
2011-05-02 18:14:20 +02:00
|
|
|
wrfile->ofile.oflags = oflags;
|
2011-04-30 19:29:40 +02:00
|
|
|
wrfile->ofile.entry.utc = time(NULL);
|
|
|
|
wrfile->truncate = truncate;
|
|
|
|
|
2011-05-01 01:24:48 +02:00
|
|
|
/* Save a copy of the inode name. */
|
|
|
|
|
|
|
|
wrfile->ofile.entry.name = strdup(name);
|
|
|
|
if (!wrfile->ofile.entry.name)
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_ofile;
|
|
|
|
}
|
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
/* Allocate FLASH memory for the file and set up for the write.
|
|
|
|
*
|
|
|
|
* Loop until the inode header is configured or until a failure occurs.
|
|
|
|
* Note that nothing is written to FLASH. The inode header is not
|
|
|
|
* written until the file is closed.
|
|
|
|
*/
|
|
|
|
|
|
|
|
packed = false;
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2011-04-30 19:29:40 +02:00
|
|
|
{
|
|
|
|
/* File a valid location to position the inode header. Start with the
|
|
|
|
* first byte in the free FLASH region.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = nxffs_hdrpos(volume, wrfile);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Find a region of memory in the block that is fully erased */
|
|
|
|
|
|
|
|
ret = nxffs_hdrerased(volume, wrfile);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Valid memory for the inode header was found. Break out of
|
|
|
|
* the loop.
|
|
|
|
*/
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no valid memory is found searching to the end of the volume,
|
|
|
|
* then -ENOSPC will be returned. Other errors are not handled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret != -ENOSPC || packed)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to find inode header memory: %d\n", -ret);
|
2011-05-01 01:24:48 +02:00
|
|
|
goto errout_with_name;
|
2011-04-30 19:29:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -ENOSPC is a special case.. It means that the volume is full.
|
|
|
|
* Try to pack the volume in order to free up some space.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = nxffs_pack(volume);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to pack the volume: %d\n", -ret);
|
2011-05-01 01:24:48 +02:00
|
|
|
goto errout_with_name;
|
2011-04-30 19:29:40 +02:00
|
|
|
}
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
/* After packing the volume, froffset will be updated to point to the
|
|
|
|
* new free flash region. Try again.
|
|
|
|
*/
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
packed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop until the inode name is configured or until a failure occurs.
|
2013-12-02 01:22:33 +01:00
|
|
|
* Note that nothing is written to FLASH.
|
2011-04-30 19:29:40 +02:00
|
|
|
*/
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
for (; ; )
|
2011-04-30 19:29:40 +02:00
|
|
|
{
|
|
|
|
/* File a valid location to position the inode name. Start with the
|
|
|
|
* first byte in the free FLASH region.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = nxffs_nampos(volume, wrfile, namlen);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
/* Find a region of memory in the block that is fully erased */
|
|
|
|
|
|
|
|
ret = nxffs_namerased(volume, wrfile, namlen);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
2011-05-04 16:20:52 +02:00
|
|
|
/* Valid memory for the inode header was found. Write the
|
|
|
|
* inode name to this location.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = nxffs_wrname(volume, &wrfile->ofile.entry, namlen);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to write the inode name: %d\n", -ret);
|
2011-05-04 16:20:52 +02:00
|
|
|
goto errout_with_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then just break out of the loop reporting success. Note
|
|
|
|
* that the alllocated inode name string is retained; it
|
|
|
|
* will be needed later to calculate the inode CRC.
|
2011-04-30 19:29:40 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no valid memory is found searching to the end of the volume,
|
|
|
|
* then -ENOSPC will be returned. Other errors are not handled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret != -ENOSPC || packed)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to find inode name memory: %d\n", -ret);
|
2011-05-01 01:24:48 +02:00
|
|
|
goto errout_with_name;
|
2011-04-30 19:29:40 +02:00
|
|
|
}
|
2011-04-28 23:05:39 +02:00
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
/* -ENOSPC is a special case.. It means that the volume is full.
|
|
|
|
* Try to pack the volume in order to free up some space.
|
|
|
|
*/
|
2011-04-28 23:05:39 +02:00
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
ret = nxffs_pack(volume);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Failed to pack the volume: %d\n", -ret);
|
2011-05-01 01:24:48 +02:00
|
|
|
goto errout_with_name;
|
2011-04-30 19:29:40 +02:00
|
|
|
}
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
/* After packing the volume, froffset will be updated to point to the
|
|
|
|
* new free flash region. Try again.
|
|
|
|
*/
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
packed = true;
|
|
|
|
}
|
2011-04-28 23:05:39 +02:00
|
|
|
|
|
|
|
/* Add the open file structure to the head of the list of open files */
|
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
wrfile->ofile.flink = volume->ofiles;
|
|
|
|
volume->ofiles = &wrfile->ofile;
|
2011-04-28 23:05:39 +02:00
|
|
|
|
|
|
|
/* Indicate that the volume is open for writing and return the open file
|
2022-09-06 08:18:45 +02:00
|
|
|
* instance. Releasing lock allows other readers while the write is
|
2011-05-01 16:48:27 +02:00
|
|
|
* in progress. But wrsem is still held for this open file, preventing
|
|
|
|
* any further writers until this inode is closed.s
|
2011-04-28 23:05:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
*ppofile = &wrfile->ofile;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&volume->lock);
|
2011-04-28 23:05:39 +02:00
|
|
|
return OK;
|
2011-04-30 19:29:40 +02:00
|
|
|
|
2011-05-01 01:24:48 +02:00
|
|
|
errout_with_name:
|
2023-08-08 00:11:20 +02:00
|
|
|
lib_free(wrfile->ofile.entry.name);
|
2011-04-30 19:29:40 +02:00
|
|
|
errout_with_ofile:
|
2013-04-30 23:54:02 +02:00
|
|
|
#ifndef CONFIG_NXFFS_PREALLOCATED
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(wrfile);
|
2011-04-30 22:06:23 +02:00
|
|
|
#endif
|
2011-05-01 16:48:27 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&volume->lock);
|
2011-05-01 16:48:27 +02:00
|
|
|
errout_with_wrsem:
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&volume->wrsem);
|
2011-04-30 19:29:40 +02:00
|
|
|
errout:
|
|
|
|
return ret;
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_rdopen
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Open an existing file for reading.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline int nxffs_rdopen(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR const char *name,
|
|
|
|
FAR struct nxffs_ofile_s **ppofile)
|
|
|
|
{
|
|
|
|
FAR struct nxffs_ofile_s *ofile;
|
|
|
|
int ret;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Get exclusive access to the volume. Note that the volume lock
|
2011-05-01 16:48:27 +02:00
|
|
|
* protects the open file list.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&volume->lock);
|
2011-05-01 16:48:27 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2023-04-06 08:16:37 +02:00
|
|
|
ferr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2011-05-01 16:48:27 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2011-04-28 23:05:39 +02:00
|
|
|
/* Check if the file has already been opened (for reading) */
|
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
ofile = nxffs_findofile(volume, name);
|
2011-04-28 23:05:39 +02:00
|
|
|
if (ofile)
|
|
|
|
{
|
|
|
|
/* The file is already open.
|
|
|
|
* Limitation: Files cannot be open both for reading and writing.
|
|
|
|
*/
|
|
|
|
|
2011-05-02 18:14:20 +02:00
|
|
|
if ((ofile->oflags & O_WROK) != 0)
|
2011-04-28 23:05:39 +02:00
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: File is open for writing\n");
|
2011-05-01 16:48:27 +02:00
|
|
|
ret = -ENOSYS;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Just increment the reference count on the ofile */
|
|
|
|
|
|
|
|
ofile->crefs++;
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("crefs: %d\n", ofile->crefs);
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The file has not yet been opened.
|
|
|
|
* Limitation: The file must exist. We do not support creation of files
|
|
|
|
* read-only.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Not already open.. create a new open structure */
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2020-11-22 01:43:25 +01:00
|
|
|
ofile = (FAR struct nxffs_ofile_s *)
|
|
|
|
kmm_zalloc(sizeof(struct nxffs_ofile_s));
|
2011-04-28 23:05:39 +02:00
|
|
|
if (!ofile)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: ofile allocation failed\n");
|
2011-05-01 16:48:27 +02:00
|
|
|
ret = -ENOMEM;
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the open file state structure */
|
|
|
|
|
2011-05-02 18:14:20 +02:00
|
|
|
ofile->crefs = 1;
|
|
|
|
ofile->oflags = O_RDOK;
|
2011-04-28 23:05:39 +02:00
|
|
|
|
|
|
|
/* Find the file on this volume associated with this file name */
|
|
|
|
|
|
|
|
ret = nxffs_findinode(volume, name, &ofile->entry);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Inode '%s' not found: %d\n", name, -ret);
|
2011-05-01 16:48:27 +02:00
|
|
|
goto errout_with_ofile;
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the open file structure to the head of the list of open files */
|
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
ofile->flink = volume->ofiles;
|
|
|
|
volume->ofiles = ofile;
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the open file state structure */
|
|
|
|
|
|
|
|
*ppofile = ofile;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&volume->lock);
|
2011-04-28 23:05:39 +02:00
|
|
|
return OK;
|
2011-05-01 16:48:27 +02:00
|
|
|
|
|
|
|
errout_with_ofile:
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(ofile);
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&volume->lock);
|
2011-05-01 16:48:27 +02:00
|
|
|
errout:
|
|
|
|
return ret;
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2011-05-02 03:30:30 +02:00
|
|
|
* Name: nxffs_remofile
|
2011-04-29 00:24:10 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2011-05-02 03:30:30 +02:00
|
|
|
* Remove an entry from the open file list.
|
2011-04-29 00:24:10 +02:00
|
|
|
*
|
2011-04-28 23:05:39 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2011-05-02 03:30:30 +02:00
|
|
|
static inline void nxffs_remofile(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR struct nxffs_ofile_s *ofile)
|
2011-04-28 23:05:39 +02:00
|
|
|
{
|
|
|
|
FAR struct nxffs_ofile_s *prev;
|
|
|
|
FAR struct nxffs_ofile_s *curr;
|
|
|
|
|
|
|
|
/* Find the open file structure to be removed */
|
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
for (prev = NULL, curr = volume->ofiles;
|
2011-04-28 23:05:39 +02:00
|
|
|
curr && curr != ofile;
|
|
|
|
prev = curr, curr = curr->flink);
|
|
|
|
|
|
|
|
/* Was it found? */
|
|
|
|
|
|
|
|
if (curr)
|
|
|
|
{
|
|
|
|
/* Yes.. at the head of the list? */
|
|
|
|
|
|
|
|
if (prev)
|
|
|
|
{
|
|
|
|
prev->flink = ofile->flink;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-30 19:29:40 +02:00
|
|
|
volume->ofiles = ofile->flink;
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
2011-05-02 03:30:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: Open inode %p not found\n", ofile);
|
2011-05-02 03:30:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_freeofile
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free resources held by an open file.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2011-04-28 23:05:39 +02:00
|
|
|
|
2011-05-02 03:30:30 +02:00
|
|
|
static inline void nxffs_freeofile(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR struct nxffs_ofile_s *ofile)
|
|
|
|
{
|
|
|
|
/* Release the open file entry */
|
2011-04-28 23:05:39 +02:00
|
|
|
|
2011-05-02 03:30:30 +02:00
|
|
|
nxffs_freeentry(&ofile->entry);
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-05-02 03:30:30 +02:00
|
|
|
/* Then free the open file container (unless this the pre-alloated
|
|
|
|
* write-only open file container)
|
|
|
|
*/
|
2011-04-30 22:06:23 +02:00
|
|
|
|
2013-04-30 23:54:02 +02:00
|
|
|
#ifdef CONFIG_NXFFS_PREALLOCATED
|
2015-10-11 19:39:29 +02:00
|
|
|
if ((FAR struct nxffs_wrfile_s *)ofile != &g_wrfile)
|
2011-04-30 22:06:23 +02:00
|
|
|
#endif
|
2011-04-28 23:05:39 +02:00
|
|
|
{
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(ofile);
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_wrclose
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform special operations when a file is closed:
|
|
|
|
* 1. Write the file block header
|
|
|
|
* 2. Remove any file with the same name that was discovered when the
|
|
|
|
* file was open for writing, and finally,
|
|
|
|
* 3. Write the new file inode.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2011-04-30 19:29:40 +02:00
|
|
|
* volume - Describes the NXFFS volume
|
|
|
|
* wrfile - Describes the state of the open file
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-05-04 00:10:52 +02:00
|
|
|
static inline int nxffs_wrclose(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR struct nxffs_wrfile_s *wrfile)
|
2011-04-30 19:29:40 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2011-05-01 20:16:03 +02:00
|
|
|
/* Is there an unfinalized write data? */
|
2011-04-30 19:29:40 +02:00
|
|
|
|
2011-05-01 20:16:03 +02:00
|
|
|
if (wrfile->datlen > 0)
|
2011-04-30 19:29:40 +02:00
|
|
|
{
|
2011-05-01 20:16:03 +02:00
|
|
|
/* Yes.. Write the final file block header */
|
|
|
|
|
|
|
|
ret = nxffs_wrblkhdr(volume, wrfile);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-11-22 01:43:25 +01:00
|
|
|
ferr("ERROR: Failed to write the final block of the file: %d\n",
|
|
|
|
-ret);
|
2011-05-01 20:16:03 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
2011-04-30 19:29:40 +02:00
|
|
|
}
|
|
|
|
|
2011-05-02 03:30:30 +02:00
|
|
|
/* Truncation is implemented by writing the new file, then deleting the
|
|
|
|
* older version of the file. Note that we removed the entry from the
|
|
|
|
* open file list earlier in the close sequence; this will prevent the
|
|
|
|
* open file check from failing when we remove the old version of the
|
|
|
|
* file.
|
|
|
|
*/
|
|
|
|
|
2011-05-01 01:24:48 +02:00
|
|
|
if (wrfile->truncate && wrfile->ofile.entry.name)
|
2011-04-30 19:29:40 +02:00
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Removing old file: %s\n", wrfile->ofile.entry.name);
|
2011-04-30 19:29:40 +02:00
|
|
|
|
|
|
|
ret = nxffs_rminode(volume, wrfile->ofile.entry.name);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
ferr("ERROR: nxffs_rminode failed: %d\n", -ret);
|
2011-04-30 19:29:40 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-04 00:10:52 +02:00
|
|
|
/* Write the inode header to FLASH */
|
2011-04-30 19:29:40 +02:00
|
|
|
|
2011-05-04 00:10:52 +02:00
|
|
|
ret = nxffs_wrinode(volume, &wrfile->ofile.entry);
|
2011-04-30 19:29:40 +02:00
|
|
|
|
|
|
|
/* The volume is now available for other writers */
|
|
|
|
|
|
|
|
errout:
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&volume->wrsem);
|
2011-04-30 19:29:40 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-04-28 23:05:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_findofile
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Search the list of already opened files to see if the inode of this
|
|
|
|
* name is one of the opened files.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* name - The name of the inode to check.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* If an inode of this name is found in the list of opened inodes, then
|
|
|
|
* a reference to the open file structure is returned. NULL is returned
|
|
|
|
* otherwise.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
FAR struct nxffs_ofile_s *nxffs_findofile(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR const char *name)
|
2011-04-28 23:05:39 +02:00
|
|
|
{
|
|
|
|
FAR struct nxffs_ofile_s *ofile;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Check every open file. Note that the volume lock protects the
|
2011-04-28 23:05:39 +02:00
|
|
|
* list of open files.
|
|
|
|
*/
|
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
for (ofile = volume->ofiles; ofile; ofile = ofile->flink)
|
2011-04-28 23:05:39 +02:00
|
|
|
{
|
|
|
|
/* Check for a name match */
|
|
|
|
|
|
|
|
if (strcmp(name, ofile->entry.name) == 0)
|
|
|
|
{
|
|
|
|
return ofile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-05-05 23:45:08 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_findwriter
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Search the list of already opened files and return the open file
|
|
|
|
* instance for the write.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* volume - Describes the NXFFS volume.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* If there is an active writer of the volume, its open file instance is
|
|
|
|
* returned. NULL is returned otherwise.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-11-22 01:43:25 +01:00
|
|
|
FAR struct nxffs_wrfile_s *nxffs_findwriter(
|
|
|
|
FAR struct nxffs_volume_s *volume)
|
2011-05-05 23:45:08 +02:00
|
|
|
{
|
|
|
|
/* We can tell if the write is in-use because it will have an allocated
|
|
|
|
* name attached.
|
|
|
|
*/
|
|
|
|
|
2013-04-30 23:54:02 +02:00
|
|
|
#ifdef CONFIG_NXFFS_PREALLOCATED
|
2011-05-05 23:45:08 +02:00
|
|
|
return g_wrfile.ofile.entry.name != NULL ? &g_wrfile : NULL;
|
|
|
|
#else
|
|
|
|
# error "Missing implementation"
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-04-28 23:05:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_open
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is the standard mountpoint open method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_open(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode)
|
|
|
|
{
|
|
|
|
FAR struct nxffs_volume_s *volume;
|
|
|
|
FAR struct nxffs_ofile_s *ofile = NULL;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Open '%s'\n", relpath);
|
2011-04-28 23:05:39 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(filep->f_priv == NULL);
|
2011-04-28 23:05:39 +02:00
|
|
|
|
|
|
|
/* Get the mountpoint private data from the NuttX inode reference in the
|
|
|
|
* file structure
|
|
|
|
*/
|
|
|
|
|
2023-08-29 06:16:49 +02:00
|
|
|
volume = filep->f_inode->i_private;
|
2011-04-28 23:05:39 +02:00
|
|
|
DEBUGASSERT(volume != NULL);
|
|
|
|
|
|
|
|
/* Limitation: A file must be opened for reading or writing, but not both.
|
2013-01-14 20:22:32 +01:00
|
|
|
* There is no general way of extending the size of a file. Extending the
|
2011-04-28 23:05:39 +02:00
|
|
|
* file size of possible if the file to be extended is the last in the
|
|
|
|
* sequence on FLASH, but since that case is not the general case, no file
|
|
|
|
* extension is supported.
|
|
|
|
*/
|
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
switch (oflags & (O_WROK | O_RDOK))
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
ferr("ERROR: One of O_WRONLY/O_RDONLY must be provided\n");
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
case O_WROK:
|
|
|
|
ret = nxffs_wropen(volume, relpath, oflags, &ofile);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case O_RDOK:
|
|
|
|
ret = nxffs_rdopen(volume, relpath, &ofile);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case O_WROK | O_RDOK:
|
|
|
|
ferr("ERROR: O_RDWR is not supported\n");
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
2011-04-28 23:05:39 +02:00
|
|
|
|
2011-05-01 16:48:27 +02:00
|
|
|
/* Save the reference to the open-specific state in filep->f_priv */
|
2011-04-28 23:05:39 +02:00
|
|
|
|
2011-05-01 16:48:27 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
filep->f_priv = ofile;
|
|
|
|
}
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-04-28 23:05:39 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-14 20:22:32 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: binfs_dup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Duplicate open file data in the new file structure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_dup(FAR const struct file *oldp, FAR struct file *newp)
|
|
|
|
{
|
2016-06-13 21:16:03 +02:00
|
|
|
#ifdef CONFIG_DEBUG_ASSERTIONS
|
2013-01-14 20:22:32 +01:00
|
|
|
FAR struct nxffs_volume_s *volume;
|
|
|
|
#endif
|
|
|
|
FAR struct nxffs_ofile_s *ofile;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Dup %p->%p\n", oldp, newp);
|
2013-01-14 20:22:32 +01:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
2016-06-13 21:16:03 +02:00
|
|
|
#ifdef CONFIG_DEBUG_ASSERTIONS
|
2013-01-14 20:22:32 +01:00
|
|
|
DEBUGASSERT(oldp->f_priv == NULL && oldp->f_inode != NULL);
|
|
|
|
|
|
|
|
/* Get the mountpoint private data from the NuttX inode reference in the
|
|
|
|
* file structure
|
|
|
|
*/
|
|
|
|
|
2023-08-29 06:16:49 +02:00
|
|
|
volume = oldp->f_inode->i_private;
|
2013-01-14 20:22:32 +01:00
|
|
|
DEBUGASSERT(volume != NULL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Recover the open file state from the struct file instance */
|
|
|
|
|
|
|
|
ofile = (FAR struct nxffs_ofile_s *)oldp->f_priv;
|
|
|
|
|
|
|
|
/* I do not think we need exclusive access to the volume to do this.
|
2022-09-06 08:18:45 +02:00
|
|
|
* The volume lock protects the open file list and, hence, would
|
2013-01-14 20:22:32 +01:00
|
|
|
* assure that the ofile is stable. However, it is assumed that the
|
|
|
|
* caller holds a value file descriptor associated with this ofile,
|
|
|
|
* so it should be stable throughout the life of this function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Limitations: I do not think we have to be concerned about the
|
|
|
|
* usual NXFFS file limitations here: dup'ing cannot resulting
|
|
|
|
* in mixed reading and writing to the same file, or multiple
|
|
|
|
* writer to different file.
|
|
|
|
*
|
|
|
|
* I notice that nxffs_wropen will prohibit multiple opens for
|
|
|
|
* writing. But I do not thing that dup'ing a file already opened
|
|
|
|
* for writing suffers from any of these issues.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Just increment the reference count on the ofile */
|
|
|
|
|
|
|
|
ofile->crefs++;
|
2022-09-06 08:18:45 +02:00
|
|
|
newp->f_priv = ofile;
|
2013-01-14 20:22:32 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2011-04-28 23:05:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_close
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is the standard mountpoint close method.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct nxffs_volume_s *volume;
|
|
|
|
FAR struct nxffs_ofile_s *ofile;
|
2014-02-11 01:08:49 +01:00
|
|
|
int ret;
|
2011-04-28 23:05:39 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Closing\n");
|
2011-04-28 23:05:39 +02:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL);
|
2011-04-28 23:05:39 +02:00
|
|
|
|
|
|
|
/* Recover the open file state from the struct file instance */
|
|
|
|
|
|
|
|
ofile = (FAR struct nxffs_ofile_s *)filep->f_priv;
|
|
|
|
|
|
|
|
/* Recover the volume state from the open file */
|
|
|
|
|
2023-08-29 06:16:49 +02:00
|
|
|
volume = filep->f_inode->i_private;
|
2011-04-28 23:05:39 +02:00
|
|
|
DEBUGASSERT(volume != NULL);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Get exclusive access to the volume. Note that the volume lock
|
2011-04-28 23:05:39 +02:00
|
|
|
* protects the open file list.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&volume->lock);
|
2011-04-28 23:05:39 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2023-04-06 08:16:37 +02:00
|
|
|
ferr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2011-04-28 23:05:39 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrement the reference count on the open file */
|
|
|
|
|
2011-04-30 19:29:40 +02:00
|
|
|
ret = OK;
|
2011-04-28 23:05:39 +02:00
|
|
|
if (ofile->crefs == 1)
|
|
|
|
{
|
2011-05-02 03:30:30 +02:00
|
|
|
/* Decrementing the reference count would take it zero.
|
|
|
|
*
|
|
|
|
* Remove the entry from the open file list. We do this early
|
|
|
|
* to avoid some chick-and-egg problems with file truncation.
|
2011-04-28 23:05:39 +02:00
|
|
|
*/
|
|
|
|
|
2011-05-02 03:30:30 +02:00
|
|
|
nxffs_remofile(volume, ofile);
|
|
|
|
|
|
|
|
/* Handle special finalization of the write operation. */
|
|
|
|
|
2011-05-02 18:14:20 +02:00
|
|
|
if ((ofile->oflags & O_WROK) != 0)
|
2011-04-30 19:29:40 +02:00
|
|
|
{
|
|
|
|
ret = nxffs_wrclose(volume, (FAR struct nxffs_wrfile_s *)ofile);
|
|
|
|
}
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Release all resources held by the open file */
|
2011-04-30 19:29:40 +02:00
|
|
|
|
|
|
|
nxffs_freeofile(volume, ofile);
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Just decrement the reference count */
|
|
|
|
|
|
|
|
ofile->crefs--;
|
|
|
|
}
|
|
|
|
|
2013-01-16 15:14:14 +01:00
|
|
|
filep->f_priv = NULL;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&volume->lock);
|
2013-01-16 15:14:14 +01:00
|
|
|
|
2011-04-28 23:05:39 +02:00
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
2011-05-04 00:10:52 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_wrinode
|
|
|
|
*
|
|
|
|
* Description:
|
2011-05-04 16:20:52 +02:00
|
|
|
* Write the inode header (only to FLASH. This is done in two contexts:
|
2011-05-04 00:10:52 +02:00
|
|
|
*
|
|
|
|
* 1. When an inode is closed, or
|
|
|
|
* 2. As part of the file system packing logic when an inode is moved.
|
|
|
|
*
|
2011-05-04 16:20:52 +02:00
|
|
|
* Note that in either case, the inode name has already been written to
|
|
|
|
* FLASH.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2011-05-04 00:10:52 +02:00
|
|
|
* volume - Describes the NXFFS volume
|
2011-05-05 23:45:08 +02:00
|
|
|
* entry - Describes the inode header to write
|
2011-05-04 00:10:52 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2020-11-22 01:43:25 +01:00
|
|
|
* Zero is returned on success; Otherwise, a negated errno value is
|
|
|
|
* returned indicating the nature of the failure.
|
2011-05-04 00:10:52 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-05-04 16:20:52 +02:00
|
|
|
int nxffs_wrinode(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR struct nxffs_entry_s *entry)
|
2011-05-04 00:10:52 +02:00
|
|
|
{
|
|
|
|
FAR struct nxffs_inode_s *inode;
|
|
|
|
uint32_t crc;
|
|
|
|
int namlen;
|
|
|
|
int ret;
|
|
|
|
|
2011-05-04 16:20:52 +02:00
|
|
|
/* Seek to the inode header position and assure that it is in the volume
|
|
|
|
* cache.
|
2011-05-04 00:10:52 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
nxffs_ioseek(volume, entry->hoffset);
|
|
|
|
ret = nxffs_rdcache(volume, volume->ioblock);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-12-05 06:06:25 +01:00
|
|
|
ferr("ERROR: Failed to read inode header block %jd: %d\n",
|
|
|
|
(intmax_t)volume->ioblock, -ret);
|
2011-05-04 00:10:52 +02:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the length of the inode name */
|
|
|
|
|
|
|
|
namlen = strlen(entry->name);
|
|
|
|
DEBUGASSERT(namlen < CONFIG_NXFFS_MAXNAMLEN); /* This was verified earlier */
|
|
|
|
|
|
|
|
/* Initialize the inode header */
|
|
|
|
|
|
|
|
inode = (FAR struct nxffs_inode_s *)&volume->cache[volume->iooffset];
|
|
|
|
memcpy(inode->magic, g_inodemagic, NXFFS_MAGICSIZE);
|
|
|
|
|
|
|
|
inode->state = CONFIG_NXFFS_ERASEDSTATE;
|
|
|
|
inode->namlen = namlen;
|
|
|
|
|
|
|
|
nxffs_wrle32(inode->noffs, entry->noffset);
|
|
|
|
nxffs_wrle32(inode->doffs, entry->doffset);
|
|
|
|
nxffs_wrle32(inode->utc, entry->utc);
|
|
|
|
nxffs_wrle32(inode->crc, 0);
|
|
|
|
nxffs_wrle32(inode->datlen, entry->datlen);
|
|
|
|
|
|
|
|
/* Calculate the CRC */
|
|
|
|
|
|
|
|
crc = crc32((FAR const uint8_t *)inode, SIZEOF_NXFFS_INODE_HDR);
|
|
|
|
crc = crc32part((FAR const uint8_t *)entry->name, namlen, crc);
|
|
|
|
|
|
|
|
/* Finish the inode header */
|
|
|
|
|
|
|
|
inode->state = INODE_STATE_FILE;
|
|
|
|
nxffs_wrle32(inode->crc, crc);
|
|
|
|
|
2011-05-04 16:20:52 +02:00
|
|
|
/* Write the block with the inode header */
|
2011-05-04 00:10:52 +02:00
|
|
|
|
|
|
|
ret = nxffs_wrcache(volume);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-12-05 06:06:25 +01:00
|
|
|
ferr("ERROR: Failed to write inode header block %jd: %d\n",
|
|
|
|
(intmax_t)volume->ioblock, -ret);
|
2011-05-04 00:10:52 +02:00
|
|
|
}
|
2011-05-04 16:20:52 +02:00
|
|
|
|
2011-05-04 00:10:52 +02:00
|
|
|
/* The volume is now available for other writers */
|
|
|
|
|
|
|
|
errout:
|
2017-10-03 23:35:24 +02:00
|
|
|
nxsem_post(&volume->wrsem);
|
2011-05-04 00:10:52 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-05 23:45:08 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_updateinode
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The packing logic has moved an inode. Check if any open files are using
|
|
|
|
* this inode and, if so, move the data in the open file structure as well.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2011-05-05 23:45:08 +02:00
|
|
|
* volume - Describes the NXFFS volume
|
|
|
|
* entry - Describes the new inode entry
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2020-11-22 01:43:25 +01:00
|
|
|
* Zero is returned on success; Otherwise, a negated errno value is
|
|
|
|
* returned indicating the nature of the failure.
|
2011-05-05 23:45:08 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_updateinode(FAR struct nxffs_volume_s *volume,
|
|
|
|
FAR struct nxffs_entry_s *entry)
|
|
|
|
{
|
|
|
|
FAR struct nxffs_ofile_s *ofile;
|
|
|
|
|
|
|
|
/* Find the open inode structure matching this name */
|
|
|
|
|
|
|
|
ofile = nxffs_findofile(volume, entry->name);
|
|
|
|
if (ofile)
|
|
|
|
{
|
|
|
|
/* Yes.. the file is open. Update the FLASH offsets to inode headers */
|
|
|
|
|
|
|
|
ofile->entry.hoffset = entry->hoffset;
|
|
|
|
ofile->entry.noffset = entry->noffset;
|
|
|
|
ofile->entry.doffset = entry->doffset;
|
|
|
|
}
|
2013-12-02 01:22:33 +01:00
|
|
|
|
2011-05-05 23:45:08 +02:00
|
|
|
return OK;
|
|
|
|
}
|