/*
* dump.c --- dump the contents of an inode out to a file
- *
+ *
* Copyright (C) 1994 Theodore Ts'o. This file may be redistributed
* under the terms of the GNU Public License.
*/
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE /* for O_LARGEFILE */
+#endif
+
+#include "config.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <utime.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
-#else
+#else
extern int optind;
extern char *optarg;
#endif
-#ifdef HAVE_OPTRESET
-extern int optreset; /* defined by BSD, but not others */
-#endif
#include "debugfs.h"
+#ifndef O_LARGEFILE
+#define O_LARGEFILE 0
+#endif
+
/*
* The mode_xlate function translates a linux mode into a native-OS mode_t.
*/
{ LINUX_S_IXOTH, S_IXOTH },
{ 0, 0 }
};
-
+
static mode_t mode_xlate(__u16 lmode)
{
mode_t mode = 0;
if (i == -1)
com_err(cmd, errno, "while changing ownership of %s", name);
- if (fd != -1)
- close(fd);
-
ut.actime = inode->i_atime;
ut.modtime = inode->i_mtime;
if (utime(name, &ut) == -1)
com_err(cmd, errno, "while setting times of %s", name);
}
-static void dump_file(char *cmdname, ino_t ino, int fd, int preserve,
- char *outname)
+static void dump_file(const char *cmdname, ext2_ino_t ino, int fd,
+ int preserve, char *outname)
{
errcode_t retval;
struct ext2_inode inode;
- struct utimbuf ut;
- char buf[8192];
+ char *buf = 0;
ext2_file_t e2_file;
int nbytes;
- unsigned int got;
-
- retval = ext2fs_read_inode(current_fs, ino, &inode);
- if (retval) {
- com_err(cmdname, retval,
- "while reading inode %u in dump_file", ino);
+ unsigned int got, blocksize = current_fs->blocksize;
+
+ if (debugfs_read_inode(ino, &inode, cmdname))
return;
- }
retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
if (retval) {
com_err(cmdname, retval, "while opening ext2 file");
return;
}
+ retval = ext2fs_get_mem(blocksize, &buf);
+ if (retval) {
+ com_err(cmdname, retval, "while allocating memory");
+ return;
+ }
while (1) {
- retval = ext2fs_file_read(e2_file, buf, sizeof(buf), &got);
- if (retval)
+ retval = ext2fs_file_read(e2_file, buf, blocksize, &got);
+ if (retval)
com_err(cmdname, retval, "while reading ext2 file");
if (got == 0)
break;
nbytes = write(fd, buf, got);
- if (nbytes != got)
+ if ((unsigned) nbytes != got)
com_err(cmdname, errno, "while writing file");
}
+ if (buf)
+ ext2fs_free_mem(&buf);
retval = ext2fs_file_close(e2_file);
if (retval) {
com_err(cmdname, retval, "while closing ext2 file");
return;
}
-
+
if (preserve)
fix_perms("dump_file", &inode, fd, outname);
- else if (fd != 1)
- close(fd);
-
+
return;
}
-void do_dump(int argc, char **argv)
+void do_dump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
+ void *infop EXT2FS_ATTR((unused)))
{
- ino_t inode;
- int fd;
- int c;
- int preserve = 0;
- const char *dump_usage = "Usage: dump_inode [-p] <file> <output_file>";
- char *in_fn, *out_fn;
-
- optind = 0;
-#ifdef HAVE_OPTRESET
- optreset = 1; /* Makes BSD getopt happy */
-#endif
+ ext2_ino_t inode;
+ int fd;
+ int c;
+ int preserve = 0;
+ char *in_fn, *out_fn;
+
+ reset_getopt();
while ((c = getopt (argc, argv, "p")) != EOF) {
switch (c) {
case 'p':
preserve++;
break;
default:
- com_err(argv[0], 0, dump_usage);
+ print_usage:
+ com_err(argv[0], 0, "Usage: dump_inode [-p] "
+ "<file> <output_file>");
return;
}
}
- if (optind != argc-2) {
- com_err(argv[0], 0, dump_usage);
- return;
- }
+ if (optind != argc-2)
+ goto print_usage;
if (check_fs_open(argv[0]))
return;
out_fn = argv[optind+1];
inode = string_to_inode(in_fn);
- if (!inode)
+ if (!inode)
return;
- fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC, 0666);
+ fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, 0666);
if (fd < 0) {
com_err(argv[0], errno, "while opening %s for dump_inode",
out_fn);
}
dump_file(argv[0], inode, fd, preserve, out_fn);
+ if (close(fd) != 0) {
+ com_err(argv[0], errno, "while closing %s for dump_inode",
+ out_fn);
+ return;
+ }
return;
}
-static void rdump_symlink(ino_t ino, struct ext2_inode *inode,
+static void rdump_symlink(ext2_ino_t ino, struct ext2_inode *inode,
const char *fullname)
{
ext2_file_t e2_file;
goto errout;
}
- /* Apparently, this is the right way to detect and handle fast
- * symlinks; see do_stat() in debugfs.c. */
- if (inode->i_blocks == 0)
+ if (ext2fs_is_fast_symlink(inode))
strcpy(buf, (char *) inode->i_block);
else {
unsigned bytes = inode->i_size;
static int rdump_dirent(struct ext2_dir_entry *, int, int, char *, void *);
-static void rdump_inode(ino_t ino, struct ext2_inode *inode,
+static void rdump_inode(ext2_ino_t ino, struct ext2_inode *inode,
const char *name, const char *dumproot)
{
char *fullname;
- struct utimbuf ut;
/* There are more efficient ways to do this, but this method
* requires only minimal debugging. */
rdump_symlink(ino, inode, fullname);
else if (LINUX_S_ISREG(inode->i_mode)) {
int fd;
- fd = open(fullname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
+ fd = open(fullname, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRWXU);
if (fd == -1) {
- com_err("rdump", errno, "while dumping %s", fullname);
+ com_err("rdump", errno, "while opening %s", fullname);
goto errout;
}
dump_file("rdump", ino, fd, 1, fullname);
+ if (close(fd) != 0) {
+ com_err("rdump", errno, "while closing %s", fullname);
+ goto errout;
+ }
}
else if (LINUX_S_ISDIR(inode->i_mode) && strcmp(name, ".") && strcmp(name, "..")) {
errcode_t retval;
/* Create the directory with 0700 permissions, because we
* expect to have to create entries it. Then fix its perms
* once we've done the traversal. */
- if (mkdir(fullname, S_IRWXU) == -1) {
+ if (name[0] && mkdir(fullname, S_IRWXU) == -1) {
com_err("rdump", errno, "while making directory %s", fullname);
goto errout;
}
free(fullname);
}
-static int rdump_dirent(struct ext2_dir_entry *dirent, int offset,
- int blocksize, char *buf, void *private)
+static int rdump_dirent(struct ext2_dir_entry *dirent,
+ int offset EXT2FS_ATTR((unused)),
+ int blocksize EXT2FS_ATTR((unused)),
+ char *buf EXT2FS_ATTR((unused)), void *private)
{
- char name[EXT2_NAME_LEN];
+ char name[EXT2_NAME_LEN + 1];
int thislen;
const char *dumproot = private;
struct ext2_inode inode;
- errcode_t retval;
- thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN
- ? (dirent->name_len & 0xFF) : EXT2_NAME_LEN);
+ thislen = ext2fs_dirent_name_len(dirent);
strncpy(name, dirent->name, thislen);
name[thislen] = 0;
- retval = ext2fs_read_inode(current_fs, dirent->inode, &inode);
- if (retval) {
- com_err("rdump", retval, "while dumping %s/%s", dumproot, name);
+ if (debugfs_read_inode(dirent->inode, &inode, name))
return 0;
- }
rdump_inode(dirent->inode, &inode, name, dumproot);
return 0;
}
-void do_rdump(int argc, char **argv)
+void do_rdump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
+ void *infop EXT2FS_ATTR((unused)))
{
- ino_t ino;
- struct ext2_inode inode;
- errcode_t retval;
struct stat st;
+ char *dest_dir;
int i;
- char *p;
-
- if (argc != 3) {
- com_err(argv[0], 0, "Usage: rdump <directory> <native directory>");
- return;
- }
- if (check_fs_open(argv[0]))
+ if (common_args_process(argc, argv, 3, INT_MAX, "rdump",
+ "<directory>... <native directory>", 0))
return;
- ino = string_to_inode(argv[1]);
- if (!ino)
- return;
+ /* Pull out last argument */
+ dest_dir = argv[argc - 1];
+ argc--;
- /* Ensure ARGV[2] is a directory. */
- i = stat(argv[2], &st);
- if (i == -1) {
- com_err("rdump", errno, "while statting %s", argv[2]);
+ /* Ensure last arg is a directory. */
+ if (stat(dest_dir, &st) == -1) {
+ com_err("rdump", errno, "while statting %s", dest_dir);
return;
}
if (!S_ISDIR(st.st_mode)) {
- com_err("rdump", 0, "%s is not a directory", argv[2]);
+ com_err("rdump", 0, "%s is not a directory", dest_dir);
return;
}
- retval = ext2fs_read_inode(current_fs, ino, &inode);
- if (retval) {
- com_err("rdump", retval, "while dumping %s", argv[1]);
- return;
- }
+ for (i = 1; i < argc; i++) {
+ char *arg = argv[i], *basename;
+ struct ext2_inode inode;
+ ext2_ino_t ino = string_to_inode(arg);
+ if (!ino)
+ continue;
- p = strrchr(argv[1], '/');
- if (p)
- p++;
- else
- p = argv[1];
-
- rdump_inode(ino, &inode, p, argv[2]);
-}
+ if (debugfs_read_inode(ino, &inode, arg))
+ continue;
-void do_cat(int argc, char **argv)
-{
- ino_t inode;
+ basename = strrchr(arg, '/');
+ if (basename)
+ basename++;
+ else
+ basename = arg;
- if (argc != 2) {
- com_err(argv[0], 0, "Usage: cat <file>");
- return;
+ rdump_inode(ino, &inode, basename, dest_dir);
}
+}
- if (check_fs_open(argv[0]))
- return;
+void do_cat(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
+ void *infop EXT2FS_ATTR((unused)))
+{
+ ext2_ino_t inode;
- inode = string_to_inode(argv[1]);
- if (!inode)
+ if (common_inode_args_process(argc, argv, &inode, 0))
return;
fflush(stdout);
fflush(stderr);
- dump_file(argv[0], inode, 1, 0, argv[2]);
+ dump_file(argv[0], inode, 1, 0, argv[2]);
return;
}