errcode_t ext2fs_create_icount_tdb(ext2_filsys fs, char *tdb_dir, int flags, ext2_icount_t *ret) { ext2_icount_t icount; errcode_t retval; char *fn, uuid[40]; int fd; retval = alloc_icount(fs, flags, &icount); if (retval) return retval; retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &fn); if (retval) goto errout; uuid_unparse(fs->super->s_uuid, uuid); sprintf(fn, "%s/%s-icount-XXXXXX", tdb_dir, uuid); fd = mkstemp(fn); icount->tdb_fn = fn; icount->tdb = tdb_open(fn, 0, TDB_CLEAR_IF_FIRST, O_RDWR | O_CREAT | O_TRUNC, 0600); if (icount->tdb) { close(fd); *ret = icount; return 0; } retval = errno; close(fd); errout: ext2fs_free_icount(icount); return(retval); }
errcode_t ext2fs_create_icount_tdb(ext2_filsys fs, char *tdb_dir, int flags, ext2_icount_t *ret) { ext2_icount_t icount; errcode_t retval; char *fn, uuid[40]; ext2_ino_t num_inodes; int fd; retval = alloc_icount(fs, flags, &icount); if (retval) return retval; retval = ext2fs_get_mem(strlen(tdb_dir) + 64, &fn); if (retval) goto errout; uuid_unparse(fs->super->s_uuid, uuid); sprintf(fn, "%s/%s-icount-XXXXXX", tdb_dir, uuid); fd = mkstemp(fn); if (fd < 0) return fd; /* * This is an overestimate of the size that we will need; the * ideal value is the number of used inodes with a count * greater than 1. OTOH the times when we really need this is * with the backup programs that use lots of hard links, in * which case the number of inodes in use approaches the ideal * value. */ num_inodes = fs->super->s_inodes_count - fs->super->s_free_inodes_count; icount->tdb_fn = fn; icount->tdb = tdb_open(fn, num_inodes, TDB_NOLOCK | TDB_NOSYNC, O_RDWR | O_CREAT | O_TRUNC, 0600); if (icount->tdb) { close(fd); *ret = icount; return 0; } retval = errno; close(fd); errout: ext2fs_free_icount(icount); return(retval); }
errcode_t ext2fs_create_icount2(ext2_filsys fs, int flags, unsigned int size, ext2_icount_t hint, ext2_icount_t *ret) { ext2_icount_t icount; errcode_t retval; size_t bytes; ext2_ino_t i; if (hint) { EXT2_CHECK_MAGIC(hint, EXT2_ET_MAGIC_ICOUNT); if (hint->size > size) size = (size_t) hint->size; } retval = alloc_icount(fs, flags, &icount); if (retval) return retval; if (size) { icount->size = size; } else { /* * Figure out how many special case inode counts we will * have. We know we will need one for each directory; * we also need to reserve some extra room for file links */ retval = ext2fs_get_num_dirs(fs, &icount->size); if (retval) goto errout; icount->size += fs->super->s_inodes_count / 50; } bytes = (size_t) (icount->size * sizeof(struct ext2_icount_el)); #if 0 printf("Icount allocated %u entries, %d bytes.\n", icount->size, bytes); #endif retval = ext2fs_get_array(icount->size, sizeof(struct ext2_icount_el), &icount->list); if (retval) goto errout; memset(icount->list, 0, bytes); icount->count = 0; icount->cursor = 0; /* * Populate the sorted list with those entries which were * found in the hint icount (since those are ones which will * likely need to be in the sorted list this time around). */ if (hint) { for (i=0; i < hint->count; i++) icount->list[i].ino = hint->list[i].ino; icount->count = hint->count; } *ret = icount; return 0; errout: ext2fs_free_icount(icount); return(retval); }