-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
123 lines (106 loc) · 3.5 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <getopt.h>
#include "main.h"
#include "ext4_undelete.h"
#define GETOPT_ARGS "hi:n:o:s"
static void print_help() {
printf("\nUsage: ./ext4_undelete device -i ino [OPTIONS]");
printf("\n\nOptions:");
printf("\n\t-h: print this help");
printf("\n\t-i ino: specifies inode number, which holds info about file which should be undeleted");
printf("\n\t-n original-filename: original filename of deleted file. Can be used only in case, that parent directory of deleted file exists.");
printf("\n\t-o filename: filename of undeleted file. Default is %s", DEFAULT_FILENAME);
printf("\n\t-s: strip trailing zeros in last block. By default, this option is off");
printf("\n");
}
static struct options parse_options(int argc, char ** argv) {
int c;
int index;
struct options opts = {
.state = 0,
.ino = 0,
.output_name = DEFAULT_FILENAME,
.original_name = NULL,
.device = NULL,
.strip = false
};
while ((c = getopt(argc, argv, GETOPT_ARGS)) != -1) {
switch (c) {
case 'h':
print_help();
opts.state = -1;
return opts;
case 'i':
opts.ino = strtoul(optarg, NULL, 0);
break;
case 'o':
opts.output_name = optarg;
break;
case 'n':
opts.original_name = optarg;
break;
case 's':
opts.strip = true;
break;
case '?':
if (optopt == 'c')
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint(optopt))
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
print_help();
opts.state = -1;
return opts;
default:
print_help();
opts.state = -1;
return opts;
}
}
argc -= optind;
argv += optind;
// process non option params
for (index = 0; index < argc; index++) {
if (index == 0) {
opts.device = argv[index];
}
}
// device param is required
if (opts.device == NULL) {
opts.state = -1;
fprintf(stderr, "Missing device param!\n");
print_help();
return opts;
}
// inode offset is required
if ((opts.ino == 0) && (opts.original_name == NULL)) {
opts.state = -1;
fprintf(stderr, "inode number or original filename param is "
"required!\n");
print_help();
return opts;
}
// if inode number and original filename is specified at the same time
if ((opts.ino != 0) && (opts.original_name != NULL)){
opts.state = -1;
fprintf(stderr, "you can't specify inode number and original filename "
"at the same time!\n");
print_help();
return opts;
}
return opts;
}
int main(int argc, char ** argv) {
struct options opts;
opts = parse_options(argc, argv);
if (opts.state < 0)
return EXIT_FAILURE;
if (undelete_file(opts.device, opts.original_name, opts.ino, opts.output_name, opts.strip) < 0) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}