/
disk.c
306 lines (286 loc) · 10.1 KB
/
disk.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <stdio.h>
#include "ndplugin.h"
#include "utility.h"
#include "disk.h"
#include "lua/lauxlib.h"
static wchar_t *_config_file = L"netdisks.ini";
NDiskEntry *available_disk_entries = NULL;
size_t available_disk_entries_length = 0;
Dictionary *available_disks = NULL;
static int ndisk_save_enumerator(const wchar_t *key, const void *value, size_t size, void **data) {
NDisk *disk;
disk = (NDisk *)value;
WritePrivateProfileStringW(key, L"type", disk->type, (wchar_t*)data);
WritePrivateProfileStringW(key, L"username", disk->username, (wchar_t*)data);
WritePrivateProfileStringW(key, L"nickname", disk->nickname, (wchar_t*)data);
WritePrivateProfileStringW(key, L"token", disk->token, (wchar_t*)data);
WritePrivateProfileStringW(key, L"secret", disk->secret, (wchar_t*)data);
return 1;
}
void ndisks_save() {
wchar_t tmp[PATH_MAX];
if(available_disks == NULL || !available_disks->length) {
return;
}
wcslcpy(tmp, my_dir, PATH_MAX);
wcscat_s(tmp, PATH_MAX, _config_file);
DeleteFileW(tmp);
dict_traverse(available_disks, ndisk_save_enumerator, (void **)&tmp);
}
#define SECTIONS_BUFFER_SIZE 4096
void ndisks_load() {
wchar_t tmp[SECTIONS_BUFFER_SIZE], sTmp[SECTIONS_BUFFER_SIZE], *pTmp, rTmp[SECTIONS_BUFFER_SIZE];
size_t i = 0, j = 0;
NDisk disk;
if(available_disks == NULL) {
return;
}
wcslcpy(tmp, my_dir, SECTIONS_BUFFER_SIZE);
wcscat_s(tmp, SECTIONS_BUFFER_SIZE, _config_file);
GetPrivateProfileSectionNamesW(sTmp, SECTIONS_BUFFER_SIZE, tmp);
for(i = 0; i < SECTIONS_BUFFER_SIZE; i++) {
if(sTmp[0] == '\0') {
break;
}
if(sTmp[i] == '\0') {
pTmp = sTmp + j;
if(!pTmp || wcscmp(pTmp, L"") == 0) {
continue;
}
j = i + 1;
memset(&disk, 0, sizeof(NDisk));
GetPrivateProfileStringW(pTmp, L"type", NULL, rTmp, SECTIONS_BUFFER_SIZE, tmp);
if(!rTmp || wcscmp(rTmp, L"") == 0) {
continue;
}
disk.type = _wcsdup(rTmp);
GetPrivateProfileStringW(pTmp, L"username", NULL, rTmp, SECTIONS_BUFFER_SIZE, tmp);
if(!rTmp || wcscmp(rTmp, L"") == 0) {
free(disk.type);
continue;
}
disk.username = _wcsdup(rTmp);
disk.nickname = _wcsdup(pTmp);
GetPrivateProfileStringW(pTmp, L"token", NULL, rTmp, SECTIONS_BUFFER_SIZE, tmp);
disk.token = _wcsdup(rTmp);
GetPrivateProfileStringW(pTmp, L"secret", NULL, rTmp, SECTIONS_BUFFER_SIZE, tmp);
disk.secret = _wcsdup(rTmp);
dict_set_element_s(available_disks, pTmp, &disk, sizeof(NDisk), ndisk_destroy_s);
}
}
}
// hiden danger for sPath buffer size;
int ndisk_parse(const wchar_t *path, NDiskEntry **entry, NDisk **disk, wchar_t *sPath) {
wchar_t tmp[PATH_MAX], *sTmp = NULL;
int r = NDISK_FATAL, i = 0;
if(!path || wcscmp(path, L"") == 0 || wcscmp(path, L"\\") == 0) {
return r;
}
sTmp = (wchar_t *)path + 1;
while(*sTmp != '\0' && *sTmp != '\\') {
sTmp++;
}
wcslcpy(tmp, path + 1, (int)(sTmp - path));
if(dict_get_element_s(available_disks, tmp, (void **)disk) == DICT_OK) {
for(i = 0; i < available_disk_entries_length; i++) {
if(wcscmp((*disk)->type, available_disk_entries[i].name) == 0) {
*entry = &available_disk_entries[i];
wcslcpy(sPath, path + wcslen(tmp) + 1, PATH_MAX);
if(*sPath == '\0') {
wcslcpy(sPath, L"\\", PATH_MAX);
}
r = NDISK_OK;
break;
}
}
}
return r;
}
static int ndisk_lua_push(lua_State *l, NDisk *disk) {
char *tmp = NULL;
wchar_t sTmp[40];
lua_newtable(l);
tmp = wtoc(disk->nickname);
lua_pushstring(l, tmp);
free(tmp);
tmp = NULL;
lua_setfield(l, -2, "nickname");
tmp = wtoc(disk->username);
lua_pushstring(l, tmp);
free(tmp);
tmp = NULL;
lua_setfield(l, -2, "username");
if(disk->token) {
tmp = wtoc(disk->token);
lua_pushstring(l, tmp);
free(tmp);
tmp = NULL;
lua_setfield(l, -2, "token");
}
if(disk->secret) {
tmp = wtoc(disk->secret);
lua_pushstring(l, tmp);
free(tmp);
tmp = NULL;
lua_setfield(l, -2, "secret");
}
return NDISK_OK;
}
int ndisk_dir(Dictionary *dict, const NDiskEntry *entry, NDisk *disk, const wchar_t *path) {
WIN32_FIND_DATAW fData;
char *tmp = NULL, *sTmp = NULL;
wchar_t *pTmp = NULL;
unsigned long size = 0;
int i = 0;
SYSTEMTIME time;
lua_reset();
lua_getglobal(script, "entries");
tmp = wtoc(entry->name);
lua_getfield(script, -1, tmp);
free(tmp);
tmp = NULL;
i = lua_gettop(script);
if(lua_pcall(script, 0, 0, 0)) {
OutputDebugStringA("Error Msg is: ");
OutputDebugStringA(lua_tostring(script, -1));
}
lua_pop(script, 1);
lua_getglobal(script, "dir");
if(!lua_isfunction(script, -1)) {
RequestProcW(PluginNumber, RT_MsgOK, L"配置错误", L"网盘脚本错误, 不能读取当前目录的内容. ", NULL, 0);
return NDISK_FATAL;
}
if(ndisk_lua_push(script, disk) == NDISK_FATAL) {
lua_settop(script, 0);
return NDISK_FATAL;
}
tmp = wtoc(path);
lua_pushstring(script, tmp);
free(tmp);
tmp = NULL;
lua_setfield(script, -2, "path");
if(lua_pcall(script, 1, 1, 0)) {
OutputDebugStringA("Error Msg is: ");
OutputDebugStringA(lua_tostring(script, -1));
}
if(lua_istable(script, -1)) {
lua_pushnil(script);
while (lua_next(script, -2)) {
if(lua_istable(script, -1)) {
memset(&fData, 0, sizeof(WIN32_FIND_DATAW));
lua_pushnil(script);
while(lua_next(script, -2)) {
tmp = (char *)lua_tostring(script, -2);
if(strcmp(tmp, "attribute") == 0) {
sTmp = (char *)lua_tostring(script, -1);
if(strcmp(sTmp, "file") == 0) {
fData.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
}
if(strcmp(sTmp, "directory") == 0) {
fData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
}
}
if(strcmp(tmp, "filename") == 0) {
sTmp = (char *)lua_tostring(script, -1);
pTmp = ctow(sTmp);
wcslcpy(fData.cFileName, pTmp, PATH_MAX);
free(pTmp);
pTmp = NULL;
}
if(strcmp(tmp, "size") == 0) {
size = (unsigned long)lua_tonumber(script, -1);
fData.nFileSizeHigh = HIWORD(size);
fData.nFileSizeLow = LOWORD(size);
}
if(strcmp(tmp, "create") == 0) {
sTmp = (char *)lua_tostring(script, -1);
pTmp = ctow(sTmp);
memset(&time, 0, sizeof(SYSTEMTIME));
if(strtotime(pTmp, &time)) {
SystemTimeToFileTime(&time, &fData.ftCreationTime);
}
free(pTmp);
pTmp = NULL;
}
if(strcmp(tmp, "access") == 0) {
sTmp = (char *)lua_tostring(script, -1);
pTmp = ctow(sTmp);
memset(&time, 0, sizeof(SYSTEMTIME));
if(strtotime(pTmp, &time)) {
SystemTimeToFileTime(&time, &fData.ftLastAccessTime);
}
free(pTmp);
pTmp = NULL;
}
if(strcmp(tmp, "write") == 0) {
sTmp = (char *)lua_tostring(script, -1);
pTmp = ctow(sTmp);
memset(&time, 0, sizeof(SYSTEMTIME));
if(strtotime(pTmp, &time)) {
SystemTimeToFileTime(&time, &fData.ftLastWriteTime);
}
free(pTmp);
pTmp = NULL;
}
lua_pop(script, 1);
}
if(fData.cFileName) {
dict_set_element(dict, fData.cFileName, &fData, sizeof(WIN32_FIND_DATAW));
}
}
lua_pop(script, 1);
}
}
lua_pop(script, 1);
return NDISK_OK;
}
int ndisk_download(const NDiskEntry *entry, NDisk *disk, const wchar_t *remote, const wchar_t *local) {
return NDISK_OK;
}
int ndisk_upload(const NDiskEntry *entry, NDisk *disk, const wchar_t *local, const wchar_t *remote) {
return NDISK_OK;
}
int ndisk_copy(const NDiskEntry *entry, NDisk *disk, const wchar_t *src, const wchar_t *des) {
return NDISK_OK;
}
int ndisk_move(const NDiskEntry *entry, NDisk *disk, const wchar_t *src, const wchar_t *des) {
return NDISK_OK;
}
int ndisk_erasure(const NDiskEntry *entry, NDisk *disk, const wchar_t *path) {
return NDISK_OK;
}
int ndisk_mkdir(const NDiskEntry *entry, NDisk *disk, const wchar_t *path) {
return NDISK_OK;
}
int ndisk_info(const NDiskEntry *entry, NDisk *disk, const wchar_t *path) {
return NDISK_OK;
}
void ndisk_destroy_s(void **disk) {
ndisk_destroy((NDisk **)disk);
}
void ndisk_destroy(NDisk **disk) {
if((*disk) == NULL) {
return;
}
if((*disk)->type != NULL) {
free((*disk)->type);
(*disk)->type = NULL;
}
if((*disk)->nickname != NULL) {
free((*disk)->nickname);
(*disk)->nickname = NULL;
}
if((*disk)->secret != NULL) {
free((*disk)->secret);
(*disk)->secret = NULL;
}
if((*disk)->token != NULL) {
free((*disk)->token);
(*disk)->token = NULL;
}
if((*disk)->username != NULL) {
free((*disk)->username);
(*disk)->username = NULL;
}
(*disk) = NULL;
}