fs/cromfs and tools/gencromfs: Various fixes for traversal relative file paths. Biggest changes is in types used: Cannot use size_t or mode_t in common structures because they have different sizes on the (64-bit) host and the (32-bit) target. Use uint32_t instead of size_t for offsets. Use uint16_t instead of mode_t.

This commit is contained in:
Gregory Nutt
2018-03-20 12:37:01 -06:00
parent 87fd4e2916
commit 22484386ee
5 changed files with 109 additions and 82 deletions
+22 -14
View File
@@ -47,6 +47,17 @@
* Public Types
****************************************************************************/
/* Maximum size of an offset. This should normally be size_t since this is
* an in-memory file system. However, uint32_t is 32-bits on most 32-bit
* target machines but 64-bits on 64-host machines. We restrict offsets to
* 32-bits for commonality (limiting the size of the CROMFS image to 4Gb).
*
* REVISIT: What about small memory systems where the size_t is only 16-bits?
*
* Similarly, the NuttX mode_t is only 16-bits so uint16_t is explicitly used
* for NuttX file modes.
*/
/* This structure describes the CROMFS volume. It provides most of the
* information needed for statfs() including:
*
@@ -70,10 +81,7 @@
*
* The volume header is followed immediately by the root directory node. An
* offset to that node is used to permit future variable length data (such as
* a volumne name) which may intervene.
*
* Since this is an in-memory file system, size_t is the most relevant type for
* internal file system offsets.
* a volume name) which may intervene.
*/
struct cromfs_volume_s
@@ -81,9 +89,9 @@ struct cromfs_volume_s
uint32_t cv_magic; /* Must be first. Must be CROMFS_MAGIC */
uint16_t cv_nnodes; /* Total number of nodes in-use */
uint16_t cv_nblocks; /* Total number of data blocks in-use */
size_t cv_root; /* Offset to the first node in the root file system */
size_t cv_fsize; /* Size of the compressed file system image */
size_t cv_bsize; /* Optimal block size for transfers */
uint32_t cv_root; /* Offset to the first node in the root file system */
uint32_t cv_fsize; /* Size of the compressed file system image */
uint32_t cv_bsize; /* Optimal block size for transfers */
};
/* This describes one node in the CROMFS file system. It holds node meta
@@ -111,16 +119,16 @@ struct cromfs_volume_s
struct cromfs_node_s
{
mode_t cn_mode; /* File type, attributes, and access mode bits */
size_t cn_name; /* Offset from the beginning of the volume header to the
uint16_t cn_mode; /* File type, attributes, and access mode bits */
uint32_t cn_name; /* Offset from the beginning of the volume header to the
* node name string. NUL-terminated. */
size_t cn_size; /* Size of the uncompressed data (in bytes) */
size_t cn_peer; /* Offset to next node in this directory (for readdir()) */
uint32_t cn_size; /* Size of the uncompressed data (in bytes) */
uint32_t cn_peer; /* Offset to next node in this directory (for readdir()) */
union
{
size_t cn_child; /* Offset to first node in sub-directory (directories only) */
size_t cn_link; /* Offset to an arbitrary node (for hard link) */
size_t cn_blocks; /* Offset to first block of compressed data (for read) */
uint32_t cn_child; /* Offset to first node in sub-directory (directories only) */
uint32_t cn_link; /* Offset to an arbitrary node (for hard link) */
uint32_t cn_blocks; /* Offset to first block of compressed data (for read) */
} u;
};
+16 -15
View File
@@ -100,8 +100,8 @@ struct cromfs_comparenode_s
/* Helpers */
static FAR void *cromfs_offset2addr(FAR const struct cromfs_volume_s *fs,
size_t offset);
static size_t cromfs_addr2offset(FAR const struct cromfs_volume_s *fs,
uint32_t offset);
static uint32_t cromfs_addr2offset(FAR const struct cromfs_volume_s *fs,
FAR const void *addr);
static int cromfs_foreach_node(FAR const struct cromfs_volume_s *fs,
cromfs_foreach_t callback, FAR void *arg);
@@ -200,7 +200,7 @@ extern const struct cromfs_volume_s g_cromfs_image;
****************************************************************************/
static FAR void *cromfs_offset2addr(FAR const struct cromfs_volume_s *fs,
size_t offset)
uint32_t offset)
{
/* Zero offset is a specials case: It corresponds to a NULL address */
@@ -222,12 +222,12 @@ static FAR void *cromfs_offset2addr(FAR const struct cromfs_volume_s *fs,
* Name: cromfs_offset2addr
****************************************************************************/
static size_t cromfs_addr2offset(FAR const struct cromfs_volume_s *fs,
FAR const void *addr)
static uint32_t cromfs_addr2offset(FAR const struct cromfs_volume_s *fs,
FAR const void *addr)
{
uintptr_t start;
uintptr_t target;
size_t offset;
uint32_t offset;
/* NULL is a specials case: It corresponds to offset zero */
@@ -346,7 +346,8 @@ static int cromfs_comparenode(FAR const struct cromfs_volume_s *fs,
* and return 1 to stop the traversal.
*/
*cpnode->node = node;
*cpnode->node = (FAR const struct cromfs_node_s *)
cromfs_offset2addr(fs, node->u.cn_child);
return 1;
}
@@ -574,7 +575,7 @@ static ssize_t cromfs_read(FAR struct file *filep, FAR char *buffer,
FAR const uint8_t *src;
off_t fpos;
size_t remaining;
size_t blkoffs;
uint32_t blkoffs;
uint16_t ulen;
uint16_t clen;
unsigned int copysize;
@@ -638,7 +639,7 @@ static ssize_t cromfs_read(FAR struct file *filep, FAR char *buffer,
do
{
size_t blksize;
uint32_t blksize;
/* Go to the next block */
@@ -652,7 +653,7 @@ static ssize_t cromfs_read(FAR struct file *filep, FAR char *buffer,
ulen = (uint16_t)hdr0->lzf_len[0] << 8 |
(uint16_t)hdr0->lzf_len[1];
blksize = (size_t)ulen + LZF_TYPE0_HDR_SIZE;
blksize = (uint32_t)ulen + LZF_TYPE0_HDR_SIZE;
}
else
{
@@ -663,7 +664,7 @@ static ssize_t cromfs_read(FAR struct file *filep, FAR char *buffer,
(uint16_t)hdr1->lzf_ulen[1];
clen = (uint16_t)hdr1->lzf_clen[0] << 8 |
(uint16_t)hdr1->lzf_clen[1];
blksize = (size_t)clen + LZF_TYPE1_HDR_SIZE;
blksize = (uint32_t)clen + LZF_TYPE1_HDR_SIZE;
}
nexthdr = (FAR struct lzf_header_s *)((FAR uint8_t *)currhdr + blksize);
@@ -836,8 +837,8 @@ 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;
size_t fsize;
size_t bsize;
uint32_t fsize;
uint32_t bsize;
/* Sanity checks */
@@ -916,7 +917,7 @@ static int cromfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
/* Set the start node and next node to the first entry in the directory */
dir->u.cromfs.cr_firstoffset = (size_t)cromfs_addr2offset(fs, node);
dir->u.cromfs.cr_firstoffset = cromfs_addr2offset(fs, node);
dir->u.cromfs.cr_curroffset = dir->u.cromfs.cr_firstoffset;
return OK;
}
@@ -933,7 +934,7 @@ static int cromfs_readdir(struct inode *mountpt, struct fs_dirent_s *dir)
FAR const struct cromfs_volume_s *fs;
FAR const struct cromfs_node_s *node;
FAR char *name;
size_t offset;
uint32_t offset;
finfo("mountpt: %p dir: %p\n", mountpt, dir);