Repository navigation
Expand file tree
/
Copy pathargs_reader.cpp
More file actions
105 lines (100 loc) · 3.46 KB
/
Copy pathargs_reader.cpp
File metadata and controls
105 lines (100 loc) · 3.46 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
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// Created by selman.ozleyen2017 on 16.05.2020.
//
#include <stdexcept>
#include <string>
#include <cmath>
#include "args_reader.h"
#include "file_system.h"
using namespace std;
void args_reader::mfs(int argc, const char **argv, int * bs, int * ic) {
if(argc_no != argc)
throw invalid_argument("Invalid argument number.");
int block_size = stoi(argv[1]);
double log_of_b = log2(block_size);
// if it is not a power of two
if (block_size < 0 || 0 > log2(block_size) || (log_of_b - floor(log_of_b) != 0)){
throw invalid_argument("Block size should be an positive integer which is power of two.");
}
//Calculating max inode count
int max_inode = 0;
double max_i = 0;
int bs_byte = block_size*(1 << 10);
max_i+= 32768;
max_i-= ((double)bs_byte)/8;
max_inode = floor(max_i);
int cur_inode = stoi(argv[2]);
if(max_inode < cur_inode)
throw invalid_argument("Given I-node count is too large with the given block size..");
if (cur_inode < 1)
throw invalid_argument("Given I-node count is too small with the given block size.");
*ic = cur_inode;
*bs = block_size;
}
void args_reader::file_oper(int argc, const char **argv) {
const char * filename = argv[1];
if(argc < 3)
throw invalid_argument("Please check your arguments.");
if(argv[2] == string("list")){
if(argc != 4)
throw invalid_argument("list only needs one argument.");
file_system fs(filename);
fs.list_folders(argv[3]);
}
else if (argv[2] == string("mkdir")){
if(argc != 4)
throw invalid_argument("mkdir only needs one argument.");
file_system fs(filename);
fs.mkdir(argv[3]);
}
else if (argv[2] == string("rmdir")){
if(argc != 4)
throw invalid_argument("rmdir only needs one argument.");
file_system fs(filename);
fs.rmdir(argv[3]);
}
else if (argv[2] == string("dumpe2fs")){
if(argc != 3)
throw invalid_argument("No arguments are required with dump2fs.");
file_system fs(filename);
fs.dumpe2fs();
}
else if (argv[2] == string("write")){
if(argc != 5)
throw invalid_argument("write needs 2 arguments.");
file_system fs(filename);
fs.copy_file(argv[3],argv[4]);
}
else if (argv[2] == string("read")){
if(argc != 5)
throw invalid_argument("read needs 2 arguments.");
file_system fs(filename);
fs.read_file(argv[3],argv[4]);
}
else if (argv[2] == string("ln")){
if(argc != 5)
throw invalid_argument("ln needs 2 arguments.");
file_system fs(filename);
fs.hard_link(argv[3],argv[4]);
}
else if (argv[2] == string("lnsym")){
if(argc != 5)
throw invalid_argument("lnsym needs 2 arguments.");
file_system fs(filename);
fs.soft_link(argv[3],argv[4]);
}
else if (argv[2] == string("fsck")){
if(argc != 3)
throw invalid_argument("No arguments are required with fsck.");
file_system fs(filename);
fs.fsck();
}else if (argv[2] == string("del")){
if(argc != 4)
throw invalid_argument("rmdir only needs one argument.");
file_system fs(filename);
fs.del(argv[3]);
}
else{
throw invalid_argument("Unrecognized command.");
}
}