Skip to content

Commit a99a088

Browse files
committed
debugfs: signal error through exit code for rdump.
1 parent 06a41d2 commit a99a088

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

debugfs/dump.c

+34-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,39 @@ 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+
retval = 1;
136+
break;
137+
}
134138
}
135-
if (buf)
139+
140+
retcode = retval;
141+
142+
if (buf)
136143
ext2fs_free_mem(&buf);
137-
retval = ext2fs_file_close(e2_file);
144+
145+
retval = ext2fs_file_close(e2_file);
138146
if (retval) {
139147
com_err(cmdname, retval, "while closing ext2 file");
140-
return;
148+
return retval;
141149
}
142150

143151
if (preserve)
144152
fix_perms("dump_file", &inode, fd, outname);
145153

146-
return;
154+
return retcode;
147155
}
148156

149157
void do_dump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
150158
void *infop EXT2FS_ATTR((unused)))
151159
{
160+
int retval;
152161
ext2_ino_t inode;
153162
int fd;
154163
int c;
155164
int preserve = 0;
156-
char *in_fn, *out_fn;
165+
char *in_fn, *out_fn;
157166

158167
reset_getopt();
159168
while ((c = getopt (argc, argv, "p")) != EOF) {
@@ -165,37 +174,36 @@ void do_dump(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
165174
print_usage:
166175
com_err(argv[0], 0, "Usage: dump_inode [-p] "
167176
"<file> <output_file>");
168-
return;
177+
exit(1);
169178
}
170179
}
171180
if (optind != argc-2)
172181
goto print_usage;
173182

174183
if (check_fs_open(argv[0]))
175-
return;
184+
exit(1);
176185

177186
in_fn = argv[optind];
178187
out_fn = argv[optind+1];
179188

180189
inode = string_to_inode(in_fn);
181190
if (!inode)
182-
return;
191+
exit(1);
183192

184193
fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, 0666);
185194
if (fd < 0) {
186195
com_err(argv[0], errno, "while opening %s for dump_inode",
187196
out_fn);
188-
return;
197+
exit(1);
189198
}
190199

191-
dump_file(argv[0], inode, fd, preserve, out_fn);
192-
if (close(fd) != 0) {
200+
retval = dump_file(argv[0], inode, fd, preserve, out_fn);
201+
if (close(fd) != 0) {
193202
com_err(argv[0], errno, "while closing %s for dump_inode",
194203
out_fn);
195-
return;
204+
exit(1);
196205
}
197-
198-
return;
206+
exit(retval);
199207
}
200208

201209
static void rdump_symlink(ext2_ino_t ino, struct ext2_inode *inode,
@@ -273,8 +281,12 @@ static void rdump_inode(ext2_ino_t ino, struct ext2_inode *inode,
273281
com_err("rdump", errno, "while opening %s", fullname);
274282
goto errout;
275283
}
276-
dump_file("rdump", ino, fd, 1, fullname);
277-
if (close(fd) != 0) {
284+
if (dump_file("rdump", ino, fd, 1, fullname) != 0) {
285+
com_err("rdump", errno, "while dumping %s", fullname);
286+
free(fullname);
287+
exit(1);
288+
}
289+
if (close(fd) != 0) {
278290
com_err("rdump", errno, "while closing %s", fullname);
279291
goto errout;
280292
}

0 commit comments

Comments
 (0)