OSDN Git Service

Allocate buffer immediately in opendir() function to avoid code duplication in future.
authorresver <resver@60bc1c72-a15a-11de-b98f-4500b42dc123>
Sun, 13 Dec 2009 10:37:32 +0000 (10:37 +0000)
committerresver <resver@60bc1c72-a15a-11de-b98f-4500b42dc123>
Sun, 13 Dec 2009 10:37:32 +0000 (10:37 +0000)
git-svn-id: http://exfat.googlecode.com/svn/trunk@67 60bc1c72-a15a-11de-b98f-4500b42dc123

libexfat/node.c

index 92cfe93..ff3ea21 100644 (file)
@@ -53,14 +53,23 @@ void exfat_put_node(struct exfat* ef, struct exfat_node* node)
        }
 }
 
-static void opendir(const struct exfat_node* dir, struct iterator* it)
+static int opendir(struct exfat* ef, const struct exfat_node* dir,
+               struct iterator* it)
 {
        if (!(dir->flags & EXFAT_ATTRIB_DIR))
                exfat_bug("`%s' is not a directory", dir->name);
        it->cluster = dir->start_cluster;
        it->offset = 0;
        it->contiguous = IS_CONTIGUOUS(*dir);
-       it->chunk = NULL;
+       it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
+       if (it->chunk == NULL)
+       {
+               exfat_error("out of memory");
+               return -ENOMEM;
+       }
+       exfat_read_raw(it->chunk, CLUSTER_SIZE(*ef->sb),
+                       exfat_c2o(ef, it->cluster), ef->fd);
+       return 0;
 }
 
 static void closedir(struct iterator* it)
@@ -113,18 +122,6 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
 
        *node = NULL;
 
-       if (it->chunk == NULL)
-       {
-               it->chunk = malloc(CLUSTER_SIZE(*ef->sb));
-               if (it->chunk == NULL)
-               {
-                       exfat_error("out of memory");
-                       return -ENOMEM;
-               }
-               exfat_read_raw(it->chunk, CLUSTER_SIZE(*ef->sb),
-                               exfat_c2o(ef, it->cluster), ef->fd);
-       }
-
        for (;;)
        {
                /* every directory (even empty one) occupies at least one cluster and
@@ -329,7 +326,9 @@ int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir)
        if (dir->flags & EXFAT_ATTRIB_CACHED)
                return 0; /* already cached */
 
-       opendir(dir, &it);
+       rc = opendir(ef, dir, &it);
+       if (rc != 0)
+               return rc;
        while ((rc = readdir(ef, dir, &node, &it)) == 0)
        {
                node->parent = dir;