From 5a51384ef2cdcb2d49965c841ff558afc1f4ee01 Mon Sep 17 00:00:00 2001 From: Theodore Ts'o Date: Sat, 25 Oct 1997 22:41:14 +0000 Subject: [PATCH] ChangeLog, debugfs.c, dump.c: debugfs.c (copy_file), dump.c (dump_file): Change to use the new fileio primitives in libext2. --- debugfs/ChangeLog | 5 ++ debugfs/debugfs.c | 140 +++++++++++++++--------------------------------------- debugfs/dump.c | 99 ++++++++------------------------------ 3 files changed, 63 insertions(+), 181 deletions(-) diff --git a/debugfs/ChangeLog b/debugfs/ChangeLog index 81a6fec..edbccd1 100644 --- a/debugfs/ChangeLog +++ b/debugfs/ChangeLog @@ -1,3 +1,8 @@ +Sat Oct 25 18:35:30 1997 Theodore Ts'o + + * debugfs.c (copy_file), dump.c (dump_file): Change to use the new + fileio primitives in libext2. + Fri Oct 24 23:47:43 1997 Theodore Ts'o * debugfs.c (main, do_open_filesys): diff --git a/debugfs/debugfs.c b/debugfs/debugfs.c index 32dc601..85d14b0 100644 --- a/debugfs/debugfs.c +++ b/debugfs/debugfs.c @@ -946,119 +946,48 @@ void do_find_free_inode(int argc, char *argv[]) printf("Free inode found: %ld\n", free_inode); } -struct copy_file_struct { - unsigned long size; - int done, fd, blocks; - errcode_t err; -}; - -static int copy_file_proc(ext2_filsys to_fs, - blk_t *blocknr, - int blockcnt, - void *private) -{ - struct copy_file_struct *cs = (struct copy_file_struct *) private; - blk_t new_blk; - static blk_t last_blk = 0; - char *block; - errcode_t retval; - int group; - int nr; - - if (*blocknr) { - new_blk = *blocknr; - } else { - retval = ext2fs_new_block(to_fs, last_blk, 0, &new_blk); - if (retval) { - cs->err = retval; - return BLOCK_ABORT; - } - } - last_blk = new_blk; - block = malloc(to_fs->blocksize); - if (!block) { - cs->err = ENOMEM; - return BLOCK_ABORT; - } - if (blockcnt >= 0) { - nr = read(cs->fd, block, to_fs->blocksize); - } else { - nr = to_fs->blocksize; - memset(block, 0, nr); - } - if (nr == 0) { - cs->done = 1; - return BLOCK_ABORT; - } - if (nr < 0) { - cs->err = nr; - return BLOCK_ABORT; - } - retval = io_channel_write_blk(to_fs->io, new_blk, 1, block); - if (retval) { - cs->err = retval; - return BLOCK_ABORT; - } - free(block); - if (blockcnt >= 0) - cs->size += nr; - cs->blocks += to_fs->blocksize / 512; - printf("%ld(%d) ", cs->size, blockcnt); - fflush(stdout); - if (nr < to_fs->blocksize) { - cs->done = 1; - printf("\n"); - } - *blocknr = new_blk; - ext2fs_mark_block_bitmap(to_fs->block_map, new_blk); - ext2fs_mark_bb_dirty(to_fs); - group = ext2fs_group_of_blk(to_fs, new_blk); - to_fs->group_desc[group].bg_free_blocks_count--; - to_fs->super->s_free_blocks_count--; - ext2fs_mark_super_dirty(to_fs); - if (cs->done) - return (BLOCK_CHANGED | BLOCK_ABORT); - else - return BLOCK_CHANGED; -} - static errcode_t copy_file(int fd, ino_t newfile) { + ext2_file_t e2_file; errcode_t retval; - struct copy_file_struct cs; - struct ext2_inode inode; - - cs.fd = fd; - cs.done = 0; - cs.err = 0; - cs.size = 0; - cs.blocks = 0; - - retval = ext2fs_block_iterate(current_fs, newfile, - BLOCK_FLAG_APPEND, - 0, copy_file_proc, &cs); - - if (cs.err) - return cs.err; - if (!cs.done) - return EXT2_ET_EXPAND_DIR_ERR; + int got, written; + char buf[8192]; + char *ptr; - /* - * Update the size and block count fields in the inode. - */ - retval = ext2fs_read_inode(current_fs, newfile, &inode); + retval = ext2fs_file_open(current_fs, newfile, + EXT2_FILE_WRITE, &e2_file); if (retval) return retval; - - inode.i_blocks += cs.blocks; - retval = ext2fs_write_inode(current_fs, newfile, &inode); - if (retval) - return retval; + while (1) { + got = read(fd, buf, sizeof(buf)); + if (got == 0) + break; + if (got < 0) { + retval = errno; + goto fail; + } + ptr = buf; + while (got > 0) { + retval = ext2fs_file_write(e2_file, ptr, + got, &written); + if (retval) + goto fail; + + got -= written; + ptr += written; + } + } + retval = ext2fs_file_close(e2_file); - return 0; + return retval; + +fail: + (void) ext2fs_file_close(e2_file); + return retval; } + void do_write(int argc, char *argv[]) { int fd; @@ -1066,6 +995,7 @@ void do_write(int argc, char *argv[]) ino_t newfile; errcode_t retval; struct ext2_inode inode; + dgrp_t group; if (check_fs_open(argv[0])) return; @@ -1092,6 +1022,10 @@ void do_write(int argc, char *argv[]) close(fd); return; } + group = ext2fs_group_of_ino(current_fs, newfile); + current_fs->group_desc[group].bg_free_inodes_count--; + current_fs->super->s_free_inodes_count--; + ext2fs_mark_super_dirty(current_fs); printf("Allocated inode: %ld\n", newfile); retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 0); if (retval) { diff --git a/debugfs/dump.c b/debugfs/dump.c index f56b2b1..6fcb140 100644 --- a/debugfs/dump.c +++ b/debugfs/dump.c @@ -61,67 +61,16 @@ static mode_t mode_xlate(__u16 lmode) return mode; } -struct dump_block_struct { - int fd; - char *buf; - int left; - errcode_t errcode; -}; - -static int dump_block(ext2_filsys fs, blk_t *blocknr, int blockcnt, - void *private) -{ - int nbytes, left; - off_t ret_off; - - struct dump_block_struct *rec = (struct dump_block_struct *) private; - - if (blockcnt < 0) - return 0; - - if (*blocknr) { - rec->errcode = io_channel_read_blk(fs->io, *blocknr, - 1, rec->buf); - if (rec->errcode) - return BLOCK_ABORT; - } else { - /* - * OK, the file has a hole. Let's try to seek past - * the hole in the destination file, so that the - * destination file has a hole too. - */ - ret_off = lseek(rec->fd, fs->blocksize, SEEK_CUR); - if (ret_off >= 0) - return 0; - memset(rec->buf, 0, fs->blocksize); - } - - left = (rec->left > fs->blocksize) ? fs->blocksize : rec->left; - rec->left -= left; - - while (left > 0) { - nbytes = write(rec->fd, rec->buf, left); - if (nbytes == -1) { - if (errno == EINTR) - continue; - rec->errcode = errno; - return BLOCK_ABORT; - } - left -= nbytes; - } - if (rec->left <= 0) - return BLOCK_ABORT; - return 0; -} - static void dump_file(char *cmdname, ino_t ino, int fd, int preserve, char *outname) { errcode_t retval; - struct dump_block_struct rec; struct ext2_inode inode; struct utimbuf ut; - + char buf[8192]; + ext2_file_t e2_file; + int nbytes, got; + retval = ext2fs_read_inode(current_fs, ino, &inode); if (retval) { com_err(cmdname, retval, @@ -129,32 +78,27 @@ static void dump_file(char *cmdname, ino_t ino, int fd, int preserve, return; } - rec.fd = fd; - rec.errcode = 0; - rec.buf = malloc(current_fs->blocksize); - rec.left = inode.i_size; - - if (rec.buf == 0) { - com_err(cmdname, ENOMEM, - "while allocating block buffer for dump_inode"); + retval = ext2fs_file_open(current_fs, ino, 0, &e2_file); + if (retval) { + com_err(cmdname, retval, "while opening ext2 file"); return; } - - retval = ext2fs_block_iterate(current_fs, ino, - BLOCK_FLAG_HOLE|BLOCK_FLAG_DATA_ONLY, - NULL, dump_block, &rec); - if (retval) { - com_err(cmdname, retval, "while iterating over blocks in %s", - outname); - goto cleanup; + while (1) { + retval = ext2fs_file_read(e2_file, buf, sizeof(buf), &got); + if (retval) + com_err(cmdname, retval, "while reading ext2 file"); + if (got == 0) + break; + nbytes = write(fd, buf, got); + if (nbytes != got) + com_err(cmdname, errno, "while writing file"); } - if (rec.errcode) { - com_err(cmdname, retval, "in dump_block while dumping %s", - outname); - goto cleanup; + retval = ext2fs_file_close(e2_file); + if (retval) { + com_err(cmdname, retval, "while closing ext2 file"); + return; } - -cleanup: + if (preserve) { #ifdef HAVE_FCHOWN if (fchown(fd, inode.i_uid, inode.i_gid) < 0) @@ -178,7 +122,6 @@ cleanup: } else if (fd != 1) close(fd); - free(rec.buf); return; } -- 1.8.3.1