2015-10-08 20:26:35 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/tmpfs/fs_tmpfs.c
|
|
|
|
*
|
2020-03-29 21:23:47 +02: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
|
2015-10-08 20:26:35 +02:00
|
|
|
*
|
2020-03-29 21:23:47 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-10-08 20:26:35 +02:00
|
|
|
*
|
2020-03-29 21:23:47 +02: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.
|
2015-10-08 20:26:35 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
2015-10-08 20:48:40 +02:00
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2015-10-09 02:27:20 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/statfs.h>
|
2015-10-08 20:26:35 +02:00
|
|
|
#include <stdint.h>
|
2015-10-09 19:02:59 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
#include <nuttx/kmalloc.h>
|
2015-10-08 20:26:35 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
2015-10-09 19:02:59 +02:00
|
|
|
#include <nuttx/fs/ioctl.h>
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
#include "fs_tmpfs.h"
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_MOUNTPOINT
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-10-09 21:33:28 +02:00
|
|
|
#if CONFIG_FS_TMPFS_DIRECTORY_FREEGUARD <= CONFIG_FS_TMPFS_DIRECTORY_ALLOCGUARD
|
|
|
|
# warning CONFIG_FS_TMPFS_DIRECTORY_FREEGUARD needs to be > ALLOCGUARD
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CONFIG_FS_TMPFS_FILE_FREEGUARD <= CONFIG_FS_TMPFS_FILE_ALLOCGUARD
|
|
|
|
# warning CONFIG_FS_TMPFS_FILE_FREEGUARD needs to be > ALLOCGUARD
|
|
|
|
#endif
|
|
|
|
|
2022-05-29 17:59:14 +02:00
|
|
|
#define tmpfs_lock(fs) \
|
|
|
|
nxrmutex_lock(&fs->tfs_lock)
|
|
|
|
#define tmpfs_lock_object(to) \
|
|
|
|
nxrmutex_lock(&to->to_lock)
|
2015-10-08 22:21:12 +02:00
|
|
|
#define tmpfs_lock_file(tfo) \
|
2022-05-29 17:59:14 +02:00
|
|
|
nxrmutex_lock(&tfo->tfo_lock)
|
2015-10-08 22:21:12 +02:00
|
|
|
#define tmpfs_lock_directory(tdo) \
|
2022-05-29 17:59:14 +02:00
|
|
|
nxrmutex_lock(&tdo->tdo_lock)
|
|
|
|
#define tmpfs_unlock(fs) \
|
|
|
|
nxrmutex_unlock(&fs->tfs_lock)
|
|
|
|
#define tmpfs_unlock_object(to) \
|
|
|
|
nxrmutex_unlock(&to->to_lock)
|
2015-10-09 19:02:59 +02:00
|
|
|
#define tmpfs_unlock_file(tfo) \
|
2022-05-29 17:59:14 +02:00
|
|
|
nxrmutex_unlock(&tfo->tfo_lock)
|
2015-10-09 19:02:59 +02:00
|
|
|
#define tmpfs_unlock_directory(tdo) \
|
2022-05-29 17:59:14 +02:00
|
|
|
nxrmutex_unlock(&tdo->tdo_lock)
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Type
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct tmpfs_dir_s
|
|
|
|
{
|
|
|
|
struct fs_dirent_s tf_base; /* Vfs directory structure */
|
|
|
|
FAR struct tmpfs_directory_s *tf_tdo; /* Directory being enumerated */
|
|
|
|
unsigned int tf_index; /* Directory index */
|
|
|
|
};
|
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
2019-03-01 22:00:00 +01:00
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
/* TMPFS helpers */
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
static int tmpfs_realloc_directory(FAR struct tmpfs_directory_s *tdo,
|
2017-02-12 20:42:27 +01:00
|
|
|
unsigned int nentries);
|
2021-06-24 16:55:08 +02:00
|
|
|
static int tmpfs_realloc_file(FAR struct tmpfs_file_s *tfo,
|
2017-02-12 20:42:27 +01:00
|
|
|
size_t newsize);
|
2015-10-09 19:02:59 +02:00
|
|
|
static void tmpfs_release_lockedobject(FAR struct tmpfs_object_s *to);
|
|
|
|
static void tmpfs_release_lockedfile(FAR struct tmpfs_file_s *tfo);
|
2017-02-12 20:42:27 +01:00
|
|
|
static int tmpfs_find_dirent(FAR struct tmpfs_directory_s *tdo,
|
2021-06-24 18:21:08 +02:00
|
|
|
FAR const char *name, size_t len);
|
2017-02-12 20:42:27 +01:00
|
|
|
static int tmpfs_remove_dirent(FAR struct tmpfs_directory_s *tdo,
|
|
|
|
FAR const char *name);
|
2021-06-24 16:55:08 +02:00
|
|
|
static int tmpfs_add_dirent(FAR struct tmpfs_directory_s *tdo,
|
2017-02-12 20:42:27 +01:00
|
|
|
FAR struct tmpfs_object_s *to, FAR const char *name);
|
2015-10-08 22:21:12 +02:00
|
|
|
static FAR struct tmpfs_file_s *tmpfs_alloc_file(void);
|
2017-02-12 20:42:27 +01:00
|
|
|
static int tmpfs_create_file(FAR struct tmpfs_s *fs,
|
|
|
|
FAR const char *relpath, FAR struct tmpfs_file_s **tfo);
|
2015-10-09 04:35:41 +02:00
|
|
|
static FAR struct tmpfs_directory_s *tmpfs_alloc_directory(void);
|
2017-02-12 20:42:27 +01:00
|
|
|
static int tmpfs_create_directory(FAR struct tmpfs_s *fs,
|
|
|
|
FAR const char *relpath, FAR struct tmpfs_directory_s **tdo);
|
|
|
|
static int tmpfs_find_object(FAR struct tmpfs_s *fs,
|
2021-06-24 18:21:08 +02:00
|
|
|
FAR const char *relpath, size_t len,
|
|
|
|
FAR struct tmpfs_object_s **object,
|
2017-02-12 20:42:27 +01:00
|
|
|
FAR struct tmpfs_directory_s **parent);
|
|
|
|
static int tmpfs_find_file(FAR struct tmpfs_s *fs,
|
|
|
|
FAR const char *relpath,
|
|
|
|
FAR struct tmpfs_file_s **tfo,
|
|
|
|
FAR struct tmpfs_directory_s **parent);
|
|
|
|
static int tmpfs_find_directory(FAR struct tmpfs_s *fs,
|
2021-06-24 18:21:08 +02:00
|
|
|
FAR const char *relpath, size_t len,
|
2017-02-12 20:42:27 +01:00
|
|
|
FAR struct tmpfs_directory_s **tdo,
|
|
|
|
FAR struct tmpfs_directory_s **parent);
|
|
|
|
static int tmpfs_statfs_callout(FAR struct tmpfs_directory_s *tdo,
|
|
|
|
unsigned int index, FAR void *arg);
|
|
|
|
static int tmpfs_free_callout(FAR struct tmpfs_directory_s *tdo,
|
|
|
|
unsigned int index, FAR void *arg);
|
|
|
|
static int tmpfs_foreach(FAR struct tmpfs_directory_s *tdo,
|
|
|
|
tmpfs_foreach_t callout, FAR void *arg);
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
/* File system operations */
|
|
|
|
|
2017-02-12 20:42:27 +01:00
|
|
|
static int tmpfs_open(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode);
|
|
|
|
static int tmpfs_close(FAR struct file *filep);
|
2015-10-08 20:26:35 +02:00
|
|
|
static ssize_t tmpfs_read(FAR struct file *filep, FAR char *buffer,
|
2017-02-12 20:42:27 +01:00
|
|
|
size_t buflen);
|
2015-10-08 20:26:35 +02:00
|
|
|
static ssize_t tmpfs_write(FAR struct file *filep, FAR const char *buffer,
|
2017-02-12 20:42:27 +01:00
|
|
|
size_t buflen);
|
2015-10-08 20:26:35 +02:00
|
|
|
static off_t tmpfs_seek(FAR struct file *filep, off_t offset, int whence);
|
2021-12-11 07:50:23 +01:00
|
|
|
static int tmpfs_sync(FAR struct file *filep);
|
2017-02-12 20:42:27 +01:00
|
|
|
static int tmpfs_dup(FAR const struct file *oldp, FAR struct file *newp);
|
|
|
|
static int tmpfs_fstat(FAR const struct file *filep, FAR struct stat *buf);
|
2018-01-03 23:03:56 +01:00
|
|
|
static int tmpfs_truncate(FAR struct file *filep, off_t length);
|
2023-01-02 14:02:51 +01:00
|
|
|
static int tmpfs_mmap(FAR struct file *filep,
|
|
|
|
FAR struct mm_map_entry_s *map);
|
2017-02-12 20:42:27 +01:00
|
|
|
|
|
|
|
static int tmpfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fs_dirent_s **dir);
|
2017-02-12 20:42:27 +01:00
|
|
|
static int tmpfs_closedir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
|
|
|
static int tmpfs_readdir(FAR struct inode *mountpt,
|
2022-07-26 08:56:41 +02:00
|
|
|
FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *entry);
|
2017-02-12 20:42:27 +01:00
|
|
|
static int tmpfs_rewinddir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
|
|
|
static int tmpfs_bind(FAR struct inode *blkdriver, FAR const void *data,
|
|
|
|
FAR void **handle);
|
|
|
|
static int tmpfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
|
|
|
|
unsigned int flags);
|
|
|
|
static int tmpfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf);
|
|
|
|
static int tmpfs_unlink(FAR struct inode *mountpt, FAR const char *relpath);
|
|
|
|
static int tmpfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath,
|
|
|
|
mode_t mode);
|
|
|
|
static int tmpfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath);
|
2020-03-29 21:23:47 +02:00
|
|
|
static int tmpfs_rename(FAR struct inode *mountpt,
|
|
|
|
FAR const char *oldrelpath, FAR const char *newrelpath);
|
2017-02-13 14:20:39 +01:00
|
|
|
static void tmpfs_stat_common(FAR struct tmpfs_object_s *to,
|
2020-03-29 21:23:47 +02:00
|
|
|
FAR struct stat *buf);
|
2017-02-12 20:42:27 +01:00
|
|
|
static int tmpfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
|
|
|
|
FAR struct stat *buf);
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-22 16:53:42 +02:00
|
|
|
const struct mountpt_operations g_tmpfs_operations =
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
|
|
|
tmpfs_open, /* open */
|
|
|
|
tmpfs_close, /* close */
|
|
|
|
tmpfs_read, /* read */
|
|
|
|
tmpfs_write, /* write */
|
|
|
|
tmpfs_seek, /* seek */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* ioctl */
|
|
|
|
tmpfs_mmap, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
tmpfs_truncate, /* truncate */
|
2018-01-03 23:03:56 +01:00
|
|
|
|
2021-12-11 07:50:23 +01:00
|
|
|
tmpfs_sync, /* sync */
|
2015-10-08 20:26:35 +02:00
|
|
|
tmpfs_dup, /* dup */
|
2017-02-12 20:42:27 +01:00
|
|
|
tmpfs_fstat, /* fstat */
|
2021-07-09 07:43:26 +02:00
|
|
|
NULL, /* fchstat */
|
2018-01-03 23:03:56 +01:00
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
tmpfs_opendir, /* opendir */
|
|
|
|
tmpfs_closedir, /* closedir */
|
|
|
|
tmpfs_readdir, /* readdir */
|
|
|
|
tmpfs_rewinddir, /* rewinddir */
|
2018-01-03 23:03:56 +01:00
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
tmpfs_bind, /* bind */
|
|
|
|
tmpfs_unbind, /* unbind */
|
|
|
|
tmpfs_statfs, /* statfs */
|
2018-01-03 23:03:56 +01:00
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
tmpfs_unlink, /* unlink */
|
|
|
|
tmpfs_mkdir, /* mkdir */
|
|
|
|
tmpfs_rmdir, /* rmdir */
|
|
|
|
tmpfs_rename, /* rename */
|
|
|
|
tmpfs_stat, /* stat */
|
2021-07-09 07:43:26 +02:00
|
|
|
NULL /* chstat */
|
2015-10-08 20:26:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_realloc_directory
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
static int tmpfs_realloc_directory(FAR struct tmpfs_directory_s *tdo,
|
2015-10-08 20:26:35 +02:00
|
|
|
unsigned int nentries)
|
|
|
|
{
|
2021-06-24 16:55:08 +02:00
|
|
|
FAR struct tmpfs_dirent_s *newentry;
|
2015-10-08 20:26:35 +02:00
|
|
|
size_t objsize;
|
2021-06-24 16:55:08 +02:00
|
|
|
int ret = tdo->tdo_nentries;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
/* Get the new object size */
|
|
|
|
|
|
|
|
objsize = SIZEOF_TMPFS_DIRECTORY(nentries);
|
2021-06-24 16:55:08 +02:00
|
|
|
if (objsize <= tdo->tdo_alloc)
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
2015-10-09 21:33:28 +02:00
|
|
|
/* Already big enough.
|
|
|
|
* REVISIT: Missing logic to shrink directory objects.
|
|
|
|
*/
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
tdo->tdo_nentries = nentries;
|
2015-10-09 21:33:28 +02:00
|
|
|
return ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Added some additional amount to the new size to account frequent
|
|
|
|
* reallocations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
objsize += CONFIG_FS_TMPFS_DIRECTORY_ALLOCGUARD;
|
|
|
|
|
|
|
|
/* Realloc the directory object */
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
newentry = kmm_realloc(tdo->tdo_entry, objsize);
|
|
|
|
if (newentry == NULL)
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
/* Return the new address of the reallocated directory object */
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
tdo->tdo_alloc = objsize;
|
|
|
|
tdo->tdo_nentries = nentries;
|
|
|
|
tdo->tdo_entry = newentry;
|
2015-10-09 19:02:59 +02:00
|
|
|
|
|
|
|
/* Return the index to the first, newly allocated directory entry */
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_realloc_file
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
static int tmpfs_realloc_file(FAR struct tmpfs_file_s *tfo,
|
2015-10-08 20:26:35 +02:00
|
|
|
size_t newsize)
|
|
|
|
{
|
2021-06-24 16:55:08 +02:00
|
|
|
FAR uint8_t *newdata;
|
2015-10-08 20:26:35 +02:00
|
|
|
size_t allocsize;
|
|
|
|
size_t delta;
|
|
|
|
|
|
|
|
/* Are we growing or shrinking the object? */
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
if (newsize <= tfo->tfo_alloc)
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
|
|
|
/* Shrinking ... Shrink unconditionally if the size is shrinking to
|
|
|
|
* zero.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (newsize > 0)
|
|
|
|
{
|
|
|
|
/* Otherwise, don't realloc unless the object has shrunk by a
|
|
|
|
* lot.
|
|
|
|
*/
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
delta = tfo->tfo_alloc - newsize;
|
2015-10-09 21:33:28 +02:00
|
|
|
if (delta <= CONFIG_FS_TMPFS_FILE_FREEGUARD)
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
|
|
|
/* Hasn't shrunk enough.. Return doing nothing for now */
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
tfo->tfo_size = newsize;
|
2015-10-08 20:26:35 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-09 21:33:28 +02:00
|
|
|
/* Added some additional amount to the new size to account frequent
|
|
|
|
* reallocations.
|
|
|
|
*/
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
allocsize = newsize + CONFIG_FS_TMPFS_FILE_ALLOCGUARD;
|
2015-10-09 21:33:28 +02:00
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
/* Realloc the file object */
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
newdata = kmm_realloc(tfo->tfo_data, allocsize);
|
|
|
|
if (newdata == NULL)
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
/* Return the new address of the reallocated file object */
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
tfo->tfo_alloc = allocsize;
|
|
|
|
tfo->tfo_size = newsize;
|
|
|
|
tfo->tfo_data = newdata;
|
2015-10-08 20:26:35 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: tmpfs_release_lockedobject
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void tmpfs_release_lockedobject(FAR struct tmpfs_object_s *to)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(to && to->to_refs > 0);
|
|
|
|
|
|
|
|
/* Is this a file object? */
|
|
|
|
|
|
|
|
if (to->to_type == TMPFS_REGULAR)
|
|
|
|
{
|
|
|
|
tmpfs_release_lockedfile((FAR struct tmpfs_file_s *)to);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
to->to_refs--;
|
|
|
|
tmpfs_unlock_object(to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tmpfs_release_lockedfile
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void tmpfs_release_lockedfile(FAR struct tmpfs_file_s *tfo)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(tfo && tfo->tfo_refs > 0);
|
|
|
|
|
|
|
|
/* If there are no longer any references to the file and the file has been
|
|
|
|
* unlinked from its parent directory, then free the file object now.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (tfo->tfo_refs == 1 && (tfo->tfo_flags & TFO_FLAG_UNLINKED) != 0)
|
|
|
|
{
|
2022-05-29 17:59:14 +02:00
|
|
|
nxrmutex_destroy(&tfo->tfo_lock);
|
2021-06-24 16:55:08 +02:00
|
|
|
kmm_free(tfo->tfo_data);
|
2015-10-09 19:02:59 +02:00
|
|
|
kmm_free(tfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, just decrement the reference count on the file object */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tfo->tfo_refs--;
|
|
|
|
tmpfs_unlock_file(tfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_find_dirent
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_find_dirent(FAR struct tmpfs_directory_s *tdo,
|
2021-06-24 18:21:08 +02:00
|
|
|
FAR const char *name, size_t len)
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2021-07-17 15:20:44 +02:00
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
else if (name[len - 1] == '/')
|
|
|
|
{
|
|
|
|
/* Ignore the tail '/' */
|
|
|
|
|
|
|
|
if (--len == 0)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
/* Search the list of directory entries for a match */
|
|
|
|
|
|
|
|
for (i = 0;
|
|
|
|
i < tdo->tdo_nentries &&
|
2021-06-24 18:21:08 +02:00
|
|
|
(strncmp(tdo->tdo_entry[i].tde_name, name, len) != 0 ||
|
|
|
|
tdo->tdo_entry[i].tde_name[len] != 0);
|
2015-10-08 20:26:35 +02:00
|
|
|
i++);
|
|
|
|
|
|
|
|
/* Return what we found, if anything */
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
return i < tdo->tdo_nentries ? i : -ENOENT;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_remove_dirent
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_remove_dirent(FAR struct tmpfs_directory_s *tdo,
|
|
|
|
FAR const char *name)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
int last;
|
|
|
|
|
|
|
|
/* Search the list of directory entries for a match */
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
index = tmpfs_find_dirent(tdo, name, strlen(name));
|
2015-10-08 20:26:35 +02:00
|
|
|
if (index < 0)
|
|
|
|
{
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the object name */
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
if (tdo->tdo_entry[index].tde_name != NULL)
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
2015-10-09 19:02:59 +02:00
|
|
|
kmm_free(tdo->tdo_entry[index].tde_name);
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
2015-10-09 02:27:20 +02:00
|
|
|
/* Remove by replacing this entry with the final directory entry */
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
last = tdo->tdo_nentries - 1;
|
|
|
|
if (index != last)
|
|
|
|
{
|
2021-06-24 16:55:08 +02:00
|
|
|
tdo->tdo_entry[index] = tdo->tdo_entry[last];
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* And decrement the count of directory entries */
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
tdo->tdo_nentries = last;
|
2015-10-08 20:26:35 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_add_dirent
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
static int tmpfs_add_dirent(FAR struct tmpfs_directory_s *tdo,
|
2015-10-08 20:26:35 +02:00
|
|
|
FAR struct tmpfs_object_s *to,
|
|
|
|
FAR const char *name)
|
|
|
|
{
|
2015-10-09 21:33:28 +02:00
|
|
|
FAR struct tmpfs_dirent_s *tde;
|
2015-10-09 19:02:59 +02:00
|
|
|
FAR char *newname;
|
2015-10-08 20:26:35 +02:00
|
|
|
unsigned int nentries;
|
2021-07-17 15:20:44 +02:00
|
|
|
size_t namelen;
|
2015-10-08 20:26:35 +02:00
|
|
|
int index;
|
|
|
|
|
|
|
|
/* Copy the name string so that it will persist as long as the
|
|
|
|
* directory entry.
|
|
|
|
*/
|
|
|
|
|
2021-07-17 15:20:44 +02:00
|
|
|
namelen = strlen(name);
|
|
|
|
if (namelen == 0)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
else if (name[namelen - 1] == '/')
|
|
|
|
{
|
|
|
|
/* Don't copy the tail '/' */
|
|
|
|
|
|
|
|
if (--namelen == 0)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newname = strndup(name, namelen);
|
2015-10-08 20:26:35 +02:00
|
|
|
if (newname == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the new number of entries */
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
nentries = tdo->tdo_nentries + 1;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
/* Reallocate the directory object (if necessary) */
|
|
|
|
|
|
|
|
index = tmpfs_realloc_directory(tdo, nentries);
|
|
|
|
if (index < 0)
|
|
|
|
{
|
2015-10-09 19:02:59 +02:00
|
|
|
kmm_free(newname);
|
2015-10-08 20:26:35 +02:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the new object info in the new directory entry */
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
tde = &tdo->tdo_entry[index];
|
2015-10-09 21:33:28 +02:00
|
|
|
tde->tde_object = to;
|
|
|
|
tde->tde_name = newname;
|
2015-10-09 19:02:59 +02:00
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_alloc_file
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-10-08 22:21:12 +02:00
|
|
|
static FAR struct tmpfs_file_s *tmpfs_alloc_file(void)
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
2015-10-08 22:21:12 +02:00
|
|
|
FAR struct tmpfs_file_s *tfo;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
/* Create a new zero length file object */
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
tfo = (FAR struct tmpfs_file_s *)kmm_malloc(sizeof(*tfo));
|
2015-10-09 04:35:41 +02:00
|
|
|
if (tfo == NULL)
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
2015-10-08 22:21:12 +02:00
|
|
|
return NULL;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
2015-10-09 20:13:57 +02:00
|
|
|
/* Initialize the new file object. NOTE that the initial state is
|
|
|
|
* locked with one reference count.
|
|
|
|
*/
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
tfo->tfo_alloc = 0;
|
2015-10-08 22:21:12 +02:00
|
|
|
tfo->tfo_type = TMPFS_REGULAR;
|
2015-10-09 20:13:57 +02:00
|
|
|
tfo->tfo_refs = 1;
|
2015-10-08 22:21:12 +02:00
|
|
|
tfo->tfo_flags = 0;
|
|
|
|
tfo->tfo_size = 0;
|
2021-06-24 16:55:08 +02:00
|
|
|
tfo->tfo_data = NULL;
|
2015-10-09 19:02:59 +02:00
|
|
|
|
2022-05-29 17:59:14 +02:00
|
|
|
nxrmutex_init(&tfo->tfo_lock);
|
|
|
|
tmpfs_lock_file(tfo);
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2015-10-08 22:21:12 +02:00
|
|
|
return tfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_create_file
|
2015-10-08 22:21:12 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_create_file(FAR struct tmpfs_s *fs,
|
|
|
|
FAR const char *relpath,
|
|
|
|
FAR struct tmpfs_file_s **tfo)
|
|
|
|
{
|
2015-10-09 04:35:41 +02:00
|
|
|
FAR struct tmpfs_directory_s *parent;
|
2015-10-08 22:21:12 +02:00
|
|
|
FAR struct tmpfs_file_s *newtfo;
|
2021-06-24 18:21:08 +02:00
|
|
|
FAR const char *name;
|
2015-10-09 19:02:59 +02:00
|
|
|
int ret;
|
2015-10-08 22:21:12 +02:00
|
|
|
|
|
|
|
/* Separate the path into the file name and the path to the parent
|
|
|
|
* directory.
|
|
|
|
*/
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
name = strrchr(relpath, '/');
|
2015-10-08 22:21:12 +02:00
|
|
|
if (name == NULL)
|
|
|
|
{
|
2015-10-09 04:35:41 +02:00
|
|
|
/* No subdirectories... use the root directory */
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
name = relpath;
|
2015-10-10 22:14:25 +02:00
|
|
|
parent = (FAR struct tmpfs_directory_s *)fs->tfs_root.tde_object;
|
2015-10-09 04:35:41 +02:00
|
|
|
|
2020-04-30 09:53:56 +02:00
|
|
|
/* Lock the root directory to emulate the behavior of
|
|
|
|
* tmpfs_find_directory()
|
|
|
|
*/
|
2015-10-09 23:17:32 +02:00
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock_directory(parent);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-09 04:35:41 +02:00
|
|
|
parent->tdo_refs++;
|
2015-10-08 22:21:12 +02:00
|
|
|
}
|
2021-07-17 15:20:44 +02:00
|
|
|
else if (name[1] != '\0')
|
2015-10-08 22:21:12 +02:00
|
|
|
{
|
2015-10-09 04:35:41 +02:00
|
|
|
/* Locate the parent directory that should contain this name.
|
2015-10-09 23:17:32 +02:00
|
|
|
* On success, tmpfs_find_directory() will lock the parent
|
2015-10-09 04:35:41 +02:00
|
|
|
* directory and increment the reference count.
|
|
|
|
*/
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
ret = tmpfs_find_directory(fs, relpath, name - relpath, &parent, NULL);
|
2015-10-08 22:21:12 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-06-24 18:21:08 +02:00
|
|
|
return ret;
|
2015-10-08 22:21:12 +02:00
|
|
|
}
|
2021-06-24 18:21:08 +02:00
|
|
|
|
|
|
|
/* Skip the '/' path separator */
|
|
|
|
|
|
|
|
name++;
|
2015-10-08 22:21:12 +02:00
|
|
|
}
|
2021-07-17 15:20:44 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return -EISDIR;
|
|
|
|
}
|
2015-10-08 22:21:12 +02:00
|
|
|
|
|
|
|
/* Verify that no object of this name already exists in the directory */
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
ret = tmpfs_find_dirent(parent, name, strlen(name));
|
2015-10-08 22:21:12 +02:00
|
|
|
if (ret != -ENOENT)
|
|
|
|
{
|
2015-10-09 04:35:41 +02:00
|
|
|
/* Something with this name already exists in the directory.
|
|
|
|
* OR perhaps some fatal error occurred.
|
|
|
|
*/
|
2015-10-08 22:21:12 +02:00
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
ret = -EEXIST;
|
|
|
|
}
|
2015-10-09 04:35:41 +02:00
|
|
|
|
|
|
|
goto errout_with_parent;
|
2015-10-08 22:21:12 +02:00
|
|
|
}
|
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
/* Allocate an empty file. The initial state of the file is locked with
|
|
|
|
* one reference count.
|
2015-10-09 20:13:57 +02:00
|
|
|
*/
|
2015-10-08 22:21:12 +02:00
|
|
|
|
|
|
|
newtfo = tmpfs_alloc_file();
|
|
|
|
if (newtfo == NULL)
|
|
|
|
{
|
2015-10-09 19:02:59 +02:00
|
|
|
ret = -ENOMEM;
|
2015-10-09 04:35:41 +02:00
|
|
|
goto errout_with_parent;
|
2015-10-08 22:21:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then add the new, empty file to the directory */
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
ret = tmpfs_add_dirent(parent, (FAR struct tmpfs_object_s *)newtfo, name);
|
2015-10-08 22:21:12 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_file;
|
|
|
|
}
|
|
|
|
|
2015-10-09 23:17:32 +02:00
|
|
|
/* Release the reference and lock on the parent directory */
|
|
|
|
|
|
|
|
parent->tdo_refs--;
|
|
|
|
tmpfs_unlock_directory(parent);
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
/* Return success */
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
*tfo = newtfo;
|
|
|
|
return OK;
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Error exits */
|
2015-10-08 22:21:12 +02:00
|
|
|
|
|
|
|
errout_with_file:
|
2022-05-29 17:59:14 +02:00
|
|
|
nxrmutex_destroy(&newtfo->tfo_lock);
|
2015-10-09 19:02:59 +02:00
|
|
|
kmm_free(newtfo);
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2015-10-09 04:35:41 +02:00
|
|
|
errout_with_parent:
|
|
|
|
parent->tdo_refs--;
|
|
|
|
tmpfs_unlock_directory(parent);
|
2015-10-08 22:21:12 +02:00
|
|
|
return ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_alloc_directory
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct tmpfs_directory_s *tmpfs_alloc_directory(void)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_directory_s *tdo;
|
|
|
|
|
|
|
|
/* Create a new zero length directory object */
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
tdo = (FAR struct tmpfs_directory_s *)kmm_malloc(sizeof(*tdo));
|
2015-10-09 04:35:41 +02:00
|
|
|
if (tdo == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the new directory object */
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
tdo->tdo_alloc = 0;
|
2015-10-09 19:02:59 +02:00
|
|
|
tdo->tdo_type = TMPFS_DIRECTORY;
|
|
|
|
tdo->tdo_refs = 0;
|
2015-10-09 04:35:41 +02:00
|
|
|
tdo->tdo_nentries = 0;
|
2021-06-24 16:55:08 +02:00
|
|
|
tdo->tdo_entry = NULL;
|
2015-10-09 19:02:59 +02:00
|
|
|
|
2022-05-29 17:59:14 +02:00
|
|
|
nxrmutex_init(&tdo->tdo_lock);
|
2015-10-09 04:35:41 +02:00
|
|
|
|
|
|
|
return tdo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tmpfs_create_directory
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_create_directory(FAR struct tmpfs_s *fs,
|
|
|
|
FAR const char *relpath,
|
|
|
|
FAR struct tmpfs_directory_s **tdo)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_directory_s *parent;
|
|
|
|
FAR struct tmpfs_directory_s *newtdo;
|
2021-06-24 18:21:08 +02:00
|
|
|
FAR const char *name;
|
2015-10-09 19:02:59 +02:00
|
|
|
int ret;
|
2015-10-09 04:35:41 +02:00
|
|
|
|
|
|
|
/* Separate the path into the file name and the path to the parent
|
|
|
|
* directory.
|
|
|
|
*/
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
name = strrchr(relpath, '/');
|
2021-07-17 15:20:44 +02:00
|
|
|
if (name && name[1] == '\0')
|
|
|
|
{
|
|
|
|
/* Ignore the tail '/' */
|
|
|
|
|
|
|
|
name = memrchr(relpath, '/', name - relpath);
|
|
|
|
}
|
|
|
|
|
2015-10-09 04:35:41 +02:00
|
|
|
if (name == NULL)
|
|
|
|
{
|
|
|
|
/* No subdirectories... use the root directory */
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
name = relpath;
|
2015-10-10 22:14:25 +02:00
|
|
|
parent = (FAR struct tmpfs_directory_s *)fs->tfs_root.tde_object;
|
2015-10-09 04:35:41 +02:00
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock_directory(parent);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-09 04:35:41 +02:00
|
|
|
parent->tdo_refs++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Locate the parent directory that should contain this name.
|
|
|
|
* On success, tmpfs_find_directory() will lockthe parent
|
|
|
|
* directory and increment the reference count.
|
|
|
|
*/
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
ret = tmpfs_find_directory(fs, relpath, name - relpath, &parent, NULL);
|
2015-10-09 04:35:41 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-06-24 18:21:08 +02:00
|
|
|
return ret;
|
2015-10-09 04:35:41 +02:00
|
|
|
}
|
2021-06-24 18:21:08 +02:00
|
|
|
|
|
|
|
/* Skip the '/' path separator */
|
|
|
|
|
|
|
|
name++;
|
2015-10-09 04:35:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that no object of this name already exists in the directory */
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
ret = tmpfs_find_dirent(parent, name, strlen(name));
|
2015-10-09 04:35:41 +02:00
|
|
|
if (ret != -ENOENT)
|
|
|
|
{
|
|
|
|
/* Something with this name already exists in the directory.
|
|
|
|
* OR perhaps some fatal error occurred.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
ret = -EEXIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto errout_with_parent;
|
|
|
|
}
|
|
|
|
|
2015-10-09 23:17:32 +02:00
|
|
|
/* Allocate an empty directory object. NOTE that there is no reference on
|
|
|
|
* the new directory and the object is not locked.
|
2015-10-09 20:13:57 +02:00
|
|
|
*/
|
2015-10-09 04:35:41 +02:00
|
|
|
|
|
|
|
newtdo = tmpfs_alloc_directory();
|
|
|
|
if (newtdo == NULL)
|
|
|
|
{
|
2015-10-09 19:02:59 +02:00
|
|
|
ret = -ENOMEM;
|
2015-10-09 04:35:41 +02:00
|
|
|
goto errout_with_parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then add the new, empty file to the directory */
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
ret = tmpfs_add_dirent(parent, (FAR struct tmpfs_object_s *)newtdo, name);
|
2015-10-09 04:35:41 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_directory;
|
|
|
|
}
|
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
/* Free the copy of the relpath, release our reference to the parent
|
|
|
|
* directory, and return success
|
2015-10-09 04:35:41 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
parent->tdo_refs--;
|
|
|
|
tmpfs_unlock_directory(parent);
|
|
|
|
|
2015-10-09 23:17:32 +02:00
|
|
|
/* Return the (unlocked, unreferenced) directory object to the caller */
|
|
|
|
|
|
|
|
if (tdo != NULL)
|
|
|
|
{
|
|
|
|
*tdo = newtdo;
|
|
|
|
}
|
|
|
|
|
2015-10-09 04:35:41 +02:00
|
|
|
return OK;
|
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Error exits */
|
2015-10-09 04:35:41 +02:00
|
|
|
|
|
|
|
errout_with_directory:
|
2022-05-29 17:59:14 +02:00
|
|
|
nxrmutex_destroy(&newtdo->tdo_lock);
|
2015-10-09 19:02:59 +02:00
|
|
|
kmm_free(newtdo);
|
2015-10-09 04:35:41 +02:00
|
|
|
|
|
|
|
errout_with_parent:
|
|
|
|
parent->tdo_refs--;
|
|
|
|
tmpfs_unlock_directory(parent);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tmpfs_find_object
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_find_object(FAR struct tmpfs_s *fs,
|
2021-06-24 18:21:08 +02:00
|
|
|
FAR const char *relpath, size_t len,
|
2015-10-08 20:26:35 +02:00
|
|
|
FAR struct tmpfs_object_s **object,
|
|
|
|
FAR struct tmpfs_directory_s **parent)
|
|
|
|
{
|
2016-07-11 19:19:58 +02:00
|
|
|
FAR struct tmpfs_object_s *to = NULL;
|
|
|
|
FAR struct tmpfs_directory_s *tdo = NULL;
|
2015-10-09 19:02:59 +02:00
|
|
|
FAR struct tmpfs_directory_s *next_tdo;
|
2021-06-24 18:21:08 +02:00
|
|
|
FAR const char *segment;
|
|
|
|
FAR const char *next_segment;
|
2015-10-08 20:26:35 +02:00
|
|
|
int index;
|
2020-03-29 21:23:47 +02:00
|
|
|
int ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
/* Traverse the file system for any object with the matching name */
|
|
|
|
|
2015-10-10 22:14:25 +02:00
|
|
|
to = fs->tfs_root.tde_object;
|
|
|
|
next_tdo = (FAR struct tmpfs_directory_s *)fs->tfs_root.tde_object;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
for (segment = relpath; len != 0; segment = next_segment + 1)
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
|
|
|
/* Get the next segment after the one we are currently working on.
|
|
|
|
* This will be NULL is we are working on the final segment of the
|
|
|
|
* relpath.
|
|
|
|
*/
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
next_segment = memchr(segment, '/', len);
|
|
|
|
if (next_segment)
|
|
|
|
{
|
|
|
|
len -= next_segment + 1 - segment;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
next_segment = segment + len;
|
|
|
|
len = 0;
|
|
|
|
}
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2017-05-11 21:15:13 +02:00
|
|
|
/* Search the next directory. */
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
tdo = next_tdo;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
/* Find the TMPFS object with the next segment name in the current
|
|
|
|
* directory.
|
|
|
|
*/
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
index = tmpfs_find_dirent(tdo, segment, next_segment - segment);
|
2015-10-08 20:26:35 +02:00
|
|
|
if (index < 0)
|
|
|
|
{
|
|
|
|
/* No object with this name exists in the directory. */
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
to = tdo->tdo_entry[index].tde_object;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
/* Is this object another directory? */
|
|
|
|
|
|
|
|
if (to->to_type != TMPFS_DIRECTORY)
|
|
|
|
{
|
|
|
|
/* No. Was this the final segment in the path? */
|
|
|
|
|
2021-07-17 15:20:44 +02:00
|
|
|
if (len == 0 && *next_segment != '/')
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
|
|
|
/* Then we can break out of the loop now */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* No, this was not the final segment of the relpath.
|
2015-10-11 02:01:14 +02:00
|
|
|
* We cannot continue the search if any of the intermediate
|
|
|
|
* segments do no correspond to directories.
|
2015-10-08 20:26:35 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
return -ENOTDIR;
|
|
|
|
}
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Search this directory for the next segment. If we
|
2015-10-08 20:26:35 +02:00
|
|
|
* exit the loop, tdo will still refer to the parent
|
|
|
|
* directory of to.
|
|
|
|
*/
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
next_tdo = (FAR struct tmpfs_directory_s *)to;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* When we exit this loop (successfully), to will point to the TMPFS
|
|
|
|
* object associated with the terminal segment of the relpath.
|
|
|
|
* Increment the reference count on the located object.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Return what we found */
|
|
|
|
|
|
|
|
if (parent)
|
|
|
|
{
|
2016-07-11 19:19:58 +02:00
|
|
|
if (tdo != NULL)
|
|
|
|
{
|
|
|
|
/* Get exclusive access to the parent and increment the reference
|
|
|
|
* count on the object.
|
|
|
|
*/
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock_directory(tdo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-07-11 19:19:58 +02:00
|
|
|
tdo->tdo_refs++;
|
|
|
|
}
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
*parent = tdo;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object)
|
|
|
|
{
|
2016-07-11 19:19:58 +02:00
|
|
|
if (to != NULL)
|
|
|
|
{
|
|
|
|
/* Get exclusive access to the object and increment the reference
|
|
|
|
* count on the object.
|
|
|
|
*/
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock_object(to);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-07-11 19:19:58 +02:00
|
|
|
to->to_refs++;
|
|
|
|
}
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
*object = to;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_find_file
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_find_file(FAR struct tmpfs_s *fs,
|
|
|
|
FAR const char *relpath,
|
|
|
|
FAR struct tmpfs_file_s **tfo,
|
|
|
|
FAR struct tmpfs_directory_s **parent)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_object_s *to;
|
2021-07-17 15:20:44 +02:00
|
|
|
size_t len;
|
2015-10-08 20:26:35 +02:00
|
|
|
int ret;
|
|
|
|
|
2021-07-17 15:20:44 +02:00
|
|
|
len = strlen(relpath);
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
else if (relpath[len - 1] == '/')
|
|
|
|
{
|
|
|
|
return -EISDIR;
|
|
|
|
}
|
|
|
|
|
2015-10-09 00:48:49 +02:00
|
|
|
/* Find the object at this path. If successful, tmpfs_find_object() will
|
|
|
|
* lock both the object and the parent directory and will increment the
|
|
|
|
* reference count on both.
|
|
|
|
*/
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2021-07-17 15:20:44 +02:00
|
|
|
ret = tmpfs_find_object(fs, relpath, len, &to, parent);
|
2015-10-08 20:26:35 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* We found it... but is it a regular file? */
|
|
|
|
|
|
|
|
if (to->to_type != TMPFS_REGULAR)
|
|
|
|
{
|
2015-10-09 00:48:49 +02:00
|
|
|
/* No... unlock the object and its parent and return an error */
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpfs_release_lockedobject(to);
|
2015-10-09 00:48:49 +02:00
|
|
|
|
|
|
|
if (parent)
|
|
|
|
{
|
2015-10-09 19:02:59 +02:00
|
|
|
FAR struct tmpfs_directory_s *tdo = *parent;
|
|
|
|
|
2015-10-09 00:48:49 +02:00
|
|
|
tdo->tdo_refs--;
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpfs_unlock_directory(tdo);
|
2015-10-09 00:48:49 +02:00
|
|
|
}
|
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
ret = -EISDIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the verified file object */
|
|
|
|
|
|
|
|
*tfo = (FAR struct tmpfs_file_s *)to;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_find_directory
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_find_directory(FAR struct tmpfs_s *fs,
|
2021-06-24 18:21:08 +02:00
|
|
|
FAR const char *relpath, size_t len,
|
2015-10-08 20:26:35 +02:00
|
|
|
FAR struct tmpfs_directory_s **tdo,
|
|
|
|
FAR struct tmpfs_directory_s **parent)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_object_s *to;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Find the object at this path */
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
ret = tmpfs_find_object(fs, relpath, len, &to, parent);
|
2015-10-08 20:26:35 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* We found it... but is it a regular file? */
|
|
|
|
|
|
|
|
if (to->to_type != TMPFS_DIRECTORY)
|
|
|
|
{
|
2015-10-09 19:02:59 +02:00
|
|
|
/* No... unlock the object and its parent and return an error */
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpfs_release_lockedobject(to);
|
2015-10-09 00:48:49 +02:00
|
|
|
|
|
|
|
if (parent)
|
|
|
|
{
|
2015-10-09 19:02:59 +02:00
|
|
|
FAR struct tmpfs_directory_s *tmptdo = *parent;
|
|
|
|
|
|
|
|
tmptdo->tdo_refs--;
|
|
|
|
tmpfs_unlock_directory(tmptdo);
|
2015-10-09 00:48:49 +02:00
|
|
|
}
|
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
ret = -ENOTDIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the verified file object */
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
*tdo = (FAR struct tmpfs_directory_s *)to;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-09 02:27:20 +02:00
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_statfs_callout
|
2015-10-09 02:27:20 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_statfs_callout(FAR struct tmpfs_directory_s *tdo,
|
|
|
|
unsigned int index, FAR void *arg)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_object_s *to;
|
|
|
|
FAR struct tmpfs_statfs_s *tmpbuf;
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
DEBUGASSERT(tdo != NULL && arg != NULL && index < tdo->tdo_nentries);
|
2015-10-09 02:27:20 +02:00
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
to = tdo->tdo_entry[index].tde_object;
|
2015-10-09 02:27:20 +02:00
|
|
|
tmpbuf = (FAR struct tmpfs_statfs_s *)arg;
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
DEBUGASSERT(to != NULL);
|
|
|
|
|
2020-04-30 09:53:56 +02:00
|
|
|
/* Accumulate statistics. Save the total memory allocated
|
|
|
|
* for this object.
|
|
|
|
*/
|
2015-10-09 02:27:20 +02:00
|
|
|
|
2021-06-25 08:35:01 +02:00
|
|
|
tmpbuf->tsf_alloc += to->to_alloc +
|
|
|
|
strlen(tdo->tdo_entry[index].tde_name) + 1;
|
2015-10-09 02:27:20 +02:00
|
|
|
|
|
|
|
/* Is this directory entry a file object? */
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
if (to->to_type == TMPFS_REGULAR)
|
2015-10-09 02:27:20 +02:00
|
|
|
{
|
2015-10-09 19:02:59 +02:00
|
|
|
FAR struct tmpfs_file_s *tmptfo;
|
2015-10-09 02:27:20 +02:00
|
|
|
|
|
|
|
/* It is a file object. Increment the number of files and update the
|
|
|
|
* amount of memory in use.
|
|
|
|
*/
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
tmptfo = (FAR struct tmpfs_file_s *)to;
|
2021-06-24 16:55:08 +02:00
|
|
|
tmpbuf->tsf_alloc += sizeof(struct tmpfs_file_s);
|
2021-06-25 08:35:01 +02:00
|
|
|
tmpbuf->tsf_avail += to->to_alloc - tmptfo->tfo_size;
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpbuf->tsf_files++;
|
2015-10-09 02:27:20 +02:00
|
|
|
}
|
2015-10-09 19:02:59 +02:00
|
|
|
else /* if (to->to_type == TMPFS_DIRECTORY) */
|
2015-10-09 02:27:20 +02:00
|
|
|
{
|
2015-10-09 19:02:59 +02:00
|
|
|
FAR struct tmpfs_directory_s *tmptdo;
|
2015-10-09 02:27:20 +02:00
|
|
|
size_t avail;
|
|
|
|
|
|
|
|
/* It is a directory object. Update the amount of memory in use
|
|
|
|
* for the directory and estimate the number of free directory nodes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
tmptdo = (FAR struct tmpfs_directory_s *)to;
|
2021-06-25 08:35:01 +02:00
|
|
|
avail = tmptdo->tdo_alloc -
|
|
|
|
SIZEOF_TMPFS_DIRECTORY(tmptdo->tdo_nentries);
|
2015-10-09 02:27:20 +02:00
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
tmpbuf->tsf_alloc += sizeof(struct tmpfs_directory_s);
|
2021-06-25 08:35:01 +02:00
|
|
|
tmpbuf->tsf_avail += avail;
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpbuf->tsf_ffree += avail / sizeof(struct tmpfs_dirent_s);
|
2015-10-09 02:27:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return TMPFS_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_free_callout
|
2015-10-09 02:27:20 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_free_callout(FAR struct tmpfs_directory_s *tdo,
|
|
|
|
unsigned int index, FAR void *arg)
|
|
|
|
{
|
2015-10-09 19:02:59 +02:00
|
|
|
FAR struct tmpfs_dirent_s *tde;
|
2015-10-09 02:27:20 +02:00
|
|
|
FAR struct tmpfs_object_s *to;
|
|
|
|
FAR struct tmpfs_file_s *tfo;
|
|
|
|
unsigned int last;
|
|
|
|
|
|
|
|
/* Free the object name */
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
if (tdo->tdo_entry[index].tde_name != NULL)
|
2015-10-09 02:27:20 +02:00
|
|
|
{
|
2015-10-09 19:02:59 +02:00
|
|
|
kmm_free(tdo->tdo_entry[index].tde_name);
|
2015-10-09 02:27:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove by replacing this entry with the final directory entry */
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
tde = &tdo->tdo_entry[index];
|
|
|
|
to = tde->tde_object;
|
2015-10-09 02:27:20 +02:00
|
|
|
last = tdo->tdo_nentries - 1;
|
|
|
|
|
|
|
|
if (index != last)
|
|
|
|
{
|
2015-10-09 21:33:28 +02:00
|
|
|
/* Move the directory entry */
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
*tde = tdo->tdo_entry[last];
|
2015-10-09 02:27:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* And decrement the count of directory entries */
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
tdo->tdo_nentries = last;
|
2015-10-09 02:27:20 +02:00
|
|
|
|
|
|
|
/* Is this directory entry a file object? */
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
if (to->to_type == TMPFS_REGULAR)
|
2015-10-09 02:27:20 +02:00
|
|
|
{
|
|
|
|
tfo = (FAR struct tmpfs_file_s *)to;
|
|
|
|
|
|
|
|
/* Are there references to the file? */
|
|
|
|
|
|
|
|
if (tfo->tfo_refs > 0)
|
|
|
|
{
|
2020-04-30 09:53:56 +02:00
|
|
|
/* Yes.. We cannot delete the file now. Just mark it as unlinked. */
|
2015-10-09 02:27:20 +02:00
|
|
|
|
|
|
|
tfo->tfo_flags |= TFO_FLAG_UNLINKED;
|
|
|
|
return TMPFS_UNLINKED;
|
|
|
|
}
|
2021-06-24 16:55:08 +02:00
|
|
|
|
|
|
|
kmm_free(tfo->tfo_data);
|
|
|
|
}
|
|
|
|
else /* if (to->to_type == TMPFS_DIRECTORY) */
|
|
|
|
{
|
|
|
|
tdo = (FAR struct tmpfs_directory_s *)to;
|
|
|
|
|
|
|
|
kmm_free(tdo->tdo_entry);
|
2015-10-09 02:27:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the object now */
|
|
|
|
|
2022-05-29 17:59:14 +02:00
|
|
|
nxrmutex_destroy(&to->to_lock);
|
2015-10-09 02:27:20 +02:00
|
|
|
kmm_free(to);
|
|
|
|
return TMPFS_DELETED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_foreach
|
2015-10-09 02:27:20 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_foreach(FAR struct tmpfs_directory_s *tdo,
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpfs_foreach_t callout, FAR void *arg)
|
2015-10-09 02:27:20 +02:00
|
|
|
{
|
|
|
|
FAR struct tmpfs_object_s *to;
|
|
|
|
unsigned int index;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Visit each directory entry */
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
for (index = 0; index < tdo->tdo_nentries; )
|
2015-10-09 02:27:20 +02:00
|
|
|
{
|
|
|
|
/* Lock the object and take a reference */
|
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
to = tdo->tdo_entry[index].tde_object;
|
|
|
|
ret = tmpfs_lock_object(to);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-09 02:27:20 +02:00
|
|
|
to->to_refs++;
|
|
|
|
|
|
|
|
/* Is the next entry a directory? */
|
|
|
|
|
|
|
|
if (to->to_type == TMPFS_DIRECTORY)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_directory_s *next =
|
|
|
|
(FAR struct tmpfs_directory_s *)to;
|
|
|
|
|
2017-05-11 21:15:13 +02:00
|
|
|
/* Yes.. traverse its children first in the case the final
|
2015-10-09 02:27:20 +02:00
|
|
|
* action will be to delete the directory.
|
|
|
|
*/
|
|
|
|
|
2020-04-30 08:23:09 +02:00
|
|
|
ret = tmpfs_foreach(next, callout, arg);
|
2015-10-09 02:27:20 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return -ECANCELED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the callout */
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
ret = callout(tdo, index, arg);
|
2015-10-09 02:27:20 +02:00
|
|
|
switch (ret)
|
|
|
|
{
|
|
|
|
case TMPFS_CONTINUE: /* Continue enumeration */
|
2019-10-27 18:48:14 +01:00
|
|
|
|
2015-10-09 02:27:20 +02:00
|
|
|
/* Release the object and index to the next entry */
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpfs_release_lockedobject(to);
|
2015-10-09 02:27:20 +02:00
|
|
|
index++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TMPFS_HALT: /* Stop enumeration */
|
2019-10-27 18:48:14 +01:00
|
|
|
|
2015-10-09 02:27:20 +02:00
|
|
|
/* Release the object and cancel the traversal */
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpfs_release_lockedobject(to);
|
2015-10-09 02:27:20 +02:00
|
|
|
return -ECANCELED;
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
case TMPFS_UNLINKED: /* Only the directory entry was deleted */
|
2019-10-27 18:48:14 +01:00
|
|
|
|
2015-10-09 02:27:20 +02:00
|
|
|
/* Release the object and continue with the same index */
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpfs_release_lockedobject(to);
|
2015-10-09 02:27:20 +02:00
|
|
|
|
|
|
|
case TMPFS_DELETED: /* Object and directory entry deleted */
|
|
|
|
break; /* Continue with the same index */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_open
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_open(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct tmpfs_s *fs;
|
|
|
|
FAR struct tmpfs_file_s *tfo;
|
2015-10-11 18:29:35 +02:00
|
|
|
off_t offset;
|
2015-10-08 20:26:35 +02:00
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("filep: %p\n", filep);
|
2015-10-08 20:26:35 +02:00
|
|
|
DEBUGASSERT(filep->f_priv == NULL && filep->f_inode != NULL);
|
|
|
|
|
|
|
|
/* Get the mountpoint inode reference from the file structure and the
|
|
|
|
* mountpoint private data from the inode structure
|
|
|
|
*/
|
|
|
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
|
|
|
|
2015-10-10 22:14:25 +02:00
|
|
|
DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the file system */
|
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock(fs);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2015-10-08 22:21:12 +02:00
|
|
|
/* Skip over any leading directory separators (shouldn't be any) */
|
|
|
|
|
|
|
|
for (; *relpath == '/'; relpath++);
|
|
|
|
|
|
|
|
/* Find the file object associated with this relative path.
|
2015-10-08 20:26:35 +02:00
|
|
|
* If successful, this action will lock both the parent directory and
|
|
|
|
* the file object, adding one to the reference count of both.
|
|
|
|
* In the event that -ENOENT, there will still be a reference and
|
|
|
|
* lock on the returned directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = tmpfs_find_file(fs, relpath, &tfo, NULL);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* The file exists. We hold the lock and one reference count
|
|
|
|
* on the file object.
|
|
|
|
*
|
|
|
|
* It would be an error if we are asked to create it exclusively
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
|
|
|
|
{
|
|
|
|
/* Already exists -- can't create it exclusively */
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
ret = -EEXIST;
|
|
|
|
goto errout_with_filelock;
|
|
|
|
}
|
|
|
|
|
2019-03-01 22:00:00 +01:00
|
|
|
/* Check if the caller has sufficient privileges to open the file.
|
|
|
|
* REVISIT: No file protection implemented
|
|
|
|
*/
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
/* If O_TRUNC is specified and the file is opened for writing,
|
|
|
|
* then truncate the file. This operation requires that the file is
|
|
|
|
* writeable, but we have already checked that. O_TRUNC without write
|
|
|
|
* access is ignored.
|
|
|
|
*/
|
|
|
|
|
2015-10-08 23:10:04 +02:00
|
|
|
if ((oflags & (O_TRUNC | O_WRONLY)) == (O_TRUNC | O_WRONLY))
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
|
|
|
/* Truncate the file to zero length (if it is not already
|
|
|
|
* zero length)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (tfo->tfo_size > 0)
|
|
|
|
{
|
2021-06-24 16:55:08 +02:00
|
|
|
ret = tmpfs_realloc_file(tfo, 0);
|
2015-10-08 20:26:35 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_filelock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ENOENT would be returned by tmpfs_find_file() if the full directory
|
|
|
|
* path was found, but the file was not found in the final directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (ret == -ENOENT)
|
|
|
|
{
|
|
|
|
/* The file does not exist. Were we asked to create it? */
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
if ((oflags & O_CREAT) == 0)
|
|
|
|
{
|
2019-10-27 18:48:14 +01:00
|
|
|
/* No.. then we fail with -ENOENT */
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
ret = -ENOENT;
|
|
|
|
goto errout_with_fslock;
|
|
|
|
}
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2015-10-09 20:13:57 +02:00
|
|
|
/* Yes.. create the file object. There will be a reference and a lock
|
|
|
|
* on the new file object.
|
|
|
|
*/
|
2015-10-08 22:21:12 +02:00
|
|
|
|
|
|
|
ret = tmpfs_create_file(fs, relpath, &tfo);
|
2015-10-08 20:26:35 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_fslock;
|
|
|
|
}
|
2019-10-27 18:48:14 +01:00
|
|
|
}
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
/* Some other error occurred */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto errout_with_fslock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the struct tmpfs_file_s instance as the file private data */
|
|
|
|
|
|
|
|
filep->f_priv = tfo;
|
|
|
|
|
2015-10-11 18:29:35 +02:00
|
|
|
/* In write/append mode, we need to set the file pointer to the end of the
|
|
|
|
* file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
if ((oflags & (O_APPEND | O_WRONLY)) == (O_APPEND | O_WRONLY))
|
|
|
|
{
|
|
|
|
offset = tfo->tfo_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
filep->f_pos = offset;
|
|
|
|
|
2015-10-09 20:13:57 +02:00
|
|
|
/* Unlock the file file object, but retain the reference count */
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
tmpfs_unlock_file(tfo);
|
|
|
|
tmpfs_unlock(fs);
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
/* Error exits */
|
|
|
|
|
|
|
|
errout_with_filelock:
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpfs_release_lockedfile(tfo);
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
errout_with_fslock:
|
|
|
|
tmpfs_unlock(fs);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_close
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_file_s *tfo;
|
2020-03-29 21:23:47 +02:00
|
|
|
int ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("filep: %p\n", filep);
|
2015-10-08 20:26:35 +02:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
tfo = filep->f_priv;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file */
|
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock_file(tfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
/* Decrement the reference count on the file */
|
|
|
|
|
|
|
|
DEBUGASSERT(tfo->tfo_refs > 0);
|
|
|
|
if (tfo->tfo_refs > 0)
|
|
|
|
{
|
|
|
|
tfo->tfo_refs--;
|
|
|
|
}
|
|
|
|
|
|
|
|
filep->f_priv = NULL;
|
|
|
|
|
|
|
|
/* If the reference count decremented to zero and the file has been
|
|
|
|
* unlinked, then free the file allocation now.
|
|
|
|
*/
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
if (tfo->tfo_refs == 0 && (tfo->tfo_flags & TFO_FLAG_UNLINKED) != 0)
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
|
|
|
/* Free the file object while we hold the lock? Weird but this
|
|
|
|
* should be safe because the object is unlinked and could not
|
|
|
|
* have any other references.
|
|
|
|
*/
|
|
|
|
|
2023-01-19 14:13:17 +01:00
|
|
|
nxrmutex_destroy(&tfo->tfo_lock);
|
2021-06-24 16:55:08 +02:00
|
|
|
kmm_free(tfo->tfo_data);
|
2015-10-08 20:26:35 +02:00
|
|
|
kmm_free(tfo);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the lock on the file */
|
|
|
|
|
|
|
|
tmpfs_unlock_file(tfo);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_read
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t tmpfs_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_file_s *tfo;
|
|
|
|
ssize_t nread;
|
2015-10-08 22:21:12 +02:00
|
|
|
off_t startpos;
|
|
|
|
off_t endpos;
|
2020-03-29 21:23:47 +02:00
|
|
|
int ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("filep: %p buffer: %p buflen: %lu\n",
|
2015-10-08 22:21:12 +02:00
|
|
|
filep, buffer, (unsigned long)buflen);
|
2015-10-08 20:26:35 +02:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
tfo = filep->f_priv;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file */
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock_file(tfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2015-10-08 22:21:12 +02:00
|
|
|
|
|
|
|
/* Handle attempts to read beyond the end of the file. */
|
|
|
|
|
|
|
|
startpos = filep->f_pos;
|
|
|
|
nread = buflen;
|
|
|
|
endpos = startpos + buflen;
|
|
|
|
|
|
|
|
if (endpos > tfo->tfo_size)
|
|
|
|
{
|
|
|
|
endpos = tfo->tfo_size;
|
|
|
|
nread = endpos - startpos;
|
|
|
|
}
|
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
/* Copy data from the memory object to the user buffer */
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2022-10-13 15:59:44 +02:00
|
|
|
if (tfo->tfo_data != NULL)
|
|
|
|
{
|
|
|
|
memcpy(buffer, &tfo->tfo_data[startpos], nread);
|
|
|
|
filep->f_pos += nread;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUGASSERT(tfo->tfo_size == 0 && nread == 0);
|
|
|
|
}
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
/* Release the lock on the file */
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpfs_unlock_file(tfo);
|
2015-10-08 20:26:35 +02:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_write
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t tmpfs_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_file_s *tfo;
|
|
|
|
ssize_t nwritten;
|
2015-10-08 22:21:12 +02:00
|
|
|
off_t startpos;
|
|
|
|
off_t endpos;
|
|
|
|
int ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("filep: %p buffer: %p buflen: %lu\n",
|
2015-10-08 22:21:12 +02:00
|
|
|
filep, buffer, (unsigned long)buflen);
|
2015-10-08 20:26:35 +02:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
tfo = filep->f_priv;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file */
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock_file(tfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
/* Handle attempts to write beyond the end of the file */
|
2015-10-08 22:21:12 +02:00
|
|
|
|
|
|
|
startpos = filep->f_pos;
|
|
|
|
nwritten = buflen;
|
|
|
|
endpos = startpos + buflen;
|
|
|
|
|
|
|
|
if (endpos > tfo->tfo_size)
|
|
|
|
{
|
|
|
|
/* Reallocate the file to handle the write past the end of the file. */
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
ret = tmpfs_realloc_file(tfo, (size_t)endpos);
|
2015-10-08 22:21:12 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy data from the memory object to the user buffer */
|
|
|
|
|
2022-10-13 15:59:44 +02:00
|
|
|
if (tfo->tfo_data != NULL)
|
|
|
|
{
|
|
|
|
memcpy(&tfo->tfo_data[startpos], buffer, nwritten);
|
|
|
|
filep->f_pos += nwritten;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUGASSERT(tfo->tfo_size == 0 && nwritten == 0);
|
|
|
|
}
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
/* Release the lock on the file */
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpfs_unlock_file(tfo);
|
2015-10-08 20:26:35 +02:00
|
|
|
return nwritten;
|
2015-10-08 22:21:12 +02:00
|
|
|
|
|
|
|
errout_with_lock:
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpfs_unlock_file(tfo);
|
2015-10-08 22:21:12 +02:00
|
|
|
return (ssize_t)ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_seek
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static off_t tmpfs_seek(FAR struct file *filep, off_t offset, int whence)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_file_s *tfo;
|
|
|
|
off_t position;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("filep: %p\n", filep);
|
2015-10-08 20:26:35 +02:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
tfo = filep->f_priv;
|
|
|
|
|
|
|
|
/* Map the offset according to the whence option */
|
|
|
|
|
|
|
|
switch (whence)
|
|
|
|
{
|
|
|
|
case SEEK_SET: /* The offset is set to offset bytes. */
|
|
|
|
position = offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_CUR: /* The offset is set to its current location plus
|
|
|
|
* offset bytes. */
|
|
|
|
position = offset + filep->f_pos;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_END: /* The offset is set to the size of the file plus
|
|
|
|
* offset bytes. */
|
|
|
|
position = offset + tfo->tfo_size;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
/* Attempts to set the position beyond the end of file will
|
2015-10-08 20:26:35 +02:00
|
|
|
* work if the file is open for write access.
|
|
|
|
*
|
|
|
|
* REVISIT: This simple implementation has no per-open storage that
|
|
|
|
* would be needed to retain the open flags.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (position > tfo->tfo_size && (tfo->tfo_oflags & O_WROK) == 0)
|
|
|
|
{
|
|
|
|
/* Otherwise, the position is limited to the file size */
|
|
|
|
|
|
|
|
position = tfo->tfo_size;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Save the new file position */
|
|
|
|
|
|
|
|
filep->f_pos = position;
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
2023-01-02 14:02:51 +01:00
|
|
|
static int tmpfs_mmap(FAR struct file *filep, FAR struct mm_map_entry_s *map)
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
|
|
|
FAR struct tmpfs_file_s *tfo;
|
2023-01-02 14:02:51 +01:00
|
|
|
int ret = -EINVAL;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
2018-07-24 14:00:40 +02:00
|
|
|
tfo = filep->f_priv;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(tfo != NULL);
|
|
|
|
|
2023-01-02 18:27:25 +01:00
|
|
|
if (map->offset >= 0 && map->offset < tfo->tfo_size &&
|
|
|
|
map->length && map->offset + map->length <= tfo->tfo_size)
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
2023-01-02 14:02:51 +01:00
|
|
|
map->vaddr = tfo->tfo_data + map->offset;
|
|
|
|
ret = OK;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
2023-01-02 14:02:51 +01:00
|
|
|
return ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
2021-12-11 07:50:23 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: tmpfs_sync
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_sync(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_dup
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_dup(FAR const struct file *oldp, FAR struct file *newp)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_file_s *tfo;
|
2020-03-29 21:23:47 +02:00
|
|
|
int ret;
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("Dup %p->%p\n", oldp, newp);
|
2015-10-09 19:02:59 +02:00
|
|
|
DEBUGASSERT(oldp->f_priv != NULL && oldp->f_inode != NULL &&
|
|
|
|
newp->f_priv == NULL && newp->f_inode != NULL);
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
2015-10-11 01:04:38 +02:00
|
|
|
tfo = oldp->f_priv;
|
2015-10-08 20:26:35 +02:00
|
|
|
DEBUGASSERT(tfo != NULL);
|
|
|
|
|
2019-03-01 22:00:00 +01:00
|
|
|
/* Increment the reference count (atomically) */
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock_file(tfo);
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
tfo->tfo_refs++;
|
|
|
|
tmpfs_unlock_file(tfo);
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
/* Save a copy of the file object as the dup'ed file. This
|
|
|
|
* simple implementation does not many any per-open data
|
|
|
|
* structures so there is not really much to the dup operation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
newp->f_priv = tfo;
|
|
|
|
ret = OK;
|
|
|
|
}
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-02-12 20:42:27 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: tmpfs_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 tmpfs_fstat(FAR const struct file *filep, FAR struct stat *buf)
|
|
|
|
{
|
2017-02-13 14:20:39 +01:00
|
|
|
FAR struct tmpfs_file_s *tfo;
|
2020-03-29 21:23:47 +02:00
|
|
|
int ret;
|
2017-02-13 14:20:39 +01:00
|
|
|
|
|
|
|
finfo("Fstat %p\n", buf);
|
|
|
|
DEBUGASSERT(filep != NULL && buf != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
|
|
|
|
tfo = filep->f_priv;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file */
|
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock_file(tfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2017-02-13 14:20:39 +01:00
|
|
|
|
2019-03-01 22:00:00 +01:00
|
|
|
/* Return information about the file in the stat buffer. */
|
2017-02-13 14:20:39 +01:00
|
|
|
|
|
|
|
tmpfs_stat_common((FAR struct tmpfs_object_s *)tfo, buf);
|
|
|
|
|
|
|
|
/* Release the lock on the file and return success. */
|
|
|
|
|
|
|
|
tmpfs_unlock_file(tfo);
|
|
|
|
return OK;
|
2017-02-12 20:42:27 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 23:03:56 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: tmpfs_truncate
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_truncate(FAR struct file *filep, off_t length)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_file_s *tfo;
|
|
|
|
size_t oldsize;
|
2020-03-29 21:23:47 +02:00
|
|
|
int ret;
|
2018-01-03 23:03:56 +01:00
|
|
|
|
|
|
|
finfo("filep: %p length: %ld\n", filep, (long)length);
|
|
|
|
DEBUGASSERT(filep != NULL && length >= 0);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
|
|
|
tfo = filep->f_priv;
|
|
|
|
|
|
|
|
/* Get exclusive access to the file */
|
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock_file(tfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2018-01-03 23:03:56 +01:00
|
|
|
|
|
|
|
/* Get the old size of the file. Do nothing if the file size is not
|
|
|
|
* changing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
oldsize = tfo->tfo_size;
|
|
|
|
if (oldsize != length)
|
|
|
|
{
|
|
|
|
/* The size is changing.. up or down. Reallocate the file memory. */
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
ret = tmpfs_realloc_file(tfo, (size_t)length);
|
2018-01-03 23:03:56 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the size has increased, then we need to zero the newly added
|
|
|
|
* memory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (length > oldsize)
|
|
|
|
{
|
|
|
|
memset(&tfo->tfo_data[oldsize], 0, length - oldsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the lock on the file */
|
|
|
|
|
|
|
|
errout_with_lock:
|
|
|
|
tmpfs_unlock_file(tfo);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_opendir
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fs_dirent_s **dir)
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
2015-10-08 22:21:12 +02:00
|
|
|
FAR struct tmpfs_s *fs;
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct tmpfs_dir_s *tdir;
|
2015-10-08 22:21:12 +02:00
|
|
|
FAR struct tmpfs_directory_s *tdo;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("mountpt: %p relpath: %s dir: %p\n",
|
2015-10-08 22:21:12 +02:00
|
|
|
mountpt, relpath, dir);
|
|
|
|
DEBUGASSERT(mountpt != NULL && relpath != NULL && dir != NULL);
|
|
|
|
|
|
|
|
/* Get the mountpoint private data from the inode structure */
|
|
|
|
|
|
|
|
fs = mountpt->i_private;
|
2015-10-10 22:14:25 +02:00
|
|
|
DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
tdir = kmm_zalloc(sizeof(*tdir));
|
|
|
|
if (tdir == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2015-10-08 22:21:12 +02:00
|
|
|
/* Get exclusive access to the file system */
|
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock(fs);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
kmm_free(tdir);
|
2020-03-29 21:23:47 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2015-10-08 22:21:12 +02:00
|
|
|
|
|
|
|
/* Skip over any leading directory separators (shouldn't be any) */
|
|
|
|
|
|
|
|
for (; *relpath == '/'; relpath++);
|
|
|
|
|
|
|
|
/* Find the directory object associated with this relative path.
|
|
|
|
* If successful, this action will lock both the parent directory and
|
|
|
|
* the file object, adding one to the reference count of both.
|
|
|
|
* In the event that -ENOENT, there will still be a reference and
|
|
|
|
* lock on the returned directory.
|
|
|
|
*/
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
ret = tmpfs_find_directory(fs, relpath, strlen(relpath), &tdo, NULL);
|
2015-10-08 22:21:12 +02:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
tdir->tf_tdo = tdo;
|
|
|
|
tdir->tf_index = tdo->tdo_nentries;
|
2018-08-13 15:39:38 +02:00
|
|
|
|
2017-09-27 05:55:37 +02:00
|
|
|
tmpfs_unlock_directory(tdo);
|
2015-10-08 22:21:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the lock on the file system and return the result */
|
|
|
|
|
|
|
|
tmpfs_unlock(fs);
|
2022-08-05 06:31:25 +02:00
|
|
|
*dir = &tdir->tf_base;
|
2015-10-08 22:21:12 +02:00
|
|
|
return ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_closedir
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_closedir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
2015-10-08 22:21:12 +02:00
|
|
|
FAR struct tmpfs_directory_s *tdo;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("mountpt: %p dir: %p\n", mountpt, dir);
|
2015-10-08 22:21:12 +02:00
|
|
|
DEBUGASSERT(mountpt != NULL && dir != NULL);
|
|
|
|
|
|
|
|
/* Get the directory structure from the dir argument */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
tdo = ((FAR struct tmpfs_dir_s *)dir)->tf_tdo;
|
2015-10-08 22:21:12 +02:00
|
|
|
DEBUGASSERT(tdo != NULL);
|
|
|
|
|
|
|
|
/* Decrement the reference count on the directory object */
|
|
|
|
|
|
|
|
tmpfs_lock_directory(tdo);
|
|
|
|
tdo->tdo_refs--;
|
|
|
|
tmpfs_unlock_directory(tdo);
|
2022-08-05 06:31:25 +02:00
|
|
|
kmm_free(dir);
|
2015-10-08 22:21:12 +02:00
|
|
|
return OK;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_readdir
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_readdir(FAR struct inode *mountpt,
|
2022-07-26 08:56:41 +02:00
|
|
|
FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *entry)
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
2015-10-09 00:48:49 +02:00
|
|
|
FAR struct tmpfs_directory_s *tdo;
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct tmpfs_dir_s *tdir;
|
2015-10-09 00:48:49 +02:00
|
|
|
unsigned int index;
|
|
|
|
int ret;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("mountpt: %p dir: %p\n", mountpt, dir);
|
2015-10-09 00:48:49 +02:00
|
|
|
DEBUGASSERT(mountpt != NULL && dir != NULL);
|
|
|
|
|
|
|
|
/* Get the directory structure from the dir argument and lock it */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
tdir = (FAR struct tmpfs_dir_s *)dir;
|
|
|
|
tdo = tdir->tf_tdo;
|
2015-10-09 00:48:49 +02:00
|
|
|
DEBUGASSERT(tdo != NULL);
|
|
|
|
|
|
|
|
tmpfs_lock_directory(tdo);
|
|
|
|
|
|
|
|
/* Have we reached the end of the directory? */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
index = tdir->tf_index;
|
2021-01-20 14:31:09 +01:00
|
|
|
if (index-- == 0)
|
2015-10-09 00:48:49 +02:00
|
|
|
{
|
|
|
|
/* We signal the end of the directory by returning the special error:
|
|
|
|
* -ENOENT
|
|
|
|
*/
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("End of directory\n");
|
2015-10-09 00:48:49 +02:00
|
|
|
ret = -ENOENT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-09 19:02:59 +02:00
|
|
|
FAR struct tmpfs_dirent_s *tde;
|
|
|
|
FAR struct tmpfs_object_s *to;
|
|
|
|
|
2015-10-09 00:48:49 +02:00
|
|
|
/* Does this entry refer to a file or a directory object? */
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
tde = &tdo->tdo_entry[index];
|
|
|
|
to = tde->tde_object;
|
|
|
|
DEBUGASSERT(to != NULL);
|
|
|
|
|
|
|
|
if (to->to_type == TMPFS_DIRECTORY)
|
2015-10-09 00:48:49 +02:00
|
|
|
{
|
|
|
|
/* A directory */
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_DIRECTORY;
|
2015-10-09 00:48:49 +02:00
|
|
|
}
|
2015-10-09 19:02:59 +02:00
|
|
|
else /* to->to_type == TMPFS_REGULAR) */
|
2015-10-09 00:48:49 +02:00
|
|
|
{
|
|
|
|
/* A regular file */
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_FILE;
|
2015-10-09 00:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the entry name */
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
strlcpy(entry->d_name, tde->tde_name, sizeof(entry->d_name));
|
2015-10-09 00:48:49 +02:00
|
|
|
|
2021-01-20 14:31:09 +01:00
|
|
|
/* Save the index for next time */
|
2015-10-09 00:48:49 +02:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
tdir->tf_index = index;
|
2015-10-09 00:48:49 +02:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpfs_unlock_directory(tdo);
|
|
|
|
return ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_rewinddir
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_rewinddir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
2021-01-20 14:31:09 +01:00
|
|
|
FAR struct tmpfs_directory_s *tdo;
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct tmpfs_dir_s *tdir;
|
2021-01-20 14:31:09 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("mountpt: %p dir: %p\n", mountpt, dir);
|
2015-10-08 22:21:12 +02:00
|
|
|
DEBUGASSERT(mountpt != NULL && dir != NULL);
|
|
|
|
|
2021-01-20 14:31:09 +01:00
|
|
|
/* Get the directory structure from the dir argument and lock it */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
tdir = (FAR struct tmpfs_dir_s *)dir;
|
|
|
|
tdo = tdir->tf_tdo;
|
2021-01-20 14:31:09 +01:00
|
|
|
DEBUGASSERT(tdo != NULL);
|
|
|
|
|
|
|
|
/* Set the readdir index pass the end */
|
2015-10-08 22:21:12 +02:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
tdir->tf_index = tdo->tdo_nentries;
|
2015-10-08 22:21:12 +02:00
|
|
|
return OK;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_bind
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_bind(FAR struct inode *blkdriver, FAR const void *data,
|
|
|
|
FAR void **handle)
|
|
|
|
{
|
2015-10-10 22:14:25 +02:00
|
|
|
FAR struct tmpfs_directory_s *tdo;
|
2015-10-09 00:48:49 +02:00
|
|
|
FAR struct tmpfs_s *fs;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("blkdriver: %p data: %p handle: %p\n", blkdriver, data, handle);
|
2015-10-09 19:02:59 +02:00
|
|
|
DEBUGASSERT(blkdriver == NULL && handle != NULL);
|
2015-10-09 00:48:49 +02:00
|
|
|
|
|
|
|
/* Create an instance of the tmpfs file system */
|
|
|
|
|
|
|
|
fs = (FAR struct tmpfs_s *)kmm_zalloc(sizeof(struct tmpfs_s));
|
|
|
|
if (fs == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2015-10-10 22:14:25 +02:00
|
|
|
/* Create a root file system. This is like a single directory entry in
|
|
|
|
* the file system structure.
|
|
|
|
*/
|
2015-10-09 19:02:59 +02:00
|
|
|
|
2015-10-10 22:14:25 +02:00
|
|
|
tdo = tmpfs_alloc_directory();
|
|
|
|
if (tdo == NULL)
|
2015-10-09 19:02:59 +02:00
|
|
|
{
|
|
|
|
kmm_free(fs);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2015-10-10 22:14:25 +02:00
|
|
|
fs->tfs_root.tde_object = (FAR struct tmpfs_object_s *)tdo;
|
|
|
|
fs->tfs_root.tde_name = "";
|
|
|
|
|
2015-10-09 00:48:49 +02:00
|
|
|
/* Initialize the file system state */
|
|
|
|
|
2022-05-29 17:59:14 +02:00
|
|
|
nxrmutex_init(&fs->tfs_lock);
|
2015-10-09 00:48:49 +02:00
|
|
|
|
|
|
|
/* Return the new file system handle */
|
|
|
|
|
|
|
|
*handle = (FAR void *)fs;
|
|
|
|
return OK;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_unbind
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
2015-10-09 19:02:59 +02:00
|
|
|
FAR struct tmpfs_s *fs = (FAR struct tmpfs_s *)handle;
|
|
|
|
FAR struct tmpfs_directory_s *tdo;
|
2015-10-09 02:27:20 +02:00
|
|
|
int ret;
|
2015-10-09 00:48:49 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("handle: %p blkdriver: %p flags: %02x\n",
|
2015-10-09 19:02:59 +02:00
|
|
|
handle, blkdriver, flags);
|
2015-10-10 22:14:25 +02:00
|
|
|
DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
|
2015-10-09 00:48:49 +02:00
|
|
|
|
|
|
|
/* Lock the file system */
|
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock(fs);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2015-10-09 00:48:49 +02:00
|
|
|
|
2015-10-09 02:27:20 +02:00
|
|
|
/* Traverse all directory entries (recursively), freeing all resources. */
|
|
|
|
|
2015-10-10 22:14:25 +02:00
|
|
|
tdo = (FAR struct tmpfs_directory_s *)fs->tfs_root.tde_object;
|
2015-10-09 19:02:59 +02:00
|
|
|
ret = tmpfs_foreach(tdo, tmpfs_free_callout, NULL);
|
|
|
|
|
|
|
|
/* Now we can destroy the root file system and the file system itself. */
|
2015-10-09 00:48:49 +02:00
|
|
|
|
2022-05-29 17:59:14 +02:00
|
|
|
nxrmutex_destroy(&tdo->tdo_lock);
|
2021-06-24 16:55:08 +02:00
|
|
|
kmm_free(tdo->tdo_entry);
|
2015-10-09 19:02:59 +02:00
|
|
|
kmm_free(tdo);
|
2015-10-09 00:48:49 +02:00
|
|
|
|
2022-05-29 17:59:14 +02:00
|
|
|
nxrmutex_destroy(&fs->tfs_lock);
|
2015-10-09 00:48:49 +02:00
|
|
|
kmm_free(fs);
|
2015-10-09 02:27:20 +02:00
|
|
|
return ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_statfs
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_s *fs;
|
2015-10-09 02:27:20 +02:00
|
|
|
FAR struct tmpfs_directory_s *tdo;
|
|
|
|
struct tmpfs_statfs_s tmpbuf;
|
|
|
|
size_t avail;
|
|
|
|
off_t blkalloc;
|
2021-06-25 08:35:01 +02:00
|
|
|
off_t blkavail;
|
2015-10-09 19:02:59 +02:00
|
|
|
int ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("mountpt: %p buf: %p\n", mountpt, buf);
|
2015-10-09 02:27:20 +02:00
|
|
|
DEBUGASSERT(mountpt != NULL && buf != NULL);
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2015-10-09 04:35:41 +02:00
|
|
|
/* Get the file system structure from the inode reference. */
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2015-10-09 02:27:20 +02:00
|
|
|
fs = mountpt->i_private;
|
2015-10-10 22:14:25 +02:00
|
|
|
DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the file system */
|
2015-10-09 02:27:20 +02:00
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock(fs);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2015-10-09 02:27:20 +02:00
|
|
|
|
2015-10-10 00:57:48 +02:00
|
|
|
/* Set up the memory use for the file system and root directory object */
|
2015-10-09 02:27:20 +02:00
|
|
|
|
2015-10-10 22:14:25 +02:00
|
|
|
tdo = (FAR struct tmpfs_directory_s *)fs->tfs_root.tde_object;
|
2021-06-25 08:35:01 +02:00
|
|
|
avail = tdo->tdo_alloc -
|
2015-10-10 00:57:48 +02:00
|
|
|
SIZEOF_TMPFS_DIRECTORY(tdo->tdo_nentries);
|
2015-10-09 02:27:20 +02:00
|
|
|
|
2021-06-25 08:35:01 +02:00
|
|
|
tmpbuf.tsf_alloc = sizeof(struct tmpfs_s) +
|
|
|
|
sizeof(struct tmpfs_directory_s) +
|
|
|
|
tdo->tdo_alloc;
|
|
|
|
tmpbuf.tsf_avail = avail;
|
2015-10-09 02:27:20 +02:00
|
|
|
tmpbuf.tsf_files = 0;
|
|
|
|
tmpbuf.tsf_ffree = avail / sizeof(struct tmpfs_dirent_s);
|
|
|
|
|
2015-10-10 00:57:48 +02:00
|
|
|
/* Traverse the file system to accurmulate statistics */
|
|
|
|
|
2015-10-10 22:14:25 +02:00
|
|
|
ret = tmpfs_foreach(tdo, tmpfs_statfs_callout, (FAR void *)&tmpbuf);
|
2015-10-09 02:27:20 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return -ECANCELED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return something for the file system description */
|
|
|
|
|
2015-10-10 00:57:48 +02:00
|
|
|
blkalloc = (tmpbuf.tsf_alloc + CONFIG_FS_TMPFS_BLOCKSIZE - 1) /
|
|
|
|
CONFIG_FS_TMPFS_BLOCKSIZE;
|
2021-06-25 08:35:01 +02:00
|
|
|
blkavail = (tmpbuf.tsf_avail + CONFIG_FS_TMPFS_BLOCKSIZE - 1) /
|
2015-10-10 00:57:48 +02:00
|
|
|
CONFIG_FS_TMPFS_BLOCKSIZE;
|
2015-10-09 02:27:20 +02:00
|
|
|
|
|
|
|
buf->f_type = TMPFS_MAGIC;
|
|
|
|
buf->f_namelen = NAME_MAX;
|
2015-10-10 00:57:48 +02:00
|
|
|
buf->f_bsize = CONFIG_FS_TMPFS_BLOCKSIZE;
|
2015-10-09 02:27:20 +02:00
|
|
|
buf->f_blocks = blkalloc;
|
2021-06-25 08:35:01 +02:00
|
|
|
buf->f_bfree = blkavail;
|
|
|
|
buf->f_bavail = blkavail;
|
2015-10-09 02:27:20 +02:00
|
|
|
buf->f_files = tmpbuf.tsf_files;
|
2015-10-09 19:02:59 +02:00
|
|
|
buf->f_ffree = tmpbuf.tsf_ffree;
|
2015-10-09 02:27:20 +02:00
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
/* Release the lock on the file system */
|
|
|
|
|
2015-10-09 02:27:20 +02:00
|
|
|
tmpfs_unlock(fs);
|
2015-10-08 20:26:35 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_unlink
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_unlink(FAR struct inode *mountpt, FAR const char *relpath)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_s *fs;
|
2015-10-09 00:48:49 +02:00
|
|
|
FAR struct tmpfs_directory_s *tdo;
|
2016-07-11 19:19:58 +02:00
|
|
|
FAR struct tmpfs_file_s *tfo = NULL;
|
2015-10-09 00:48:49 +02:00
|
|
|
FAR const char *name;
|
|
|
|
int ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("mountpt: %p relpath: %s\n", mountpt, relpath);
|
2015-10-09 00:48:49 +02:00
|
|
|
DEBUGASSERT(mountpt != NULL && relpath != NULL);
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2015-10-09 04:35:41 +02:00
|
|
|
/* Get the file system structure from the inode reference. */
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2015-10-09 04:35:41 +02:00
|
|
|
fs = mountpt->i_private;
|
2015-10-10 22:14:25 +02:00
|
|
|
DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the file system */
|
2015-10-09 00:48:49 +02:00
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock(fs);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2015-10-09 00:48:49 +02:00
|
|
|
|
|
|
|
/* Find the file object and parent directory associated with this relative
|
|
|
|
* path. If successful, tmpfs_find_file will lock both the file object
|
|
|
|
* and the parent directory and take one reference count on each.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = tmpfs_find_file(fs, relpath, &tfo, &tdo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
2016-07-11 19:19:58 +02:00
|
|
|
DEBUGASSERT(tfo != NULL);
|
|
|
|
|
2015-10-09 00:48:49 +02:00
|
|
|
/* Get the file name from the relative path */
|
|
|
|
|
|
|
|
name = strrchr(relpath, '/');
|
|
|
|
if (name != NULL)
|
|
|
|
{
|
|
|
|
/* Skip over the file '/' character */
|
|
|
|
|
|
|
|
name++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The name must lie in the root directory */
|
|
|
|
|
|
|
|
name = relpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the file from parent directory */
|
|
|
|
|
|
|
|
ret = tmpfs_remove_dirent(tdo, name);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_objects;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the reference count is not one, then just mark the file as
|
2015-10-08 20:26:35 +02:00
|
|
|
* unlinked
|
|
|
|
*/
|
2015-10-09 00:48:49 +02:00
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
if (tfo->tfo_refs > 1)
|
2015-10-09 00:48:49 +02:00
|
|
|
{
|
|
|
|
/* Make the file object as unlinked */
|
|
|
|
|
|
|
|
tfo->tfo_flags |= TFO_FLAG_UNLINKED;
|
|
|
|
|
|
|
|
/* Release the reference count on the file object */
|
|
|
|
|
|
|
|
tfo->tfo_refs--;
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpfs_unlock_file(tfo);
|
2015-10-09 00:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise we can free the object now */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2022-05-29 17:59:14 +02:00
|
|
|
nxrmutex_destroy(&tfo->tfo_lock);
|
2021-06-24 16:55:08 +02:00
|
|
|
kmm_free(tfo->tfo_data);
|
2015-10-09 00:48:49 +02:00
|
|
|
kmm_free(tfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Release the reference and lock on the parent directory */
|
|
|
|
|
|
|
|
tdo->tdo_refs--;
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpfs_unlock_directory(tdo);
|
2015-10-09 00:48:49 +02:00
|
|
|
tmpfs_unlock(fs);
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
return OK;
|
2015-10-09 00:48:49 +02:00
|
|
|
|
|
|
|
errout_with_objects:
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpfs_release_lockedfile(tfo);
|
2015-10-09 00:48:49 +02:00
|
|
|
|
|
|
|
tdo->tdo_refs--;
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpfs_unlock_directory(tdo);
|
2015-10-09 00:48:49 +02:00
|
|
|
|
|
|
|
errout_with_lock:
|
|
|
|
tmpfs_unlock(fs);
|
|
|
|
return ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_mkdir
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath,
|
|
|
|
mode_t mode)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_s *fs;
|
2015-10-09 04:35:41 +02:00
|
|
|
int ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("mountpt: %p relpath: %s mode: %04x\n", mountpt, relpath, mode);
|
2015-10-09 04:35:41 +02:00
|
|
|
DEBUGASSERT(mountpt != NULL && relpath != NULL);
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2015-10-09 04:35:41 +02:00
|
|
|
/* Get the file system structure from the inode reference. */
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2015-10-09 04:35:41 +02:00
|
|
|
fs = mountpt->i_private;
|
2015-10-10 22:14:25 +02:00
|
|
|
DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the file system */
|
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock(fs);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2015-10-09 04:35:41 +02:00
|
|
|
|
2015-10-09 23:17:32 +02:00
|
|
|
/* Create the directory. */
|
2015-10-09 04:35:41 +02:00
|
|
|
|
|
|
|
ret = tmpfs_create_directory(fs, relpath, NULL);
|
|
|
|
tmpfs_unlock(fs);
|
|
|
|
return ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_rmdir
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_s *fs;
|
|
|
|
FAR struct tmpfs_directory_s *parent;
|
|
|
|
FAR struct tmpfs_directory_s *tdo;
|
2015-10-09 04:35:41 +02:00
|
|
|
FAR const char *name;
|
|
|
|
int ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("mountpt: %p relpath: %s\n", mountpt, relpath);
|
2015-10-09 04:35:41 +02:00
|
|
|
DEBUGASSERT(mountpt != NULL && relpath != NULL);
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2015-10-09 04:35:41 +02:00
|
|
|
/* Get the file system structure from the inode reference. */
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2015-10-09 04:35:41 +02:00
|
|
|
fs = mountpt->i_private;
|
2015-10-10 22:14:25 +02:00
|
|
|
DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
/* Get exclusive access to the file system */
|
2015-10-09 04:35:41 +02:00
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock(fs);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2015-10-09 04:35:41 +02:00
|
|
|
|
|
|
|
/* Find the directory object and parent directory associated with this
|
|
|
|
* relative path. If successful, tmpfs_find_file will lock both the
|
|
|
|
* directory object and the parent directory and take one reference count
|
|
|
|
* on each.
|
|
|
|
*/
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
ret = tmpfs_find_directory(fs, relpath, strlen(relpath), &tdo, &parent);
|
2015-10-09 04:35:41 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the directory empty? We cannot remove directories that still
|
|
|
|
* contain references to file system objects. No can we remove the
|
|
|
|
* directory if there are outstanding references on it (other than
|
|
|
|
* our reference).
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (tdo->tdo_nentries > 0 || tdo->tdo_refs > 1)
|
|
|
|
{
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout_with_objects;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the directory name from the relative path */
|
|
|
|
|
|
|
|
name = strrchr(relpath, '/');
|
2021-07-17 15:20:44 +02:00
|
|
|
if (name && name[1] == '\0')
|
|
|
|
{
|
|
|
|
/* Ignore the tail '/' */
|
|
|
|
|
|
|
|
name = memrchr(relpath, '/', name - relpath);
|
|
|
|
}
|
|
|
|
|
2015-10-09 04:35:41 +02:00
|
|
|
if (name != NULL)
|
|
|
|
{
|
|
|
|
/* Skip over the fidirectoryle '/' character */
|
|
|
|
|
|
|
|
name++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The name must lie in the root directory */
|
|
|
|
|
|
|
|
name = relpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the directory from parent directory */
|
|
|
|
|
|
|
|
ret = tmpfs_remove_dirent(parent, name);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_objects;
|
|
|
|
}
|
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
/* Free the directory object */
|
2015-10-09 04:35:41 +02:00
|
|
|
|
2022-05-29 17:59:14 +02:00
|
|
|
nxrmutex_destroy(&tdo->tdo_lock);
|
2021-06-24 16:55:08 +02:00
|
|
|
kmm_free(tdo->tdo_entry);
|
2015-10-09 04:35:41 +02:00
|
|
|
kmm_free(tdo);
|
|
|
|
|
|
|
|
/* Release the reference and lock on the parent directory */
|
|
|
|
|
|
|
|
parent->tdo_refs--;
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpfs_unlock_directory(parent);
|
2015-10-09 04:35:41 +02:00
|
|
|
tmpfs_unlock(fs);
|
2015-10-08 20:26:35 +02:00
|
|
|
|
|
|
|
return OK;
|
2015-10-09 04:35:41 +02:00
|
|
|
|
|
|
|
errout_with_objects:
|
|
|
|
tdo->tdo_refs--;
|
|
|
|
tmpfs_unlock_directory(tdo);
|
|
|
|
|
|
|
|
parent->tdo_refs--;
|
|
|
|
tmpfs_unlock_directory(parent);
|
|
|
|
|
|
|
|
errout_with_lock:
|
|
|
|
tmpfs_unlock(fs);
|
|
|
|
return ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-09 04:35:41 +02:00
|
|
|
* Name: tmpfs_rename
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
static int tmpfs_rename(FAR struct inode *mountpt,
|
|
|
|
FAR const char *oldrelpath,
|
2015-10-08 20:26:35 +02:00
|
|
|
FAR const char *newrelpath)
|
|
|
|
{
|
2015-10-09 19:02:59 +02:00
|
|
|
FAR struct tmpfs_directory_s *oldparent;
|
|
|
|
FAR struct tmpfs_directory_s *newparent;
|
|
|
|
FAR struct tmpfs_object_s *to;
|
2015-10-08 20:26:35 +02:00
|
|
|
FAR struct tmpfs_s *fs;
|
2015-10-09 19:02:59 +02:00
|
|
|
FAR const char *oldname;
|
2021-06-24 18:21:08 +02:00
|
|
|
FAR const char *newname;
|
2015-10-09 19:02:59 +02:00
|
|
|
int ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
finfo("mountpt: %p oldrelpath: %s newrelpath: %s\n",
|
2015-10-09 20:13:57 +02:00
|
|
|
mountpt, oldrelpath, newrelpath);
|
2015-10-09 19:02:59 +02:00
|
|
|
DEBUGASSERT(mountpt != NULL && oldrelpath != NULL && newrelpath != NULL);
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2015-10-09 04:35:41 +02:00
|
|
|
/* Get the file system structure from the inode reference. */
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2015-10-09 04:35:41 +02:00
|
|
|
fs = mountpt->i_private;
|
2015-10-10 22:14:25 +02:00
|
|
|
DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
|
2015-10-09 19:02:59 +02:00
|
|
|
|
2015-10-08 20:26:35 +02:00
|
|
|
/* Get exclusive access to the file system */
|
2015-10-09 19:02:59 +02:00
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock(fs);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2015-10-09 19:02:59 +02:00
|
|
|
|
|
|
|
/* Separate the new path into the new file name and the path to the new
|
|
|
|
* parent directory.
|
2015-10-08 20:26:35 +02:00
|
|
|
*/
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
newname = strrchr(newrelpath, '/');
|
2021-07-17 15:20:44 +02:00
|
|
|
if (newname && newname[1] == '\0')
|
|
|
|
{
|
|
|
|
/* Ignore the tail '/' */
|
|
|
|
|
|
|
|
newname = memrchr(newrelpath, '/', newname - newrelpath);
|
|
|
|
}
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
if (newname == NULL)
|
|
|
|
{
|
|
|
|
/* No subdirectories... use the root directory */
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
newname = newrelpath;
|
2015-10-10 22:14:25 +02:00
|
|
|
newparent = (FAR struct tmpfs_directory_s *)fs->tfs_root.tde_object;
|
2015-10-09 19:02:59 +02:00
|
|
|
|
|
|
|
tmpfs_lock_directory(newparent);
|
|
|
|
newparent->tdo_refs++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Locate the parent directory that should contain this name.
|
|
|
|
* On success, tmpfs_find_directory() will lockthe parent
|
|
|
|
* directory and increment the reference count.
|
|
|
|
*/
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
ret = tmpfs_find_directory(fs, newrelpath, newname - newrelpath,
|
|
|
|
&newparent, NULL);
|
2015-10-09 19:02:59 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
2021-06-24 18:21:08 +02:00
|
|
|
|
|
|
|
/* Skip the '/' path separator */
|
|
|
|
|
|
|
|
newname++;
|
2015-10-09 19:02:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that no object of this name already exists in the destination
|
|
|
|
* directory.
|
|
|
|
*/
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
ret = tmpfs_find_dirent(newparent, newname, strlen(newname));
|
2015-10-09 19:02:59 +02:00
|
|
|
if (ret != -ENOENT)
|
|
|
|
{
|
|
|
|
/* Something with this name already exists in the directory.
|
|
|
|
* OR perhaps some fatal error occurred.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
ret = -EEXIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto errout_with_newparent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the old object at oldpath. If successful, tmpfs_find_object()
|
|
|
|
* will lock both the object and the parent directory and will increment
|
|
|
|
* the reference count on both.
|
|
|
|
*/
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
ret = tmpfs_find_object(fs, oldrelpath, strlen(oldrelpath),
|
|
|
|
&to, &oldparent);
|
2015-10-09 19:02:59 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-10-27 18:48:14 +01:00
|
|
|
goto errout_with_newparent;
|
2015-10-09 19:02:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the old file name from the relative path */
|
|
|
|
|
|
|
|
oldname = strrchr(oldrelpath, '/');
|
2021-07-17 15:20:44 +02:00
|
|
|
if (oldname && oldname[1] == '\0')
|
|
|
|
{
|
|
|
|
/* Ignore the tail '/' */
|
|
|
|
|
|
|
|
oldname = memrchr(oldrelpath, '/', oldname - oldrelpath);
|
|
|
|
}
|
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
if (oldname != NULL)
|
|
|
|
{
|
|
|
|
/* Skip over the file '/' character */
|
|
|
|
|
|
|
|
oldname++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The name must lie in the root directory */
|
|
|
|
|
|
|
|
oldname = oldrelpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the entry from the parent directory */
|
|
|
|
|
|
|
|
ret = tmpfs_remove_dirent(oldparent, oldname);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_oldparent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add an entry to the new parent directory. */
|
|
|
|
|
2021-06-24 16:55:08 +02:00
|
|
|
ret = tmpfs_add_dirent(newparent, to, newname);
|
2015-10-09 19:02:59 +02:00
|
|
|
|
|
|
|
errout_with_oldparent:
|
|
|
|
oldparent->tdo_refs--;
|
|
|
|
tmpfs_unlock_directory(oldparent);
|
|
|
|
|
|
|
|
tmpfs_release_lockedobject(to);
|
|
|
|
|
|
|
|
errout_with_newparent:
|
|
|
|
newparent->tdo_refs--;
|
|
|
|
tmpfs_unlock_directory(newparent);
|
|
|
|
|
|
|
|
errout_with_lock:
|
|
|
|
tmpfs_unlock(fs);
|
|
|
|
return ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-02-13 14:20:39 +01:00
|
|
|
* Name: tmpfs_stat_common
|
2015-10-08 20:26:35 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-13 14:20:39 +01:00
|
|
|
static void tmpfs_stat_common(FAR struct tmpfs_object_s *to,
|
|
|
|
FAR struct stat *buf)
|
2015-10-08 20:26:35 +02:00
|
|
|
{
|
2015-10-09 00:48:49 +02:00
|
|
|
size_t objsize;
|
2015-10-08 20:26:35 +02:00
|
|
|
|
2017-02-13 14:20:39 +01:00
|
|
|
/* Is the tmpfs object a regular file? */
|
2015-10-09 00:48:49 +02:00
|
|
|
|
|
|
|
memset(buf, 0, sizeof(struct stat));
|
|
|
|
|
|
|
|
if (to->to_type == TMPFS_REGULAR)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_file_s *tfo =
|
|
|
|
(FAR struct tmpfs_file_s *)to;
|
|
|
|
|
|
|
|
/* -rwxrwxrwx */
|
|
|
|
|
|
|
|
buf->st_mode = S_IRWXO | S_IRWXG | S_IRWXU | S_IFREG;
|
|
|
|
|
|
|
|
/* Get the size of the object */
|
|
|
|
|
|
|
|
objsize = tfo->tfo_size;
|
|
|
|
}
|
|
|
|
else /* if (to->to_type == TMPFS_DIRECTORY) */
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_directory_s *tdo =
|
|
|
|
(FAR struct tmpfs_directory_s *)to;
|
|
|
|
|
|
|
|
/* drwxrwxrwx */
|
|
|
|
|
|
|
|
buf->st_mode = S_IRWXO | S_IRWXG | S_IRWXU | S_IFDIR;
|
|
|
|
|
|
|
|
/* Get the size of the object */
|
|
|
|
|
|
|
|
objsize = SIZEOF_TMPFS_DIRECTORY(tdo->tdo_nentries);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fake the rest of the information */
|
|
|
|
|
|
|
|
buf->st_size = objsize;
|
2015-10-10 00:57:48 +02:00
|
|
|
buf->st_blksize = CONFIG_FS_TMPFS_BLOCKSIZE;
|
|
|
|
buf->st_blocks = (objsize + CONFIG_FS_TMPFS_BLOCKSIZE - 1) /
|
|
|
|
CONFIG_FS_TMPFS_BLOCKSIZE;
|
2017-02-13 14:20:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tmpfs_stat
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int tmpfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
|
|
|
|
FAR struct stat *buf)
|
|
|
|
{
|
|
|
|
FAR struct tmpfs_s *fs;
|
|
|
|
FAR struct tmpfs_object_s *to;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
finfo("mountpt=%p relpath=%s buf=%p\n", mountpt, relpath, buf);
|
|
|
|
DEBUGASSERT(mountpt != NULL && relpath != NULL && buf != NULL);
|
|
|
|
|
|
|
|
/* Get the file system structure from the inode reference. */
|
|
|
|
|
|
|
|
fs = mountpt->i_private;
|
|
|
|
DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
|
|
|
|
|
|
|
|
/* Get exclusive access to the file system */
|
|
|
|
|
2020-03-29 21:23:47 +02:00
|
|
|
ret = tmpfs_lock(fs);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2017-02-13 14:20:39 +01:00
|
|
|
|
|
|
|
/* Find the tmpfs object at the relpath. If successful,
|
|
|
|
* tmpfs_find_object() will lock the object and increment the
|
|
|
|
* reference count on the object.
|
|
|
|
*/
|
|
|
|
|
2021-06-24 18:21:08 +02:00
|
|
|
ret = tmpfs_find_object(fs, relpath, strlen(relpath), &to, NULL);
|
2017-02-13 14:20:39 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout_with_fslock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We found it... Return information about the file object in the stat
|
|
|
|
* buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(to != NULL);
|
|
|
|
tmpfs_stat_common(to, buf);
|
2015-10-09 00:48:49 +02:00
|
|
|
|
2017-02-13 14:20:39 +01:00
|
|
|
/* Unlock the object and return success */
|
2015-10-09 00:48:49 +02:00
|
|
|
|
2015-10-09 19:02:59 +02:00
|
|
|
tmpfs_release_lockedobject(to);
|
2015-10-09 00:48:49 +02:00
|
|
|
ret = OK;
|
|
|
|
|
|
|
|
errout_with_fslock:
|
|
|
|
tmpfs_unlock(fs);
|
|
|
|
return ret;
|
2015-10-08 20:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#endif /* CONFIG_DISABLE_MOUNTPOINT */
|