[U-Boot] [PATCH 7/8] EXT2: Rework ext2fs_iterate_dir()

Marek Vasut marex at denx.de
Fri Jun 8 19:31:52 CEST 2012


* Indent cleanup
* Pull most variables defined inside the code to the begining
  (note: there are variables marked with "FIXME" now, those will be
         processed in subsequent patch)

Signed-off-by: Marek Vasut <marex at denx.de>
Cc: Wolfgang Denk <wd at denx.de>
---
 fs/ext2/ext2fs.c |  139 +++++++++++++++++++++++++++---------------------------
 1 file changed, 69 insertions(+), 70 deletions(-)

diff --git a/fs/ext2/ext2fs.c b/fs/ext2/ext2fs.c
index 10745f6..d55c8c5 100644
--- a/fs/ext2/ext2fs.c
+++ b/fs/ext2/ext2fs.c
@@ -322,135 +322,134 @@ static int ext2fs_read_file(struct ext2fs_node *node, int pos,
 }
 
 int ext2fs_iterate_dir(struct ext2fs_node *dir, char *name,
-					struct ext2fs_node **fnode, int *ftype)
+			struct ext2fs_node **fnode, int *ftype)
 {
 	unsigned int fpos = 0;
 	int status;
-	struct ext2fs_node *diro = (struct ext2fs_node *) dir;
+	int type;
+	struct ext2fs_node *diro = (struct ext2fs_node *)dir;
+	struct ext2fs_node *fdiro;
+	struct ext2_dirent dirent;
 
-#ifdef DEBUG
 	if (name != NULL)
-		printf ("Iterate dir %s\n", name);
-#endif /* of DEBUG */
+		debug("EXT2: Iterate dir %s\n", name);
+
 	if (!diro->inode_read) {
-		status = ext2fs_read_inode (diro->data, diro->ino,
-					    &diro->inode);
-		if (status == 0) {
-			return (0);
-		}
+		status = ext2fs_read_inode(diro->data, diro->ino, &diro->inode);
+		if (status == 0)
+			return 0;
 	}
-	/* Search the file.  */
-	while (fpos < __le32_to_cpu (diro->inode.size)) {
-		struct ext2_dirent dirent;
-
-		status = ext2fs_read_file (diro, fpos,
-					   sizeof (struct ext2_dirent),
-					   (char *) &dirent);
-		if (status < 1) {
-			return (0);
-		}
+
+	/* Search the file. */
+	while (fpos < __le32_to_cpu(diro->inode.size)) {
+		status = ext2fs_read_file(diro, fpos,
+					  sizeof(struct ext2_dirent),
+					  (char *)&dirent);
+		if (status < 1)
+			return 0;
+
 		if (dirent.namelen != 0) {
-			char filename[dirent.namelen + 1];
-			struct ext2fs_node *fdiro;
-			int type = FILETYPE_UNKNOWN;
-
-			status = ext2fs_read_file (diro,
-						   fpos + sizeof (struct ext2_dirent),
-						   dirent.namelen, filename);
-			if (status < 1) {
-				return (0);
-			}
-			fdiro = malloc (sizeof (struct ext2fs_node));
-			if (!fdiro) {
-				return (0);
-			}
+			char filename[dirent.namelen + 1];	/* FIXME */
+			type = FILETYPE_UNKNOWN;
+			fdiro = NULL;
+
+			status = ext2fs_read_file(diro,
+						  fpos +
+						  sizeof(struct ext2_dirent),
+						  dirent.namelen, filename);
+			if (status < 1)
+				return 0;
+
+			fdiro = malloc(sizeof(struct ext2fs_node));
+			if (!fdiro)
+				return 0;
 
 			fdiro->data = diro->data;
-			fdiro->ino = __le32_to_cpu (dirent.inode);
+			fdiro->ino = __le32_to_cpu(dirent.inode);
 
 			filename[dirent.namelen] = '\0';
 
 			if (dirent.filetype != FILETYPE_UNKNOWN) {
 				fdiro->inode_read = 0;
 
-				if (dirent.filetype == FILETYPE_DIRECTORY) {
+				if (dirent.filetype == FILETYPE_DIRECTORY)
 					type = FILETYPE_DIRECTORY;
-				} else if (dirent.filetype ==
-					   FILETYPE_SYMLINK) {
+				else if (dirent.filetype == FILETYPE_SYMLINK)
 					type = FILETYPE_SYMLINK;
-				} else if (dirent.filetype == FILETYPE_REG) {
+				else if (dirent.filetype == FILETYPE_REG)
 					type = FILETYPE_REG;
-				}
 			} else {
-				/* The filetype can not be read from the dirent, get it from inode */
-
-				status = ext2fs_read_inode (diro->data,
-							    __le32_to_cpu(dirent.inode),
-							    &fdiro->inode);
+				/*
+				 * The filetype can not be read from the dirent,
+				 * get it from inode.
+				 */
+				status = ext2fs_read_inode(diro->data,
+						   __le32_to_cpu(dirent.inode),
+						   &fdiro->inode);
 				if (status == 0) {
-					free (fdiro);
-					return (0);
+					free(fdiro);
+					return 0;
 				}
 				fdiro->inode_read = 1;
 
-				if ((__le16_to_cpu (fdiro->inode.mode) &
+				if ((__le16_to_cpu(fdiro->inode.mode) &
 				     FILETYPE_INO_MASK) ==
 				    FILETYPE_INO_DIRECTORY) {
 					type = FILETYPE_DIRECTORY;
-				} else if ((__le16_to_cpu (fdiro->inode.mode)
+				} else if ((__le16_to_cpu(fdiro->inode.mode)
 					    & FILETYPE_INO_MASK) ==
 					   FILETYPE_INO_SYMLINK) {
 					type = FILETYPE_SYMLINK;
-				} else if ((__le16_to_cpu (fdiro->inode.mode)
+				} else if ((__le16_to_cpu(fdiro->inode.mode)
 					    & FILETYPE_INO_MASK) ==
 					   FILETYPE_INO_REG) {
 					type = FILETYPE_REG;
 				}
 			}
-#ifdef DEBUG
-			printf ("iterate >%s<\n", filename);
-#endif /* of DEBUG */
+
+			debug("EXT2: iterate >%s<\n", filename);
+
 			if ((name != NULL) && (fnode != NULL)
 			    && (ftype != NULL)) {
-				if (strcmp (filename, name) == 0) {
+				if (strcmp(filename, name) == 0) {
 					*ftype = type;
 					*fnode = fdiro;
-					return (1);
+					return 1;
 				}
 			} else {
 				if (fdiro->inode_read == 0) {
-					status = ext2fs_read_inode (diro->data,
-							    __le32_to_cpu (dirent.inode),
-							    &fdiro->inode);
+					status = ext2fs_read_inode(diro->data,
+							 __le32_to_cpu(dirent.inode),
+							 &fdiro->inode);
 					if (status == 0) {
-						free (fdiro);
-						return (0);
+						free(fdiro);
+						return 0;
 					}
 					fdiro->inode_read = 1;
 				}
 				switch (type) {
 				case FILETYPE_DIRECTORY:
-					printf ("<DIR> ");
+					printf("<DIR> ");
 					break;
 				case FILETYPE_SYMLINK:
-					printf ("<SYM> ");
+					printf("<SYM> ");
 					break;
 				case FILETYPE_REG:
-					printf ("      ");
+					printf("      ");
 					break;
 				default:
-					printf ("< ? > ");
+					printf("< ? > ");
 					break;
 				}
-				printf ("%10d %s\n",
-					__le32_to_cpu (fdiro->inode.size),
-					filename);
+				printf("%10d %s\n",
+				       __le32_to_cpu(fdiro->inode.size),
+				       filename);
 			}
-			free (fdiro);
+			free(fdiro);
 		}
-		fpos += __le16_to_cpu (dirent.direntlen);
+		fpos += __le16_to_cpu(dirent.direntlen);
 	}
-	return (0);
+	return 0;
 }
 
 static char *ext2fs_read_symlink(struct ext2fs_node *node)
-- 
1.7.10



More information about the U-Boot mailing list