* fs/rpmsgfs/rpmsgfs.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <debug.h>
#include <limits.h>
#include <nuttx/lib/lib.h>
#include <nuttx/mutex.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/ioctl.h>
#include <nuttx/signal.h>
#include "rpmsgfs.h"
#include "fs_heap.h"
* Pre-processor Definitions
****************************************************************************/
#define RPMSGFS_RETRY_DELAY_MS 10
* Private Types
****************************************************************************/
struct rpmsgfs_dir_s
{
struct fs_dirent_s base;
FAR void *dir;
};
* is protected by the volume semaphore.
*/
struct rpmsgfs_ofile_s
{
struct rpmsgfs_ofile_s *fnext;
int16_t crefs;
mode_t oflags;
int fd;
};
* this structure is retained as inode private data on each mountpoint that
* is mounted with a rpmsgfs filesystem.
*/
struct rpmsgfs_mountpt_s
{
mutex_t fs_lock;
FAR struct rpmsgfs_ofile_s *fs_head;
char fs_root[PATH_MAX];
void *handle;
int timeout;
};
* Private Function Prototypes
****************************************************************************/
static int rpmsgfs_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode);
static int rpmsgfs_close(FAR struct file *filep);
static ssize_t rpmsgfs_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static ssize_t rpmsgfs_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen);
static off_t rpmsgfs_seek(FAR struct file *filep, off_t offset,
int whence);
static int rpmsgfs_ioctl(FAR struct file *filep, int cmd,
unsigned long arg);
static int rpmsgfs_sync(FAR struct file *filep);
static int rpmsgfs_dup(FAR const struct file *oldp,
FAR struct file *newp);
static int rpmsgfs_fstat(FAR const struct file *filep,
FAR struct stat *buf);
static int rpmsgfs_fchstat(FAR const struct file *filep,
FAR const struct stat *buf, int flags);
static int rpmsgfs_truncate(FAR struct file *filep,
off_t length);
static int rpmsgfs_opendir(FAR struct inode *mountpt,
FAR const char *relpath,
FAR struct fs_dirent_s **dir);
static int rpmsgfs_closedir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir);
static int rpmsgfs_readdir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir,
FAR struct dirent *entry);
static int rpmsgfs_rewinddir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir);
static int rpmsgfs_bind(FAR struct inode *blkdriver,
FAR const void *data, FAR void **handle);
static int rpmsgfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
unsigned int flags);
static int rpmsgfs_statfs(FAR struct inode *mountpt,
FAR struct statfs *buf);
static int rpmsgfs_unlink(FAR struct inode *mountpt,
FAR const char *relpath);
static int rpmsgfs_mkdir(FAR struct inode *mountpt,
FAR const char *relpath, mode_t mode);
static int rpmsgfs_rmdir(FAR struct inode *mountpt, const char *relpath);
static int rpmsgfs_rename(FAR struct inode *mountpt,
FAR const char *oldrelpath,
FAR const char *newrelpath);
static int rpmsgfs_stat(FAR struct inode *mountpt,
FAR const char *relpath, FAR struct stat *buf);
static int rpmsgfs_chstat(FAR struct inode *mountpt,
FAR const char *relpath,
FAR const struct stat *buf, int flags);
* Public Data
****************************************************************************/
* We use the old-fashioned kind of initializers so that this will compile
* with any compiler.
*/
const struct mountpt_operations g_rpmsgfs_operations =
{
rpmsgfs_open,
rpmsgfs_close,
rpmsgfs_read,
rpmsgfs_write,
rpmsgfs_seek,
rpmsgfs_ioctl,
NULL,
rpmsgfs_truncate,
NULL,
NULL,
NULL,
rpmsgfs_sync,
rpmsgfs_dup,
rpmsgfs_fstat,
rpmsgfs_fchstat,
rpmsgfs_opendir,
rpmsgfs_closedir,
rpmsgfs_readdir,
rpmsgfs_rewinddir,
rpmsgfs_bind,
rpmsgfs_unbind,
rpmsgfs_statfs,
rpmsgfs_unlink,
rpmsgfs_mkdir,
rpmsgfs_rmdir,
rpmsgfs_rename,
rpmsgfs_stat,
rpmsgfs_chstat,
};
* Private Functions
****************************************************************************/
* Name: rpmsgfs_mkpath
*
* Description: Build absolute host path from relative NuttX path.
*
****************************************************************************/
static void rpmsgfs_mkpath(FAR struct rpmsgfs_mountpt_s *fs,
FAR const char *relpath,
FAR char *path, int pathlen)
{
int depth = 0;
int first;
int x;
strlcpy(path, fs->fs_root, pathlen);
* mounted path.
*/
x = 0;
while (relpath[x] == '/')
{
x++;
}
first = x;
while (relpath[x] != '\0')
{
if (strncmp(&relpath[x], "..", 2) == 0)
{
depth--;
x += 2;
}
else if (relpath[x] == '/' && relpath[x + 1] != '/' &&
relpath[x + 1] != '\0')
{
depth++;
x++;
}
else
{
x++;
}
}
if (depth >= 0)
{
strlcat(path, &relpath[first], pathlen - strlen(path));
}
while (fs->timeout > 0)
{
struct stat buf;
int ret;
ret = rpmsgfs_client_stat(fs->handle, fs->fs_root, &buf);
if (ret == 0)
{
break;
}
nxsig_usleep(RPMSGFS_RETRY_DELAY_MS * USEC_PER_MSEC);
fs->timeout -= RPMSGFS_RETRY_DELAY_MS;
}
}
* Name: rpmsgfs_open
****************************************************************************/
static int rpmsgfs_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode)
{
FAR struct inode *inode;
FAR struct rpmsgfs_mountpt_s *fs;
FAR struct rpmsgfs_ofile_s *hf;
FAR char *path;
int ret;
DEBUGASSERT((filep->f_priv == NULL) && (filep->f_inode != NULL));
* mountpoint private data from the inode structure
*/
inode = filep->f_inode;
fs = inode->i_private;
DEBUGASSERT(fs != NULL);
path = lib_get_tempbuffer(PATH_MAX);
if (path == NULL)
{
return -ENOMEM;
}
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
lib_put_tempbuffer(path);
return ret;
}
hf = fs_heap_malloc(sizeof *hf);
if (hf == NULL)
{
ret = -ENOMEM;
goto errout_with_lock;
}
rpmsgfs_mkpath(fs, relpath, path, PATH_MAX);
hf->fd = rpmsgfs_client_open(fs->handle, path, oflags, mode);
if (hf->fd < 0)
{
ret = hf->fd;
goto errout_with_buffer;
}
* file.
*/
if ((oflags & (O_APPEND | O_WRONLY)) == (O_APPEND | O_WRONLY))
{
filep->f_pos = rpmsgfs_client_lseek(fs->handle, hf->fd, 0, SEEK_END);
if (filep->f_pos < 0)
{
ret = filep->f_pos;
goto errout_with_buffer;
}
}
filep->f_priv = hf;
* It needs to be there (1) to handle error conditions that effect
* all files, and (2) to inform the umount logic that we are busy
* (but a simple reference count could have done that).
*/
hf->fnext = fs->fs_head;
hf->crefs = 1;
hf->oflags = oflags;
fs->fs_head = hf;
ret = OK;
goto errout_with_lock;
errout_with_buffer:
fs_heap_free(hf);
errout_with_lock:
nxmutex_unlock(&fs->fs_lock);
lib_put_tempbuffer(path);
if (ret == -EINVAL)
{
ret = -EIO;
}
return ret;
}
* Name: rpmsgfs_close
****************************************************************************/
static int rpmsgfs_close(FAR struct file *filep)
{
FAR struct inode *inode;
FAR struct rpmsgfs_mountpt_s *fs;
FAR struct rpmsgfs_ofile_s *hf;
FAR struct rpmsgfs_ofile_s *nextfile;
FAR struct rpmsgfs_ofile_s *prevfile;
int ret;
DEBUGASSERT(filep->f_priv != NULL);
inode = filep->f_inode;
fs = inode->i_private;
hf = filep->f_priv;
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
return ret;
}
* only close if we are.
*/
if (hf->crefs > 1)
{
* reference count and return.
*/
hf->crefs--;
goto okout;
}
nextfile = fs->fs_head;
prevfile = nextfile;
while ((nextfile != hf) && (nextfile != NULL))
{
prevfile = nextfile;
nextfile = nextfile->fnext;
}
if (nextfile != NULL)
{
if (nextfile == fs->fs_head)
{
fs->fs_head = nextfile->fnext;
}
else
{
prevfile->fnext = nextfile->fnext;
}
}
rpmsgfs_client_close(fs->handle, hf->fd);
filep->f_priv = NULL;
fs_heap_free(hf);
okout:
nxmutex_unlock(&fs->fs_lock);
return OK;
}
* Name: rpmsgfs_read
****************************************************************************/
static ssize_t rpmsgfs_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
FAR struct inode *inode;
FAR struct rpmsgfs_mountpt_s *fs;
FAR struct rpmsgfs_ofile_s *hf;
ssize_t ret;
DEBUGASSERT(filep->f_priv != NULL);
hf = filep->f_priv;
inode = filep->f_inode;
fs = inode->i_private;
DEBUGASSERT(fs != NULL);
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
return ret;
}
ret = rpmsgfs_client_read(fs->handle, hf->fd, buffer, buflen);
if (ret > 0)
{
filep->f_pos += ret;
}
nxmutex_unlock(&fs->fs_lock);
return ret;
}
* Name: rpmsgfs_write
****************************************************************************/
static ssize_t rpmsgfs_write(FAR struct file *filep, const char *buffer,
size_t buflen)
{
FAR struct inode *inode;
FAR struct rpmsgfs_mountpt_s *fs;
FAR struct rpmsgfs_ofile_s *hf;
ssize_t ret;
DEBUGASSERT(filep->f_priv != NULL);
hf = filep->f_priv;
inode = filep->f_inode;
fs = inode->i_private;
DEBUGASSERT(fs != NULL);
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
return ret;
}
* write flags.
*/
if ((hf->oflags & O_WROK) == 0)
{
ret = -EACCES;
goto errout_with_lock;
}
ret = rpmsgfs_client_write(fs->handle, hf->fd, buffer, buflen);
if (ret > 0)
{
filep->f_pos += ret;
}
errout_with_lock:
nxmutex_unlock(&fs->fs_lock);
return ret;
}
* Name: rpmsgfs_seek
****************************************************************************/
static off_t rpmsgfs_seek(FAR struct file *filep, off_t offset, int whence)
{
FAR struct inode *inode;
FAR struct rpmsgfs_mountpt_s *fs;
FAR struct rpmsgfs_ofile_s *hf;
off_t ret;
DEBUGASSERT(filep->f_priv != NULL);
hf = filep->f_priv;
inode = filep->f_inode;
fs = inode->i_private;
DEBUGASSERT(fs != NULL);
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
return ret;
}
ret = rpmsgfs_client_lseek(fs->handle, hf->fd, offset, whence);
if (ret >= 0)
{
filep->f_pos = ret;
}
nxmutex_unlock(&fs->fs_lock);
return ret;
}
* Name: rpmsgfs_ioctl
****************************************************************************/
static int rpmsgfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
FAR struct inode *inode;
FAR struct rpmsgfs_mountpt_s *fs;
FAR struct rpmsgfs_ofile_s *hf;
int ret;
DEBUGASSERT(filep->f_priv != NULL);
hf = filep->f_priv;
inode = filep->f_inode;
fs = inode->i_private;
DEBUGASSERT(fs != NULL);
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
return ret;
}
ret = rpmsgfs_client_ioctl(fs->handle, hf->fd, cmd, arg);
if (ret == 0 && (cmd == FIONBIO || cmd == FIOCLEX || cmd == FIONCLEX))
{
ret = -ENOTTY;
}
nxmutex_unlock(&fs->fs_lock);
return ret;
}
* Name: rpmsgfs_sync
*
* Description: Synchronize the file state on disk to match internal, in-
* memory state.
*
****************************************************************************/
static int rpmsgfs_sync(FAR struct file *filep)
{
FAR struct inode *inode;
FAR struct rpmsgfs_mountpt_s *fs;
FAR struct rpmsgfs_ofile_s *hf;
int ret;
DEBUGASSERT(filep->f_priv != NULL);
hf = filep->f_priv;
inode = filep->f_inode;
fs = inode->i_private;
DEBUGASSERT(fs != NULL);
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
return ret;
}
ret = rpmsgfs_client_sync(fs->handle, hf->fd);
nxmutex_unlock(&fs->fs_lock);
return ret;
}
* Name: rpmsgfs_dup
*
* Description: Duplicate open file data in the new file structure.
*
****************************************************************************/
static int rpmsgfs_dup(FAR const struct file *oldp, FAR struct file *newp)
{
FAR struct rpmsgfs_ofile_s *sf;
DEBUGASSERT(oldp->f_priv != NULL &&
newp->f_priv == NULL &&
newp->f_inode != NULL);
sf = oldp->f_priv;
DEBUGASSERT(sf != NULL);
sf->crefs++;
newp->f_priv = (FAR void *)sf;
return OK;
}
* Name: rpmsgfs_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'.
*
****************************************************************************/
static int rpmsgfs_fstat(FAR const struct file *filep, FAR struct stat *buf)
{
FAR struct inode *inode;
FAR struct rpmsgfs_mountpt_s *fs;
FAR struct rpmsgfs_ofile_s *hf;
int ret = OK;
DEBUGASSERT(buf != NULL);
DEBUGASSERT(filep->f_priv != NULL);
hf = filep->f_priv;
inode = filep->f_inode;
fs = inode->i_private;
DEBUGASSERT(fs != NULL);
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
return ret;
}
ret = rpmsgfs_client_fstat(fs->handle, hf->fd, buf);
nxmutex_unlock(&fs->fs_lock);
return ret;
}
* Name: rpmsgfs_fchstat
*
* Description:
* Change information about an open file associated with the file
* descriptor 'fd'.
*
****************************************************************************/
static int rpmsgfs_fchstat(FAR const struct file *filep,
FAR const struct stat *buf, int flags)
{
FAR struct inode *inode;
FAR struct rpmsgfs_mountpt_s *fs;
FAR struct rpmsgfs_ofile_s *hf;
int ret = OK;
DEBUGASSERT(buf != NULL);
DEBUGASSERT(filep->f_priv != NULL);
hf = filep->f_priv;
inode = filep->f_inode;
fs = inode->i_private;
DEBUGASSERT(fs != NULL);
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
return ret;
}
ret = rpmsgfs_client_fchstat(fs->handle, hf->fd, buf, flags);
nxmutex_unlock(&fs->fs_lock);
return ret;
}
* Name: rpmsgfs_truncate
*
* Description:
* Set the length of the open, regular file associated with the file
* structure 'filep' to 'length'.
*
****************************************************************************/
static int rpmsgfs_truncate(FAR struct file *filep, off_t length)
{
FAR struct inode *inode;
FAR struct rpmsgfs_mountpt_s *fs;
FAR struct rpmsgfs_ofile_s *hf;
int ret = OK;
DEBUGASSERT(filep->f_priv != NULL);
hf = filep->f_priv;
inode = filep->f_inode;
fs = inode->i_private;
DEBUGASSERT(fs != NULL);
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
return ret;
}
ret = rpmsgfs_client_ftruncate(fs->handle, hf->fd, length);
nxmutex_unlock(&fs->fs_lock);
return ret;
}
* Name: rpmsgfs_opendir
*
* Description: Open a directory for read access
*
****************************************************************************/
static int rpmsgfs_opendir(FAR struct inode *mountpt,
FAR const char *relpath,
FAR struct fs_dirent_s **dir)
{
FAR struct rpmsgfs_mountpt_s *fs;
FAR struct rpmsgfs_dir_s *rdir;
FAR char *path;
int ret;
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
fs = mountpt->i_private;
rdir = fs_heap_zalloc(sizeof(struct rpmsgfs_dir_s));
if (rdir == NULL)
{
return -ENOMEM;
}
path = lib_get_tempbuffer(PATH_MAX);
if (path == NULL)
{
fs_heap_free(rdir);
return -ENOMEM;
}
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
goto errout_with_rdir;
}
rpmsgfs_mkpath(fs, relpath, path, PATH_MAX);
rdir->dir = rpmsgfs_client_opendir(fs->handle, path);
if (rdir->dir == NULL)
{
ret = -ENOENT;
goto errout_with_lock;
}
*dir = (FAR struct fs_dirent_s *)rdir;
nxmutex_unlock(&fs->fs_lock);
lib_put_tempbuffer(path);
return OK;
errout_with_lock:
nxmutex_unlock(&fs->fs_lock);
errout_with_rdir:
lib_put_tempbuffer(path);
fs_heap_free(rdir);
return ret;
}
* Name: rpmsgfs_closedir
*
* Description: Open a directory for read access
*
****************************************************************************/
static int rpmsgfs_closedir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir)
{
FAR struct rpmsgfs_mountpt_s *fs;
FAR struct rpmsgfs_dir_s *rdir;
int ret;
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
fs = mountpt->i_private;
rdir = (FAR struct rpmsgfs_dir_s *)dir;
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
return ret;
}
rpmsgfs_client_closedir(fs->handle, rdir->dir);
nxmutex_unlock(&fs->fs_lock);
fs_heap_free(rdir);
return OK;
}
* Name: rpmsgfs_readdir
*
* Description: Read the next directory entry
*
****************************************************************************/
static int rpmsgfs_readdir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir,
FAR struct dirent *entry)
{
FAR struct rpmsgfs_mountpt_s *fs;
FAR struct rpmsgfs_dir_s *rdir;
int ret;
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
fs = mountpt->i_private;
rdir = (FAR struct rpmsgfs_dir_s *)dir;
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
return ret;
}
ret = rpmsgfs_client_readdir(fs->handle, rdir->dir, entry);
nxmutex_unlock(&fs->fs_lock);
return ret;
}
* Name: rpmsgfs_rewindir
*
* Description: Reset directory read to the first entry
*
****************************************************************************/
static int rpmsgfs_rewinddir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir)
{
FAR struct rpmsgfs_mountpt_s *fs;
FAR struct rpmsgfs_dir_s *rdir;
int ret;
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
fs = mountpt->i_private;
rdir = (FAR struct rpmsgfs_dir_s *)dir;
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
return ret;
}
rpmsgfs_client_rewinddir(fs->handle, rdir->dir);
nxmutex_unlock(&fs->fs_lock);
return OK;
}
* Name: rpmsgfs_bind
*
* Description: This implements a portion of the mount operation. This
* function allocates and initializes the mountpoint private data and
* binds the blockdriver inode to the filesystem private data. The final
* binding of the private data (containing the blockdriver) to the
* mountpoint is performed by mount().
*
****************************************************************************/
static int rpmsgfs_bind(FAR struct inode *blkdriver, FAR const void *data,
FAR void **handle)
{
size_t data_size = strlen(data) + 1;
FAR struct rpmsgfs_mountpt_s *fs;
FAR const char *cpuname = NULL;
FAR char *options;
char *saveptr;
char *ptr;
int len;
int ret;
if (blkdriver || !data)
{
return -ENODEV;
}
fs = (FAR struct rpmsgfs_mountpt_s *)
fs_heap_zalloc(sizeof(struct rpmsgfs_mountpt_s));
if (fs == NULL)
{
return -ENOMEM;
}
* "fs=whatever,cpu=cpuname", remote dir
* "timeout=xx", connect timeout, unit (ms)
*/
options = lib_get_tempbuffer(data_size);
if (!options)
{
fs_heap_free(fs);
return -ENOMEM;
}
memcpy(options, data, data_size);
fs->timeout = INT_MAX;
ptr = strtok_r(options, ",", &saveptr);
while (ptr != NULL)
{
if ((strncmp(ptr, "fs=", 3) == 0))
{
strlcpy(fs->fs_root, &ptr[3], sizeof(fs->fs_root));
}
else if ((strncmp(ptr, "cpu=", 4) == 0))
{
cpuname = &ptr[4];
}
else if ((strncmp(ptr, "timeout=", 8) == 0))
{
fs->timeout = atoi(&ptr[8]);
}
ptr = strtok_r(NULL, ",", &saveptr);
}
ret = rpmsgfs_client_bind(&fs->handle, cpuname);
lib_put_tempbuffer(options);
if (ret < 0)
{
fs_heap_free(fs);
return ret;
}
nxmutex_init(&fs->fs_lock);
* responsible for one reference ont the blkdriver inode and does not
* have to addref() here (but does have to release in ubind().
*/
fs->fs_head = NULL;
len = strlen(fs->fs_root);
if (len > 1 && fs->fs_root[len - 1] == '/')
{
fs->fs_root[len - 1] = '\0';
}
if (fs->fs_root[len - 1] != '/')
{
strlcat(fs->fs_root, "/", sizeof(fs->fs_root));
}
*handle = (FAR void *)fs;
return OK;
}
* Name: rpmsgfs_unbind
*
* Description: This implements the filesystem portion of the umount
* operation.
*
****************************************************************************/
static int rpmsgfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
unsigned int flags)
{
FAR struct rpmsgfs_mountpt_s *fs = (FAR struct rpmsgfs_mountpt_s *)handle;
int ret;
if (!fs)
{
return -EINVAL;
}
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
return ret;
}
if (fs->fs_head != NULL)
{
nxmutex_unlock(&fs->fs_lock);
* no open file references.
*/
return (flags != 0) ? -ENOSYS : -EBUSY;
}
ret = rpmsgfs_client_unbind(fs->handle);
nxmutex_unlock(&fs->fs_lock);
if (ret < 0)
{
return ret;
}
nxmutex_destroy(&fs->fs_lock);
fs_heap_free(fs);
return 0;
}
* Name: rpmsgfs_statfs
*
* Description: Return filesystem statistics
*
****************************************************************************/
static int rpmsgfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf)
{
FAR struct rpmsgfs_mountpt_s *fs;
int ret;
DEBUGASSERT(mountpt && mountpt->i_private);
fs = mountpt->i_private;
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
return ret;
}
ret = rpmsgfs_client_statfs(fs->handle, fs->fs_root, buf);
buf->f_type = RPMSGFS_MAGIC;
nxmutex_unlock(&fs->fs_lock);
return ret;
}
* Name: rpmsgfs_unlink
*
* Description: Remove a file
*
****************************************************************************/
static int rpmsgfs_unlink(FAR struct inode *mountpt, FAR const char *relpath)
{
FAR struct rpmsgfs_mountpt_s *fs;
FAR char *path;
int ret;
DEBUGASSERT(mountpt && mountpt->i_private);
fs = mountpt->i_private;
path = lib_get_tempbuffer(PATH_MAX);
if (path == NULL)
{
return -ENOMEM;
}
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
lib_put_tempbuffer(path);
return ret;
}
rpmsgfs_mkpath(fs, relpath, path, PATH_MAX);
ret = rpmsgfs_client_unlink(fs->handle, path);
nxmutex_unlock(&fs->fs_lock);
lib_put_tempbuffer(path);
return ret;
}
* Name: rpmsgfs_mkdir
*
* Description: Create a directory
*
****************************************************************************/
static int rpmsgfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath,
mode_t mode)
{
FAR struct rpmsgfs_mountpt_s *fs;
FAR char *path;
int ret;
DEBUGASSERT(mountpt && mountpt->i_private);
fs = mountpt->i_private;
path = lib_get_tempbuffer(PATH_MAX);
if (path == NULL)
{
return -ENOMEM;
}
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
lib_put_tempbuffer(path);
return ret;
}
rpmsgfs_mkpath(fs, relpath, path, PATH_MAX);
ret = rpmsgfs_client_mkdir(fs->handle, path, mode);
nxmutex_unlock(&fs->fs_lock);
lib_put_tempbuffer(path);
return ret;
}
* Name: rpmsgfs_rmdir
*
* Description: Remove a directory
*
****************************************************************************/
int rpmsgfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath)
{
FAR struct rpmsgfs_mountpt_s *fs;
FAR char *path;
int ret;
DEBUGASSERT(mountpt && mountpt->i_private);
fs = mountpt->i_private;
path = lib_get_tempbuffer(PATH_MAX);
if (path == NULL)
{
return -ENOMEM;
}
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
lib_put_tempbuffer(path);
return ret;
}
rpmsgfs_mkpath(fs, relpath, path, PATH_MAX);
ret = rpmsgfs_client_rmdir(fs->handle, path);
nxmutex_unlock(&fs->fs_lock);
lib_put_tempbuffer(path);
return ret;
}
* Name: rpmsgfs_rename
*
* Description: Rename a file or directory
*
****************************************************************************/
int rpmsgfs_rename(FAR struct inode *mountpt, FAR const char *oldrelpath,
FAR const char *newrelpath)
{
FAR struct rpmsgfs_mountpt_s *fs;
FAR char *oldpath;
FAR char *newpath;
int ret;
oldpath = lib_get_tempbuffer(PATH_MAX);
if (oldpath == NULL)
{
return -ENOMEM;
}
newpath = lib_get_tempbuffer(PATH_MAX);
if (newpath == NULL)
{
lib_put_tempbuffer(oldpath);
return -ENOMEM;
}
DEBUGASSERT(mountpt && mountpt->i_private);
fs = mountpt->i_private;
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
lib_put_tempbuffer(oldpath);
lib_put_tempbuffer(newpath);
return ret;
}
strlcpy(oldpath, fs->fs_root, PATH_MAX);
strlcat(oldpath, oldrelpath, PATH_MAX);
strlcpy(newpath, fs->fs_root, PATH_MAX);
strlcat(newpath, newrelpath, PATH_MAX);
ret = rpmsgfs_client_rename(fs->handle, oldpath, newpath);
nxmutex_unlock(&fs->fs_lock);
lib_put_tempbuffer(oldpath);
lib_put_tempbuffer(newpath);
return ret;
}
* Name: rpmsgfs_stat
*
* Description: Return information about a file or directory
*
****************************************************************************/
static int rpmsgfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
FAR struct stat *buf)
{
FAR struct rpmsgfs_mountpt_s *fs;
FAR char *path;
int ret;
DEBUGASSERT(mountpt && mountpt->i_private);
fs = mountpt->i_private;
path = lib_get_tempbuffer(PATH_MAX);
if (path == NULL)
{
return -ENOMEM;
}
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
lib_put_tempbuffer(path);
return ret;
}
rpmsgfs_mkpath(fs, relpath, path, PATH_MAX);
ret = rpmsgfs_client_stat(fs->handle, path, buf);
nxmutex_unlock(&fs->fs_lock);
lib_put_tempbuffer(path);
return ret;
}
* Name: rpmsgfs_chstat
*
* Description: Change information about a file or directory
*
****************************************************************************/
static int rpmsgfs_chstat(FAR struct inode *mountpt, FAR const char *relpath,
FAR const struct stat *buf, int flags)
{
FAR struct rpmsgfs_mountpt_s *fs;
FAR char *path;
int ret;
DEBUGASSERT(mountpt && mountpt->i_private);
fs = mountpt->i_private;
path = lib_get_tempbuffer(PATH_MAX);
if (path == NULL)
{
return -ENOMEM;
}
ret = nxmutex_lock(&fs->fs_lock);
if (ret < 0)
{
lib_put_tempbuffer(path);
return ret;
}
rpmsgfs_mkpath(fs, relpath, path, PATH_MAX);
ret = rpmsgfs_client_chstat(fs->handle, path, buf, flags);
nxmutex_unlock(&fs->fs_lock);
lib_put_tempbuffer(path);
return ret;
}