forked from meganz/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileattributefetch.cpp
More file actions
180 lines (157 loc) · 4.48 KB
/
fileattributefetch.cpp
File metadata and controls
180 lines (157 loc) · 4.48 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
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
/**
* @file fileattributefetch.cpp
* @brief Classes for file attributes fetching
*
* (c) 2013-2014 by Mega Limited, Auckland, 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/fileattributefetch.h"
#include "mega/megaclient.h"
#include "mega/megaapp.h"
#include "mega/logging.h"
namespace mega {
FileAttributeFetchChannel::FileAttributeFetchChannel(MegaClient* client)
: client(client), bt(client->rng), timeout(client->rng)
{
req.binary = true;
req.status = REQ_READY;
urltime = 0;
fahref = UNDEF;
inbytes = 0;
e = API_EINTERNAL;
}
FileAttributeFetch::FileAttributeFetch(handle h, string key, fatype t, int ctag)
{
nodehandle = h;
nodekey = key;
type = t;
retries = 0;
tag = ctag;
}
void FileAttributeFetchChannel::dispatch()
{
faf_map::iterator it;
// reserve space
req.outbuf.clear();
req.outbuf.reserve((fafs[0].size() + fafs[1].size()) * sizeof(handle));
for (int i = 2; i--; )
{
for (it = fafs[i].begin(); it != fafs[i].end(); )
{
req.outbuf.append((char*)&it->first, sizeof(handle));
if (!i)
{
// move from fresh to pending
fafs[1][it->first] = it->second;
fafs[0].erase(it++);
}
else
{
it++;
}
}
}
if (req.outbuf.size())
{
LOG_debug << "Getting file attribute";
e = API_EFAILED;
inbytes = 0;
req.in.clear();
req.posturl = posturl;
req.post(client);
timeout.backoff(150);
}
else
{
timeout.reset();
req.status = REQ_PREPARED;
}
}
// communicate received file attributes to the application
void FileAttributeFetchChannel::parse(int /*fac*/, bool final)
{
#pragma pack(push,1)
struct FaHeader
{
handle h;
uint32_t len;
};
#pragma pack(pop)
const char* ptr = req.data();
const char* endptr = ptr + req.size();
faf_map::iterator it;
uint32_t falen = 0;
// data is structured as (handle.8.le / position.4.le) + attribute data
// attributes are CBC-encrypted with the file's key
for (;;)
{
if (ptr == endptr) break;
if (ptr + sizeof(FaHeader) > endptr || ptr + sizeof(FaHeader) + (falen = ((FaHeader*)ptr)->len) > endptr)
{
if (final || falen > 16*1048576)
{
break;
}
else
{
req.purge(ptr - req.data());
}
break;
}
it = fafs[1].find(((FaHeader*)ptr)->h);
ptr += sizeof(FaHeader);
// locate fetch request (could have been deleted by the application in the meantime)
if (it != fafs[1].end())
{
client->restag = it->second->tag;
if (!(falen & (SymmCipher::BLOCKSIZE - 1)))
{
if (client->tmpnodecipher.setkey(&it->second->nodekey))
{
client->tmpnodecipher.cbc_decrypt((byte*)ptr, falen);
client->app->fa_complete(it->second->nodehandle, it->second->type, ptr, falen);
}
delete it->second;
fafs[1].erase(it);
}
}
ptr += falen;
}
}
// notify the application of the request failure and remove records no longer needed
void FileAttributeFetchChannel::failed()
{
for (faf_map::iterator it = fafs[1].begin(); it != fafs[1].end(); )
{
client->restag = it->second->tag;
if (client->app->fa_failed(it->second->nodehandle, it->second->type, it->second->retries, e))
{
// no retry desired
delete it->second;
fafs[1].erase(it++);
}
else
{
// retry
it->second->retries++;
// move from pending to fresh
fafs[0][it->first] = it->second;
fafs[1].erase(it++);
req.status = REQ_PREPARED;
}
}
}
} // namespace