Whamcloud - gitweb
Prevent i_dtime from being mistaken for an inode number post-2038 wraparound
[tools/e2fsprogs.git] / e2fsck / dirinfo.c
index dbaf471..9873e38 100644 (file)
 #include <fcntl.h>
 #include "uuid/uuid.h"
 
+#include "ext2fs/ext2fs.h"
 #include <ext2fs/tdb.h>
 
 struct dir_info_db {
-       int             count;
-       int             size;
+       ext2_ino_t      count;
+       ext2_ino_t      size;
        struct dir_info *array;
        struct dir_info *last_lookup;
+#ifdef CONFIG_TDB
        char            *tdb_fn;
        TDB_CONTEXT     *tdb;
+#endif
 };
 
 struct dir_info_iter {
-       int     i;
+       ext2_ino_t      i;
+#ifdef CONFIG_TDB
        TDB_DATA        tdb_iter;
+#endif
 };
 
 struct dir_info_ent {
@@ -37,10 +42,11 @@ struct dir_info_ent {
 
 static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir);
 
+#ifdef CONFIG_TDB
 static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)
 {
        struct dir_info_db      *db = ctx->dir_info;
-       unsigned int            threshold;
+       ext2_ino_t              threshold;
        errcode_t               retval;
        mode_t                  save_umask;
        char                    *tdb_dir, uuid[40];
@@ -78,6 +84,7 @@ static void setup_tdb(e2fsck_t ctx, ext2_ino_t num_dirs)
                           O_RDWR | O_CREAT | O_TRUNC, 0600);
        close(fd);
 }
+#endif
 
 static void setup_db(e2fsck_t ctx)
 {
@@ -97,6 +104,7 @@ static void setup_db(e2fsck_t ctx)
        if (retval)
                num_dirs = 1024;        /* Guess */
 
+#ifdef CONFIG_TDB
        setup_tdb(ctx, num_dirs);
 
        if (db->tdb) {
@@ -105,6 +113,7 @@ static void setup_db(e2fsck_t ctx)
 #endif
                return;
        }
+#endif
 
        db->size = num_dirs + 10;
        db->array  = (struct dir_info *)
@@ -120,39 +129,47 @@ static void setup_db(e2fsck_t ctx)
  */
 void e2fsck_add_dir_info(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent)
 {
-       struct dir_info_db      *db;
-       struct dir_info         *dir, ent;
-       int                     i, j;
+       struct dir_info         *dir, *old_array;
+       ext2_ino_t              i, j;
        errcode_t               retval;
        unsigned long           old_size;
 
 #ifdef DIRINFO_DEBUG
-       printf("add_dir_info for inode (%lu, %lu)...\n", ino, parent);
+       printf("add_dir_info for inode (%u, %u)...\n", ino, parent);
 #endif
        if (!ctx->dir_info)
                setup_db(ctx);
-       db = ctx->dir_info;
 
        if (ctx->dir_info->count >= ctx->dir_info->size) {
                old_size = ctx->dir_info->size * sizeof(struct dir_info);
                ctx->dir_info->size += 10;
+               old_array = ctx->dir_info->array;
                retval = ext2fs_resize_mem(old_size, ctx->dir_info->size *
                                           sizeof(struct dir_info),
                                           &ctx->dir_info->array);
                if (retval) {
+                       fprintf(stderr, "Couldn't reallocate dir_info "
+                               "structure to %u entries\n",
+                               ctx->dir_info->size);
+                       fatal_error(ctx, 0);
                        ctx->dir_info->size -= 10;
                        return;
                }
+               if (old_array != ctx->dir_info->array)
+                       ctx->dir_info->last_lookup = NULL;
        }
 
-       ent.ino = ino;
-       ent.parent = parent;
-       ent.dotdot = parent;
+#ifdef CONFIG_TDB
+       if (ctx->dir_info->tdb) {
+               struct dir_info ent;
 
-       if (db->tdb) {
+               ent.ino = ino;
+               ent.parent = parent;
+               ent.dotdot = parent;
                e2fsck_put_dir_info(ctx, &ent);
                return;
        }
+#endif
 
        /*
         * Normally, add_dir_info is called with each inode in
@@ -187,19 +204,20 @@ void e2fsck_add_dir_info(e2fsck_t ctx, ext2_ino_t ino, ext2_ino_t parent)
 static struct dir_info *e2fsck_get_dir_info(e2fsck_t ctx, ext2_ino_t ino)
 {
        struct dir_info_db      *db = ctx->dir_info;
-       int                     low, high, mid;
-       struct dir_info_ent     *buf;
-       static struct dir_info  ret_dir_info;
+       ext2_ino_t low, high, mid;
 
        if (!db)
                return 0;
 
 #ifdef DIRINFO_DEBUG
-       printf("e2fsck_get_dir_info %d...", ino);
+       printf("e2fsck_get_dir_info %u...", ino);
 #endif
 
+#ifdef CONFIG_TDB
        if (db->tdb) {
+               static struct dir_info  ret_dir_info;
                TDB_DATA key, data;
+               struct dir_info_ent     *buf;
 
                key.dptr = (unsigned char *) &ino;
                key.dsize = sizeof(ext2_ino_t);
@@ -217,20 +235,21 @@ static struct dir_info *e2fsck_get_dir_info(e2fsck_t ctx, ext2_ino_t ino)
                ret_dir_info.dotdot = buf->dotdot;
                ret_dir_info.parent = buf->parent;
 #ifdef DIRINFO_DEBUG
-               printf("(%d,%d,%d)\n", ino, buf->dotdot, buf->parent);
+               printf("(%u,%u,%u)\n", ino, buf->dotdot, buf->parent);
 #endif
                free(data.dptr);
                return &ret_dir_info;
        }
+#endif
 
        if (db->last_lookup && db->last_lookup->ino == ino)
                return db->last_lookup;
 
        low = 0;
-       high = ctx->dir_info->count-1;
+       high = ctx->dir_info->count - 1;
        if (ino == ctx->dir_info->array[low].ino) {
 #ifdef DIRINFO_DEBUG
-               printf("(%d,%d,%d)\n", ino,
+               printf("(%u,%u,%u)\n", ino,
                       ctx->dir_info->array[low].dotdot,
                       ctx->dir_info->array[low].parent);
 #endif
@@ -238,7 +257,7 @@ static struct dir_info *e2fsck_get_dir_info(e2fsck_t ctx, ext2_ino_t ino)
        }
        if (ino == ctx->dir_info->array[high].ino) {
 #ifdef DIRINFO_DEBUG
-               printf("(%d,%d,%d)\n", ino,
+               printf("(%u,%u,%u)\n", ino,
                       ctx->dir_info->array[high].dotdot,
                       ctx->dir_info->array[high].parent);
 #endif
@@ -246,12 +265,13 @@ static struct dir_info *e2fsck_get_dir_info(e2fsck_t ctx, ext2_ino_t ino)
        }
 
        while (low < high) {
-               mid = (low+high)/2;
+               /* sum may overflow, but result will fit into mid again */
+               mid = (unsigned long long)(low + high) / 2;
                if (mid == low || mid == high)
                        break;
                if (ino == ctx->dir_info->array[mid].ino) {
 #ifdef DIRINFO_DEBUG
-                       printf("(%d,%d,%d)\n", ino,
+                       printf("(%u,%u,%u)\n", ino,
                               ctx->dir_info->array[mid].dotdot,
                               ctx->dir_info->array[mid].parent);
 #endif
@@ -265,17 +285,21 @@ static struct dir_info *e2fsck_get_dir_info(e2fsck_t ctx, ext2_ino_t ino)
        return 0;
 }
 
-static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir)
+static void e2fsck_put_dir_info(e2fsck_t ctx EXT2FS_NO_TDB_UNUSED,
+                               struct dir_info *dir EXT2FS_NO_TDB_UNUSED)
 {
+#ifdef CONFIG_TDB
        struct dir_info_db      *db = ctx->dir_info;
        struct dir_info_ent     buf;
        TDB_DATA                key, data;
+#endif
 
 #ifdef DIRINFO_DEBUG
-       printf("e2fsck_put_dir_info (%d, %d, %d)...", dir->ino, dir->dotdot,
+       printf("e2fsck_put_dir_info (%u, %u, %u)...", dir->ino, dir->dotdot,
               dir->parent);
 #endif
 
+#ifdef CONFIG_TDB
        if (!db->tdb)
                return;
 
@@ -290,7 +314,7 @@ static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir)
        if (tdb_store(db->tdb, key, data, TDB_REPLACE) == -1) {
                printf("store failed: %s\n", tdb_errorstr(db->tdb));
        }
-       return;
+#endif
 }
 
 /*
@@ -299,12 +323,16 @@ static void e2fsck_put_dir_info(e2fsck_t ctx, struct dir_info *dir)
 void e2fsck_free_dir_info(e2fsck_t ctx)
 {
        if (ctx->dir_info) {
+#ifdef CONFIG_TDB
                if (ctx->dir_info->tdb)
                        tdb_close(ctx->dir_info->tdb);
                if (ctx->dir_info->tdb_fn) {
-                       unlink(ctx->dir_info->tdb_fn);
-                       free(ctx->dir_info->tdb_fn);
+                       if (unlink(ctx->dir_info->tdb_fn) < 0)
+                               com_err("e2fsck_free_dir_info", errno,
+                                       _("while freeing dir_info tdb file"));
+                       ext2fs_free_mem(&ctx->dir_info->tdb_fn);
                }
+#endif
                if (ctx->dir_info->array)
                        ext2fs_free_mem(&ctx->dir_info->array);
                ctx->dir_info->array = 0;
@@ -326,13 +354,14 @@ int e2fsck_get_num_dirinfo(e2fsck_t ctx)
 struct dir_info_iter *e2fsck_dir_info_iter_begin(e2fsck_t ctx)
 {
        struct dir_info_iter *iter;
-       struct dir_info_db *db = ctx->dir_info;
 
        iter = e2fsck_allocate_memory(ctx, sizeof(struct dir_info_iter),
                                      "dir_info iterator");
 
-       if (db->tdb)
-               iter->tdb_iter = tdb_firstkey(db->tdb);
+#ifdef CONFIG_TDB
+       if (ctx->dir_info->tdb)
+               iter->tdb_iter = tdb_firstkey(ctx->dir_info->tdb);
+#endif
 
        return iter;
 }
@@ -340,48 +369,51 @@ struct dir_info_iter *e2fsck_dir_info_iter_begin(e2fsck_t ctx)
 void e2fsck_dir_info_iter_end(e2fsck_t ctx EXT2FS_ATTR((unused)),
                              struct dir_info_iter *iter)
 {
+#ifdef CONFIG_TDB
        free(iter->tdb_iter.dptr);
+#endif
        ext2fs_free_mem(&iter);
 }
 
 /*
- * A simple interator function
+ * A simple iterator function
  */
 struct dir_info *e2fsck_dir_info_iter(e2fsck_t ctx, struct dir_info_iter *iter)
 {
-       TDB_DATA data, key;
-       struct dir_info_db *db = ctx->dir_info;
-       struct dir_info_ent *buf;
-       static struct dir_info ret_dir_info;
-
        if (!ctx->dir_info || !iter)
                return 0;
 
-       if (db->tdb) {
+#ifdef CONFIG_TDB
+       if (ctx->dir_info->tdb) {
+               static struct dir_info ret_dir_info;
+               struct dir_info_ent *buf;
+               TDB_DATA data, key;
+
                if (iter->tdb_iter.dptr == 0)
                        return 0;
                key = iter->tdb_iter;
-               data = tdb_fetch(db->tdb, key);
+               data = tdb_fetch(ctx->dir_info->tdb, key);
                if (!data.dptr) {
                        printf("iter fetch failed: %s\n",
-                              tdb_errorstr(db->tdb));
+                              tdb_errorstr(ctx->dir_info->tdb));
                        return 0;
                }
                buf = (struct dir_info_ent *) data.dptr;
                ret_dir_info.ino = *((ext2_ino_t *) iter->tdb_iter.dptr);
                ret_dir_info.dotdot = buf->dotdot;
                ret_dir_info.parent = buf->parent;
-               iter->tdb_iter = tdb_nextkey(db->tdb, key);
+               iter->tdb_iter = tdb_nextkey(ctx->dir_info->tdb, key);
                free(key.dptr);
                free(data.dptr);
                return &ret_dir_info;
        }
+#endif
 
        if (iter->i >= ctx->dir_info->count)
                return 0;
 
 #ifdef DIRINFO_DEBUG
-       printf("iter(%d, %d, %d)...", ctx->dir_info->array[iter->i].ino,
+       printf("iter(%u, %u, %u)...", ctx->dir_info->array[iter->i].ino,
               ctx->dir_info->array[iter->i].dotdot,
               ctx->dir_info->array[iter->i].parent);
 #endif