forked from meganz/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlsmega.cpp
More file actions
150 lines (119 loc) · 4.04 KB
/
lsmega.cpp
File metadata and controls
150 lines (119 loc) · 4.04 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
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
/**
* @file doc/example/lsmega.cpp
* @brief Sample application, which uses libmega
*
* (c) 2013 by Mega Limited, Wellsford, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the the rules set forth in the Terms of Service.
*
* The MEGA SDK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
#include <mega.h>
using namespace mega;
struct LsApp : public MegaApp
{
void nodes_updated(Node**, int);
void login_result(error e);
void request_error(error e);
};
// globals
MegaClient* client;
static handle cwd = UNDEF;
bool mega::debug;
static const char* accesslevels[] = { "read-only", "read/write", "full access" };
// this callback function is called when nodes have been updated
// save root node handle
void LsApp::nodes_updated(Node** n, int count)
{
if (ISUNDEF(cwd)) cwd = client->rootnodes[0];
}
// this callback function is called when we have login result (success or error)
// TODO: check for errors
void LsApp::login_result(error e)
{
cout << "Logged in !" << endl;
// get the list of nodes
client->fetchnodes();
}
// this callback function is called when request-level error occurred
void LsApp::request_error(error e)
{
cout << "FATAL: Request failed exiting" << endl;
exit(0);
}
// traverse tree and list nodes
static void dumptree(Node* n, int recurse, int depth = 0, const char* title = NULL)
{
if (depth)
{
if (!title && !(title = n->displayname())) title = "CRYPTO_ERROR";
for (int i = depth; i--; ) cout << "\t";
cout << title << " (";
switch (n->type)
{
case FILENODE:
cout << n->size;
const char* p;
if ((p = strchr(n->fileattrstring.c_str(),':'))) cout << ", has attributes " << p+1;
break;
case FOLDERNODE:
cout << "folder";
for (share_map::iterator it = n->outshares.begin(); it != n->outshares.end(); it++)
{
if (it->first) cout << ", shared with " << it->second->user->email << ", access " << accesslevels[it->second->access];
else cout << ", shared as exported folder link";
}
if (n->inshare) cout << ", inbound " << accesslevels[n->inshare->access] << " share";
break;
default:
cout << "unsupported type, please upgrade";
}
cout << ")" << (n->removed ? " (DELETED)" : "") << endl;
if (!recurse) return;
}
if (n->type != FILENODE) for (node_list::iterator it = n->children.begin(); it != n->children.end(); it++) dumptree(*it,recurse,depth+1);
}
int main (int argc, char *argv[])
{
static byte pwkey[SymmCipher::KEYLENGTH];
Node* n;
// enable debugging if source is compiled with -DDEBUG=1 (check Makefile)
#ifdef DEBUG
debug = true;
#else
debug = false;
#endif
if (!getenv ("MEGA_EMAIL") || !getenv ("MEGA_PWD")) {
cout << "Please set both MEGA_EMAIL and MEGA_PWD env variables!" << endl;
return 1;
}
// create MegaClient, providing our custom MegaApp and Waiter classes
client = new MegaClient(new LsApp, new WAIT_CLASS, new HTTPIO_CLASS, new FSACCESS_CLASS, new DBACCESS_CLASS, "lsmega");
// get values from env
client->pw_key (getenv ("MEGA_PWD"), pwkey);
client->login (getenv ("MEGA_EMAIL"), pwkey);
cout << "Initiated login attempt..." << endl;
// loop while we are not logged in
while (! client->loggedin ()) {
client->wait();
client->exec();
}
// get the root node
while (! (n = client->nodebyhandle(cwd))) {
client->wait();
client->exec();
}
// display objects
dumptree(n, 1);
return 0;
}