From cb4e99201e1702a62af73debe342ea289f0d8362 Mon Sep 17 00:00:00 2001 From: Joel Sherrill Date: Tue, 19 Jan 2010 19:31:00 +0000 Subject: 2010-01-19 Joel Sherrill * 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. --- cpukit/libfs/src/imfs/imfs.h | 17 ++-- cpukit/libfs/src/imfs/imfs_creat.c | 135 ++++++++++++++++++---------- cpukit/libfs/src/imfs/imfs_initsupp.c | 21 ++--- cpukit/libfs/src/imfs/imfs_link.c | 13 ++- cpukit/libfs/src/imfs/imfs_load_tar.c | 165 ++++++++++++++++++---------------- cpukit/libfs/src/imfs/imfs_mknod.c | 10 ++- cpukit/libfs/src/imfs/imfs_symlink.c | 14 ++- 7 files changed, 210 insertions(+), 165 deletions(-) (limited to 'cpukit/libfs') diff --git a/cpukit/libfs/src/imfs/imfs.h b/cpukit/libfs/src/imfs/imfs.h index 5345269dcb..d198c5d884 100644 --- a/cpukit/libfs/src/imfs/imfs.h +++ b/cpukit/libfs/src/imfs/imfs.h @@ -1,7 +1,7 @@ /* * Header file for the In-Memory File System * - * COPYRIGHT (c) 1989-2007. + * COPYRIGHT (c) 1989-2010. * On-Line Applications Research Corporation (OAR). * * 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 */ ); - extern int IMFS_link( rtems_filesystem_location_info_t *to_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 */ ); +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( rtems_filesystem_location_info_t *parent_loc, /* IN */ IMFS_jnode_types_t type, /* IN */ @@ -360,9 +367,9 @@ extern IMFS_jnode_t *IMFS_create_node( ); extern int IMFS_evaluate_for_make( - const char *path, /* IN */ - rtems_filesystem_location_info_t *pathloc, /* IN/OUT */ - const char **name /* OUT */ + const char *path, /* IN */ + rtems_filesystem_location_info_t *pathloc, /* IN/OUT */ + const char **name /* OUT */ ); extern int IMFS_mount( diff --git a/cpukit/libfs/src/imfs/imfs_creat.c b/cpukit/libfs/src/imfs/imfs_creat.c index 946e71d729..9313d6c6f5 100644 --- a/cpukit/libfs/src/imfs/imfs_creat.c +++ b/cpukit/libfs/src/imfs/imfs_creat.c @@ -3,7 +3,7 @@ * * Routine to create a new in memory file system node. * - * COPYRIGHT (c) 1989-1999. + * COPYRIGHT (c) 1989-2010. * On-Line Applications Research Corporation (OAR). * * The license and distribution terms for this file may be @@ -23,6 +23,10 @@ #include "imfs.h" #include +/* + * Create an IMFS filesystem node of an arbitrary type that is NOT + * the root directory node. + */ IMFS_jnode_t *IMFS_create_node( rtems_filesystem_location_info_t *parent_loc, IMFS_jnode_types_t type, @@ -32,57 +36,25 @@ IMFS_jnode_t *IMFS_create_node( ) { IMFS_jnode_t *node; - struct timeval tv; - IMFS_jnode_t *parent = NULL; + IMFS_jnode_t *parent; IMFS_fs_info_t *fs_info; - if ( parent_loc != NULL ) - parent = parent_loc->node_access; - /* - * Allocate an IMFS jnode + * MUST have a parent node to call this routine. */ - - node = calloc( 1, sizeof( IMFS_jnode_t ) ); - if ( !node ) + if ( parent_loc == 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. + * Allocate filesystem node and fill in basic information */ - - 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; + node = IMFS_allocate_node( type, name, mode & ~rtems_filesystem_umask ); + if ( !node ) + return NULL; /* * Set the type specific information */ - switch (type) { case IMFS_DIRECTORY: 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 ) { - rtems_chain_append( &parent->info.directory.Entries, &node->Node ); - node->Parent = parent; + node->Parent = parent; + node->st_ino = ++fs_info->ino_count; - 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; } diff --git a/cpukit/libfs/src/imfs/imfs_initsupp.c b/cpukit/libfs/src/imfs/imfs_initsupp.c index 648150257b..510915e922 100644 --- a/cpukit/libfs/src/imfs/imfs_initsupp.c +++ b/cpukit/libfs/src/imfs/imfs_initsupp.c @@ -1,7 +1,7 @@ /* * IMFS Initialization * - * COPYRIGHT (c) 1989-1999. + * COPYRIGHT (c) 1989-2010. * On-Line Applications Research Corporation (OAR). * * 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; int bit_mask; + /* * check, whether requested bytes per block is valid */ - for (bit_mask = 16; - !is_valid && (bit_mask <= 512); - bit_mask <<= 1) { + for (bit_mask = 16; !is_valid && (bit_mask <= 512); bit_mask <<= 1) { if (bit_mask == requested_bytes_per_block) { is_valid = true; } @@ -57,14 +56,12 @@ static int IMFS_determine_bytes_per_block( ? requested_bytes_per_block : default_bytes_per_block); return 0; - } /* * IMFS_initialize */ - int IMFS_initialize_support( rtems_filesystem_mount_table_entry_t *temp_mt_entry, 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). */ - - temp_mt_entry->mt_fs_root.node_access = IMFS_create_node( - NULL, - IMFS_DIRECTORY, - "", - ( S_IFDIR | 0755 ), - NULL - ); - + temp_mt_entry->mt_fs_root.node_access = IMFS_create_root_node(); temp_mt_entry->mt_fs_root.handlers = directory_handlers; temp_mt_entry->mt_fs_root.ops = op_table; temp_mt_entry->pathconf_limits_and_options = IMFS_LIMITS_AND_OPTIONS; @@ -104,7 +93,7 @@ int IMFS_initialize_support( * Create custom file system data. */ fs_info = calloc( 1, sizeof( IMFS_fs_info_t ) ); - if ( !fs_info ){ + if ( !fs_info ) { free(temp_mt_entry->mt_fs_root.node_access); rtems_set_errno_and_return_minus_one(ENOMEM); } diff --git a/cpukit/libfs/src/imfs/imfs_link.c b/cpukit/libfs/src/imfs/imfs_link.c index b9bab7a520..4c2136ba71 100644 --- a/cpukit/libfs/src/imfs/imfs_link.c +++ b/cpukit/libfs/src/imfs/imfs_link.c @@ -38,7 +38,6 @@ int IMFS_link( /* * Verify this node can be linked to. */ - info.hard_link.link_node = to_loc->node_access; if ( info.hard_link.link_node->st_nlink >= LINK_MAX ) rtems_set_errno_and_return_minus_one( EMLINK ); @@ -46,17 +45,18 @@ int IMFS_link( /* * Remove any separators at the end of the string. */ - IMFS_get_token( token, strlen( token ), new_name, &i ); /* * Create a new link 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 19. + * NOTE: Coverity Id 19 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. */ - new_node = IMFS_create_node( parent_loc, IMFS_HARD_LINK, @@ -71,7 +71,6 @@ int IMFS_link( /* * Increment the link count of the node being pointed to. */ - info.hard_link.link_node->st_nlink++; IMFS_update_ctime( info.hard_link.link_node ); diff --git a/cpukit/libfs/src/imfs/imfs_load_tar.c b/cpukit/libfs/src/imfs/imfs_load_tar.c index 4b9392e71a..5e53787437 100644 --- a/cpukit/libfs/src/imfs/imfs_load_tar.c +++ b/cpukit/libfs/src/imfs/imfs_load_tar.c @@ -79,29 +79,34 @@ * create_node. */ int rtems_tarfs_load( - char *mountpoint, + char *mountpoint, uint8_t *tar_image, - size_t tar_size + size_t tar_size ) { - rtems_filesystem_location_info_t root_loc; - rtems_filesystem_location_info_t loc; - const char *hdr_ptr; - char filename[100]; - char full_filename[256]; - int hdr_chksum; - unsigned char linkflag; - unsigned long file_size; - unsigned long file_mode; - int offset; - unsigned long nblocks; - IMFS_jnode_t *node; - int status; - - status = rtems_filesystem_evaluate_path(mountpoint, strlen(mountpoint), - 0, &root_loc, 0); + rtems_filesystem_location_info_t root_loc; + rtems_filesystem_location_info_t loc; + const char *hdr_ptr; + char filename[100]; + char full_filename[256]; + int hdr_chksum; + unsigned char linkflag; + unsigned long file_size; + unsigned long file_mode; + int offset; + unsigned long nblocks; + IMFS_jnode_t *node; + int status; + + status = rtems_filesystem_evaluate_path( + mountpoint, + strlen(mountpoint), + 0, + &root_loc, + 0 + ); if (status != 0) - return(-1); + return -1; if (root_loc.ops != &IMFS_ops) return -1; @@ -111,66 +116,70 @@ int rtems_tarfs_load( */ offset = 0; while (1) { - if (offset + 512 > tar_size) - break; - - /* - * Read a header. - */ - hdr_ptr = (char *) &tar_image[offset]; - offset += 512; - if (strncmp(&hdr_ptr[257], "ustar ", 7)) - break; - - strncpy(filename, hdr_ptr, MAX_NAME_FIELD_SIZE); - filename[MAX_NAME_FIELD_SIZE] = '\0'; - - linkflag = hdr_ptr[156]; - file_mode = _rtems_octal2ulong(&hdr_ptr[100], 8); - file_size = _rtems_octal2ulong(&hdr_ptr[124], 12); - hdr_chksum = _rtems_octal2ulong(&hdr_ptr[148], 8); - - if (_rtems_tar_header_checksum(hdr_ptr) != hdr_chksum) - break; - - /* - * Generate an IMFS node depending on the file type. - * - For directories, just create directories as usual. IMFS - * will take care of the rest. - * - For files, create a file node with special tarfs properties. - */ - if (linkflag == DIRTYPE) { - strcpy(full_filename, mountpoint); - if (full_filename[strlen(full_filename)-1] != '/') - strcat(full_filename, "/"); - strcat(full_filename, filename); - mkdir(full_filename, S_IRWXU | S_IRWXG | S_IRWXO); - } - /* - * 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; - 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]; - } - - nblocks = (((file_size) + 511) & ~511) / 512; - offset += 512 * nblocks; + if (offset + 512 > tar_size) + break; + + /* + * Read a header. + */ + hdr_ptr = (char *) &tar_image[offset]; + offset += 512; + if (strncmp(&hdr_ptr[257], "ustar ", 7)) + break; + + strncpy(filename, hdr_ptr, MAX_NAME_FIELD_SIZE); + filename[MAX_NAME_FIELD_SIZE] = '\0'; + + linkflag = hdr_ptr[156]; + file_mode = _rtems_octal2ulong(&hdr_ptr[100], 8); + file_size = _rtems_octal2ulong(&hdr_ptr[124], 12); + hdr_chksum = _rtems_octal2ulong(&hdr_ptr[148], 8); + + if (_rtems_tar_header_checksum(hdr_ptr) != hdr_chksum) + break; + + /* + * Generate an IMFS node depending on the file type. + * - For directories, just create directories as usual. IMFS + * will take care of the rest. + * - For files, create a file node with special tarfs properties. + */ + if (linkflag == DIRTYPE) { + strcpy(full_filename, mountpoint); + if (full_filename[strlen(full_filename)-1] != '/') + strcat(full_filename, "/"); + strcat(full_filename, filename); + 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]; } - } - return status; + nblocks = (((file_size) + 511) & ~511) / 512; + offset += 512 * nblocks; + } + } + return status; } diff --git a/cpukit/libfs/src/imfs/imfs_mknod.c b/cpukit/libfs/src/imfs/imfs_mknod.c index fe852b6b0f..28ca79e201 100644 --- a/cpukit/libfs/src/imfs/imfs_mknod.c +++ b/cpukit/libfs/src/imfs/imfs_mknod.c @@ -46,7 +46,6 @@ int IMFS_mknod( /* * Figure out what type of IMFS node this is. */ - if ( S_ISDIR(mode) ) type = IMFS_DIRECTORY; else if ( S_ISREG(mode) ) @@ -64,9 +63,12 @@ int IMFS_mknod( /* * Allocate and fill in an IMFS jnode * - * 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 21. + * NOTE: Coverity Id 21 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. */ new_node = IMFS_create_node( pathloc, diff --git a/cpukit/libfs/src/imfs/imfs_symlink.c b/cpukit/libfs/src/imfs/imfs_symlink.c index a6d2247e46..7094f219fb 100644 --- a/cpukit/libfs/src/imfs/imfs_symlink.c +++ b/cpukit/libfs/src/imfs/imfs_symlink.c @@ -40,13 +40,11 @@ int IMFS_symlink( /* * Remove any separators at the end of the string. */ - IMFS_get_token( node_name, strlen( node_name ), new_name, &i ); /* * Duplicate link name */ - info.sym_link.name = strdup(link_name); if (info.sym_link.name == NULL) { rtems_set_errno_and_return_minus_one(ENOMEM); @@ -55,13 +53,13 @@ int IMFS_symlink( /* * Create a new link node. * - * NOTE: Coverity CID 22 notes this as a resource leak. We are ignoring - * this analysis because in this particular case it is wrong. This - * method creates a symbolic link node for the IMFS. The memory - * allocated must persist for the life of the symbolic link not - * the life of the method. + * NOTE: Coverity CID 22 notes this as a resource 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. */ - new_node = IMFS_create_node( parent_loc, IMFS_SYM_LINK, -- cgit v1.2.3