blob: e5b2d203bacb8207984cdf858cb40e1ece98720b [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * lsdel.c --- routines to try to help a user recover a deleted file.
3 *
4 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
5 * Theodore Ts'o. This file may be redistributed under the terms of
6 * the GNU Public License.
7 */
8
9#include "config.h"
10#include <stdio.h>
11#include <unistd.h>
12#include <stdlib.h>
13#include <ctype.h>
14#include <string.h>
15#include <time.h>
16#ifdef HAVE_ERRNO_H
17#include <errno.h>
18#endif
19#include <sys/types.h>
20#include <sys/stat.h>
21
22#include "debugfs.h"
23
24struct deleted_info {
25 ext2_ino_t ino;
26 unsigned short mode;
27 __u32 uid;
28 __u64 size;
29 time_t dtime;
30 e2_blkcnt_t num_blocks;
31 e2_blkcnt_t free_blocks;
32};
33
34struct lsdel_struct {
35 ext2_ino_t inode;
36 e2_blkcnt_t num_blocks;
37 e2_blkcnt_t free_blocks;
38 e2_blkcnt_t bad_blocks;
39};
40
41static int deleted_info_compare(const void *a, const void *b)
42{
43 const struct deleted_info *arg1, *arg2;
44
45 arg1 = (const struct deleted_info *) a;
46 arg2 = (const struct deleted_info *) b;
47
48 return arg1->dtime - arg2->dtime;
49}
50
51static int lsdel_proc(ext2_filsys fs,
52 blk64_t *block_nr,
53 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
54 blk64_t ref_block EXT2FS_ATTR((unused)),
55 int ref_offset EXT2FS_ATTR((unused)),
56 void *private)
57{
58 struct lsdel_struct *lsd = (struct lsdel_struct *) private;
59
60 lsd->num_blocks++;
61
62 if (*block_nr < fs->super->s_first_data_block ||
63 *block_nr >= ext2fs_blocks_count(fs->super)) {
64 lsd->bad_blocks++;
65 return BLOCK_ABORT;
66 }
67
68 if (!ext2fs_test_block_bitmap2(fs->block_map,*block_nr))
69 lsd->free_blocks++;
70
71 return 0;
72}
73
74void do_lsdel(int argc, char **argv)
75{
76 struct lsdel_struct lsd;
77 struct deleted_info *delarray;
78 int num_delarray, max_delarray;
79 ext2_inode_scan scan = 0;
80 ext2_ino_t ino;
81 struct ext2_inode inode;
82 errcode_t retval;
83 char *block_buf;
84 int i;
85 long secs = 0;
86 char *tmp;
87 time_t now;
88 FILE *out;
89
90 if (common_args_process(argc, argv, 1, 2, "list_deleted_inodes",
91 "[secs]", 0))
92 return;
93
94 if (argc > 1) {
95 secs = strtol(argv[1],&tmp,0);
96 if (*tmp) {
97 com_err(argv[0], 0, "Bad time - %s",argv[1]);
98 return;
99 }
100 }
101
102 now = current_fs->now ? current_fs->now : time(0);
103 max_delarray = 100;
104 num_delarray = 0;
105 delarray = malloc(max_delarray * sizeof(struct deleted_info));
106 if (!delarray) {
107 com_err("ls_deleted_inodes", ENOMEM,
108 "while allocating deleted information storage");
109 exit(1);
110 }
111
112 block_buf = malloc(current_fs->blocksize * 3);
113 if (!block_buf) {
114 com_err("ls_deleted_inodes", ENOMEM, "while allocating block buffer");
115 goto error_out;
116 }
117
118 retval = ext2fs_open_inode_scan(current_fs, 0, &scan);
119 if (retval) {
120 com_err("ls_deleted_inodes", retval,
121 "while opening inode scan");
122 goto error_out;
123 }
124
125 do {
126 retval = ext2fs_get_next_inode(scan, &ino, &inode);
127 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
128 if (retval) {
129 com_err("ls_deleted_inodes", retval,
130 "while starting inode scan");
131 goto error_out;
132 }
133
134 while (ino) {
135 if ((inode.i_dtime == 0) ||
136 (secs && ((unsigned) abs(now - secs) > inode.i_dtime)))
137 goto next;
138
139 lsd.inode = ino;
140 lsd.num_blocks = 0;
141 lsd.free_blocks = 0;
142 lsd.bad_blocks = 0;
143
144 retval = ext2fs_block_iterate3(current_fs, ino,
145 BLOCK_FLAG_READ_ONLY, block_buf,
146 lsdel_proc, &lsd);
147 if (retval) {
148 com_err("ls_deleted_inodes", retval,
149 "while calling ext2fs_block_iterate2");
150 goto next;
151 }
152 if (lsd.free_blocks && !lsd.bad_blocks) {
153 if (num_delarray >= max_delarray) {
154 max_delarray += 50;
155 delarray = realloc(delarray,
156 max_delarray * sizeof(struct deleted_info));
157 if (!delarray) {
158 com_err("ls_deleted_inodes",
159 ENOMEM,
160 "while reallocating array");
161 exit(1);
162 }
163 }
164
165 delarray[num_delarray].ino = ino;
166 delarray[num_delarray].mode = inode.i_mode;
167 delarray[num_delarray].uid = inode_uid(inode);
168 delarray[num_delarray].size = EXT2_I_SIZE(&inode);
169 delarray[num_delarray].dtime = inode.i_dtime;
170 delarray[num_delarray].num_blocks = lsd.num_blocks;
171 delarray[num_delarray].free_blocks = lsd.free_blocks;
172 num_delarray++;
173 }
174
175 next:
176 do {
177 retval = ext2fs_get_next_inode(scan, &ino, &inode);
178 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
179 if (retval) {
180 com_err("ls_deleted_inodes", retval,
181 "while doing inode scan");
182 goto error_out;
183 }
184 }
185
186 out = open_pager();
187
188 fprintf(out, " Inode Owner Mode Size Blocks Time deleted\n");
189
190 qsort(delarray, num_delarray, sizeof(struct deleted_info),
191 deleted_info_compare);
192
193 for (i = 0; i < num_delarray; i++) {
194 fprintf(out, "%6u %6d %6o %6llu %6lld/%6lld %s",
195 delarray[i].ino,
196 delarray[i].uid, delarray[i].mode, delarray[i].size,
197 delarray[i].free_blocks, delarray[i].num_blocks,
198 time_to_string(delarray[i].dtime));
199 }
200 fprintf(out, "%d deleted inodes found.\n", num_delarray);
201 close_pager(out);
202
203error_out:
204 free(block_buf);
205 free(delarray);
206 if (scan)
207 ext2fs_close_inode_scan(scan);
208 return;
209}
210
211
212