2011-04-29 05:05:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/nxffs/nxffs_stat.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>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
2017-02-13 22:22:05 +08:00
|
|
|
#include <errno.h>
|
2011-04-29 05:05:39 +08:00
|
|
|
#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_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-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;
|
2017-10-05 05:22:27 +08:00
|
|
|
ret = nxsem_wait(&volume->exclsem);
|
|
|
|
if (ret < 0)
|
2011-04-29 05:05:39 +08:00
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
2013-12-03 09:00:59 +08:00
|
|
|
/* Fill in the statfs info
|
|
|
|
*
|
|
|
|
* REVISIT: Need f_bfree, f_bavail, f_files, f_ffree calculation
|
|
|
|
*/
|
2011-04-29 05:05:39 +08:00
|
|
|
|
|
|
|
memset(buf, 0, sizeof(struct statfs));
|
|
|
|
buf->f_type = NXFFS_MAGIC;
|
|
|
|
buf->f_bsize = volume->geo.blocksize;
|
2011-05-01 01:29:40 +08:00
|
|
|
buf->f_blocks = volume->nblocks;
|
2019-10-28 01:48:14 +08:00
|
|
|
buf->f_namelen = volume->geo.blocksize - SIZEOF_NXFFS_BLOCK_HDR -
|
|
|
|
SIZEOF_NXFFS_INODE_HDR;
|
2011-04-29 05:05:39 +08:00
|
|
|
ret = OK;
|
|
|
|
|
2017-10-04 05:35:24 +08:00
|
|
|
nxsem_post(&volume->exclsem);
|
2017-10-05 05:22:27 +08:00
|
|
|
|
2011-04-29 05:05:39 +08: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-12 01:59:51 +08:00
|
|
|
finfo("Entry\n");
|
2011-04-29 05:05:39 +08:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt && mountpt->i_private && buf);
|
|
|
|
|
|
|
|
/* Get the mountpoint private data from the NuttX inode structure */
|
|
|
|
|
|
|
|
volume = mountpt->i_private;
|
2017-10-05 05:22:27 +08:00
|
|
|
ret = nxsem_wait(&volume->exclsem);
|
2011-04-29 05:05:39 +08: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 08:58:50 +08:00
|
|
|
ferr("ERROR: Inode '%s' not found: %d\n", relpath, -ret);
|
2011-04-29 05:05:39 +08:00
|
|
|
goto errout_with_semaphore;
|
|
|
|
}
|
|
|
|
|
2017-02-13 22:22:05 +08:00
|
|
|
/* Return status information based on the directory entry */
|
|
|
|
|
2019-10-28 01:48:14 +08:00
|
|
|
buf->st_blocks = entry.datlen /
|
|
|
|
(volume->geo.blocksize - SIZEOF_NXFFS_BLOCK_HDR);
|
2015-10-12 01:39:29 +08:00
|
|
|
buf->st_mode = S_IFREG | S_IXOTH | S_IXGRP | S_IXUSR;
|
2011-04-29 05:05:39 +08:00
|
|
|
buf->st_size = entry.datlen;
|
|
|
|
buf->st_atime = entry.utc;
|
|
|
|
buf->st_mtime = entry.utc;
|
|
|
|
buf->st_ctime = entry.utc;
|
2014-03-06 22:01:56 +08:00
|
|
|
|
|
|
|
/* Free inode resources */
|
|
|
|
|
|
|
|
nxffs_freeentry(&entry);
|
2011-04-29 05:05:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* It's a read/execute-only directory name */
|
|
|
|
|
2015-10-12 01:39:29 +08:00
|
|
|
buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR | S_IXOTH |
|
|
|
|
S_IXGRP | S_IXUSR;
|
2011-04-29 05:05:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
|
|
|
|
errout_with_semaphore:
|
2017-10-04 05:35:24 +08:00
|
|
|
nxsem_post(&volume->exclsem);
|
2017-10-05 05:22:27 +08:00
|
|
|
|
2011-04-29 05:05:39 +08:00
|
|
|
errout:
|
|
|
|
return ret;
|
|
|
|
}
|
2017-02-13 03:42:27 +08: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 22:22:05 +08:00
|
|
|
FAR struct nxffs_volume_s *volume;
|
|
|
|
FAR struct nxffs_ofile_s *ofile;
|
|
|
|
int ret;
|
|
|
|
|
2020-11-22 08:58:50 +08:00
|
|
|
finfo("Buf %p\n", buf);
|
2017-02-13 22:22:05 +08: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);
|
|
|
|
|
|
|
|
/* Get exclusive access to the volume. Note that the volume exclsem
|
|
|
|
* protects the open file list.
|
|
|
|
*/
|
|
|
|
|
2017-10-05 05:22:27 +08:00
|
|
|
ret = nxsem_wait(&volume->exclsem);
|
2017-02-13 22:22:05 +08:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2017-10-05 05:22:27 +08:00
|
|
|
ferr("ERROR: nxsem_wait failed: %d\n", ret);
|
|
|
|
return ret;
|
2017-02-13 22:22:05 +08: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;
|
|
|
|
|
2017-10-04 05:35:24 +08:00
|
|
|
nxsem_post(&volume->exclsem);
|
2017-02-13 22:22:05 +08:00
|
|
|
return OK;
|
|
|
|
}
|