-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparse.c
199 lines (153 loc) · 5.2 KB
/
parse.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "parse.h"
#include "externs.h"
#include "retriever/retriever.h"
#include "support/gutils.h"
#include "structure/necessary.h"
#include "config.h"
// definitions
#define OPT_FULL "-f"
#define OPT_FULL_FULL "--full"
#define OPT_INCREMENTS "-r"
#define OPT_INCREMENTS_FULL "--revisions"
#define OPT_CACHING "-c"
#define OPT_CACHING_FULL "--caching"
#define OPT_DIR "-d"
#define OPT_DIR_FULL "--directory"
#define OPT_DEBUG_FULL "--debug"
#define OPT_LAST "-l"
#define OPT_LAST_FULL "--last"
#define OPT_VERSION "-v"
#define OPT_VERSION_FULL "--version"
#define OPT_FUSE "-o"
#define OPT_DIR_LOCAL_TIME_FULL "--local-time"
#define isOption(string) ((string[0] == '-') ? 1 : 0)
// private functions
int set_caching(int argc, char **argv, int *index){
if ((*index + 1 >= argc) || (isOption(argv[*index + 1]) == 1))
return -1;
cache_limit = atoi(argv[*index + 1]);
if (cache_limit < 0)
return -1;
*index += 1;
return 0;
};
int set_increments(int argc, char **argv, int *index){
if ((*index + 1 >= argc) || (isOption(argv[*index + 1]) == 1))
return -1;
necessary_limit = atoi(argv[*index + 1]);
if (cache_limit < 0)
return -1;
*index += 1;
return 0;
};
int set_debug_level(int argc, char **argv, int *index){
if ((*index + 1 >= argc) || (isOption(argv[*index + 1]) == 1))
return -1;
debug = 1;
debug_level = atoi(argv[*index + 1]);
*index += 1;
return 0;
}
int set_directory(int argc, char **argv, int *index){
#define set_directory_finish(value) { \
if (temp != NULL) \
free(temp); \
return value; \
}
struct stat *temp = NULL;
if ((*index + 1 >= argc) || (isOption(argv[*index + 1]) == 1))
set_directory_finish(-1);
temp = single(struct stat);
if ((stat(argv[*index + 1], temp) != 0) || ((temp->st_mode & S_IFDIR) == 0))
set_directory_finish(-1);
*index += 1;
if (gstrcpy(&tmp_dir, argv[*index]) != 0)
set_directory_finish(-1);
set_directory_finish(0);
};
int add_fuse_option(int argc, char **argv, int *index){
if ((*index + 1 >= argc) || (isOption(argv[*index + 1]) == 1))
return -1;
if (gmstrcpy(&fuse_options, "-o", argv[*index + 1], NULL))
return -1;
*index += 1;
return 0;
};
void parse_option(struct file_system_info *fsinfo, int argc, char **argv, int *index){
if ((strcmp(argv[*index], OPT_INCREMENTS) == 0) || (strcmp(argv[*index], OPT_INCREMENTS_FULL) == 0)){
if (set_increments(argc, argv, index) != 0)
fail(ERR_PARAMETRES);
}
else if ((strcmp(argv[*index], OPT_FULL) == 0) || (strcmp(argv[*index], OPT_FULL_FULL) == 0))
structure = STRUCTURE_FULL;
else if ((strcmp(argv[*index], OPT_LAST) == 0) || (strcmp(argv[*index], OPT_LAST_FULL) == 0)) {
layout = LAYOUT_LAST;
structure = STRUCTURE_FULL;
}
else if ((strcmp(argv[*index], OPT_CACHING) == 0) || (strcmp(argv[*index], OPT_CACHING_FULL) == 0)){
if (set_caching(argc, argv, index) != 0)
fail(ERR_PARAMETRES);
}
else if ((strcmp(argv[*index], OPT_DIR) == 0) || (strcmp(argv[*index], OPT_DIR_FULL) == 0)){
if (set_directory(argc, argv, index) != 0)
fail(ERR_PARAMETRES);
}
else if ((strcmp(argv[*index], OPT_VERSION) == 0) || (strcmp(argv[*index], OPT_VERSION_FULL) == 0)) {
printf(PROGRAM_NAME " - filesystem in userspace for rdiff-backup repositories; version %s\n", PACKAGE_VERSION);
exit(0);
}
else if (strcmp(argv[*index], OPT_DEBUG_FULL) == 0) {
if (set_debug_level(argc, argv, index) != 0)
fail(ERR_PARAMETRES);
}
else if (strcmp(argv[*index], OPT_FUSE) == 0) {
if (add_fuse_option(argc, argv, index) != 0)
fail(ERR_PARAMETRES);
}
else if (strcmp(argv[*index], OPT_DIR_LOCAL_TIME_FULL) == 0)
fsinfo->rev_dir_time = REV_LOCAL_TIME;
else
fail(ERR_UNKNOWN_OPTION);
};
void parse_repo(struct file_system_info *fsinfo, int argc, char** argv, int *index){
int i = 0;
if (fsinfo->repo_count != 0)
fail(ERR_PARAMETRES);
for (i = *index; (i < argc) && (!isOption(argv[i])); i++){ };
fsinfo->repo_count = i - *index;
fsinfo->repos = calloc(fsinfo->repo_count, sizeof(char *));
for (i = *index; (i < argc) && (!isOption(argv[i])); i++)
gstrcpy(&fsinfo->repos[i - *index], argv[i]);
*index += fsinfo->repo_count - 1;
};
void parse_mount(char *arg){
if (mount != NULL)
fail(ERR_PARAMETRES);
if (gstrcpy(&mount, arg) != 0)
fail(-1);
};
// public functions
int parse(struct file_system_info *fsinfo, int argc, char **argv){
int i = 0;
if ((argc == 2) && ((strcmp(argv[1], OPT_VERSION) == 0) || (strcmp(argv[1], OPT_VERSION_FULL) == 0))){
printf(PROGRAM_NAME " - filesystem in userspace for rdiff-backup repositories; version %s\n", PACKAGE_VERSION);
exit(0);
};
if (argc < 3)
fail(ERR_PARAMETRES);
for (i = 1; i < argc; i++){
if (isOption(argv[i]) == 1)
parse_option(fsinfo, argc, argv, &i);
else if (mount == NULL)
parse_mount(argv[i]);
else
parse_repo(fsinfo, argc, argv, &i);
};
if (mount == NULL)
fail(ERR_NO_MOUNT);
if (fsinfo->repo_count == 0)
fail(ERR_NO_REPO);
if ((layout == LAYOUT_LAST) && (structure != STRUCTURE_FULL))
fail(ERR_FULL_ONLY);
return 0;
};