forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_system.cpp
1062 lines (852 loc) · 25.7 KB
/
file_system.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
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
/* Some of the original code to handle PIDLs come from the
MiniExplorer example of the Vaca library:
https://github.com/dacap/vaca
Copyright (C) by David Capello (MIT License)
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/file_system.h"
#include "base/fs.h"
#include "base/string.h"
#include "os/display.h"
#include "os/surface.h"
#include "os/system.h"
#include <algorithm>
#include <cstdio>
#include <map>
#include <utility>
#include <vector>
#ifdef _WIN32
#include <windows.h>
#include <shlobj.h>
#include <shlwapi.h>
#define MYPC_CSLID "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
#else
#include <dirent.h>
#include <sys/stat.h>
#endif
//////////////////////////////////////////////////////////////////////
#ifndef MAX_PATH
#define MAX_PATH 4096
#endif
#define NOTINITIALIZED "{*empty*}"
#define FS_TRACE(...) // TRACE
namespace app {
namespace {
class FileItem;
typedef std::map<std::string, FileItem*> FileItemMap;
// the root of the file-system
FileItem* rootitem = nullptr;
FileItemMap* fileitems_map = nullptr;
unsigned int current_file_system_version = 0;
#ifdef _WIN32
IMalloc* shl_imalloc = nullptr;
IShellFolder* shl_idesktop = nullptr;
#endif
// a position in the file-system
class FileItem : public IFileItem {
public:
// TODO make all these fields private
std::string m_keyname;
std::string m_filename;
std::string m_displayname;
FileItem* m_parent;
FileItemList m_children;
unsigned int m_version;
bool m_removed;
mutable bool m_is_folder;
double m_thumbnailProgress;
os::Surface* m_thumbnail;
#ifdef _WIN32
LPITEMIDLIST m_pidl; // relative to parent
LPITEMIDLIST m_fullpidl; // relative to the Desktop folder
// (like a full path-name, because the
// desktop is the root on Windows)
#endif
FileItem(FileItem* parent);
~FileItem();
void insertChildSorted(FileItem* child);
int compare(const FileItem& that) const;
bool operator<(const FileItem& that) const { return compare(that) < 0; }
bool operator>(const FileItem& that) const { return compare(that) > 0; }
bool operator==(const FileItem& that) const { return compare(that) == 0; }
bool operator!=(const FileItem& that) const { return compare(that) != 0; }
// IFileItem impl
bool isFolder() const override;
bool isBrowsable() const override;
bool isHidden() const override;
bool isExistent() const override;
const std::string& keyName() const override;
const std::string& fileName() const override;
const std::string& displayName() const override;
IFileItem* parent() const override;
const FileItemList& children() override;
void createDirectory(const std::string& dirname) override;
bool hasExtension(const base::paths& extensions) override;
double getThumbnailProgress() override { return m_thumbnailProgress; }
void setThumbnailProgress(double progress) override {
m_thumbnailProgress = progress;
}
os::Surface* getThumbnail() override;
void setThumbnail(os::Surface* thumbnail) override;
// Calls "delete this"
void deleteItem() {
FileSystemModule::instance()->ItemRemoved(this);
if (m_parent) {
auto& container = m_parent->m_children;
auto it = std::find(container.begin(), container.end(), this);
if (it != container.end())
container.erase(it);
}
auto it = fileitems_map->find(m_keyname);
if (it != fileitems_map->end())
fileitems_map->erase(it);
// Delete all children recursively
for (auto ichild : m_children) {
FileItem* child = static_cast<FileItem*>(ichild);
child->m_parent = nullptr;
child->deleteItem();
}
delete this;
}
};
} // anonymous namespace
// A more easy PIDLs interface (without using the SH* & IL* routines of W2K)
#ifdef _WIN32
static void update_by_pidl(FileItem* fileitem, SFGAOF attrib);
static LPITEMIDLIST concat_pidl(LPITEMIDLIST pidlHead, LPITEMIDLIST pidlTail);
static UINT get_pidl_size(LPITEMIDLIST pidl);
static LPITEMIDLIST get_next_pidl(LPITEMIDLIST pidl);
static LPITEMIDLIST get_last_pidl(LPITEMIDLIST pidl);
static LPITEMIDLIST clone_pidl(LPITEMIDLIST pidl);
static LPITEMIDLIST remove_last_pidl(LPITEMIDLIST pidl);
static void free_pidl(LPITEMIDLIST pidl);
static std::string get_key_for_pidl(LPITEMIDLIST pidl);
static FileItem* get_fileitem_by_fullpidl(LPITEMIDLIST pidl, bool create_if_not);
static void put_fileitem(FileItem* fileitem);
#else
static FileItem* get_fileitem_by_path(const std::string& path, bool create_if_not);
static std::string remove_backslash_if_needed(const std::string& filename);
static std::string get_key_for_filename(const std::string& filename);
static void put_fileitem(FileItem* fileitem);
#endif
FileSystemModule* FileSystemModule::m_instance = nullptr;
FileSystemModule::FileSystemModule()
{
ASSERT(m_instance == NULL);
m_instance = this;
fileitems_map = new FileItemMap;
#ifdef _WIN32
/* get the IMalloc interface */
HRESULT hr = SHGetMalloc(&shl_imalloc);
if (hr != S_OK)
throw std::runtime_error("Error initializing file system. Report this problem. (SHGetMalloc failed.)");
/* get desktop IShellFolder interface */
hr = SHGetDesktopFolder(&shl_idesktop);
if (hr != S_OK)
throw std::runtime_error("Error initializing file system. Report this problem. (SHGetDesktopFolder failed.)");
#endif
// first version of the file system
++current_file_system_version;
// get the root element of the file system (this will create
// the 'rootitem' FileItem)
getRootFileItem();
}
FileSystemModule::~FileSystemModule()
{
ASSERT(m_instance == this);
for (auto it=fileitems_map->begin(); it!=fileitems_map->end(); ++it) {
delete it->second;
}
fileitems_map->clear();
#ifdef _WIN32
// relase desktop IShellFolder interface
shl_idesktop->Release();
// release IMalloc interface
shl_imalloc->Release();
shl_imalloc = NULL;
#endif
delete fileitems_map;
m_instance = NULL;
}
FileSystemModule* FileSystemModule::instance()
{
return m_instance;
}
void FileSystemModule::refresh()
{
++current_file_system_version;
}
IFileItem* FileSystemModule::getRootFileItem()
{
FileItem* fileitem;
if (rootitem)
return rootitem;
fileitem = new FileItem(NULL);
rootitem = fileitem;
//LOG("FS: Creating root fileitem %p\n", rootitem);
#ifdef _WIN32
{
// get the desktop PIDL
LPITEMIDLIST pidl = NULL;
HRESULT hr = SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOP, &pidl);
if (hr != S_OK) {
// TODO do something better
ASSERT(false);
exit(1);
}
fileitem->m_pidl = pidl;
fileitem->m_fullpidl = pidl;
SFGAOF attrib = SFGAO_FOLDER;
shl_idesktop->GetAttributesOf(1, (LPCITEMIDLIST *)&pidl, &attrib);
update_by_pidl(fileitem, attrib);
}
#else
{
const char* root = "/";
fileitem->m_filename = root;
fileitem->m_displayname = root;
fileitem->m_is_folder = true;
}
#endif
// insert the file-item in the hash-table
put_fileitem(fileitem);
return fileitem;
}
IFileItem* FileSystemModule::getFileItemFromPath(const std::string& path)
{
IFileItem* fileitem = NULL;
//LOG("FS: get_fileitem_from_path(%s)\n", path.c_str());
#ifdef _WIN32
{
ULONG cbEaten = 0UL;
LPITEMIDLIST fullpidl = NULL;
SFGAOF attrib = SFGAO_FOLDER;
// Default folder is desktop folder (the root item in the hierarchy)
if (path.empty()) {
fileitem = getRootFileItem();
//LOG("FS: > %p (root)\n", fileitem);
return fileitem;
}
if (shl_idesktop->ParseDisplayName
(NULL, NULL,
const_cast<LPWSTR>(base::from_utf8(path).c_str()),
&cbEaten, &fullpidl, &attrib) != S_OK) {
//LOG("FS: > (null)\n");
return NULL;
}
fileitem = get_fileitem_by_fullpidl(fullpidl, true);
free_pidl(fullpidl);
}
#else
{
// The default folder is the user home folder
if (path.empty()) {
fileitem = get_fileitem_by_path(base::get_user_docs_folder(), true);
}
else {
std::string buf = remove_backslash_if_needed(path);
fileitem = get_fileitem_by_path(buf, true);
}
}
#endif
//LOG("FS: get_fileitem_from_path(%s) -> %p\n", path.c_str(), fileitem);
return fileitem;
}
// ======================================================================
// FileItem class (IFileItem implementation)
// ======================================================================
bool FileItem::isFolder() const
{
return m_is_folder;
}
bool FileItem::isBrowsable() const
{
ASSERT(m_filename != NOTINITIALIZED);
return m_is_folder;
}
bool FileItem::isHidden() const
{
ASSERT(m_displayname != NOTINITIALIZED);
#ifdef _WIN32
return false;
#else
return m_displayname[0] == '.';
#endif
}
bool FileItem::isExistent() const
{
const std::string& fn = fileName();
#ifdef _WIN32
if (!fn.empty() && fn.front() == ':') { // It's a PIDL of a special location
FS_TRACE("FS: isExistent() %s -> PIDL exists\n", fn.c_str());
return true;
}
#endif
bool result = false;
if (base::is_directory(fn)) {
result = true;
if (!m_is_folder)
m_is_folder = true; // Update the "is folder" flag
}
else if (base::is_file(fn)) {
result = true;
if (m_is_folder)
m_is_folder = false;
}
FS_TRACE("FS: isExistent() %s -> %s\n", fn.c_str(), (result ? "exists": "DOESN'T EXIST"));
return result;
}
const std::string& FileItem::keyName() const
{
ASSERT(m_keyname != NOTINITIALIZED);
return m_keyname;
}
const std::string& FileItem::fileName() const
{
ASSERT(m_filename != NOTINITIALIZED);
return m_filename;
}
const std::string& FileItem::displayName() const
{
ASSERT(m_displayname != NOTINITIALIZED);
return m_displayname;
}
IFileItem* FileItem::parent() const
{
if (this == rootitem)
return NULL;
else {
ASSERT(m_parent);
return m_parent;
}
}
const FileItemList& FileItem::children()
{
// Is the file-item a folder?
if (isFolder() &&
// if the children list is empty, or the file-system version
// change (it's like to say: the current m_children list
// is outdated)...
(m_children.empty() ||
current_file_system_version > m_version)) {
FileItemList::iterator it;
FileItem* child;
// we have to mark current items as deprecated
for (it=m_children.begin();
it!=m_children.end(); ++it) {
child = static_cast<FileItem*>(*it);
child->m_removed = true;
}
//LOG("FS: Loading files for %p (%s)\n", fileitem, fileitem->displayname);
#ifdef _WIN32
{
IShellFolder* pFolder = NULL;
HRESULT hr;
if (this == rootitem)
pFolder = shl_idesktop;
else {
hr = shl_idesktop->BindToObject(m_fullpidl,
NULL, IID_IShellFolder, (LPVOID *)&pFolder);
if (hr != S_OK)
pFolder = NULL;
}
if (pFolder != NULL) {
IEnumIDList *pEnum = NULL;
ULONG c, fetched;
/* get the interface to enumerate subitems */
hr = pFolder->EnumObjects(reinterpret_cast<HWND>(os::instance()->defaultDisplay()->nativeHandle()),
SHCONTF_FOLDERS | SHCONTF_NONFOLDERS, &pEnum);
if (hr == S_OK && pEnum != NULL) {
LPITEMIDLIST itempidl[256];
SFGAOF attribs[256];
/* enumerate the items in the folder */
while (pEnum->Next(256, itempidl, &fetched) == S_OK && fetched > 0) {
/* request the SFGAO_FOLDER attribute to know what of the
item is a folder */
for (c=0; c<fetched; ++c) {
attribs[c] = SFGAO_FOLDER;
pFolder->GetAttributesOf(1, (LPCITEMIDLIST *)itempidl, attribs+c);
}
/* generate the FileItems */
for (c=0; c<fetched; ++c) {
LPITEMIDLIST fullpidl = concat_pidl(m_fullpidl,
itempidl[c]);
child = get_fileitem_by_fullpidl(fullpidl, false);
if (!child) {
child = new FileItem(this);
child->m_pidl = itempidl[c];
child->m_fullpidl = fullpidl;
update_by_pidl(child, attribs[c]);
put_fileitem(child);
}
else {
ASSERT(child->m_parent == this);
free_pidl(fullpidl);
free_pidl(itempidl[c]);
}
insertChildSorted(child);
}
}
pEnum->Release();
}
if (pFolder != shl_idesktop)
pFolder->Release();
}
}
#else
{
DIR* dir = opendir(m_filename.c_str());
if (dir) {
dirent* entry;
while ((entry = readdir(dir)) != NULL) {
FileItem* child;
std::string fn = entry->d_name;
std::string fullfn = base::join_path(m_filename, fn);
if (fn == "." || fn == "..")
continue;
child = get_fileitem_by_path(fullfn, false);
if (!child) {
child = new FileItem(this);
bool is_folder;
struct stat fileStat;
stat(fullfn.c_str(), &fileStat);
if ((fileStat.st_mode & S_IFMT) == S_IFLNK) {
is_folder = base::is_directory(fullfn);
}
else {
is_folder = ((fileStat.st_mode & S_IFMT) == S_IFDIR);
}
child->m_filename = fullfn;
child->m_displayname = fn;
child->m_is_folder = is_folder;
put_fileitem(child);
}
else {
ASSERT(child->m_parent == this);
}
insertChildSorted(child);
}
closedir(dir);
}
}
#endif
// check old file-items (maybe removed directories or file-items)
for (it=m_children.begin();
it!=m_children.end(); ) {
child = static_cast<FileItem*>(*it);
ASSERT(child);
if (child && child->m_removed) {
it = m_children.erase(it);
child->m_parent = nullptr;
child->deleteItem();
}
else
++it;
}
// now this file-item is updated
m_version = current_file_system_version;
}
return m_children;
}
void FileItem::createDirectory(const std::string& dirname)
{
base::make_directory(base::join_path(m_filename, dirname));
// Invalidate the children list.
m_version = 0;
}
bool FileItem::hasExtension(const base::paths& extensions)
{
ASSERT(m_filename != NOTINITIALIZED);
return base::has_file_extension(m_filename, extensions);
}
os::Surface* FileItem::getThumbnail()
{
return m_thumbnail;
}
void FileItem::setThumbnail(os::Surface* thumbnail)
{
if (m_thumbnail)
m_thumbnail->dispose();
m_thumbnail = thumbnail;
}
FileItem::FileItem(FileItem* parent)
{
FS_TRACE("FS: Creating %p fileitem with parent %p\n", this, parent);
m_keyname = NOTINITIALIZED;
m_filename = NOTINITIALIZED;
m_displayname = NOTINITIALIZED;
m_parent = parent;
m_version = current_file_system_version;
m_removed = false;
m_is_folder = false;
m_thumbnailProgress = 0.0;
m_thumbnail = nullptr;
#ifdef _WIN32
m_pidl = NULL;
m_fullpidl = NULL;
#endif
}
FileItem::~FileItem()
{
FS_TRACE("FS: Destroying FileItem() with parent %p\n", m_parent);
if (m_thumbnail)
m_thumbnail->dispose();
#ifdef _WIN32
if (m_fullpidl && m_fullpidl != m_pidl) {
free_pidl(m_fullpidl);
m_fullpidl = NULL;
}
if (m_pidl) {
free_pidl(m_pidl);
m_pidl = NULL;
}
#endif
}
void FileItem::insertChildSorted(FileItem* child)
{
// this file-item wasn't removed from the last lookup
child->m_removed = false;
// if the fileitem is already in the list we can go back
if (std::find(m_children.begin(), m_children.end(), child) != m_children.end())
return;
for (auto it=m_children.begin(), end=m_children.end(); it!=end; ++it) {
if (*child < *static_cast<FileItem*>(*it)) {
m_children.insert(it, child);
return;
}
}
m_children.push_back(child);
}
int FileItem::compare(const FileItem& that) const
{
if (isFolder()) {
if (!that.isFolder())
return -1;
}
else if (that.isFolder())
return 1;
return base::compare_filenames(m_displayname, that.m_displayname);
}
//////////////////////////////////////////////////////////////////////
// PIDLS: Only for Win32
//////////////////////////////////////////////////////////////////////
#ifdef _WIN32
static bool calc_is_folder(std::string filename, SFGAOF attrib)
{
return ((attrib & SFGAO_FOLDER) == SFGAO_FOLDER)
&& (base::get_file_extension(filename) != "zip")
&& ((!filename.empty() && (*filename.begin()) != ':') || (filename == MYPC_CSLID));
}
// Updates the names of the file-item through its PIDL
static void update_by_pidl(FileItem* fileitem, SFGAOF attrib)
{
STRRET strret;
WCHAR pszName[MAX_PATH];
IShellFolder* pFolder = NULL;
HRESULT hr;
if (fileitem == rootitem)
pFolder = shl_idesktop;
else {
ASSERT(fileitem->m_parent);
hr = shl_idesktop->BindToObject(fileitem->m_parent->m_fullpidl,
NULL, IID_IShellFolder, (LPVOID *)&pFolder);
if (hr != S_OK)
pFolder = NULL;
}
// Get the file name
if (pFolder &&
pFolder->GetDisplayNameOf(fileitem->m_pidl,
SHGDN_NORMAL | SHGDN_FORPARSING,
&strret) == S_OK) {
StrRetToBuf(&strret, fileitem->m_pidl, pszName, MAX_PATH);
fileitem->m_filename = base::to_utf8(pszName);
}
else if (shl_idesktop->GetDisplayNameOf(fileitem->m_fullpidl,
SHGDN_NORMAL | SHGDN_FORPARSING,
&strret) == S_OK) {
StrRetToBuf(&strret, fileitem->m_fullpidl, pszName, MAX_PATH);
fileitem->m_filename = base::to_utf8(pszName);
}
else
fileitem->m_filename = "ERR";
// Is it a folder?
fileitem->m_is_folder = calc_is_folder(fileitem->m_filename, attrib);
// Get the name to display
if (fileitem->isFolder() &&
pFolder &&
pFolder->GetDisplayNameOf(fileitem->m_pidl,
SHGDN_INFOLDER,
&strret) == S_OK) {
StrRetToBuf(&strret, fileitem->m_pidl, pszName, MAX_PATH);
fileitem->m_displayname = base::to_utf8(pszName);
}
else if (fileitem->isFolder() &&
shl_idesktop->GetDisplayNameOf(fileitem->m_fullpidl,
SHGDN_INFOLDER,
&strret) == S_OK) {
StrRetToBuf(&strret, fileitem->m_fullpidl, pszName, MAX_PATH);
fileitem->m_displayname = base::to_utf8(pszName);
}
else {
fileitem->m_displayname = base::get_file_name(fileitem->m_filename);
}
if (pFolder && pFolder != shl_idesktop) {
pFolder->Release();
}
}
static LPITEMIDLIST concat_pidl(LPITEMIDLIST pidlHead, LPITEMIDLIST pidlTail)
{
LPITEMIDLIST pidlNew;
UINT cb1, cb2;
ASSERT(pidlHead);
ASSERT(pidlTail);
cb1 = get_pidl_size(pidlHead) - sizeof(pidlHead->mkid.cb);
cb2 = get_pidl_size(pidlTail);
pidlNew = (LPITEMIDLIST)shl_imalloc->Alloc(cb1 + cb2);
if (pidlNew) {
CopyMemory(pidlNew, pidlHead, cb1);
CopyMemory(((LPSTR)pidlNew) + cb1, pidlTail, cb2);
}
return pidlNew;
}
static UINT get_pidl_size(LPITEMIDLIST pidl)
{
UINT cbTotal = 0;
if (pidl) {
cbTotal += sizeof(pidl->mkid.cb); /* null terminator */
while (pidl) {
cbTotal += pidl->mkid.cb;
pidl = get_next_pidl(pidl);
}
}
return cbTotal;
}
static LPITEMIDLIST get_next_pidl(LPITEMIDLIST pidl)
{
if (pidl != NULL && pidl->mkid.cb > 0) {
pidl = (LPITEMIDLIST)(((LPBYTE)(pidl)) + pidl->mkid.cb);
if (pidl->mkid.cb > 0)
return pidl;
}
return NULL;
}
static LPITEMIDLIST get_last_pidl(LPITEMIDLIST pidl)
{
LPITEMIDLIST pidlLast = pidl;
LPITEMIDLIST pidlNew = NULL;
while (pidl) {
pidlLast = pidl;
pidl = get_next_pidl(pidl);
}
if (pidlLast) {
ULONG sz = get_pidl_size(pidlLast);
pidlNew = (LPITEMIDLIST)shl_imalloc->Alloc(sz);
CopyMemory(pidlNew, pidlLast, sz);
}
return pidlNew;
}
static LPITEMIDLIST clone_pidl(LPITEMIDLIST pidl)
{
ULONG sz = get_pidl_size(pidl);
LPITEMIDLIST pidlNew = (LPITEMIDLIST)shl_imalloc->Alloc(sz);
CopyMemory(pidlNew, pidl, sz);
return pidlNew;
}
static LPITEMIDLIST remove_last_pidl(LPITEMIDLIST pidl)
{
LPITEMIDLIST pidlFirst = pidl;
LPITEMIDLIST pidlLast = pidl;
while (pidl) {
pidlLast = pidl;
pidl = get_next_pidl(pidl);
}
if (pidlLast)
pidlLast->mkid.cb = 0;
return pidlFirst;
}
static void free_pidl(LPITEMIDLIST pidl)
{
shl_imalloc->Free(pidl);
}
static std::string get_key_for_pidl(LPITEMIDLIST pidl)
{
#if 0
char *key = base_malloc(get_pidl_size(pidl)+1);
UINT c, i = 0;
while (pidl) {
for (c=0; c<pidl->mkid.cb; ++c) {
if (pidl->mkid.abID[c])
key[i++] = pidl->mkid.abID[c];
else
key[i++] = 1;
}
pidl = get_next_pidl(pidl);
}
key[i] = 0;
return key;
#else
STRRET strret;
WCHAR pszName[MAX_PATH];
WCHAR key[4096] = { 0 };
int len;
// Go pidl by pidl from the fullpidl to the root (desktop)
//LOG("FS: ***\n");
pidl = clone_pidl(pidl);
while (pidl->mkid.cb > 0) {
if (shl_idesktop->GetDisplayNameOf(pidl,
SHGDN_INFOLDER | SHGDN_FORPARSING,
&strret) == S_OK) {
if (StrRetToBuf(&strret, pidl, pszName, MAX_PATH) != S_OK)
pszName[0] = 0;
//LOG("FS: + %s\n", pszName);
len = wcslen(pszName);
if (len > 0) {
if (*key) {
if (pszName[len-1] != L'\\') {
memmove(key+len+1, key, sizeof(WCHAR)*(wcslen(key)+1));
key[len] = L'\\';
}
else
memmove(key+len, key, sizeof(WCHAR)*(wcslen(key)+1));
}
else
key[len] = 0;
memcpy(key, pszName, sizeof(WCHAR)*len);
}
}
remove_last_pidl(pidl);
}
free_pidl(pidl);
//LOG("FS: =%s\n***\n", key);
return base::to_utf8(key);
#endif
}
static FileItem* get_fileitem_by_fullpidl(LPITEMIDLIST fullpidl, bool create_if_not)
{
auto key = get_key_for_pidl(fullpidl);
auto it = fileitems_map->find(key);
if (it != fileitems_map->end()) {
FileItem* item = it->second;
if (item->isExistent())
return item;
else {
item->deleteItem();
return nullptr;
}
}
if (!create_if_not)
return nullptr;
// Check if the pidl exists
SFGAOF attrib = SFGAO_FOLDER | SFGAO_VALIDATE;
HRESULT hr = shl_idesktop->GetAttributesOf(1, (LPCITEMIDLIST*)&fullpidl, &attrib);
if (hr != S_OK)
return nullptr;
// new file-item
FileItem* fileitem = new FileItem(nullptr);
fileitem->m_fullpidl = clone_pidl(fullpidl);
{
LPITEMIDLIST parent_fullpidl = clone_pidl(fileitem->m_fullpidl);
remove_last_pidl(parent_fullpidl);
fileitem->m_pidl = get_last_pidl(fileitem->m_fullpidl);
fileitem->m_parent = get_fileitem_by_fullpidl(parent_fullpidl, true);
free_pidl(parent_fullpidl);
}
update_by_pidl(fileitem, attrib);
put_fileitem(fileitem);
//LOG("FS: fileitem %p created %s with parent %p\n", fileitem, fileitem->keyname.c_str(), fileitem->parent);
return fileitem;
}
/**
* Inserts the @a fileitem in the hash map of items.
*/
static void put_fileitem(FileItem* fileitem)
{
ASSERT(fileitem->m_filename != NOTINITIALIZED);
ASSERT(fileitem->m_keyname == NOTINITIALIZED);
fileitem->m_keyname = get_key_for_pidl(fileitem->m_fullpidl);
ASSERT(fileitem->m_keyname != NOTINITIALIZED);
#ifdef _DEBUG
auto it = fileitems_map->find(get_key_for_pidl(fileitem->m_fullpidl));
ASSERT(it == fileitems_map->end());
#endif
// insert this file-item in the hash-table
fileitems_map->insert(std::make_pair(fileitem->m_keyname, fileitem));
}
#else
//////////////////////////////////////////////////////////////////////
// POSIX functions
//////////////////////////////////////////////////////////////////////
static FileItem* get_fileitem_by_path(const std::string& path, bool create_if_not)
{
if (path.empty())
return rootitem;
auto key = get_key_for_filename(path);
auto it = fileitems_map->find(key);
if (it != fileitems_map->end()) {
FileItem* item = it->second;
if (item->isExistent())
return item;
else {
item->deleteItem();
return nullptr;
}
}
if (!create_if_not)
return NULL;
// get the attributes of the file
bool is_folder = false;