-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnyufile.c
More file actions
92 lines (86 loc) · 3.14 KB
/
nyufile.c
File metadata and controls
92 lines (86 loc) · 3.14 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
#include "fat32_struct.h"
#include "helper.h"
#include "core.h"
#include "common.h"
// Usage: ./nyufile disk <options>
// -i Print the file system information.
// -l List the root directory.
// -r filename [-s sha1] Recover a contiguous file.
// -R filename -s sha1 Recover a possibly non-contiguous file.
int main(int argc, char *argv[]) {
int opt;
char filename[13] = {0};
char sha1[SHA_DIGEST_LENGTH + 1] = {0};
bool isFileRecovery = false;
bool isContiguous = false;
bool printFSInfo = false;
bool listRootDir = false;
while ((opt = getopt(argc, argv, "ilr:R:s:")) != -1) {
switch (opt) {
case 'i':
printFSInfo = true;
break;
case 'l':
listRootDir = true;
break;
case 'r':
isFileRecovery = true;
isContiguous = true;
strncpy(filename, optarg, 12);
filename[12] = '\0';
break;
case 'R':
isFileRecovery = true;
isContiguous = false;
strncpy(filename, optarg, 12);
filename[12] = '\0';
// printf("filename: %s\n", filename);
break;
case 's':
strncpy(sha1, optarg, 41);
break;
default:
fprintf(stderr, "Usage: %s disk <options>\n", argv[0]);
fprintf(stderr, " -i Print the file system information.\n"
" -l List the root directory.\n"
" -r filename [-s sha1] Recover a contiguous file.\n"
" -R filename -s sha1 Recover a possibly non-contiguous file.\n");
return 1;
}
}
char *disk = argv[optind];
if (disk == NULL) {
fprintf(stderr, "Usage: %s disk <options>\n", argv[0]);
fprintf(stderr, " -i Print the file system information.\n"
" -l List the root directory.\n"
" -r filename [-s sha1] Recover a contiguous file.\n"
" -R filename -s sha1 Recover a possibly non-contiguous file.\n");
return 1;
}
if (printFSInfo) {
print_file_system_info(disk);
return 0;
} else if (listRootDir) {
list_root_directory(disk);
return 0;
} else if (isFileRecovery) {
if (isContiguous) {
recover_contiguous_file(disk, filename, sha1);
} else {
recover_non_contiguous_file(disk, filename, sha1);
}
} else {
fprintf(stderr, "Usage: %s disk <options>\n", argv[0]);
fprintf(stderr, " -i Print the file system information.\n"
" -l List the root directory.\n"
" -r filename [-s sha1] Recover a contiguous file.\n"
" -R filename -s sha1 Recover a possibly non-contiguous file.\n");
return 1;
}
return 0;
}