2010-01-19 Joel Sherrill <joel.sherrill@oarcorp.com>

* libfs/src/imfs/imfs.h, libfs/src/imfs/imfs_creat.c,
	libfs/src/imfs/imfs_initsupp.c, libfs/src/imfs/imfs_link.c,
	libfs/src/imfs/imfs_load_tar.c, libfs/src/imfs/imfs_mknod.c,
	libfs/src/imfs/imfs_symlink.c: Create special helper method for
	creating the j-node for the root directory. This lets us assume that
	every j-node created otherwise has a parent node.
This commit is contained in:
Joel Sherrill
2010-01-19 19:31:00 +00:00
parent f1eb199a2e
commit cb4e99201e
8 changed files with 215 additions and 161 deletions

View File

@@ -1,3 +1,12 @@
2010-01-19 Joel Sherrill <joel.sherrill@oarcorp.com>
* libfs/src/imfs/imfs.h, libfs/src/imfs/imfs_creat.c,
libfs/src/imfs/imfs_initsupp.c, libfs/src/imfs/imfs_link.c,
libfs/src/imfs/imfs_load_tar.c, libfs/src/imfs/imfs_mknod.c,
libfs/src/imfs/imfs_symlink.c: Create special helper method for
creating the j-node for the root directory. This lets us assume that
every j-node created otherwise has a parent node.
2010-01-18 Sebastian Huber <sebastian.huber@embedded-brains.de> 2010-01-18 Sebastian Huber <sebastian.huber@embedded-brains.de>
* libblock/include/rtems/bdbuf.h: Documentation. Renamed * libblock/include/rtems/bdbuf.h: Documentation. Renamed

View File

@@ -1,7 +1,7 @@
/* /*
* Header file for the In-Memory File System * Header file for the In-Memory File System
* *
* COPYRIGHT (c) 1989-2007. * COPYRIGHT (c) 1989-2010.
* On-Line Applications Research Corporation (OAR). * On-Line Applications Research Corporation (OAR).
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
@@ -322,7 +322,6 @@ extern int IMFS_eval_path(
rtems_filesystem_location_info_t *pathloc /* IN/OUT */ rtems_filesystem_location_info_t *pathloc /* IN/OUT */
); );
extern int IMFS_link( extern int IMFS_link(
rtems_filesystem_location_info_t *to_loc, /* IN */ rtems_filesystem_location_info_t *to_loc, /* IN */
rtems_filesystem_location_info_t *parent_loc, /* IN */ rtems_filesystem_location_info_t *parent_loc, /* IN */
@@ -351,6 +350,14 @@ extern int IMFS_mknod(
rtems_filesystem_location_info_t *pathloc /* IN/OUT */ rtems_filesystem_location_info_t *pathloc /* IN/OUT */
); );
extern IMFS_jnode_t *IMFS_allocate_node(
IMFS_jnode_types_t type, /* IN */
const char *name, /* IN */
mode_t mode /* IN */
);
extern IMFS_jnode_t *IMFS_create_root_node(void);
extern IMFS_jnode_t *IMFS_create_node( extern IMFS_jnode_t *IMFS_create_node(
rtems_filesystem_location_info_t *parent_loc, /* IN */ rtems_filesystem_location_info_t *parent_loc, /* IN */
IMFS_jnode_types_t type, /* IN */ IMFS_jnode_types_t type, /* IN */
@@ -360,9 +367,9 @@ extern IMFS_jnode_t *IMFS_create_node(
); );
extern int IMFS_evaluate_for_make( extern int IMFS_evaluate_for_make(
const char *path, /* IN */ const char *path, /* IN */
rtems_filesystem_location_info_t *pathloc, /* IN/OUT */ rtems_filesystem_location_info_t *pathloc, /* IN/OUT */
const char **name /* OUT */ const char **name /* OUT */
); );
extern int IMFS_mount( extern int IMFS_mount(

View File

@@ -3,7 +3,7 @@
* *
* Routine to create a new in memory file system node. * Routine to create a new in memory file system node.
* *
* COPYRIGHT (c) 1989-1999. * COPYRIGHT (c) 1989-2010.
* On-Line Applications Research Corporation (OAR). * On-Line Applications Research Corporation (OAR).
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
@@ -23,6 +23,10 @@
#include "imfs.h" #include "imfs.h"
#include <rtems/libio_.h> #include <rtems/libio_.h>
/*
* Create an IMFS filesystem node of an arbitrary type that is NOT
* the root directory node.
*/
IMFS_jnode_t *IMFS_create_node( IMFS_jnode_t *IMFS_create_node(
rtems_filesystem_location_info_t *parent_loc, rtems_filesystem_location_info_t *parent_loc,
IMFS_jnode_types_t type, IMFS_jnode_types_t type,
@@ -32,57 +36,25 @@ IMFS_jnode_t *IMFS_create_node(
) )
{ {
IMFS_jnode_t *node; IMFS_jnode_t *node;
struct timeval tv; IMFS_jnode_t *parent;
IMFS_jnode_t *parent = NULL;
IMFS_fs_info_t *fs_info; IMFS_fs_info_t *fs_info;
if ( parent_loc != NULL ) /*
parent = parent_loc->node_access; * MUST have a parent node to call this routine.
*/
if ( parent_loc == NULL )
return NULL;
/* /*
* Allocate an IMFS jnode * Allocate filesystem node and fill in basic information
*/ */
node = IMFS_allocate_node( type, name, mode & ~rtems_filesystem_umask );
node = calloc( 1, sizeof( IMFS_jnode_t ) );
if ( !node ) if ( !node )
return NULL; return NULL;
/*
* Fill in the basic information
*/
node->st_nlink = 1;
node->type = type;
strncpy( node->name, name, IMFS_NAME_MAX );
/*
* Fill in the mode and permission information for the jnode structure.
*/
node->st_mode = mode & ~rtems_filesystem_umask;
#if defined(RTEMS_POSIX_API)
node->st_uid = geteuid();
node->st_gid = getegid();
#else
node->st_uid = 0;
node->st_gid = 0;
#endif
/*
* Now set all the times.
*/
gettimeofday( &tv, 0 );
node->stat_atime = (time_t) tv.tv_sec;
node->stat_mtime = (time_t) tv.tv_sec;
node->stat_ctime = (time_t) tv.tv_sec;
/* /*
* Set the type specific information * Set the type specific information
*/ */
switch (type) { switch (type) {
case IMFS_DIRECTORY: case IMFS_DIRECTORY:
rtems_chain_initialize_empty(&node->info.directory.Entries); rtems_chain_initialize_empty(&node->info.directory.Entries);
@@ -122,17 +94,86 @@ IMFS_jnode_t *IMFS_create_node(
} }
/* /*
* If this node has a parent, then put it in that directory list. * This node MUST have a parent, so put it in that directory list.
*/ */
parent = parent_loc->node_access;
fs_info = parent_loc->mt_entry->fs_info;
if ( parent ) { node->Parent = parent;
rtems_chain_append( &parent->info.directory.Entries, &node->Node ); node->st_ino = ++fs_info->ino_count;
node->Parent = parent;
fs_info = parent_loc->mt_entry->fs_info;
node->st_ino = ++fs_info->ino_count;
}
rtems_chain_append( &parent->info.directory.Entries, &node->Node );
return node;
}
/*
* Allocate filesystem node and fill in basic information
*/
IMFS_jnode_t *IMFS_allocate_node(
IMFS_jnode_types_t type,
const char *name,
mode_t mode
)
{
IMFS_jnode_t *node;
struct timeval tv;
/*
* Allocate an IMFS jnode
*/
node = calloc( 1, sizeof( IMFS_jnode_t ) );
if ( !node )
return NULL;
/*
* Fill in the basic information
*/
node->st_nlink = 1;
node->type = type;
strncpy( node->name, name, IMFS_NAME_MAX );
/*
* Fill in the mode and permission information for the jnode structure.
*/
node->st_mode = mode;
#if defined(RTEMS_POSIX_API)
node->st_uid = geteuid();
node->st_gid = getegid();
#else
node->st_uid = 0;
node->st_gid = 0;
#endif
/*
* Now set all the times.
*/
gettimeofday( &tv, 0 );
node->stat_atime = (time_t) tv.tv_sec;
node->stat_mtime = (time_t) tv.tv_sec;
node->stat_ctime = (time_t) tv.tv_sec;
return node;
}
IMFS_jnode_t *IMFS_create_root_node(void)
{
IMFS_jnode_t *node;
/*
* Allocate filesystem node and fill in basic information
*/
node = IMFS_allocate_node( IMFS_DIRECTORY, "", (S_IFDIR | 0755) );
if ( !node )
return NULL;
/*
* Set the type specific information
*
* NOTE: Root node is always a directory.
*/
rtems_chain_initialize_empty(&node->info.directory.Entries);
return node; return node;
} }

View File

@@ -1,7 +1,7 @@
/* /*
* IMFS Initialization * IMFS Initialization
* *
* COPYRIGHT (c) 1989-1999. * COPYRIGHT (c) 1989-2010.
* On-Line Applications Research Corporation (OAR). * On-Line Applications Research Corporation (OAR).
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
@@ -43,12 +43,11 @@ static int IMFS_determine_bytes_per_block(
{ {
bool is_valid = false; bool is_valid = false;
int bit_mask; int bit_mask;
/* /*
* check, whether requested bytes per block is valid * check, whether requested bytes per block is valid
*/ */
for (bit_mask = 16; for (bit_mask = 16; !is_valid && (bit_mask <= 512); bit_mask <<= 1) {
!is_valid && (bit_mask <= 512);
bit_mask <<= 1) {
if (bit_mask == requested_bytes_per_block) { if (bit_mask == requested_bytes_per_block) {
is_valid = true; is_valid = true;
} }
@@ -57,14 +56,12 @@ static int IMFS_determine_bytes_per_block(
? requested_bytes_per_block ? requested_bytes_per_block
: default_bytes_per_block); : default_bytes_per_block);
return 0; return 0;
} }
/* /*
* IMFS_initialize * IMFS_initialize
*/ */
int IMFS_initialize_support( int IMFS_initialize_support(
rtems_filesystem_mount_table_entry_t *temp_mt_entry, rtems_filesystem_mount_table_entry_t *temp_mt_entry,
const rtems_filesystem_operations_table *op_table, const rtems_filesystem_operations_table *op_table,
@@ -87,15 +84,7 @@ int IMFS_initialize_support(
* *
* NOTE: UNIX root is 755 and owned by root/root (0/0). * NOTE: UNIX root is 755 and owned by root/root (0/0).
*/ */
temp_mt_entry->mt_fs_root.node_access = IMFS_create_root_node();
temp_mt_entry->mt_fs_root.node_access = IMFS_create_node(
NULL,
IMFS_DIRECTORY,
"",
( S_IFDIR | 0755 ),
NULL
);
temp_mt_entry->mt_fs_root.handlers = directory_handlers; temp_mt_entry->mt_fs_root.handlers = directory_handlers;
temp_mt_entry->mt_fs_root.ops = op_table; temp_mt_entry->mt_fs_root.ops = op_table;
temp_mt_entry->pathconf_limits_and_options = IMFS_LIMITS_AND_OPTIONS; temp_mt_entry->pathconf_limits_and_options = IMFS_LIMITS_AND_OPTIONS;
@@ -104,7 +93,7 @@ int IMFS_initialize_support(
* Create custom file system data. * Create custom file system data.
*/ */
fs_info = calloc( 1, sizeof( IMFS_fs_info_t ) ); fs_info = calloc( 1, sizeof( IMFS_fs_info_t ) );
if ( !fs_info ){ if ( !fs_info ) {
free(temp_mt_entry->mt_fs_root.node_access); free(temp_mt_entry->mt_fs_root.node_access);
rtems_set_errno_and_return_minus_one(ENOMEM); rtems_set_errno_and_return_minus_one(ENOMEM);
} }

View File

@@ -38,7 +38,6 @@ int IMFS_link(
/* /*
* Verify this node can be linked to. * Verify this node can be linked to.
*/ */
info.hard_link.link_node = to_loc->node_access; info.hard_link.link_node = to_loc->node_access;
if ( info.hard_link.link_node->st_nlink >= LINK_MAX ) if ( info.hard_link.link_node->st_nlink >= LINK_MAX )
rtems_set_errno_and_return_minus_one( EMLINK ); rtems_set_errno_and_return_minus_one( EMLINK );
@@ -46,17 +45,18 @@ int IMFS_link(
/* /*
* Remove any separators at the end of the string. * Remove any separators at the end of the string.
*/ */
IMFS_get_token( token, strlen( token ), new_name, &i ); IMFS_get_token( token, strlen( token ), new_name, &i );
/* /*
* Create a new link node. * Create a new link node.
* *
* NOTE: Coverity thinks this is a resource leak since a node * NOTE: Coverity Id 19 reports this as a leak
* is created but never deleted. The scope of the allocation * While technically not a leak, it indicated that IMFS_create_node
* is that of a file -- not this method. Coverity Id 19. * was ONLY passed a NULL when we created the root node. We
* added a new IMFS_create_root_node() so this path no longer
* existed. The result was simpler code which should not have
* this path.
*/ */
new_node = IMFS_create_node( new_node = IMFS_create_node(
parent_loc, parent_loc,
IMFS_HARD_LINK, IMFS_HARD_LINK,
@@ -71,7 +71,6 @@ int IMFS_link(
/* /*
* Increment the link count of the node being pointed to. * Increment the link count of the node being pointed to.
*/ */
info.hard_link.link_node->st_nlink++; info.hard_link.link_node->st_nlink++;
IMFS_update_ctime( info.hard_link.link_node ); IMFS_update_ctime( info.hard_link.link_node );

View File

@@ -79,29 +79,34 @@
* create_node. * create_node.
*/ */
int rtems_tarfs_load( int rtems_tarfs_load(
char *mountpoint, char *mountpoint,
uint8_t *tar_image, uint8_t *tar_image,
size_t tar_size size_t tar_size
) )
{ {
rtems_filesystem_location_info_t root_loc; rtems_filesystem_location_info_t root_loc;
rtems_filesystem_location_info_t loc; rtems_filesystem_location_info_t loc;
const char *hdr_ptr; const char *hdr_ptr;
char filename[100]; char filename[100];
char full_filename[256]; char full_filename[256];
int hdr_chksum; int hdr_chksum;
unsigned char linkflag; unsigned char linkflag;
unsigned long file_size; unsigned long file_size;
unsigned long file_mode; unsigned long file_mode;
int offset; int offset;
unsigned long nblocks; unsigned long nblocks;
IMFS_jnode_t *node; IMFS_jnode_t *node;
int status; int status;
status = rtems_filesystem_evaluate_path(mountpoint, strlen(mountpoint), status = rtems_filesystem_evaluate_path(
0, &root_loc, 0); mountpoint,
strlen(mountpoint),
0,
&root_loc,
0
);
if (status != 0) if (status != 0)
return(-1); return -1;
if (root_loc.ops != &IMFS_ops) if (root_loc.ops != &IMFS_ops)
return -1; return -1;
@@ -111,66 +116,70 @@ int rtems_tarfs_load(
*/ */
offset = 0; offset = 0;
while (1) { while (1) {
if (offset + 512 > tar_size) if (offset + 512 > tar_size)
break; break;
/* /*
* Read a header. * Read a header.
*/ */
hdr_ptr = (char *) &tar_image[offset]; hdr_ptr = (char *) &tar_image[offset];
offset += 512; offset += 512;
if (strncmp(&hdr_ptr[257], "ustar ", 7)) if (strncmp(&hdr_ptr[257], "ustar ", 7))
break; break;
strncpy(filename, hdr_ptr, MAX_NAME_FIELD_SIZE); strncpy(filename, hdr_ptr, MAX_NAME_FIELD_SIZE);
filename[MAX_NAME_FIELD_SIZE] = '\0'; filename[MAX_NAME_FIELD_SIZE] = '\0';
linkflag = hdr_ptr[156]; linkflag = hdr_ptr[156];
file_mode = _rtems_octal2ulong(&hdr_ptr[100], 8); file_mode = _rtems_octal2ulong(&hdr_ptr[100], 8);
file_size = _rtems_octal2ulong(&hdr_ptr[124], 12); file_size = _rtems_octal2ulong(&hdr_ptr[124], 12);
hdr_chksum = _rtems_octal2ulong(&hdr_ptr[148], 8); hdr_chksum = _rtems_octal2ulong(&hdr_ptr[148], 8);
if (_rtems_tar_header_checksum(hdr_ptr) != hdr_chksum) if (_rtems_tar_header_checksum(hdr_ptr) != hdr_chksum)
break; break;
/* /*
* Generate an IMFS node depending on the file type. * Generate an IMFS node depending on the file type.
* - For directories, just create directories as usual. IMFS * - For directories, just create directories as usual. IMFS
* will take care of the rest. * will take care of the rest.
* - For files, create a file node with special tarfs properties. * - For files, create a file node with special tarfs properties.
*/ */
if (linkflag == DIRTYPE) { if (linkflag == DIRTYPE) {
strcpy(full_filename, mountpoint); strcpy(full_filename, mountpoint);
if (full_filename[strlen(full_filename)-1] != '/') if (full_filename[strlen(full_filename)-1] != '/')
strcat(full_filename, "/"); strcat(full_filename, "/");
strcat(full_filename, filename); strcat(full_filename, filename);
mkdir(full_filename, S_IRWXU | S_IRWXG | S_IRWXO); mkdir(full_filename, S_IRWXU | S_IRWXG | S_IRWXO);
}
/*
* Create a LINEAR_FILE node
*
* NOTE: Coverity Id 20 reports this as a leak.
* While technically not a leak, it indicated that
* IMFS_create_node was ONLY passed a NULL when we created the
* root node. We added a new IMFS_create_root_node() so this
* path no longer existed. The result was simpler code which
* should not have this path.
*/
else if (linkflag == REGTYPE) {
const char *name;
loc = root_loc;
if (IMFS_evaluate_for_make(filename, &loc, &name) == 0) {
node = IMFS_create_node(
&loc,
IMFS_LINEAR_FILE, (char *)name,
(file_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) | S_IFREG,
NULL
);
node->info.linearfile.size = file_size;
node->info.linearfile.direct = &tar_image[offset];
} }
/*
* Create a LINEAR_FILE node
*
* NOTE: Coverity thinks this is a resource leak since a node
* is created but never deleted. The scope of the allocation
* is that of a file -- not this method. Coverity Id 20.
*/
else if (linkflag == REGTYPE) {
const char *name;
loc = root_loc; nblocks = (((file_size) + 511) & ~511) / 512;
if (IMFS_evaluate_for_make(filename, &loc, &name) == 0) { offset += 512 * nblocks;
node = IMFS_create_node(&loc, }
IMFS_LINEAR_FILE, (char *)name, }
(file_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) | S_IFREG, return status;
NULL);
node->info.linearfile.size = file_size;
node->info.linearfile.direct = &tar_image[offset];
}
nblocks = (((file_size) + 511) & ~511) / 512;
offset += 512 * nblocks;
}
}
return status;
} }

View File

@@ -46,7 +46,6 @@ int IMFS_mknod(
/* /*
* Figure out what type of IMFS node this is. * Figure out what type of IMFS node this is.
*/ */
if ( S_ISDIR(mode) ) if ( S_ISDIR(mode) )
type = IMFS_DIRECTORY; type = IMFS_DIRECTORY;
else if ( S_ISREG(mode) ) else if ( S_ISREG(mode) )
@@ -64,9 +63,12 @@ int IMFS_mknod(
/* /*
* Allocate and fill in an IMFS jnode * Allocate and fill in an IMFS jnode
* *
* NOTE: Coverity thinks this is a resource leak since a node * NOTE: Coverity Id 21 reports this as a leak.
* is created but never deleted. The scope of the allocation * While technically not a leak, it indicated that IMFS_create_node
* is that of a file -- not this method. Coverity Id 21. * was ONLY passed a NULL when we created the root node. We
* added a new IMFS_create_root_node() so this path no longer
* existed. The result was simpler code which should not have
* this path.
*/ */
new_node = IMFS_create_node( new_node = IMFS_create_node(
pathloc, pathloc,

View File

@@ -40,13 +40,11 @@ int IMFS_symlink(
/* /*
* Remove any separators at the end of the string. * Remove any separators at the end of the string.
*/ */
IMFS_get_token( node_name, strlen( node_name ), new_name, &i ); IMFS_get_token( node_name, strlen( node_name ), new_name, &i );
/* /*
* Duplicate link name * Duplicate link name
*/ */
info.sym_link.name = strdup(link_name); info.sym_link.name = strdup(link_name);
if (info.sym_link.name == NULL) { if (info.sym_link.name == NULL) {
rtems_set_errno_and_return_minus_one(ENOMEM); rtems_set_errno_and_return_minus_one(ENOMEM);
@@ -55,13 +53,13 @@ int IMFS_symlink(
/* /*
* Create a new link node. * Create a new link node.
* *
* NOTE: Coverity CID 22 notes this as a resource leak. We are ignoring * NOTE: Coverity CID 22 notes this as a resource leak.
* this analysis because in this particular case it is wrong. This * While technically not a leak, it indicated that IMFS_create_node
* method creates a symbolic link node for the IMFS. The memory * was ONLY passed a NULL when we created the root node. We
* allocated must persist for the life of the symbolic link not * added a new IMFS_create_root_node() so this path no longer
* the life of the method. * existed. The result was simpler code which should not have
* this path.
*/ */
new_node = IMFS_create_node( new_node = IMFS_create_node(
parent_loc, parent_loc,
IMFS_SYM_LINK, IMFS_SYM_LINK,