-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwd_bridge.cpp
78 lines (63 loc) · 2.1 KB
/
wd_bridge.cpp
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
#include "wdfs.h"
#include "bridge.hpp"
#include <stdio.h>
#include <stddef.h>
#include <string_view>
#define WDFS_OPT(t, p, v) { t, offsetof(struct WdFsConfig, p), v }
// Filesystem config object
struct WdFsConfig {
char* username;
char* password;
char* host;
};
// Configuration for fuse argument parser
static struct fuse_opt WdFsOpts[] = {
WDFS_OPT("user=%s", username, 0),
WDFS_OPT("pass=%s", password, 0),
WDFS_OPT("host=%s", host, 0),
FUSE_OPT_END
};
int main (int argc, char *argv[]) {
fuse_args args = FUSE_ARGS_INIT(argc, argv);
WdFsConfig conf;
memset(&conf, 0, sizeof(conf));
fuse_opt_parse(&args, &conf, WdFsOpts, NULL);
if (conf.username == NULL || conf.password == NULL || conf.host == NULL) {
fprintf(stderr, "Error: too few arguments given\n");
fprintf(stderr, "Usage: wd_bridge [-f] <mount_point> -ouser=<username>,pass=<password>,host=<device_id>\n");
return 1;
}
std::string authorization_header;
// Initialize the network bridge
if (!bridge::init_bridge()) {
fprintf(stderr, "Network bridge initialization failed... shutting down\n");
bridge::release_bridge();
return 1;
}
// Login to WD
std::string access_token;
std::string_view user(conf.username);
std::string_view pass(conf.password);
bool login_result = bridge::login(user, pass, authorization_header, &access_token);
free(conf.username);
free(conf.password);
if (!login_result) {
fprintf(stderr, "Login failed... shutting down\n");
bridge::release_bridge();
return 1;
}
// Select device endpoint to use
std::string_view device_id(conf.host);
bool endpoint_result = bridge::detect_endpoint(authorization_header, device_id);
if (!endpoint_result) {
fprintf(stderr, "Failed to detect the endpoint... shutting down\n");
bridge::release_bridge();
return 1;
}
WdFs fs;
fs.set_authorization_header(authorization_header);
int result = fs.run(args.argc, args.argv);
bridge::release_bridge();
free(conf.host);
return result;
}