Skip to content

Commit e13db13

Browse files
committed
debugfs: signal error through exit code for rdump.
1 parent a6de65b commit e13db13

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

debugfs/dump.c

+33-22
Original file line numberDiff line numberDiff line change
@@ -97,28 +97,29 @@ static void fix_perms(const char *cmd, const struct ext2_inode *inode,
9797
com_err(cmd, errno, "while setting times of %s", name);
9898
}
9999

100-
static void dump_file(const char *cmdname, ext2_ino_t ino, int fd,
100+
static int dump_file(const char *cmdname, ext2_ino_t ino, int fd,
101101
int preserve, char *outname)
102102
{
103103
errcode_t retval;
104+
errcode_t retcode;
104105
struct ext2_inode inode;
105106
char *buf = 0;
106107
ext2_file_t e2_file;
107108
int nbytes;
108109
unsigned int got, blocksize = current_fs->blocksize;
109110

110111
if (debugfs_read_inode(ino, &inode, cmdname))
111-
return;
112+
return 1;
112113

113114
retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
114115
if (retval) {
115116
com_err(cmdname, retval, "while opening ext2 file");
116-
return;
117+
return retval;
117118
}
118119
retval = ext2fs_get_mem(blocksize, &buf);
119120
if (retval) {
120121
com_err(cmdname, retval, "while allocating memory");
121-
return;
122+
return retval;
122123
}
123124
while (1) {
124125
retval = ext2fs_file_read(e2_file, buf, blocksize, &got);
@@ -129,31 +130,38 @@ static void dump_file(const char *cmdname, ext2_ino_t ino, int fd,
129130
if (got == 0)
130131
break;
131132
nbytes = write(fd, buf, got);
132-
if ((unsigned) nbytes != got)
133-
com_err(cmdname, errno, "while writing file");
133+
if ((unsigned) nbytes != got) {
134+
com_err(cmdname, errno, "while writing file");
135+
break;
136+
}
134137
}
135-
if (buf)
138+
139+
retcode = retval;
140+
141+
if (buf)
136142
ext2fs_free_mem(&buf);
137-
retval = ext2fs_file_close(e2_file);
143+
144+
retval = ext2fs_file_close(e2_file);
138145
if (retval) {
139146
com_err(cmdname, retval, "while closing ext2 file");
140-
return;
147+
return retval;
141148
}
142149

143150
if (preserve)
144151
fix_perms("dump_file", &inode, fd, outname);
145152

146-
return;
153+
return retcode;
147154
}
148155

149156
void do_dump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
150157
void *infop EXT2FS_ATTR((unused)))
151158
{
159+
int retval;
152160
ext2_ino_t inode;
153161
int fd;
154162
int c;
155163
int preserve = 0;
156-
char *in_fn, *out_fn;
164+
char *in_fn, *out_fn;
157165

158166
reset_getopt();
159167
while ((c = getopt (argc, argv, "p")) != EOF) {
@@ -165,37 +173,36 @@ void do_dump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
165173
print_usage:
166174
com_err(argv[0], 0, "Usage: dump_inode [-p] "
167175
"<file> <output_file>");
168-
return;
176+
exit(1);
169177
}
170178
}
171179
if (optind != argc-2)
172180
goto print_usage;
173181

174182
if (check_fs_open(argv[0]))
175-
return;
183+
exit(1);
176184

177185
in_fn = argv[optind];
178186
out_fn = argv[optind+1];
179187

180188
inode = string_to_inode(in_fn);
181189
if (!inode)
182-
return;
190+
exit(1);
183191

184192
fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, 0666);
185193
if (fd < 0) {
186194
com_err(argv[0], errno, "while opening %s for dump_inode",
187195
out_fn);
188-
return;
196+
exit(1);
189197
}
190198

191-
dump_file(argv[0], inode, fd, preserve, out_fn);
192-
if (close(fd) != 0) {
199+
retval = dump_file(argv[0], inode, fd, preserve, out_fn);
200+
if (close(fd) != 0) {
193201
com_err(argv[0], errno, "while closing %s for dump_inode",
194202
out_fn);
195-
return;
203+
exit(1);
196204
}
197-
198-
return;
205+
exit(retval);
199206
}
200207

201208
static void rdump_symlink(ext2_ino_t ino, struct ext2_inode *inode,
@@ -273,8 +280,12 @@ static void rdump_inode(ext2_ino_t ino, struct ext2_inode *inode,
273280
com_err("rdump", errno, "while opening %s", fullname);
274281
goto errout;
275282
}
276-
dump_file("rdump", ino, fd, 1, fullname);
277-
if (close(fd) != 0) {
283+
if (dump_file("rdump", ino, fd, 1, fullname) != 0) {
284+
com_err("rdump", errno, "while dumping %s", fullname);
285+
free(fullname);
286+
exit(1);
287+
}
288+
if (close(fd) != 0) {
278289
com_err("rdump", errno, "while closing %s", fullname);
279290
goto errout;
280291
}

0 commit comments

Comments
 (0)