2011-04-28 23:05:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/nxffs/nxffs_stat.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>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
2017-02-13 15:22:05 +01:00
|
|
|
#include <errno.h>
|
2011-04-28 23:05:39 +02:00
|
|
|
#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_statfs
|
|
|
|
*
|
|
|
|
* Description: Return filesystem statistics
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf)
|
|
|
|
{
|
|
|
|
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;
|
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-04-28 23:05:39 +02:00
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2013-12-03 02:00:59 +01:00
|
|
|
/* Fill in the statfs info
|
|
|
|
*
|
|
|
|
* REVISIT: Need f_bfree, f_bavail, f_files, f_ffree calculation
|
|
|
|
*/
|
2011-04-28 23:05:39 +02:00
|
|
|
|
|
|
|
buf->f_type = NXFFS_MAGIC;
|
|
|
|
buf->f_bsize = volume->geo.blocksize;
|
2011-04-30 19:29:40 +02:00
|
|
|
buf->f_blocks = volume->nblocks;
|
2019-10-27 18:48:14 +01:00
|
|
|
buf->f_namelen = volume->geo.blocksize - SIZEOF_NXFFS_BLOCK_HDR -
|
|
|
|
SIZEOF_NXFFS_INODE_HDR;
|
2011-04-28 23:05:39 +02:00
|
|
|
ret = OK;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&volume->lock);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
2011-04-28 23:05:39 +02:00
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_stat
|
|
|
|
*
|
|
|
|
* Description: Return information about a file or directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_stat(FAR struct inode *mountpt, FAR const char *relpath,
|
|
|
|
FAR struct stat *buf)
|
|
|
|
{
|
|
|
|
FAR struct nxffs_volume_s *volume;
|
|
|
|
struct nxffs_entry_s entry;
|
|
|
|
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 && buf);
|
|
|
|
|
|
|
|
/* Get the mountpoint private data from the NuttX inode structure */
|
|
|
|
|
|
|
|
volume = mountpt->i_private;
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&volume->lock);
|
2011-04-28 23:05:39 +02:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the return stat instance */
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(struct stat));
|
|
|
|
buf->st_blksize = volume->geo.blocksize;
|
|
|
|
|
|
|
|
/* The requested directory must be the volume-relative "root" directory */
|
|
|
|
|
|
|
|
if (relpath && relpath[0] != '\0')
|
|
|
|
{
|
|
|
|
/* Not the top directory.. find the NXFFS inode with this name */
|
|
|
|
|
|
|
|
ret = nxffs_findinode(volume, relpath, &entry);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-11-22 01:58:50 +01:00
|
|
|
ferr("ERROR: Inode '%s' not found: %d\n", relpath, -ret);
|
2022-09-06 08:18:45 +02:00
|
|
|
goto errout_with_lock;
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
2017-02-13 15:22:05 +01:00
|
|
|
/* Return status information based on the directory entry */
|
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
buf->st_blocks = entry.datlen /
|
|
|
|
(volume->geo.blocksize - SIZEOF_NXFFS_BLOCK_HDR);
|
2015-10-11 19:39:29 +02:00
|
|
|
buf->st_mode = S_IFREG | S_IXOTH | S_IXGRP | S_IXUSR;
|
2011-04-28 23:05:39 +02:00
|
|
|
buf->st_size = entry.datlen;
|
|
|
|
buf->st_atime = entry.utc;
|
|
|
|
buf->st_mtime = entry.utc;
|
|
|
|
buf->st_ctime = entry.utc;
|
2014-03-06 15:01:56 +01:00
|
|
|
|
|
|
|
/* Free inode resources */
|
|
|
|
|
|
|
|
nxffs_freeentry(&entry);
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* It's a read/execute-only directory name */
|
|
|
|
|
2015-10-11 19:39:29 +02:00
|
|
|
buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR | S_IXOTH |
|
|
|
|
S_IXGRP | S_IXUSR;
|
2011-04-28 23:05:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
errout_with_lock:
|
|
|
|
nxmutex_unlock(&volume->lock);
|
2017-10-04 23:22:27 +02:00
|
|
|
|
2011-04-28 23:05:39 +02:00
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
2017-02-12 20:42:27 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxffs_fstat
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Obtain information about an open file associated with the file
|
|
|
|
* descriptor 'fd', and will write it to the area pointed to by 'buf'.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxffs_fstat(FAR const struct file *filep, FAR struct stat *buf)
|
|
|
|
{
|
2017-02-13 15:22:05 +01:00
|
|
|
FAR struct nxffs_volume_s *volume;
|
|
|
|
FAR struct nxffs_ofile_s *ofile;
|
|
|
|
int ret;
|
|
|
|
|
2020-11-22 01:58:50 +01:00
|
|
|
finfo("Buf %p\n", buf);
|
2017-02-13 15:22:05 +01:00
|
|
|
DEBUGASSERT(filep != NULL && buf != NULL);
|
|
|
|
|
|
|
|
/* Recover the open file state from the struct file instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
|
|
|
|
ofile = (FAR struct nxffs_ofile_s *)filep->f_priv;
|
|
|
|
|
|
|
|
/* Recover the volume state from the open file */
|
|
|
|
|
|
|
|
volume = (FAR struct nxffs_volume_s *)filep->f_inode->i_private;
|
|
|
|
DEBUGASSERT(volume != NULL);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Get exclusive access to the volume. Note that the volume lock
|
2017-02-13 15:22:05 +01:00
|
|
|
* protects the open file list.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&volume->lock);
|
2017-02-13 15:22:05 +01:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2023-04-06 08:16:37 +02:00
|
|
|
ferr("ERROR: nxmutex_lock failed: %d\n", ret);
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2017-02-13 15:22:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return status information based on the directory entry */
|
|
|
|
|
|
|
|
buf->st_blocks = ofile->entry.datlen /
|
|
|
|
(volume->geo.blocksize - SIZEOF_NXFFS_BLOCK_HDR);
|
|
|
|
buf->st_mode = S_IFREG | S_IXOTH | S_IXGRP | S_IXUSR;
|
|
|
|
buf->st_size = ofile->entry.datlen;
|
|
|
|
buf->st_atime = ofile->entry.utc;
|
|
|
|
buf->st_mtime = ofile->entry.utc;
|
|
|
|
buf->st_ctime = ofile->entry.utc;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&volume->lock);
|
2017-02-13 15:22:05 +01:00
|
|
|
return OK;
|
|
|
|
}
|