Skip to content

Commit

Permalink
smartfs: add support for FIOC_FILEPATH ioctl
Browse files Browse the repository at this point in the history
The FIOC_FILEPATH ioctl call is required if smartfs is to be used
together with inotify monitoring system. This implements the
call support to smartfs file system. The path to the file has to
be stored in smartfs_ofile_s structure during file open (and is freed
during close) as smartfs currently is not able to obtain the path
knowing only the file node. The full path is concatenated with the file
name and creates the full path needed for inotify to detect whether
the file is on the watchlist.

Signed-off-by: Michal Lenc <[email protected]>
  • Loading branch information
michallenc committed Sep 26, 2024
1 parent 9afcd34 commit 734dbb6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions fs/smartfs/smartfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ struct smartfs_ofile_s
* a seek, or more data is written that
* causes the sector to change.
*/
char path[1]; /* The full path to the file */
};

/* This structure represents the overall mountpoint state. An instance of
Expand Down
45 changes: 42 additions & 3 deletions fs/smartfs/smartfs_smart.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <nuttx/mtd/mtd.h>
#include <nuttx/fs/smart.h>

#include "inode/inode.h"
#include "smartfs.h"

/****************************************************************************
Expand Down Expand Up @@ -181,6 +182,7 @@ static int smartfs_open(FAR struct file *filep, FAR const char *relpath,
FAR struct smartfs_mountpt_s *fs;
int ret;
uint16_t parentdirsector;
size_t pathlen;
FAR const char *filename;
FAR struct smartfs_ofile_s *sf;
#ifdef CONFIG_SMARTFS_USE_SECTOR_BUFFER
Expand Down Expand Up @@ -210,13 +212,20 @@ static int smartfs_open(FAR struct file *filep, FAR const char *relpath,

/* Locate the directory entry for this path */

sf = kmm_malloc(sizeof *sf);
pathlen = strlen(relpath) + 1;
sf = kmm_malloc(sizeof(*sf) + pathlen - 1);
if (sf == NULL)
{
ret = -ENOMEM;
goto errout_with_lock;
}

/* Save the full path to smartfs_ofile_s stuctrue. This is needed
* to FIOC_FILEPATH ioctlc all support.
*/

memcpy(sf->path, relpath, pathlen);

/* Allocate a sector buffer if CRC enabled in the MTD layer */

#ifdef CONFIG_SMARTFS_USE_SECTOR_BUFFER
Expand Down Expand Up @@ -997,9 +1006,39 @@ static off_t smartfs_seek(FAR struct file *filep, off_t offset, int whence)

static int smartfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
/* We don't use any ioctls */
FAR struct smartfs_ofile_s *priv;
FAR struct inode *inode;
int ret;

/* Recover our private data from the struct file instance */

priv = filep->f_priv;
inode = filep->f_inode;

switch (cmd)
{
case FIOC_FILEPATH:
{
FAR char *path = (FAR char *)(uintptr_t)arg;
ret = inode_getpath(inode, path, PATH_MAX);
if (ret >= 0)
{
size_t len = strlen(path);
if (path[len - 1] != '/')
{
path[len++] = '/';
}

return -ENOSYS;
strlcat(path, priv->path, PATH_MAX - len);
}
}
break;
default:
ret = -ENOSYS;
break;
}

return ret;
}

/****************************************************************************
Expand Down

0 comments on commit 734dbb6

Please sign in to comment.