2018-03-19 14:08:40 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/cromfs/fs_cromfs.c
|
|
|
|
*
|
2020-08-10 19:08:08 +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
|
2018-03-19 14:08:40 +01:00
|
|
|
*
|
2020-08-10 19:08:08 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-19 14:08:40 +01:00
|
|
|
*
|
2020-08-10 19:08:08 +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.
|
2018-03-19 14:08:40 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/statfs.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2021-04-20 17:31:55 +02:00
|
|
|
#include <inttypes.h>
|
2018-03-19 14:08:40 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <lzf.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
|
|
|
|
|
|
|
#include "cromfs.h"
|
|
|
|
|
|
|
|
#if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_CROMFS)
|
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define CROMFS_MAX_LINKS 64
|
|
|
|
|
2018-03-19 14:08:40 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
struct cromfs_dir_s
|
|
|
|
{
|
|
|
|
struct fs_dirent_s cr_base; /* VFS directory structure */
|
|
|
|
uint32_t cr_firstoffset; /* Offset to the first entry in the directory */
|
|
|
|
uint32_t cr_curroffset; /* Current offset into the directory contents */
|
|
|
|
};
|
|
|
|
|
2018-03-19 14:08:40 +01:00
|
|
|
/* This structure represents an open, regular file */
|
|
|
|
|
|
|
|
struct cromfs_file_s
|
|
|
|
{
|
|
|
|
FAR const struct cromfs_node_s *ff_node; /* The open file node */
|
2018-03-24 21:44:38 +01:00
|
|
|
uint32_t ff_offset; /* Cached block offset (zero means none) */
|
|
|
|
uint16_t ff_ulen; /* Length of decompressed data in cache */
|
|
|
|
FAR uint8_t *ff_buffer; /* Cached, decompressed data */
|
2018-03-19 14:08:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This is the form of the callback from cromfs_foreach_node(): */
|
|
|
|
|
|
|
|
typedef CODE int (*cromfs_foreach_t)(FAR const struct cromfs_volume_s *fs,
|
|
|
|
FAR const struct cromfs_node_s *node,
|
2020-08-10 19:08:08 +02:00
|
|
|
uint32_t offset, FAR void *arg);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
/* The cromfs_nodeinfo_s structure is an abbreviated version of
|
|
|
|
* cromfs_node_s structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct cromfs_nodeinfo_s
|
|
|
|
{
|
|
|
|
uint16_t ci_mode; /* File type, attributes, and access mode bits */
|
|
|
|
uint32_t ci_size; /* Size of the uncompressed data (in bytes) */
|
|
|
|
uint32_t ci_child; /* Value associated with the directory file type */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This is the form of the argument provided to the cromfs_compare_node()
|
2018-03-19 14:08:40 +01:00
|
|
|
* callback.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct cromfs_comparenode_s
|
|
|
|
{
|
2020-08-10 19:08:08 +02:00
|
|
|
FAR struct cromfs_nodeinfo_s *info; /* Location to return the node info */
|
|
|
|
FAR const char *relpath; /* Full relative path */
|
|
|
|
FAR const char *segment; /* Reference to start of the
|
|
|
|
* path segment. */
|
|
|
|
uint32_t offset; /* Physical offset in ROM */
|
|
|
|
uint16_t seglen; /* Length of the next path segment */
|
2018-03-19 14:08:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Helpers */
|
|
|
|
|
|
|
|
static FAR void *cromfs_offset2addr(FAR const struct cromfs_volume_s *fs,
|
2020-08-10 19:08:08 +02:00
|
|
|
uint32_t offset);
|
2018-03-20 19:37:01 +01:00
|
|
|
static uint32_t cromfs_addr2offset(FAR const struct cromfs_volume_s *fs,
|
2020-08-10 19:08:08 +02:00
|
|
|
FAR const void *addr);
|
|
|
|
static int cromfs_follow_link(FAR const struct cromfs_volume_s *fs,
|
|
|
|
FAR const struct cromfs_node_s **ppnode, bool follow,
|
|
|
|
FAR struct cromfs_node_s *newnode);
|
2018-03-19 14:08:40 +01:00
|
|
|
static int cromfs_foreach_node(FAR const struct cromfs_volume_s *fs,
|
2020-08-10 19:08:08 +02:00
|
|
|
FAR const struct cromfs_node_s *node,
|
|
|
|
bool follow, cromfs_foreach_t callback, FAR void *arg);
|
2018-03-19 14:08:40 +01:00
|
|
|
static uint16_t cromfs_seglen(FAR const char *relpath);
|
2020-08-10 19:08:08 +02:00
|
|
|
static int cromfs_child_node(FAR const struct cromfs_volume_s *fs,
|
|
|
|
FAR const struct cromfs_node_s *node,
|
|
|
|
FAR struct cromfs_nodeinfo_s *info);
|
|
|
|
static int cromfs_compare_node(FAR const struct cromfs_volume_s *fs,
|
|
|
|
FAR const struct cromfs_node_s *node, uint32_t offset,
|
|
|
|
FAR void *arg);
|
|
|
|
static int cromfs_find_node(FAR const struct cromfs_volume_s *fs,
|
|
|
|
FAR const char *relpath,
|
|
|
|
FAR struct cromfs_nodeinfo_s *info,
|
|
|
|
FAR uint32_t *offset);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* Common file system methods */
|
|
|
|
|
|
|
|
static int cromfs_open(FAR struct file *filep, const char *relpath,
|
2020-08-10 19:08:08 +02:00
|
|
|
int oflags, mode_t mode);
|
2018-03-19 14:08:40 +01:00
|
|
|
static int cromfs_close(FAR struct file *filep);
|
2020-07-05 17:30:43 +02:00
|
|
|
static ssize_t cromfs_read(FAR struct file *filep,
|
2020-08-10 19:08:08 +02:00
|
|
|
char *buffer, size_t buflen);
|
2020-07-05 17:30:43 +02:00
|
|
|
static int cromfs_ioctl(FAR struct file *filep,
|
2020-08-10 19:08:08 +02:00
|
|
|
int cmd, unsigned long arg);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
2020-07-05 17:30:43 +02:00
|
|
|
static int cromfs_dup(FAR const struct file *oldp,
|
2020-08-10 19:08:08 +02:00
|
|
|
FAR struct file *newp);
|
2020-07-05 17:30:43 +02:00
|
|
|
static int cromfs_fstat(FAR const struct file *filep,
|
2020-08-10 19:08:08 +02:00
|
|
|
FAR struct stat *buf);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
static int cromfs_opendir(FAR struct inode *mountpt,
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR const char *relpath, FAR struct fs_dirent_s **dir);
|
|
|
|
static int cromfs_closedir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir);
|
2018-03-19 14:08:40 +01:00
|
|
|
static int cromfs_readdir(FAR struct inode *mountpt,
|
2022-07-26 08:56:41 +02:00
|
|
|
FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *entry);
|
2018-03-19 14:08:40 +01:00
|
|
|
static int cromfs_rewinddir(FAR struct inode *mountpt,
|
2020-08-10 19:08:08 +02:00
|
|
|
FAR struct fs_dirent_s *dir);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
2020-07-05 17:30:43 +02:00
|
|
|
static int cromfs_bind(FAR struct inode *blkdriver,
|
2020-08-10 19:08:08 +02:00
|
|
|
FAR const void *data, FAR void **handle);
|
2018-03-19 14:08:40 +01:00
|
|
|
static int cromfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
|
2020-08-10 19:08:08 +02:00
|
|
|
unsigned int flags);
|
2018-03-19 14:08:40 +01:00
|
|
|
static int cromfs_statfs(FAR struct inode *mountpt,
|
2020-08-10 19:08:08 +02:00
|
|
|
FAR struct statfs *buf);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
2020-07-05 17:30:43 +02:00
|
|
|
static int cromfs_stat(FAR struct inode *mountpt,
|
2020-08-10 19:08:08 +02:00
|
|
|
FAR const char *relpath, FAR struct stat *buf);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* See fs_mount.c -- this structure is explicitly extern'ed there.
|
|
|
|
* We use the old-fashioned kind of initializers so that this will compile
|
|
|
|
* with any compiler.
|
|
|
|
*/
|
|
|
|
|
2023-04-22 16:53:42 +02:00
|
|
|
const struct mountpt_operations g_cromfs_operations =
|
2018-03-19 14:08:40 +01:00
|
|
|
{
|
|
|
|
cromfs_open, /* open */
|
|
|
|
cromfs_close, /* close */
|
|
|
|
cromfs_read, /* read */
|
|
|
|
NULL, /* write */
|
|
|
|
NULL, /* seek */
|
|
|
|
cromfs_ioctl, /* ioctl */
|
2023-01-02 14:02:51 +01:00
|
|
|
NULL, /* mmap */
|
2023-01-02 18:06:12 +01:00
|
|
|
NULL, /* truncate */
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
NULL, /* sync */
|
|
|
|
cromfs_dup, /* dup */
|
|
|
|
cromfs_fstat, /* fstat */
|
2021-07-09 07:43:26 +02:00
|
|
|
NULL, /* fchstat */
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
cromfs_opendir, /* opendir */
|
2022-08-05 06:31:25 +02:00
|
|
|
cromfs_closedir, /* closedir */
|
2018-03-19 14:08:40 +01:00
|
|
|
cromfs_readdir, /* readdir */
|
|
|
|
cromfs_rewinddir, /* rewinddir */
|
|
|
|
|
|
|
|
cromfs_bind, /* bind */
|
|
|
|
cromfs_unbind, /* unbind */
|
|
|
|
cromfs_statfs, /* statfs */
|
|
|
|
|
|
|
|
NULL, /* unlink */
|
|
|
|
NULL, /* mkdir */
|
|
|
|
NULL, /* rmdir */
|
|
|
|
NULL, /* rename */
|
2021-07-09 07:43:26 +02:00
|
|
|
cromfs_stat, /* stat */
|
|
|
|
NULL /* chstat */
|
2018-03-19 14:08:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* The CROMFS uses a global, in-memory instance of the file system image
|
|
|
|
* rather than a ROMDISK as does, same the ROMFS file system. This is
|
|
|
|
* primarily because the compression logic needs contiguous, in-memory
|
|
|
|
* data. One consequence of this is that there can only only be a single
|
|
|
|
* CROMFS file system in the build.
|
|
|
|
*
|
|
|
|
* This is the address of the single CROMFS file system image:
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern const struct cromfs_volume_s g_cromfs_image;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_offset2addr
|
2020-08-10 19:08:08 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Convert an offset into an address in the CROMFS flat memory image.
|
|
|
|
*
|
2018-03-19 14:08:40 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR void *cromfs_offset2addr(FAR const struct cromfs_volume_s *fs,
|
2018-03-20 19:37:01 +01:00
|
|
|
uint32_t offset)
|
2018-03-19 14:08:40 +01:00
|
|
|
{
|
2020-08-10 19:08:08 +02:00
|
|
|
/* Zero offset is a special case: It corresponds to a NULL address */
|
2018-03-19 14:08:40 +01:00
|
|
|
|
2018-12-06 23:42:20 +01:00
|
|
|
if (offset == 0 || offset >= fs->cv_fsize)
|
2018-03-19 14:08:40 +01:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The root node lies at the beginning of the CROMFS image so we can
|
|
|
|
* convert the offset into the image by simply adding the the address
|
|
|
|
* of the root node.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(offset < fs->cv_fsize);
|
|
|
|
return (FAR void *)((FAR uint8_t *)fs + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-12-06 23:42:20 +01:00
|
|
|
* Name: cromfs_addr2offset
|
2020-08-10 19:08:08 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Convert a CROMFS flat image address into the file system offset.
|
|
|
|
*
|
2018-03-19 14:08:40 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2018-03-20 19:37:01 +01:00
|
|
|
static uint32_t cromfs_addr2offset(FAR const struct cromfs_volume_s *fs,
|
|
|
|
FAR const void *addr)
|
2018-03-19 14:08:40 +01:00
|
|
|
{
|
|
|
|
uintptr_t start;
|
|
|
|
uintptr_t target;
|
2018-03-20 19:37:01 +01:00
|
|
|
uint32_t offset;
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* NULL is a specials case: It corresponds to offset zero */
|
|
|
|
|
|
|
|
if (addr == NULL)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that the address is "after" the start of file system image. */
|
|
|
|
|
|
|
|
start = (uintptr_t)fs;
|
|
|
|
target = (uintptr_t)addr;
|
|
|
|
DEBUGASSERT(target >= start);
|
|
|
|
|
|
|
|
/* Get the non-zero offset and make sure that the offset is within the file
|
|
|
|
* system image.
|
|
|
|
*/
|
|
|
|
|
|
|
|
offset = target - start;
|
|
|
|
DEBUGASSERT(offset < fs->cv_fsize);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_follow_link
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* If the node it a hardlink, then follow the node to the final target
|
|
|
|
* node which will be a directory or a file.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int cromfs_follow_link(FAR const struct cromfs_volume_s *fs,
|
|
|
|
FAR const struct cromfs_node_s **ppnode,
|
|
|
|
bool follow,
|
|
|
|
FAR struct cromfs_node_s *newnode)
|
|
|
|
{
|
|
|
|
FAR const struct cromfs_node_s *linknode;
|
|
|
|
FAR const struct cromfs_node_s *node = *ppnode;
|
|
|
|
FAR const char *name;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Loop while we are redirected by hardlinks */
|
|
|
|
|
|
|
|
for (i = 0; i < CROMFS_MAX_LINKS; i++)
|
|
|
|
{
|
|
|
|
/* Check for a hard link */
|
|
|
|
|
|
|
|
if ((node->cn_mode & S_IFMT) != S_IFLNK)
|
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the link target node */
|
|
|
|
|
|
|
|
linknode = (FAR const struct cromfs_node_s *)
|
|
|
|
cromfs_offset2addr(fs, node->u.cn_link);
|
|
|
|
DEBUGASSERT(linknode != NULL);
|
|
|
|
|
|
|
|
/* Special case: Don't follow either "." or ".." These will generate
|
|
|
|
* loops in both cases.
|
|
|
|
*
|
|
|
|
* REVISIT: This kludge is necessary due to an issue in gencromfs:
|
|
|
|
* The "." entry and ".." refer to the first entry in the directory
|
|
|
|
* list, ".", instead of to the directory entry itself. Hence, we
|
|
|
|
* cannot traverse "." or ".." to determine that these are
|
|
|
|
* directories. NOTE also that there is no root directory entry for
|
|
|
|
* the top "." to refer to.
|
|
|
|
*/
|
|
|
|
|
|
|
|
name = (FAR const char *)cromfs_offset2addr(fs, node->cn_name);
|
|
|
|
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
|
|
|
|
{
|
|
|
|
/* We assume this is a "." directory opener. Create a directory
|
|
|
|
* node on the stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
newnode->cn_mode = S_IFDIR | (node->cn_mode & ~S_IFMT);
|
|
|
|
newnode->cn_pad = 0;
|
|
|
|
newnode->cn_name = node->cn_name;
|
|
|
|
newnode->cn_size = 0;
|
|
|
|
newnode->cn_peer = node->cn_peer;
|
|
|
|
newnode->u.cn_child = node->u.cn_child;
|
|
|
|
|
|
|
|
/* Switch from the original read-only in ROM to the writable copy
|
|
|
|
* in on the stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
*ppnode = newnode;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the origin node file name into the writable node copy */
|
|
|
|
|
|
|
|
newnode->cn_name = node->cn_name;
|
|
|
|
newnode->cn_pad = 0;
|
|
|
|
|
|
|
|
/* Copy all attributes of the target node, but retain the hard link
|
|
|
|
* file name and, possibly, the peer node reference.
|
|
|
|
*/
|
|
|
|
|
|
|
|
newnode->cn_mode = linknode->cn_mode;
|
|
|
|
newnode->cn_size = linknode->cn_size;
|
|
|
|
newnode->u.cn_link = linknode->u.cn_link;
|
|
|
|
|
|
|
|
/* Copy the peer node offset, changing the peer only if we are
|
|
|
|
* following the hard link in the traversal.
|
|
|
|
*/
|
|
|
|
|
|
|
|
newnode->cn_peer = follow ? linknode->cn_peer : node->cn_peer;
|
|
|
|
|
|
|
|
/* Switch from the original read-only in ROM to the writable copy in
|
|
|
|
* on the stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
*ppnode = newnode;
|
|
|
|
node = newnode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ELOOP;
|
|
|
|
}
|
|
|
|
|
2018-03-19 14:08:40 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_foreach_node
|
2020-08-10 19:08:08 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Visit each node in the file system, performing the requested callback
|
|
|
|
* for each node. The attributes of the hard link are replaced with the
|
|
|
|
* attributes of the target node. Optionally, traversal can be forced to
|
|
|
|
* follow the hard link paths.
|
|
|
|
*
|
2018-03-19 14:08:40 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int cromfs_foreach_node(FAR const struct cromfs_volume_s *fs,
|
2018-03-20 21:30:05 +01:00
|
|
|
FAR const struct cromfs_node_s *node,
|
2020-08-10 19:08:08 +02:00
|
|
|
bool follow, cromfs_foreach_t callback,
|
|
|
|
FAR void *arg)
|
2018-03-19 14:08:40 +01:00
|
|
|
{
|
2020-08-10 19:08:08 +02:00
|
|
|
FAR const struct cromfs_node_s *pnode;
|
|
|
|
struct cromfs_node_s newnode;
|
|
|
|
uint32_t offset;
|
2018-03-19 14:08:40 +01:00
|
|
|
int ret = OK;
|
|
|
|
|
2018-03-20 21:30:05 +01:00
|
|
|
/* Traverse all entries in this directory (i.e., following the 'peer'
|
|
|
|
* links).
|
|
|
|
*/
|
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
pnode = node;
|
|
|
|
offset = cromfs_addr2offset(fs, node);
|
|
|
|
|
|
|
|
while (pnode != NULL)
|
2018-03-19 14:08:40 +01:00
|
|
|
{
|
2020-08-10 19:08:08 +02:00
|
|
|
/* Follow any hard links */
|
|
|
|
|
|
|
|
ret = cromfs_follow_link(fs, &pnode, follow, &newnode);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the callback for the node */
|
|
|
|
|
|
|
|
ret = callback(fs, pnode, offset, arg);
|
2018-03-19 14:08:40 +01:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
offset = pnode->cn_peer;
|
|
|
|
pnode = (FAR const struct cromfs_node_s *)
|
|
|
|
cromfs_offset2addr(fs, offset);
|
2018-03-19 14:08:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_seglen
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static uint16_t cromfs_seglen(FAR const char *relpath)
|
|
|
|
{
|
|
|
|
FAR char *delimiter;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
delimiter = strchr(relpath, '/');
|
|
|
|
if (delimiter == NULL)
|
|
|
|
{
|
|
|
|
len = strlen(relpath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = (int)((uintptr_t)delimiter - (uintptr_t)relpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGASSERT((unsigned int)len <= UINT16_MAX);
|
|
|
|
return (uint16_t)len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-08-10 19:08:08 +02:00
|
|
|
* Name: cromfs_child_node
|
2018-03-19 14:08:40 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
static int cromfs_child_node(FAR const struct cromfs_volume_s *fs,
|
|
|
|
FAR const struct cromfs_node_s *node,
|
|
|
|
FAR struct cromfs_nodeinfo_s *info)
|
|
|
|
{
|
|
|
|
FAR const struct cromfs_node_s *pnode;
|
|
|
|
FAR const struct cromfs_node_s *child;
|
|
|
|
struct cromfs_node_s newnode;
|
|
|
|
uint32_t offset;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Get the child node referred by the directory entry */
|
|
|
|
|
|
|
|
offset = node->u.cn_child;
|
|
|
|
child = (FAR const struct cromfs_node_s *)cromfs_offset2addr(fs, offset);
|
|
|
|
|
|
|
|
/* Get the attributes of the child node by following the hard link. This
|
|
|
|
* first node under the directory will be the hard link ".".
|
|
|
|
*/
|
|
|
|
|
|
|
|
pnode = child;
|
|
|
|
ret = cromfs_follow_link(fs, &pnode, false, &newnode);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->ci_mode = pnode->cn_mode;
|
|
|
|
info->ci_size = pnode->cn_size;
|
|
|
|
info->ci_child = offset;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_compare_node
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int cromfs_compare_node(FAR const struct cromfs_volume_s *fs,
|
|
|
|
FAR const struct cromfs_node_s *node,
|
|
|
|
uint32_t offset, FAR void *arg)
|
2018-03-19 14:08:40 +01:00
|
|
|
{
|
|
|
|
FAR struct cromfs_comparenode_s *cpnode;
|
2018-03-20 23:20:31 +01:00
|
|
|
FAR const struct cromfs_node_s *child;
|
2018-03-19 14:08:40 +01:00
|
|
|
FAR char *name;
|
|
|
|
int namlen;
|
2020-08-10 19:08:08 +02:00
|
|
|
int ret;
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
DEBUGASSERT(fs != NULL && node != NULL && arg != NULL);
|
|
|
|
cpnode = (FAR struct cromfs_comparenode_s *)arg;
|
|
|
|
|
|
|
|
/* Get the name of the node */
|
|
|
|
|
|
|
|
name = (FAR char *)cromfs_offset2addr(fs, node->cn_name);
|
|
|
|
namlen = strlen(name);
|
|
|
|
|
2021-04-20 17:31:55 +02:00
|
|
|
finfo("Compare %s to %s[0-%" PRIu16 "]\n", name, cpnode->segment,
|
|
|
|
cpnode->seglen);
|
2018-03-20 23:20:31 +01:00
|
|
|
|
2018-03-19 14:08:40 +01:00
|
|
|
/* If the lengths of the name does not match the length of the next path
|
|
|
|
* segment, then this is not the node we are looking for.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (namlen != cpnode->seglen)
|
|
|
|
{
|
|
|
|
return 0; /* Keep looking */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The are the same length... are they the same? */
|
|
|
|
|
|
|
|
if (strncmp(name, cpnode->segment, namlen) == 0)
|
|
|
|
{
|
|
|
|
FAR const char *segment = cpnode->segment;
|
|
|
|
|
|
|
|
/* Got it! Was this the last segment of the path? If so, then
|
|
|
|
* the segment length is equal to the length of the remaining
|
|
|
|
* relpath and we should find a NUL terminator at that location.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (segment[namlen] == '\0')
|
|
|
|
{
|
|
|
|
/* We have it. Save the terminal node with the final match
|
|
|
|
* and return 1 to stop the traversal.
|
|
|
|
*/
|
|
|
|
|
2018-03-20 23:20:31 +01:00
|
|
|
#if 1 /* REVISIT: This seems to work, but I don't fully follow the logic. */
|
|
|
|
if (S_ISDIR(node->cn_mode))
|
|
|
|
{
|
2020-08-10 19:08:08 +02:00
|
|
|
/* This first node under the directory will be the hard
|
|
|
|
* link ".".
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = cromfs_child_node(fs, node, cpnode->info);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2018-03-20 23:20:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-10 19:08:08 +02:00
|
|
|
cpnode->info->ci_mode = node->cn_mode;
|
|
|
|
cpnode->info->ci_size = node->cn_size;
|
|
|
|
cpnode->info->ci_child = node->u.cn_child;
|
2018-03-20 23:20:31 +01:00
|
|
|
}
|
|
|
|
#else
|
2020-08-10 19:08:08 +02:00
|
|
|
{
|
|
|
|
/* This first node under the directory will be the hard
|
|
|
|
* link ".".
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = cromfs_child_node(fs, node, cpnode->info);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2018-03-20 23:20:31 +01:00
|
|
|
#endif
|
2020-08-10 19:08:08 +02:00
|
|
|
|
|
|
|
cpnode->offset = offset;
|
2018-03-19 14:08:40 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-03-20 21:30:05 +01:00
|
|
|
/* A special case is if the path ends in "/". In this case I suppose
|
2018-03-19 14:08:40 +01:00
|
|
|
* we need to interpret the as matching as long as it is a directory?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (segment[namlen] == '/' && segment[namlen = 1] == '\0')
|
|
|
|
{
|
2020-08-10 19:08:08 +02:00
|
|
|
cpnode->info->ci_mode = node->cn_mode;
|
|
|
|
cpnode->info->ci_size = node->cn_size;
|
|
|
|
cpnode->info->ci_child = node->u.cn_child;
|
2018-03-19 14:08:40 +01:00
|
|
|
return S_ISDIR(node->cn_mode) ? 1 : -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this is a valid, non-terminal segment on the path, then it must
|
|
|
|
* be a directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!S_ISDIR(node->cn_mode))
|
|
|
|
{
|
|
|
|
/* Terminate the traversal with an error */
|
|
|
|
|
|
|
|
return -ENOTDIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up to continue the traversal in the sub-directory. NOTE that
|
|
|
|
* this recurses and could potentially eat up a lot of stack.
|
|
|
|
*/
|
|
|
|
|
2018-03-20 23:20:31 +01:00
|
|
|
child = (FAR const struct cromfs_node_s *)
|
|
|
|
cromfs_offset2addr(fs, node->u.cn_child);
|
2018-03-19 14:08:40 +01:00
|
|
|
segment = cpnode->segment + cpnode->seglen;
|
|
|
|
|
|
|
|
/* Skip over any '/' delimiter */
|
|
|
|
|
|
|
|
while (*segment == '/')
|
|
|
|
{
|
|
|
|
segment++;
|
|
|
|
}
|
|
|
|
|
|
|
|
cpnode->segment = segment;
|
|
|
|
cpnode->seglen = cromfs_seglen(segment);
|
|
|
|
DEBUGASSERT(cpnode->seglen > 0);
|
|
|
|
|
|
|
|
/* Then recurse */
|
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
return cromfs_foreach_node(fs, child, true, cromfs_compare_node,
|
|
|
|
cpnode);
|
2018-03-19 14:08:40 +01:00
|
|
|
}
|
2020-08-10 19:08:08 +02:00
|
|
|
|
|
|
|
return 0; /* Keep looking in this directory */
|
2018-03-19 14:08:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2020-08-10 19:08:08 +02:00
|
|
|
* Name: cromfs_find_node
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find the CROMFS node at the provide mountpoint relative path.
|
|
|
|
*
|
2018-03-19 14:08:40 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
static int cromfs_find_node(FAR const struct cromfs_volume_s *fs,
|
|
|
|
FAR const char *relpath,
|
|
|
|
FAR struct cromfs_nodeinfo_s *info,
|
|
|
|
FAR uint32_t *offset)
|
2018-03-19 14:08:40 +01:00
|
|
|
{
|
|
|
|
struct cromfs_comparenode_s cpnode;
|
2018-03-20 21:30:05 +01:00
|
|
|
FAR const struct cromfs_node_s *root;
|
2018-03-19 14:08:40 +01:00
|
|
|
int ret;
|
|
|
|
|
2018-03-20 23:20:31 +01:00
|
|
|
finfo("relpath: %s\n", relpath);
|
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
/* Get the root node. The root is the entry "." which is a hard link. */
|
2018-03-20 21:30:05 +01:00
|
|
|
|
|
|
|
root = (FAR const struct cromfs_node_s *)
|
|
|
|
cromfs_offset2addr(fs, fs->cv_root);
|
|
|
|
|
2018-03-19 14:08:40 +01:00
|
|
|
/* NULL or empty string refers to the root node */
|
|
|
|
|
|
|
|
if (relpath == NULL || relpath[0] == '\0')
|
|
|
|
{
|
2020-08-10 19:08:08 +02:00
|
|
|
struct cromfs_node_s newnode;
|
|
|
|
|
|
|
|
/* Get the attributes of the root node by following the hard link.
|
|
|
|
* We do this even though the attributes of the root node are well
|
|
|
|
* defined.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = cromfs_follow_link(fs, &root, false, &newnode);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->ci_mode = root->cn_mode;
|
|
|
|
info->ci_size = root->cn_size;
|
|
|
|
info->ci_child = root->u.cn_child;
|
|
|
|
*offset = fs->cv_root;
|
2018-03-19 14:08:40 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not the root directory. Relative so it should not begin with '/'. */
|
|
|
|
|
|
|
|
if (relpath[0] == '/')
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up for the traversal */
|
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
cpnode.info = info;
|
2018-03-19 14:08:40 +01:00
|
|
|
cpnode.relpath = relpath;
|
|
|
|
cpnode.segment = relpath;
|
2020-08-10 19:08:08 +02:00
|
|
|
cpnode.offset = fs->cv_root;
|
2018-03-19 14:08:40 +01:00
|
|
|
cpnode.seglen = (uint16_t)cromfs_seglen(relpath);
|
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
ret = cromfs_foreach_node(fs, root, false, cromfs_compare_node, &cpnode);
|
2018-03-19 14:08:40 +01:00
|
|
|
if (ret > 0)
|
|
|
|
{
|
2020-08-10 19:08:08 +02:00
|
|
|
*offset = cpnode.offset;
|
2018-03-19 14:08:40 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
else if (ret == OK)
|
|
|
|
{
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int cromfs_open(FAR struct file *filep, FAR const char *relpath,
|
|
|
|
int oflags, mode_t mode)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR const struct cromfs_volume_s *fs;
|
2020-08-10 19:08:08 +02:00
|
|
|
struct cromfs_nodeinfo_s info;
|
2018-03-19 14:08:40 +01:00
|
|
|
FAR struct cromfs_file_s *ff;
|
2020-08-10 19:08:08 +02:00
|
|
|
uint32_t offset;
|
2018-03-19 14:08:40 +01:00
|
|
|
int ret;
|
|
|
|
|
2018-03-20 23:20:31 +01:00
|
|
|
finfo("Open: %s\n", relpath);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(filep->f_priv == NULL);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* Get the mountpoint inode reference from the file structure and the
|
|
|
|
* volume private data from the inode structure
|
|
|
|
*/
|
|
|
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
|
|
|
|
|
|
|
DEBUGASSERT(fs != NULL);
|
|
|
|
|
|
|
|
/* CROMFS is read-only. Any attempt to open with any kind of write
|
|
|
|
* access is not permitted.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((oflags & O_WRONLY) != 0 || (oflags & O_RDONLY) == 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Only O_RDONLY supported\n");
|
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Locate the node for this relative path */
|
2018-03-19 14:08:40 +01:00
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
ret = cromfs_find_node(fs, relpath, &info, &offset);
|
2018-03-19 14:08:40 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-07-05 17:30:43 +02:00
|
|
|
/* Nothing exists at that relative path (or a really bad error
|
|
|
|
* occurred)
|
|
|
|
*/
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that the node is a regular file */
|
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
if (!S_ISREG(info.ci_mode))
|
2018-03-19 14:08:40 +01:00
|
|
|
{
|
|
|
|
return -EISDIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create an instance of the file private date to describe the opened
|
|
|
|
* file.
|
|
|
|
*/
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
ff = kmm_zalloc(sizeof(struct cromfs_file_s));
|
2018-03-19 14:08:40 +01:00
|
|
|
if (ff == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a file buffer to support partial sector accesses */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
ff->ff_buffer = kmm_malloc(fs->cv_bsize);
|
2018-03-19 14:08:40 +01:00
|
|
|
if (!ff->ff_buffer)
|
|
|
|
{
|
|
|
|
kmm_free(ff);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the node in the open file instance */
|
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
ff->ff_node = (FAR const struct cromfs_node_s *)
|
|
|
|
cromfs_offset2addr(fs, offset);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* Save the index as the open-specific state in filep->f_priv */
|
|
|
|
|
|
|
|
filep->f_priv = (FAR void *)ff;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int cromfs_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct cromfs_file_s *ff;
|
|
|
|
|
|
|
|
finfo("Closing\n");
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* Get the open file instance from the file structure */
|
|
|
|
|
|
|
|
ff = filep->f_priv;
|
|
|
|
DEBUGASSERT(ff->ff_node != NULL && ff->ff_buffer != NULL);
|
|
|
|
|
|
|
|
/* Free all resources consumed by the opened file */
|
|
|
|
|
|
|
|
kmm_free(ff->ff_buffer);
|
|
|
|
kmm_free(ff);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t cromfs_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR const struct cromfs_volume_s *fs;
|
|
|
|
FAR struct cromfs_file_s *ff;
|
|
|
|
FAR struct lzf_header_s *currhdr;
|
|
|
|
FAR struct lzf_header_s *nexthdr;
|
|
|
|
FAR uint8_t *dest;
|
|
|
|
FAR const uint8_t *src;
|
|
|
|
off_t fpos;
|
|
|
|
size_t remaining;
|
2018-03-20 19:37:01 +01:00
|
|
|
uint32_t blkoffs;
|
2018-03-19 14:08:40 +01:00
|
|
|
uint16_t ulen;
|
|
|
|
uint16_t clen;
|
|
|
|
unsigned int copysize;
|
|
|
|
unsigned int copyoffs;
|
|
|
|
|
2020-11-24 23:20:32 +01:00
|
|
|
finfo("Read %zu bytes from offset %jd\n", buflen, (intmax_t)filep->f_pos);
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* Get the mountpoint inode reference from the file structure and the
|
|
|
|
* volume private data from the inode structure
|
|
|
|
*/
|
|
|
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
|
|
|
DEBUGASSERT(fs != NULL);
|
|
|
|
|
|
|
|
/* Get the open file instance from the file structure */
|
|
|
|
|
|
|
|
ff = (FAR struct cromfs_file_s *)filep->f_priv;
|
|
|
|
DEBUGASSERT(ff->ff_node != NULL && ff->ff_buffer != NULL);
|
|
|
|
|
|
|
|
/* Check for a read past the end of the file */
|
|
|
|
|
|
|
|
if (filep->f_pos > ff->ff_node->cn_size)
|
|
|
|
{
|
2018-03-20 17:37:42 +01:00
|
|
|
/* Start read position is past the end of file. Return the end-of-
|
|
|
|
* file indication.
|
|
|
|
*/
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-03-20 17:37:42 +01:00
|
|
|
else if ((filep->f_pos + buflen) > ff->ff_node->cn_size)
|
|
|
|
{
|
|
|
|
/* The final read position is past the end of file. Truncate the
|
|
|
|
* read length.
|
|
|
|
*/
|
|
|
|
|
|
|
|
buflen = ff->ff_node->cn_size - filep->f_pos;
|
|
|
|
}
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* Find the compressed block containing the current offset, f_pos */
|
|
|
|
|
|
|
|
dest = (FAR uint8_t *)buffer;
|
|
|
|
remaining = buflen;
|
|
|
|
fpos = filep->f_pos;
|
|
|
|
blkoffs = 0;
|
|
|
|
ulen = 0;
|
|
|
|
nexthdr = (FAR struct lzf_header_s *)
|
|
|
|
cromfs_offset2addr(fs, ff->ff_node->u.cn_blocks);
|
|
|
|
|
|
|
|
/* Look until we find the compressed block containing the start of the
|
|
|
|
* requested data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (remaining > 0)
|
|
|
|
{
|
|
|
|
/* Search for the next block containing the fpos file offset. This is
|
2020-07-05 17:30:43 +02:00
|
|
|
* real search on the first time through but the remaining blocks
|
|
|
|
* should be contiguous so that the logic should not loop.
|
2018-03-21 02:49:42 +01:00
|
|
|
*
|
2018-03-19 14:08:40 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-03-20 19:37:01 +01:00
|
|
|
uint32_t blksize;
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* Go to the next block */
|
|
|
|
|
|
|
|
currhdr = nexthdr;
|
2018-03-20 17:37:42 +01:00
|
|
|
blkoffs += ulen;
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
if (currhdr->lzf_type == LZF_TYPE0_HDR)
|
|
|
|
{
|
|
|
|
FAR struct lzf_type0_header_s *hdr0 =
|
|
|
|
(FAR struct lzf_type0_header_s *)currhdr;
|
|
|
|
|
|
|
|
ulen = (uint16_t)hdr0->lzf_len[0] << 8 |
|
2018-03-20 17:37:42 +01:00
|
|
|
(uint16_t)hdr0->lzf_len[1];
|
2018-03-20 19:37:01 +01:00
|
|
|
blksize = (uint32_t)ulen + LZF_TYPE0_HDR_SIZE;
|
2018-03-19 14:08:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR struct lzf_type1_header_s * hdr1 =
|
|
|
|
(FAR struct lzf_type1_header_s *)currhdr;
|
|
|
|
|
|
|
|
ulen = (uint16_t)hdr1->lzf_ulen[0] << 8 |
|
2018-03-20 17:37:42 +01:00
|
|
|
(uint16_t)hdr1->lzf_ulen[1];
|
2018-03-19 14:08:40 +01:00
|
|
|
clen = (uint16_t)hdr1->lzf_clen[0] << 8 |
|
2018-03-20 17:37:42 +01:00
|
|
|
(uint16_t)hdr1->lzf_clen[1];
|
2018-03-20 19:37:01 +01:00
|
|
|
blksize = (uint32_t)clen + LZF_TYPE1_HDR_SIZE;
|
2018-03-19 14:08:40 +01:00
|
|
|
}
|
|
|
|
|
2018-03-21 02:49:42 +01:00
|
|
|
nexthdr = (FAR struct lzf_header_s *)
|
|
|
|
((FAR uint8_t *)currhdr + blksize);
|
2018-03-19 14:08:40 +01:00
|
|
|
}
|
2018-03-21 02:49:42 +01:00
|
|
|
while (fpos >= (blkoffs + ulen));
|
2018-03-19 14:08:40 +01:00
|
|
|
|
2020-07-05 17:30:43 +02:00
|
|
|
/* Check if we need to decompress the next block into the user
|
|
|
|
* buffer.
|
|
|
|
*/
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
if (currhdr->lzf_type == LZF_TYPE0_HDR)
|
|
|
|
{
|
2018-03-21 02:49:42 +01:00
|
|
|
/* Just copy the uncompressed data copy data from image to the
|
|
|
|
* user buffer.
|
|
|
|
*/
|
2018-03-19 14:08:40 +01:00
|
|
|
|
2018-03-21 02:49:42 +01:00
|
|
|
copyoffs = (blkoffs >= filep->f_pos) ? 0 : filep->f_pos - blkoffs;
|
2018-03-19 14:08:40 +01:00
|
|
|
DEBUGASSERT(ulen > copyoffs);
|
|
|
|
copysize = ulen - copyoffs;
|
|
|
|
|
|
|
|
if (copysize > remaining) /* Clip to the size really needed */
|
|
|
|
{
|
|
|
|
copysize = remaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
src = (FAR const uint8_t *)currhdr + LZF_TYPE0_HDR_SIZE;
|
|
|
|
memcpy(dest, &src[copyoffs], copysize);
|
2018-03-24 18:30:35 +01:00
|
|
|
|
2021-04-20 17:31:55 +02:00
|
|
|
finfo("blkoffs=%" PRIu32 " ulen=%" PRIu16 " copysize=%u\n",
|
|
|
|
blkoffs, ulen, copysize);
|
2018-03-19 14:08:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If the source of the data is at the beginning of the compressed
|
2018-03-24 18:30:35 +01:00
|
|
|
* data buffer and if the uncompressed data would not overrun the
|
|
|
|
* buffer, then we can decompress directly into the user buffer.
|
2018-03-19 14:08:40 +01:00
|
|
|
*/
|
|
|
|
|
2018-03-24 18:30:35 +01:00
|
|
|
if (filep->f_pos <= blkoffs && ulen <= remaining)
|
2018-03-19 14:08:40 +01:00
|
|
|
{
|
2018-03-24 18:30:35 +01:00
|
|
|
uint32_t voloffs;
|
|
|
|
|
2018-03-19 14:08:40 +01:00
|
|
|
copyoffs = 0;
|
|
|
|
copysize = ulen;
|
2018-03-24 18:30:35 +01:00
|
|
|
|
|
|
|
/* Get the address and offset in the CROMFS image to obtain
|
|
|
|
* the data. Check if we already have this offset in the
|
|
|
|
* cache.
|
|
|
|
*/
|
|
|
|
|
|
|
|
src = (FAR const uint8_t *)currhdr + LZF_TYPE1_HDR_SIZE;
|
|
|
|
voloffs = cromfs_addr2offset(fs, src);
|
|
|
|
if (voloffs != ff->ff_offset)
|
2018-03-19 14:08:40 +01:00
|
|
|
{
|
2018-03-24 18:30:35 +01:00
|
|
|
unsigned int decomplen;
|
|
|
|
|
|
|
|
decomplen = lzf_decompress(src, clen, dest, fs->cv_bsize);
|
|
|
|
|
|
|
|
ff->ff_offset = voloffs;
|
|
|
|
ff->ff_ulen = decomplen;
|
2018-03-19 14:08:40 +01:00
|
|
|
}
|
|
|
|
|
2021-04-20 17:31:55 +02:00
|
|
|
finfo("voloffs=%" PRIu32 " blkoffs=%" PRIu32
|
|
|
|
" ulen=%" PRIu16 " ff_offset=%" PRIu32 " copysize=%u\n",
|
|
|
|
voloffs, blkoffs, ulen, ff->ff_offset, copysize);
|
2018-03-24 18:30:35 +01:00
|
|
|
DEBUGASSERT(ff->ff_ulen >= copysize);
|
2018-03-19 14:08:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-24 18:30:35 +01:00
|
|
|
uint32_t voloffs;
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* No, we will need to decompress into the our intermediate
|
|
|
|
* decompression buffer.
|
|
|
|
*/
|
|
|
|
|
2020-07-05 17:30:43 +02:00
|
|
|
copyoffs = (blkoffs >= filep->f_pos) ?
|
|
|
|
0 : filep->f_pos - blkoffs;
|
2018-03-19 14:08:40 +01:00
|
|
|
DEBUGASSERT(ulen > copyoffs);
|
|
|
|
copysize = ulen - copyoffs;
|
|
|
|
|
|
|
|
if (copysize > remaining) /* Clip to the size really needed */
|
|
|
|
{
|
|
|
|
copysize = remaining;
|
|
|
|
}
|
|
|
|
|
2018-03-24 18:30:35 +01:00
|
|
|
DEBUGASSERT((copyoffs + copysize) <= fs->cv_bsize);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
src = (FAR const uint8_t *)currhdr + LZF_TYPE1_HDR_SIZE;
|
2018-03-24 18:30:35 +01:00
|
|
|
voloffs = cromfs_addr2offset(fs, src);
|
|
|
|
if (voloffs != ff->ff_offset)
|
|
|
|
{
|
|
|
|
unsigned int decomplen;
|
|
|
|
|
|
|
|
decomplen = lzf_decompress(src, clen, ff->ff_buffer,
|
|
|
|
fs->cv_bsize);
|
|
|
|
|
|
|
|
ff->ff_offset = voloffs;
|
|
|
|
ff->ff_ulen = decomplen;
|
|
|
|
}
|
|
|
|
|
2021-04-20 17:31:55 +02:00
|
|
|
finfo("voloffs=%" PRIu32 " blkoffs=%" PRIu32 " ulen=%" PRIu16
|
|
|
|
" clen=%" PRIu16 " ff_offset=%" PRIu32
|
|
|
|
" copyoffs=%u copysize=%u\n",
|
|
|
|
voloffs, blkoffs, ulen, clen, ff->ff_offset,
|
|
|
|
copyoffs, copysize);
|
2018-03-24 18:30:35 +01:00
|
|
|
DEBUGASSERT(ff->ff_ulen >= (copyoffs + copysize));
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* Then copy to user buffer */
|
|
|
|
|
|
|
|
memcpy(dest, &ff->ff_buffer[copyoffs], copysize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 17:37:42 +01:00
|
|
|
/* Adjust pointers counts and offset */
|
2018-03-19 14:08:40 +01:00
|
|
|
|
2018-03-20 17:37:42 +01:00
|
|
|
dest += copysize;
|
|
|
|
remaining -= copysize;
|
|
|
|
fpos += copysize;
|
|
|
|
}
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* Update the file pointer */
|
|
|
|
|
|
|
|
filep->f_pos = fpos;
|
|
|
|
return buflen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int cromfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
finfo("cmd: %d arg: %08lx\n", cmd, arg);
|
|
|
|
|
|
|
|
/* No IOCTL commands yet supported */
|
|
|
|
|
|
|
|
return -ENOTTY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_dup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Duplicate open file data in the new file structure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int cromfs_dup(FAR const struct file *oldp, FAR struct file *newp)
|
|
|
|
{
|
|
|
|
FAR struct cromfs_volume_s *fs;
|
|
|
|
FAR struct cromfs_file_s *oldff;
|
|
|
|
FAR struct cromfs_file_s *newff;
|
|
|
|
|
|
|
|
finfo("Dup %p->%p\n", oldp, newp);
|
|
|
|
DEBUGASSERT(oldp->f_priv != NULL && oldp->f_inode != NULL &&
|
|
|
|
newp->f_priv == NULL && newp->f_inode != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the struct file instance */
|
|
|
|
|
2023-08-29 06:16:49 +02:00
|
|
|
fs = oldp->f_inode->i_private;
|
2018-03-19 14:08:40 +01:00
|
|
|
DEBUGASSERT(fs != NULL);
|
|
|
|
|
|
|
|
/* Get the open file instance from the file structure */
|
|
|
|
|
|
|
|
oldff = oldp->f_priv;
|
|
|
|
DEBUGASSERT(oldff->ff_node != NULL && oldff->ff_buffer != NULL);
|
|
|
|
|
|
|
|
/* Allocate and initialize an new open file instance referring to the
|
|
|
|
* same node.
|
|
|
|
*/
|
|
|
|
|
2020-07-05 17:30:43 +02:00
|
|
|
newff = kmm_zalloc(sizeof(struct cromfs_file_s));
|
2018-03-19 14:08:40 +01:00
|
|
|
if (newff == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a file buffer to support partial sector accesses */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
newff->ff_buffer = kmm_malloc(fs->cv_bsize);
|
2018-03-19 14:08:40 +01:00
|
|
|
if (newff->ff_buffer == NULL)
|
|
|
|
{
|
|
|
|
kmm_free(newff);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the node in the open file instance */
|
|
|
|
|
|
|
|
newff->ff_node = oldff->ff_node;
|
|
|
|
|
|
|
|
/* Copy the index from the old to the new file structure */
|
|
|
|
|
|
|
|
newp->f_priv = newff;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_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 cromfs_fstat(FAR const struct file *filep, FAR struct stat *buf)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct cromfs_volume_s *fs;
|
|
|
|
FAR struct cromfs_file_s *ff;
|
2018-03-20 19:37:01 +01:00
|
|
|
uint32_t fsize;
|
|
|
|
uint32_t bsize;
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
2023-08-28 11:17:05 +02:00
|
|
|
DEBUGASSERT(filep->f_priv != NULL);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* Get the mountpoint inode reference from the file structure and the
|
|
|
|
* volume private data from the inode structure
|
|
|
|
*/
|
|
|
|
|
|
|
|
ff = filep->f_priv;
|
|
|
|
DEBUGASSERT(ff->ff_node != NULL && ff->ff_buffer != NULL);
|
|
|
|
|
|
|
|
inode = filep->f_inode;
|
|
|
|
fs = inode->i_private;
|
|
|
|
|
|
|
|
/* Return the stat info */
|
|
|
|
|
|
|
|
fsize = ff->ff_node->cn_size;
|
|
|
|
bsize = fs->cv_bsize;
|
|
|
|
|
|
|
|
buf->st_mode = ff->ff_node->cn_mode;
|
|
|
|
buf->st_size = fsize;
|
|
|
|
buf->st_blksize = bsize;
|
|
|
|
buf->st_blocks = (fsize + (bsize - 1)) / bsize;
|
|
|
|
buf->st_atime = 0;
|
|
|
|
buf->st_mtime = 0;
|
|
|
|
buf->st_ctime = 0;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_opendir
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Open a directory for read access
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int cromfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct fs_dirent_s **dir)
|
2018-03-19 14:08:40 +01:00
|
|
|
{
|
|
|
|
FAR const struct cromfs_volume_s *fs;
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct cromfs_dir_s *cdir;
|
2020-08-10 19:08:08 +02:00
|
|
|
FAR struct cromfs_nodeinfo_s info;
|
|
|
|
uint32_t offset;
|
2018-03-19 14:08:40 +01:00
|
|
|
int ret;
|
|
|
|
|
2018-03-20 23:20:31 +01:00
|
|
|
finfo("relpath: %s\n", relpath);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the inode instance */
|
|
|
|
|
|
|
|
fs = mountpt->i_private;
|
|
|
|
|
2019-10-27 18:48:14 +01:00
|
|
|
/* Locate the node for this relative path */
|
2018-03-19 14:08:40 +01:00
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
ret = cromfs_find_node(fs, relpath, &info, &offset);
|
2018-03-19 14:08:40 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-07-05 17:30:43 +02:00
|
|
|
/* Nothing exists at that relative path (or a really bad error
|
|
|
|
* occurred)
|
|
|
|
*/
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that the node is a directory */
|
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
if (!S_ISDIR(info.ci_mode))
|
2018-03-19 14:08:40 +01:00
|
|
|
{
|
|
|
|
return -ENOTDIR;
|
|
|
|
}
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
cdir = kmm_zalloc(sizeof(*cdir));
|
|
|
|
if (cdir == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2018-03-19 14:08:40 +01:00
|
|
|
/* Set the start node and next node to the first entry in the directory */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
cdir->cr_firstoffset = info.ci_child;
|
|
|
|
cdir->cr_curroffset = info.ci_child;
|
|
|
|
*dir = &cdir->cr_base;
|
2018-03-19 14:08:40 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_closedir
|
|
|
|
*
|
|
|
|
* Description: close directory.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int cromfs_closedir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(mountpt != NULL);
|
|
|
|
kmm_free(dir);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-19 14:08:40 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_readdir
|
|
|
|
*
|
|
|
|
* Description: Read the next directory entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-07-26 08:56:41 +02:00
|
|
|
static int cromfs_readdir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir,
|
|
|
|
FAR struct dirent *entry)
|
2018-03-19 14:08:40 +01:00
|
|
|
{
|
|
|
|
FAR const struct cromfs_volume_s *fs;
|
|
|
|
FAR const struct cromfs_node_s *node;
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct cromfs_dir_s *cdir;
|
2020-08-10 19:08:08 +02:00
|
|
|
struct cromfs_node_s newnode;
|
2018-03-19 14:08:40 +01:00
|
|
|
FAR char *name;
|
2018-03-20 19:37:01 +01:00
|
|
|
uint32_t offset;
|
2020-08-10 19:08:08 +02:00
|
|
|
int ret;
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
finfo("mountpt: %p dir: %p\n", mountpt, dir);
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the inode instance */
|
|
|
|
|
|
|
|
fs = mountpt->i_private;
|
2022-08-05 06:31:25 +02:00
|
|
|
cdir = (FAR struct cromfs_dir_s *)dir;
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* Have we reached the end of the directory */
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
offset = cdir->cr_curroffset;
|
2018-03-19 14:08:40 +01:00
|
|
|
if (offset == 0)
|
|
|
|
{
|
|
|
|
/* We signal the end of the directory by returning the
|
|
|
|
* special error -ENOENT
|
|
|
|
*/
|
|
|
|
|
2021-04-20 17:31:55 +02:00
|
|
|
finfo("Entry %" PRIu32 ": End of directory\n", offset);
|
2018-03-19 14:08:40 +01:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert the offset to a node address (assuming that everything is in-
|
|
|
|
* memory)
|
|
|
|
*/
|
|
|
|
|
|
|
|
node = (FAR const struct cromfs_node_s *)cromfs_offset2addr(fs, offset);
|
2018-12-06 23:42:20 +01:00
|
|
|
if (node == NULL)
|
|
|
|
{
|
|
|
|
/* We signal the end of the directory by returning the
|
|
|
|
* special error -ENOENT
|
|
|
|
*/
|
|
|
|
|
2021-04-20 17:31:55 +02:00
|
|
|
finfo("Entry %" PRIu32 ": End of directory\n", offset);
|
2018-12-06 23:42:20 +01:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
2018-03-19 14:08:40 +01:00
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
/* Get the attributes of the node by following the hard link. */
|
|
|
|
|
|
|
|
ret = cromfs_follow_link(fs, &node, false, &newnode);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-19 14:08:40 +01:00
|
|
|
/* Save the filename and file type */
|
|
|
|
|
|
|
|
name = (FAR char *)cromfs_offset2addr(fs, node->cn_name);
|
2021-04-20 17:31:55 +02:00
|
|
|
finfo("Entry %" PRIu32 ": %s\n", offset, name);
|
2022-07-26 08:56:41 +02:00
|
|
|
strlcpy(entry->d_name, name, sizeof(entry->d_name));
|
2018-03-19 14:08:40 +01:00
|
|
|
|
2020-07-05 17:30:43 +02:00
|
|
|
switch (node->cn_mode & S_IFMT)
|
2018-03-19 14:08:40 +01:00
|
|
|
{
|
|
|
|
case S_IFDIR: /* Directory */
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_DIRECTORY;
|
2018-03-19 14:08:40 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case S_IFREG: /* Regular file */
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_FILE;
|
2018-03-19 14:08:40 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case S_IFIFO: /* FIFO */
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_FIFO;
|
2020-07-05 18:34:32 +02:00
|
|
|
break;
|
|
|
|
|
2018-03-19 14:08:40 +01:00
|
|
|
case S_IFCHR: /* Character driver */
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_CHR;
|
2020-07-05 18:34:32 +02:00
|
|
|
break;
|
|
|
|
|
2018-03-19 14:08:40 +01:00
|
|
|
case S_IFBLK: /* Block driver */
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_BLK;
|
2020-07-05 18:34:32 +02:00
|
|
|
break;
|
|
|
|
|
2018-03-19 14:08:40 +01:00
|
|
|
case S_IFMQ: /* Message queue */
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_MQ;
|
2020-07-05 18:34:32 +02:00
|
|
|
break;
|
|
|
|
|
2018-03-19 14:08:40 +01:00
|
|
|
case S_IFSEM: /* Semaphore */
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_SEM;
|
2020-07-05 18:34:32 +02:00
|
|
|
break;
|
|
|
|
|
2018-03-19 14:08:40 +01:00
|
|
|
case S_IFSHM: /* Shared memory */
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_SHM;
|
2020-07-05 18:34:32 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case S_IFMTD: /* MTD driver */
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_MTD;
|
2020-07-05 18:34:32 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case S_IFSOCK: /* Socket */
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_SOCK;
|
2020-07-05 18:34:32 +02:00
|
|
|
break;
|
|
|
|
|
2018-03-19 14:08:40 +01:00
|
|
|
default:
|
|
|
|
DEBUGPANIC();
|
2022-07-26 08:56:41 +02:00
|
|
|
entry->d_type = DTYPE_UNKNOWN;
|
2018-03-19 14:08:40 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up the next directory entry offset. NOTE that we could use the
|
|
|
|
* standard f_pos instead of our own private fb_index.
|
|
|
|
*/
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
cdir->cr_curroffset = node->cn_peer;
|
2018-03-19 14:08:40 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_rewindir
|
|
|
|
*
|
|
|
|
* Description: Reset directory read to the first entry
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
static int cromfs_rewinddir(FAR struct inode *mountpt,
|
|
|
|
FAR struct fs_dirent_s *dir)
|
2018-03-19 14:08:40 +01:00
|
|
|
{
|
2022-08-05 06:31:25 +02:00
|
|
|
FAR struct cromfs_dir_s *cdir;
|
|
|
|
|
2018-03-20 23:20:31 +01:00
|
|
|
finfo("mountpt: %p dir: %p\n", mountpt, dir);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
2022-08-05 06:31:25 +02:00
|
|
|
cdir = (FAR struct cromfs_dir_s *)dir;
|
|
|
|
cdir->cr_curroffset = cdir->cr_firstoffset;
|
2018-03-19 14:08:40 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_bind
|
|
|
|
*
|
|
|
|
* Description: This implements a portion of the mount operation. This
|
|
|
|
* function allocates and initializes the mountpoint private data and
|
|
|
|
* binds the blockdriver inode to the filesystem private data. The final
|
|
|
|
* binding of the private data (containing the blockdriver) to the
|
|
|
|
* mountpoint is performed by mount().
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int cromfs_bind(FAR struct inode *blkdriver, const void *data,
|
|
|
|
void **handle)
|
|
|
|
{
|
|
|
|
finfo("blkdriver: %p data: %p handle: %p\n", blkdriver, data, handle);
|
2018-03-20 17:37:42 +01:00
|
|
|
|
2018-03-19 14:08:40 +01:00
|
|
|
DEBUGASSERT(blkdriver == NULL && handle != NULL);
|
2018-03-20 17:37:42 +01:00
|
|
|
DEBUGASSERT(g_cromfs_image.cv_magic == CROMFS_MAGIC);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* Return the new file system handle */
|
|
|
|
|
|
|
|
*handle = (FAR void *)&g_cromfs_image;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_unbind
|
|
|
|
*
|
|
|
|
* Description: This implements the filesystem portion of the umount
|
|
|
|
* operation.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int cromfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
2018-03-20 23:20:31 +01:00
|
|
|
finfo("handle: %p blkdriver: %p flags: %02x\n",
|
|
|
|
handle, blkdriver, flags);
|
2018-03-19 14:08:40 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_statfs
|
|
|
|
*
|
|
|
|
* Description: Return filesystem statistics
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int cromfs_statfs(struct inode *mountpt, struct statfs *buf)
|
|
|
|
{
|
|
|
|
FAR struct cromfs_volume_s *fs;
|
|
|
|
|
|
|
|
finfo("mountpt: %p buf: %p\n", mountpt, buf);
|
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
|
|
|
|
|
|
|
|
/* Recover our private data from the inode instance */
|
|
|
|
|
|
|
|
fs = mountpt->i_private;
|
|
|
|
|
|
|
|
/* Fill in the statfs info. */
|
|
|
|
|
|
|
|
buf->f_type = CROMFS_MAGIC;
|
|
|
|
buf->f_namelen = NAME_MAX;
|
|
|
|
buf->f_bsize = fs->cv_bsize;
|
|
|
|
buf->f_blocks = fs->cv_nblocks;
|
|
|
|
buf->f_files = fs->cv_nnodes;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: cromfs_stat
|
|
|
|
*
|
|
|
|
* Description: Return information about a file or directory
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int cromfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
|
|
|
|
FAR struct stat *buf)
|
|
|
|
{
|
|
|
|
FAR const struct cromfs_volume_s *fs;
|
2020-08-10 19:08:08 +02:00
|
|
|
struct cromfs_nodeinfo_s info;
|
|
|
|
uint32_t offset;
|
2018-03-19 14:08:40 +01:00
|
|
|
int ret;
|
|
|
|
|
2018-03-20 23:20:31 +01:00
|
|
|
finfo("mountpt: %p relpath: %s buf: %p\n", mountpt, relpath, buf);
|
2018-03-19 14:08:40 +01:00
|
|
|
|
|
|
|
/* Sanity checks */
|
|
|
|
|
2019-03-01 22:00:00 +01:00
|
|
|
DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL && buf != NULL);
|
2018-03-19 14:08:40 +01:00
|
|
|
memset(buf, 0, sizeof(struct stat));
|
|
|
|
|
|
|
|
/* Recover our private data from the inode instance */
|
|
|
|
|
|
|
|
fs = mountpt->i_private;
|
|
|
|
|
|
|
|
/* Locate the node for this relative path */
|
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
ret = cromfs_find_node(fs, relpath, &info, &offset);
|
2018-03-19 14:08:40 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* Return the struct stat info associate with this node */
|
|
|
|
|
2020-08-10 19:08:08 +02:00
|
|
|
buf->st_mode = info.ci_mode;
|
|
|
|
buf->st_size = info.ci_size;
|
2018-03-19 14:08:40 +01:00
|
|
|
buf->st_blksize = fs->cv_bsize;
|
2020-08-10 19:08:08 +02:00
|
|
|
buf->st_blocks = (info.ci_size + (fs->cv_bsize - 1)) / fs->cv_bsize;
|
2018-03-19 14:08:40 +01:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_CROMFS */
|