[U-Boot] [PATCH 4/8] EXT2: Rework ext2fs_read_file()

Marek Vasut
2012-06-08T17:31:49Z

* Fix indentation
* Move all definitions of variables to the begining of the function

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

diff --git a/fs/ext2/ext2fs.c b/fs/ext2/ext2fs.c
index 69fe1d1..0d54ae6 100644
--- a/fs/ext2/ext2fs.c
+++ b/fs/ext2/ext2fs.c
@@ -256,66 +256,69 @@ static int ext2fs_read_block(struct ext2fs_node *node, int fileblock)
return (blknr);
}

-
-int ext2fs_read_file
- (struct ext2fs_node *node, int pos, unsigned int len, char *buf)
+static int ext2fs_read_file(struct ext2fs_node *node, int pos,
+ unsigned int len, char *buf)
{
+ const int log2blocksize = LOG2_EXT2_BLOCK_SIZE (node->data);
+ const int blocksize = 1 << (log2blocksize + DISK_SECTOR_BITS);
+ const unsigned int filesize = __le32_to_cpu(node->inode.size);
+
int i;
int blockcnt;
- int log2blocksize = LOG2_EXT2_BLOCK_SIZE (node->data);
- int blocksize = 1 << (log2blocksize + DISK_SECTOR_BITS);
- unsigned int filesize = __le32_to_cpu(node->inode.size);
+ int status;
+ int blknr;
+ int blockoff;
+ int blockend;
+ int skipfirst;

- /* Adjust len so it we can't read past the end of the file. */
- if (len > filesize) {
+ /* Adjust len so it we can't read past the end of the file. */
+ if (len > filesize)
len = filesize;
- }
+
blockcnt = ((len + pos) + blocksize - 1) / blocksize;

for (i = pos / blocksize; i < blockcnt; i++) {
- int blknr;
- int blockoff = pos % blocksize;
- int blockend = blocksize;
-
- int skipfirst = 0;
+ blockoff = pos % blocksize;
+ blockend = blocksize;
+ skipfirst = 0;

blknr = ext2fs_read_block (node, i);
- if (blknr < 0) {
- return (-1);
- }
+ if (blknr < 0)
+ return -1;
+
blknr = blknr << log2blocksize;

/* Last block. */
if (i == blockcnt - 1) {
blockend = (len + pos) % blocksize;

- /* The last portion is exactly blocksize. */
- if (!blockend) {
+ /* The last portion is exactly blocksize. */
+ if (!blockend)
blockend = blocksize;
- }
}

- /* First block. */
+ /* First block. */
if (i == pos / blocksize) {
skipfirst = blockoff;
blockend -= skipfirst;
}

- /* If the block number is 0 this block is not stored on disk but
- is zero filled instead. */
+ /*
+ * If the block number is 0 this block is not stored on disk
+ * but is zero filled instead.
+ */
if (blknr) {
- int status;
-
- status = ext2fs_devread (blknr, skipfirst, blockend, buf);
- if (status == 0) {
- return (-1);
- }
+ status = ext2fs_devread(blknr, skipfirst, blockend, buf);
+ if (status == 0)
+ return -1;
} else {
- memset (buf, 0, blocksize - skipfirst);
+ memset(buf, 0, blocksize - skipfirst);
}
+
buf += blocksize - skipfirst;
}
- return (len);
+
+ return len;
}

int ext2fs_iterate_dir(struct ext2fs_node *dir, char *name,
--
1.7.10