-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsplugin.cpp
More file actions
841 lines (735 loc) · 28.5 KB
/
fsplugin.cpp
File metadata and controls
841 lines (735 loc) · 28.5 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
/*
MTP plugin for Double Commander
Wfx plugin for working with MTP (Media Transfer Protocol) devices in macOS.
Copyright (C) 2025 Iskander Sultanov (BigIskander@gmail.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "common.h"
#include "fsplugin.h"
#include "utils.hpp"
#include "fsutils.hpp"
#include <libmtp.h>
#include "pathutils.h"
#define _plugin_name "MTP"
int DCPCALL FsInitW(
int PluginNr, tProgressProcW pProgressProc, tLogProcW pLogProc, tRequestProcW pRequestProc
)
{
gProgressProc = pProgressProc;
gLogProc = pLogProc;
gRequestProc = pRequestProc;
gPluginNumber = PluginNr;
return 0;
}
// MTP Devices data
LIBMTP_raw_device_t * rawdevices;
int numrawdevices;
bool isInit = false;
bool isPut = false;
HANDLE DCPCALL FsFindFirstW(WCHAR* Path, WIN32_FIND_DATAW *FindData)
{
pResources pRes = NULL;
wcharstring wPath(Path);
if(wPath.length() == 0) return (HANDLE)-1;
std::replace(wPath.begin(), wPath.end(), u'\\', u'/');
// fix issue with traversing files and folders
if(wPath.size() > 1 && wPath.substr(wPath.size() - 1) == (WCHAR*)u"/")
{
wPath = wPath.substr(0, wPath.size() - 1);
// ignore this kind of request when put files, for speed
if(isPut) return (HANDLE)-1;
}
if(!isInit)
{
LIBMTP_Init(); // call this function only once
isInit = true;
}
if(wPath.length() == 1) { // root folder of plugin
int err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices);
switch(err) {
case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
{
filterConnectedDevices(); // to remove disconnected devices from the list
gLogProc(gPluginNumber, MSGTYPE_DETAILS, (WCHAR*) u"No MTP device found.");
pRes = NULL;
break;
}
case LIBMTP_ERROR_CONNECTING:
{
gLogProc(
gPluginNumber,
MSGTYPE_IMPORTANTERROR,
(WCHAR*) u"Libmtp: connection error."
);
pRes = NULL;
gRequestProc(gPluginNumber, RT_MsgOK,
(WCHAR*)u"",
(WCHAR*)u"Libmtp: connection error.",
(WCHAR*)u"", 0
);
break;
}
case LIBMTP_ERROR_MEMORY_ALLOCATION:
{
gLogProc(
gPluginNumber,
MSGTYPE_IMPORTANTERROR,
(WCHAR*) u"Libmtp: memory allocation error."
);
pRes = NULL;
gRequestProc(gPluginNumber, RT_MsgOK,
(WCHAR*)u"",
(WCHAR*)u"Libmtp: memory allocation error.",
(WCHAR*)u"", 0
);
break;
}
case LIBMTP_ERROR_NONE:
{
LIBMTP_mtpdevice_t *newDevice;
filterConnectedDevices();
for(int i = 0; i < numrawdevices; i++) {
newDevice = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]);
addDevice(newDevice);
}
LIBMTP_FreeMemory(rawdevices);
if(availableDevices.size() == 0) {
gLogProc(
gPluginNumber,
MSGTYPE_DETAILS,
(WCHAR*) u"No MTP device with storage found."
);
pRes = NULL;
break;
}
pRes = showDevices();
break;
}
case LIBMTP_ERROR_GENERAL:
default:
{
gLogProc(
gPluginNumber,
MSGTYPE_IMPORTANTERROR,
(WCHAR*) u"Libmtp: unknown connection error."
);
pRes = NULL;
gRequestProc(gPluginNumber, RT_MsgOK,
(WCHAR*)u"",
(WCHAR*)u"Libmtp: unknown connection error.",
(WCHAR*)u"", 0
);
}
}
} else {
LIBMTP_mtpdevice_t *device;
wcharstring deviceName, storageName, internalPath;
parsePath(wPath, deviceName, storageName, internalPath);
device = getDevice(deviceName);
if (device == NULL) {
gLogProc(
gPluginNumber,
MSGTYPE_IMPORTANTERROR,
(WCHAR*) wcharstring((WCHAR*)u"MTP device \"")
.append(deviceName)
.append((WCHAR*)u"\" not found.")
.data()
);
pRes = NULL;
} else {
LIBMTP_devicestorage_t *storage;
if(storageName == UTF8toUTF16("")) {
pRes = showStorages(device);
if(pRes == NULL)
gLogProc(
gPluginNumber,
MSGTYPE_IMPORTANTERROR,
(WCHAR*) wcharstring((WCHAR*)u"MTP device \"")
.append(deviceName)
.append((WCHAR*)u"\" no storage found.")
.data()
);
} else {
storage = getStorage(device, storageName);
if(storage != NULL) {
uint32_t leaf;
if(getPathLeaf(device, storage, deviceName, storageName, internalPath, leaf)) {
pRes = showFilesAndFolders(device, storage, wPath, leaf);
} else {
gLogProc(
gPluginNumber,
MSGTYPE_DETAILS,
(WCHAR*) wcharstring((WCHAR*)u"MTP device \"")
.append(deviceName)
.append((WCHAR*)u"\" storage name \"")
.append(storageName)
.append((WCHAR*)u"\" folder with path \"")
.append(internalPath)
.append((WCHAR*)u"\" not found.")
.data()
);
pRes = NULL;
}
} else {
gLogProc(
gPluginNumber,
MSGTYPE_IMPORTANTERROR,
(WCHAR*) wcharstring((WCHAR*)u"MTP device \"")
.append(deviceName)
.append((WCHAR*)u"\" storage name \"")
.append(storageName)
.append((WCHAR*)u"\" not found.")
.data()
);
pRes = NULL;
}
}
}
}
if(!pRes || pRes->resource_array.size()==0)
{
if(pRes) { // to release memory
pRes->resource_array.clear();
delete pRes;
}
return (HANDLE)-1;
}
if(pRes->resource_array.size()>0){
memset(FindData, 0, sizeof(WIN32_FIND_DATAW));
memcpy(FindData, &(pRes->resource_array[0]), sizeof(WIN32_FIND_DATAW));
pRes->nCount++;
}
return (HANDLE) pRes;
}
BOOL DCPCALL FsFindNextW(HANDLE Hdl, WIN32_FIND_DATAW *FindData)
{
pResources pRes = (pResources) Hdl;
if(pRes && (pRes->nCount < pRes->resource_array.size()) ){
memcpy(FindData, &pRes->resource_array[pRes->nCount], sizeof(WIN32_FIND_DATAW));
pRes->nCount++;
return true;
} else {
if(pRes != NULL)
{
pRes->resource_array.clear();
delete pRes;
}
return false;
}
}
int DCPCALL FsFindClose(HANDLE Hdl)
{
// this function is not even called
return 0;
}
void DCPCALL FsGetDefRootName(char* DefRootName, int maxlen)
{
memcpy(DefRootName, _plugin_name, maxlen * sizeof(char));
}
int DCPCALL FsGetFileW(WCHAR* RemoteName, WCHAR* LocalName, int CopyFlags, RemoteInfoStruct* ri)
{
if(CopyFlags & FS_COPYFLAGS_RESUME) // resume copy not supported
return FS_FILE_NOTSUPPORTED;
wcharstring wPath(RemoteName), deviceName, storageName, internalPath, wLocal(LocalName);
if(wPath.length() == 0 || wLocal.length() == 0) return FS_FILE_OK; // just ignore this case
std::replace(wPath.begin(), wPath.end(), u'\\', u'/');
// no copy file if it is root folder of plugin or if it is root folder of device (not supported)
if(wPath == (WCHAR*)u"/")
return FS_FILE_NOTSUPPORTED;
parsePath(wPath, deviceName, storageName, internalPath);
if(wPath == wcharstring((WCHAR*)u"/").append(deviceName))
return FS_FILE_NOTSUPPORTED;
if(wPath == wcharstring((WCHAR*)u"/").append(deviceName).append((WCHAR*)u"/").append(storageName))
return FS_FILE_NOTSUPPORTED;
// do not copy if file exists and no overwrite flag is set
BOOL isFileExists = file_exists(UTF16toUTF8(LocalName));
if(isFileExists && !(CopyFlags & FS_COPYFLAGS_OVERWRITE) )
return FS_FILE_EXISTS;
LIBMTP_mtpdevice_t* device = getDevice(deviceName);
if(device == NULL)
return FS_FILE_NOTFOUND;
LIBMTP_devicestorage_t* storage = getStorage(device, storageName);
if(storage == NULL)
return FS_FILE_NOTFOUND;
if(gProgressProc(gPluginNumber, RemoteName, LocalName, 0) != 0)
return FS_FILE_USERABORT;
wcharstring folderPath, fileName;
getFolderPath(internalPath, folderPath);
// check if parent folder exists
uint32_t leaf;
if(!getPathLeaf(device, storage, deviceName, storageName, folderPath, leaf))
return FS_FILE_READERROR; // fail if parent folder was moved or renamed
// search and get leaf from cache (for speed)
if(!getLeafFromCachedFolder(device, wPath, leaf))
return FS_FILE_NOTFOUND; // if it is not in cache it is not exists probably
copyFromTo pData;
pData.From = RemoteName;
pData.To = LocalName;
if (
LIBMTP_Get_File_To_File(
device, leaf, UTF16toUTF8(LocalName).data(), progressFunc, &pData
) != 0
) {
return FS_FILE_READERROR;
}
gProgressProc(gPluginNumber, RemoteName, LocalName, 100);
return FS_FILE_OK;
}
int DCPCALL FsPutFileW(WCHAR* LocalName, WCHAR* RemoteName, int CopyFlags) {
if (CopyFlags & FS_COPYFLAGS_RESUME)
return FS_FILE_NOTSUPPORTED;
// this hint is never sent
if(((CopyFlags & FS_COPYFLAGS_EXISTS_SAMECASE) || (CopyFlags & FS_COPYFLAGS_EXISTS_DIFFERENTCASE))
&& !(CopyFlags & FS_COPYFLAGS_OVERWRITE))
return FS_FILE_EXISTS;
wcharstring wPath(RemoteName), deviceName, storageName, internalPath,
folderPath, fileName, wLocal(LocalName);
if(wPath.length() == 0 || wLocal.length() == 0) return FS_FILE_OK; // just ignore this case
std::replace(wPath.begin(), wPath.end(), u'\\', u'/');
// no copy file to root folder of plugin or to root folder of device (not supported)
getFolderPath(wPath, folderPath);
if(folderPath == (WCHAR*)u"/")
return FS_FILE_NOTSUPPORTED;
parsePath(wPath, deviceName, storageName, internalPath);
if(folderPath == wcharstring((WCHAR*)u"/").append(deviceName))
return FS_FILE_NOTSUPPORTED;
LIBMTP_mtpdevice_t* device = getDevice(deviceName);
if(device == NULL)
return FS_FILE_WRITEERROR;
LIBMTP_devicestorage_t* storage = getStorage(device, storageName);
if(storage == NULL)
return FS_FILE_WRITEERROR;
getFolderPath(internalPath, folderPath);
uint32_t folderLeaf;
if(!getPathLeaf(device, storage, deviceName, storageName, folderPath, folderLeaf))
return FS_FILE_WRITEERROR;
uint64_t fileSize;
if(!get_file_size(UTF16toUTF8(LocalName), fileSize))
return FS_FILE_READERROR;
getFileName(internalPath, fileName);
if(gProgressProc(gPluginNumber, LocalName, RemoteName, 0) != 0)
return FS_FILE_USERABORT;
getFolderPath(wPath, folderPath);
// make cache if cache not exists (skip this step otherwise)
if(!isFolderBusy(folderPath))
{
makeFolderItemsCache(folderPath);
addBusyFolder(folderPath);
}
bool isLeafFound = false;
uint32_t leaf;
// search and get leaf from cache (for speed)
if(getLeafFromCachedFolder(device, wPath, leaf))
isLeafFound = true;
// get storage again because
// LIBMTP changes storage ID itself for some reason and this might causes an error
storage = getStorage(device, storageName);
if(storage == NULL)
return FS_FILE_WRITEERROR;
// check if file already exists
if(isLeafFound)
{
if(!(CopyFlags & FS_COPYFLAGS_OVERWRITE))
{
return FS_FILE_EXISTS;
} else {
// delete already existing file or folder (to replace with new one)
if(!deleteFileOrFolder(device, storage, leaf))
return FS_FILE_WRITEERROR;
}
}
LIBMTP_file_t *genfile = LIBMTP_new_file_t();
genfile->filesize = fileSize;
genfile->filename = strdup(UTF16toUTF8(fileName.data()).data());
genfile->filetype = find_filetype(UTF16toUTF8(fileName.data()).data());
genfile->parent_id = folderLeaf;
genfile->storage_id = storage->id;
copyFromTo pData;
pData.From = LocalName;
pData.To = RemoteName;
if (
LIBMTP_Send_File_From_File(
device, UTF16toUTF8(LocalName).data(), genfile, progressFunc, &pData
) !=0
) {
LIBMTP_destroy_file_t(genfile);
return FS_FILE_WRITEERROR;
}
LIBMTP_destroy_file_t(genfile);
gProgressProc(gPluginNumber, LocalName, RemoteName, 100);
return FS_FILE_OK;
}
int DCPCALL FsRenMovFileW(WCHAR* OldName, WCHAR* NewName, BOOL Move, BOOL OverWrite, RemoteInfoStruct* ri)
{
wcharstring wPathOld(OldName), wPathNew(NewName);
if(wPathOld.length() == 0 || wPathNew.length() == 0) return FS_FILE_OK; // just ignore this case
wcharstring deviceNameOld, storageNameOld, internalPathOld, folderPathOld;
wcharstring deviceNameNew, storageNameNew, internalPathNew, folderPathNew;
std::replace(wPathOld.begin(), wPathOld.end(), u'\\', u'/');
std::replace(wPathNew.begin(), wPathNew.end(), u'\\', u'/');
parsePath(wPathOld, deviceNameOld, storageNameOld, internalPathOld);
parsePath(wPathNew, deviceNameNew, storageNameNew, internalPathNew);
getFolderPath(wPathOld, folderPathOld);
getFolderPath(wPathNew, folderPathNew);
// move or copy directly from one device to another not supported
if(deviceNameNew != deviceNameOld)
return FS_FILE_NOTSUPPORTED;
// no copy file if it is root folder of plugin or if it is root folder of device (not supported)
if(wPathOld == (WCHAR*)u"/")
return FS_FILE_NOTSUPPORTED;
if(wPathOld == wcharstring((WCHAR*)u"/").append(deviceNameOld))
return FS_FILE_NOTSUPPORTED;
if(wPathOld == wcharstring((WCHAR*)u"/")
.append(deviceNameOld)
.append((WCHAR*)u"/")
.append(storageNameOld)
)
return FS_FILE_NOTSUPPORTED;
// no copy file to root folder of plugin or to root folder of device (not supported)
if(folderPathNew == (WCHAR*)u"/")
return FS_FILE_NOTSUPPORTED;
if(folderPathNew == wcharstring((WCHAR*)u"/").append(deviceNameNew))
return FS_FILE_NOTSUPPORTED;
wcharstring fileNameNew;
getFileName(wPathNew, fileNameNew);
if(fileNameNew == (WCHAR*)u"") // no empty name
return FS_FILE_WRITEERROR;
LIBMTP_mtpdevice_t* deviceOld = getDevice(deviceNameOld);
if(deviceOld == NULL)
return FS_FILE_NOTFOUND;
LIBMTP_devicestorage_t* storageOld = getStorage(deviceOld, storageNameOld);
if(storageOld == NULL)
return FS_FILE_NOTFOUND;
bool isOldFolder = false;
uint32_t leafOld;
// search and get leaf from cache (for speed)
if(!getLeafFromCachedFolder(deviceOld, wPathOld, leafOld))
return FS_FILE_NOTFOUND; // if it is not in cache it is not exists probably
// detect is it folder or not
LIBMTP_file_t* file = LIBMTP_Get_Filemetadata(deviceOld, leafOld);
isOldFolder = file->filetype == LIBMTP_FILETYPE_FOLDER;
LIBMTP_destroy_file_t(file);
// renaming file or folder if necessary
if(folderPathNew == folderPathOld)
{
// Just rename it
if(isOldFolder)
{
// rename folder
LIBMTP_folder_t *folder = LIBMTP_new_folder_t();
if(folder == NULL)
return FS_FILE_WRITEERROR; // it's extreemly unlikely though
folder->folder_id = leafOld;
if(
LIBMTP_Set_Folder_Name(
deviceOld, folder, UTF16toUTF8(fileNameNew.data()).data()
) == 0
) {
LIBMTP_destroy_folder_t(folder);
return FS_FILE_OK;
}
LIBMTP_destroy_folder_t(folder);
return FS_FILE_WRITEERROR;
} else {
LIBMTP_file_t *file = LIBMTP_new_file_t();
if(file == NULL)
return FS_FILE_WRITEERROR; // it's extreemly unlikely though
file->item_id = leafOld;
file->filetype = find_filetype(UTF16toUTF8(fileNameNew.data()).data());
if(
LIBMTP_Set_File_Name(
deviceOld, file, UTF16toUTF8(fileNameNew.data()).data()
) == 0
) {
LIBMTP_destroy_file_t(file);
return FS_FILE_OK;
}
LIBMTP_destroy_file_t(file);
return FS_FILE_WRITEERROR;
}
}
// no copy or move folder
if(isOldFolder)
return FS_FILE_NOTSUPPORTED; // or some other error
LIBMTP_devicestorage_t* storageNew;
if(storageNameNew == storageNameOld)
{
storageNew = storageOld;
} else {
storageNew = getStorage(deviceOld, storageNameNew);
if(storageNew == NULL)
return FS_FILE_WRITEERROR;
}
wcharstring parentFolderNew;
bool isNewExists = false;
uint32_t leafNew;
getFolderPath(wPathNew, parentFolderNew);
// make cache if cache not exists (skip this step otherwise)
if(!isFolderBusy(parentFolderNew))
{
makeFolderItemsCache(parentFolderNew);
addBusyFolder(parentFolderNew);
}
// search and get leaf from cache (for speed)
if(getLeafFromCachedFolder(deviceOld, wPathNew, leafNew))
isNewExists = true;
if(isNewExists && !OverWrite)
return FS_FILE_EXISTS;
wcharstring internalParentNew;
getFolderPath(internalPathNew, internalParentNew);
uint32_t parentLeafNew;
if(!getPathLeaf(
deviceOld, storageNew, deviceNameOld, storageNameNew, internalParentNew, parentLeafNew
))
return FS_FILE_WRITEERROR;
// to copy to root folder of storage
if(parentLeafNew == LIBMTP_FILES_AND_FOLDERS_ROOT) parentLeafNew = 0;
if(gProgressProc(gPluginNumber, OldName, NewName, 0) != 0)
return FS_FILE_USERABORT;
// get storage again because
// LIBMTP changes storage ID itself for some reason and this causes an error
storageNew = getStorage(deviceOld, storageNameNew);
if(storageNew == NULL)
return FS_FILE_WRITEERROR;
if(isNewExists & OverWrite)
{
// delete already existing file or folder (to replace with new one)
if(!deleteFileOrFolder(deviceOld, storageNew, leafNew))
return FS_FILE_WRITEERROR;
}
// move or copy the file
if(Move)
{
if(
LIBMTP_Move_Object(
deviceOld, leafOld, storageNew->id, parentLeafNew
) == 0
) {
gProgressProc(gPluginNumber, OldName, NewName, 100);
return FS_FILE_OK;
}
return FS_FILE_WRITEERROR;
} else {
if(
LIBMTP_Copy_Object(
deviceOld, leafOld, storageNew->id, parentLeafNew
) == 0
) {
gProgressProc(gPluginNumber, OldName, NewName, 100);
return FS_FILE_OK;
}
return FS_FILE_WRITEERROR;
}
}
BOOL DCPCALL FsDeleteFileW(WCHAR* RemoteName)
{
wcharstring wPath(RemoteName), deviceName, storageName, internalPath;
if(wPath.length() == 0) return true; // just ignore this case
std::replace(wPath.begin(), wPath.end(), u'\\', u'/');
// no delete file if it is root folder of plugin or if it is root folder of device (not supported)
if(wPath == (WCHAR*)u"/")
return false;
parsePath(wPath, deviceName, storageName, internalPath);
if(wPath == wcharstring((WCHAR*)u"/").append(deviceName))
return false;
if(wPath == wcharstring((WCHAR*)u"/").append(deviceName).append((WCHAR*)u"/").append(storageName))
return false;
LIBMTP_mtpdevice_t* device = getDevice(deviceName);
if(device == NULL)
return false;
LIBMTP_devicestorage_t* storage = getStorage(device, storageName);
if(storage == NULL)
return false;
uint32_t leaf;
// search and get leaf from cache (for speed)
if(!getLeafFromCachedFolder(device, wPath, leaf))
return false; // if it is not in cache it is not exists probably
if(!deleteFileOrFolder(device, storage, leaf))
return false;
return true;
}
BOOL DCPCALL FsRemoveDirW(WCHAR* RemoteName)
{
wcharstring wPath(RemoteName), deviceName, storageName, internalPath;
if(wPath.length() == 0) return true; // just ignore this case
std::replace(wPath.begin(), wPath.end(), u'\\', u'/');
// no delete file if it is root folder of plugin or if it is root folder of device (not supported)
if(wPath == (WCHAR*)u"/")
return false;
parsePath(wPath, deviceName, storageName, internalPath);
if(wPath == wcharstring((WCHAR*)u"/").append(deviceName))
return false;
if(wPath == wcharstring((WCHAR*)u"/").append(deviceName).append((WCHAR*)u"/").append(storageName))
return false;
LIBMTP_mtpdevice_t* device = getDevice(deviceName);
if(device == NULL)
return false;
LIBMTP_devicestorage_t* storage = getStorage(device, storageName);
if(storage == NULL)
return false;
uint32_t leaf;
// search and get leaf from cache (for speed)
if(!getLeafFromCachedFolder(device, wPath, leaf))
return false;
// Delete folder
if(!deleteFileOrFolder(device, storage, leaf))
return false;
// remove folder from cache after deletion
removeLeafsFromCache(device, wPath);
return true;
}
BOOL DCPCALL FsMkDirW(WCHAR* Path)
{
wcharstring wPath(Path), deviceName, storageName, internalPath, folderPath, fileName;
if(wPath.length() == 0) return true; // just ignore this case
std::replace(wPath.begin(), wPath.end(), u'\\', u'/');
// no create folder in root folder of plugin or in root folder of device (not supported)
getFolderPath(wPath, folderPath);
if(folderPath == (WCHAR*)u"/")
return false;
parsePath(wPath, deviceName, storageName, internalPath);
if(folderPath == wcharstring((WCHAR*)u"/").append(deviceName))
return false;
LIBMTP_mtpdevice_t* device = getDevice(deviceName);
if(device == NULL)
return false;
LIBMTP_devicestorage_t* storage = getStorage(device, storageName);
if(storage == NULL)
return false;
getFolderPath(internalPath, folderPath);
uint32_t folderLeaf;
if(!getPathLeaf(device, storage, deviceName, storageName, folderPath, folderLeaf))
return false;
// check if folder or file already exists
uint32_t leaf;
// search and get leaf from cache (for speed)
if(getLeafFromCachedFolder(device, wPath, leaf))
{
// detect is it folder or not
LIBMTP_file_t* file = LIBMTP_Get_Filemetadata(device, leaf);
if(file->filetype == LIBMTP_FILETYPE_FOLDER)
{
LIBMTP_destroy_file_t(file);
return true;
}
LIBMTP_destroy_file_t(file);
return false;
}
getFileName(internalPath, fileName);
// get storage again because
// LIBMTP changes storage ID itself for some reason and this might causes an error
storage = getStorage(device, storageName);
if(storage == NULL)
return false;
if(LIBMTP_Create_Folder(
device,
(char*)UTF16toUTF8((WCHAR*)fileName.data()).data(),
folderLeaf,
storage->id
) == 0
)
return false;
return true;
}
// managing cache in this function
void DCPCALL FsStatusInfoW(WCHAR* RemoteDir, int InfoStartEnd, int InfoOperation)
{
wcharstring wPath(RemoteDir);
// fix for wierd issue when Double Commander sends RemoteDir as empty (basic_string)
if(wPath.length() == 0) return;
std::replace(wPath.begin(), wPath.end(), u'\\', u'/');
if(wPath.size() > 1 && wPath.substr(wPath.size() - 1) == (WCHAR*)u"/")
{
wPath = wPath.substr(0, wPath.size() - 1);
}
// get file or files
if(InfoOperation == FS_STATUS_OP_GET_SINGLE || InfoOperation == FS_STATUS_OP_GET_MULTI)
{
if(InfoStartEnd == FS_STATUS_START)
{
// make cache if cache not exists (skip this step otherwise)
makeParentFolderItemsCacheIfNotExists(wPath);
}
}
// put file or files
if(InfoOperation == FS_STATUS_OP_PUT_SINGLE || InfoOperation == FS_STATUS_OP_PUT_MULTI)
{
if(InfoStartEnd == FS_STATUS_START)
{
isPut = true;
makeFolderItemsCache(wPath);
busyFolders.clear();
addBusyFolder(wPath);
}
if(InfoStartEnd == FS_STATUS_END)
{
isPut = false;
}
}
// ren move file or folder
if(InfoOperation == FS_STATUS_OP_RENMOV_SINGLE || InfoOperation == FS_STATUS_OP_RENMOV_MULTI)
{
if(InfoStartEnd == FS_STATUS_START)
{
// make cache if cache not exists (skip this step otherwise) [move copy from folder]
makeParentFolderItemsCacheIfNotExists(wPath);
busyFolders.clear();
}
}
// delete file or folder
if(InfoOperation == FS_STATUS_OP_DELETE)
{
if(InfoStartEnd == FS_STATUS_START)
{
// make cache if cache not exists (skip this step otherwise)
makeParentFolderItemsCacheIfNotExists(wPath);
}
}
// mkdir
if(InfoOperation == FS_STATUS_OP_MKDIR)
{
if(InfoStartEnd == FS_STATUS_START)
{
// make cache if cache not exists (skip this step otherwise)
makeParentFolderItemsCacheIfNotExists(wPath);
}
}
}
int DCPCALL FsExecuteFileW(HWND MainWin, WCHAR* RemoteName, WCHAR* Verb)
{
wcharstring wPath(RemoteName), wVerb(Verb), deviceName, storageName, internalPath, fileName;
if(wVerb != wcharstring((WCHAR*)u"open")) return FS_EXEC_OK;
if(wPath.length() == 0) return FS_EXEC_OK;
std::replace(wPath.begin(), wPath.end(), u'\\', u'/');
parsePath(wPath, deviceName, storageName, internalPath);
if(wPath == wcharstring((WCHAR*)u"/")) return FS_EXEC_OK;
if(wPath == wcharstring((WCHAR*)u"/").append(deviceName)) return FS_EXEC_OK;
if(
wPath == wcharstring((WCHAR*)u"/")
.append(deviceName).append((WCHAR*)u"/").append(storageName)
) return FS_EXEC_OK;
if(internalPath.length() == 0) return FS_EXEC_OK;
getFileName(internalPath, fileName);
if(fileName.length() == 0) return FS_EXEC_OK;
if(gRequestProc(
gPluginNumber,
RT_MsgYesNo,
NULL,
(WCHAR*)wcharstring((WCHAR*)u"File \"").append(fileName)
.append((WCHAR*)u"\" will be copied to temporary folder in your computer and opened. \n")
.append((WCHAR*)u"Continue?").data(),
NULL,
0)
) return FS_EXEC_YOURSELF;
return FS_EXEC_OK;
}