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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
|
/*=========================================================================
Program: ParaView
Module: vtkPVFileInformation.cxx
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkPVFileInformation.h"
#include "vtkPVConfig.h"
#include "vtkClientServerStream.h"
#include "vtkCollection.h"
#include "vtkCollectionIterator.h"
#include "vtkFileSequenceParser.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkProcessModule.h"
#include "vtkPVFileInformationHelper.h"
#include "vtkSmartPointer.h"
#if defined(_WIN32)
# define _WIN32_IE 0x0400 // special folder support
# define _WIN32_WINNT 0x0400 // shared folder support
# include <windows.h> // FindFirstFile, FindNextFile, FindClose, ...
# include <direct.h> // _getcwd
# include <shlobj.h> // SHGetFolderPath
# include <sys/stat.h> // stat
# include <string.h> // for strcasecmp
# define vtkPVServerFileListingGetCWD _getcwd
#else
# include <sys/types.h> // DIR, struct dirent, struct stat
# include <sys/stat.h> // stat
# include <dirent.h> // opendir, readdir, closedir
# include <unistd.h> // access, getcwd
# include <errno.h> // errno
# include <string.h> // strerror
# include <stdlib.h> // getenv
# define vtkPVServerFileListingGetCWD getcwd
#endif
#if defined (__APPLE__)
#include "vtkPVMacFileInformationHelper.h"
#include <vector>
#endif
#include <vtksys/SystemTools.hxx>
#include <vtksys/RegularExpression.hxx>
#include <set>
#include <string>
vtkStandardNewMacro(vtkPVFileInformation);
inline void vtkPVFileInformationAddTerminatingSlash(std::string& name)
{
if (name.size()>0)
{
char last = *(name.end()-1);
if (last != '/' && last != '\\')
{
#if defined(_WIN32)
name += "\\";
#else
name += "/";
#endif
}
}
}
#if defined(_WIN32)
static std::string WindowsNetworkRoot = "Windows Network";
// assumes native back-slashes
static bool IsUncPath(const std::string& path)
{
if(path.size() >= 2 && path[0] == '\\' && path[1] == '\\')
{
return true;
}
return false;
}
static bool IsNetworkPath(const std::string& path)
{
if(path.compare(0, WindowsNetworkRoot.size(), WindowsNetworkRoot) == 0)
{
return true;
}
return false;
}
// returns true if path is valid, false otherwise
// returns subdirs, if any
static bool getNetworkSubdirs(const std::string& name,
DWORD DisplayType,
std::vector<std::string>& subdirs)
{
HANDLE han=0;
NETRESOURCEA rc;
rc.dwScope = RESOURCE_GLOBALNET;
rc.dwType = RESOURCETYPE_ANY; // TODO, restrict this to disk at server level?
rc.dwDisplayType = DisplayType;
rc.dwUsage = RESOURCEUSAGE_CONTAINER;
if(DisplayType == RESOURCEDISPLAYTYPE_NETWORK)
{
rc.dwUsage |= RESOURCEUSAGE_RESERVED; // wonder why this is needed?
}
rc.lpLocalName = NULL;
rc.lpProvider = NULL;
rc.lpComment = NULL;
rc.lpRemoteName = const_cast<char*>(name.c_str());
DWORD ret = WNetOpenEnumA(RESOURCE_GLOBALNET,
RESOURCETYPE_ANY, 0, &rc, &han);
if(NO_ERROR == ret)
{
DWORD count = 10;
NETRESOURCE res[10];
DWORD bufsize = sizeof(NETRESOURCE) * count;
do
{
ret = WNetEnumResourceA(han, &count, res, &bufsize);
if(ret == NO_ERROR || ret == ERROR_MORE_DATA)
{
for(DWORD i=0; i<count; i++)
{
std::string subdir = res[i].lpRemoteName;
if(subdir.compare(0, name.size(), name) == 0)
{
subdir = subdir.c_str() + name.size() + 1;
}
subdirs.push_back(subdir);
}
}
} while(ret == ERROR_MORE_DATA);
}
return NO_ERROR == ret;
}
static bool getNetworkSubdirs(const std::string& path,
std::vector<std::string>& subdirs,
int& type)
{
if(!IsNetworkPath(path))
{
return false;
}
// path follows this convention
// Windows Network\Domain\Server\Share
// this doesn't return subdirectories of "Share", use normal
// win32 API for that
static const int MaxTokens = 4;
std::vector<vtksys::String> pathtokens;
pathtokens = vtksys::SystemTools::SplitString(path.c_str()+1, '\\');
static DWORD DisplayType[MaxTokens] =
{
RESOURCEDISPLAYTYPE_NETWORK,
RESOURCEDISPLAYTYPE_DOMAIN,
RESOURCEDISPLAYTYPE_SERVER,
RESOURCEDISPLAYTYPE_SHARE
};
int tokenIndex = static_cast<int>(pathtokens.size())-1;
if(tokenIndex >= MaxTokens)
return false;
std::string name = pathtokens[tokenIndex];
if(tokenIndex == 0)
{
type = vtkPVFileInformation::NETWORK_DOMAIN;
}
else if(tokenIndex == 1)
{
type = vtkPVFileInformation::NETWORK_SERVER;
}
else if(tokenIndex == 2)
{
type = vtkPVFileInformation::NETWORK_SHARE;
name = "\\\\" + name;
}
return getNetworkSubdirs(name, DisplayType[tokenIndex], subdirs);
}
static bool getUncSharesOnServer(const std::string& server,
std::vector<std::string>& ret)
{
return getNetworkSubdirs(std::string("\\\\") + server,
RESOURCEDISPLAYTYPE_SERVER, ret);
}
#endif
static int vtkPVFileInformationGetType(const char* path)
{
int type = vtkPVFileInformation::INVALID;
std::string realpath = path;
if(vtksys::SystemTools::FileExists(realpath.c_str()))
{
type = vtkPVFileInformation::SINGLE_FILE;
}
if(vtksys::SystemTools::FileIsDirectory(realpath.c_str()))
{
type = vtkPVFileInformation::DIRECTORY;
}
#if defined(_WIN32)
// doing stat on root of devices doesn't work
// is it the root of a drive?
if (realpath.size() > 0 && realpath[1] == ':' && realpath.size() == 2 ||
(realpath.size() == 3 && realpath[2] == '\\'))
{
// Path may be drive letter.
DWORD n = GetLogicalDrives();
int which = tolower(realpath[0]) - 'a';
if(n & (1 << which))
{
type = vtkPVFileInformation::DRIVE;
}
}
if(IsNetworkPath(realpath))
{
// this code doesn't give out anything with
// "Windows Network\..." unless its a directory.
// that may change
std::vector<vtksys::String> pathtokens;
pathtokens = vtksys::SystemTools::SplitString(realpath.c_str(), '\\');
if(pathtokens.size() == 1)
{
type = vtkPVFileInformation::NETWORK_ROOT;
}
else if(pathtokens.size() == 2)
{
type = vtkPVFileInformation::NETWORK_DOMAIN;
}
else if(pathtokens.size() == 3)
{
type = vtkPVFileInformation::NETWORK_SERVER;
}
else
{
type = vtkPVFileInformation::NETWORK_SHARE;
}
}
// is it the root of a shared folder?
if(IsUncPath(realpath))
{
std::vector<vtksys::String> parts =
vtksys::SystemTools::SplitString(realpath.c_str()+2, '\\', true);
if(parts.empty())
{
// global network
type = vtkPVFileInformation::DIRECTORY;
}
else
{
std::vector<std::string> shares;
bool ret = getUncSharesOnServer(parts[0], shares);
if(parts.size() == 1 && ret)
{
// server exists
type = vtkPVFileInformation::NETWORK_SERVER;
}
else if(parts.size() == 2 && ret)
{
for(unsigned int i = 0; i<shares.size(); i++)
{
if(parts[1] == shares[i])
{
// share on server exists
type = vtkPVFileInformation::DIRECTORY;
break;
}
}
}
}
}
#endif
return type;
}
#if defined(_WIN32)
static std::string vtkPVFileInformationResolveLink(const std::string& fname,
WIN32_FIND_DATA& wfd)
{
IShellLink* shellLink;
HRESULT hr;
char Link[MAX_PATH];
bool coInit = false;
std::string result;
hr = ::CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink,
(LPVOID*)&shellLink);
if(hr == CO_E_NOTINITIALIZED)
{
coInit = true;
::CoInitialize(NULL);
hr = ::CoCreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER, IID_IShellLink,
(LPVOID*)&shellLink);
}
if(SUCCEEDED(hr))
{
IPersistFile* ppf;
hr = shellLink->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
if(SUCCEEDED(hr))
{
std::wstring wfname(fname.begin(), fname.end());
hr = ppf->Load((LPOLESTR)wfname.c_str(), STGM_READ);
if(SUCCEEDED(hr))
{
if(shellLink->GetPath(Link, MAX_PATH, &wfd, SLGP_UNCPRIORITY) == NOERROR)
{
result = Link;
}
ppf->Release();
}
}
shellLink->Release();
}
if(coInit)
{
CoUninitialize();
}
return result;
}
#endif
std::string MakeAbsolutePath(const std::string& path,
const std::string& working_dir)
{
std::string ret = path;
#if defined(_WIN32)
if(!IsUncPath(path) && !IsNetworkPath(path))
#endif
{
ret = vtksys::SystemTools::CollapseFullPath(path.c_str(),
working_dir.c_str());
}
return ret;
}
//-----------------------------------------------------------------------------
class vtkPVFileInformationSet :
public std::set<vtkSmartPointer<vtkPVFileInformation> >
{
};
//-----------------------------------------------------------------------------
vtkPVFileInformation::vtkPVFileInformation()
{
this->RootOnly = 1;
this->Contents = vtkCollection::New();
this->SequenceParser = vtkFileSequenceParser::New();
this->Type = INVALID;
this->Name = NULL;
this->FullPath = NULL;
this->FastFileTypeDetection = 0;
this->Hidden = false;
}
//-----------------------------------------------------------------------------
vtkPVFileInformation::~vtkPVFileInformation()
{
this->Contents->Delete();
this->SequenceParser->Delete();
this->SetName(NULL);
this->SetFullPath(NULL);
}
bool vtkPVFileInformation::IsDirectory(int t)
{
return t == DIRECTORY || t == DIRECTORY_LINK ||
t == DRIVE || t == NETWORK_ROOT ||
t == NETWORK_DOMAIN || t == NETWORK_SERVER ||
t == NETWORK_SHARE;
}
//-----------------------------------------------------------------------------
void vtkPVFileInformation::CopyFromObject(vtkObject* object)
{
this->Initialize();
vtkPVFileInformationHelper* helper =
vtkPVFileInformationHelper::SafeDownCast(object);
if (!helper)
{
vtkErrorMacro(
"Can collect information only from a vtkPVFileInformationHelper.");
return;
}
if (helper->GetSpecialDirectories())
{
this->GetSpecialDirectories();
return;
}
this->FastFileTypeDetection = helper->GetFastFileTypeDetection();
std::string working_directory =
vtksys::SystemTools::GetCurrentWorkingDirectory().c_str();
if (helper->GetWorkingDirectory() && helper->GetWorkingDirectory()[0])
{
working_directory = helper->GetWorkingDirectory();
}
std::string path = MakeAbsolutePath(helper->GetPath(), working_directory);
this->SetName(helper->GetPath());
bool isLink = false;
#if defined(_WIN32)
std::string::size_type idx;
for(idx = path.find('/', 0);
idx != std::string::npos;
idx = path.find('/', idx))
{
path.replace(idx, 1, 1, '\\');
}
int len = static_cast<int>(path.size());
if(len > 4 && path.compare(len-4, 4, ".lnk") == 0)
{
WIN32_FIND_DATA data;
path = vtkPVFileInformationResolveLink(path, data);
isLink = true;
}
#endif
this->SetFullPath(path.c_str());
this->Type = vtkPVFileInformationGetType(this->FullPath);
if(isLink && this->Type == SINGLE_FILE)
{
this->Type = SINGLE_FILE_LINK;
}
else if(isLink && this->Type == DIRECTORY)
{
this->Type = DIRECTORY_LINK;
}
//determine if this is a hidden directory/file
this->SetHiddenFlag();
if (this->IsDirectory(this->Type) && helper->GetDirectoryListing())
{
// Since we want a directory listing, we now to platform specific listing
// with intelligent pattern matching hee-haa.
#if defined(_WIN32)
this->GetWindowsDirectoryListing();
#else
this->GetDirectoryListing();
#endif
}
}
//-----------------------------------------------------------------------------
void vtkPVFileInformation::GetSpecialDirectories()
{
// FIXME: Use vtkPVLibraryInfo (see paraview/paraview!798) once it's available
// to get such paths. Hardcoding it for now.
if (vtkProcessModule* pm = vtkProcessModule::GetProcessModule())
{
#if defined(_WIN32) || defined(__APPLE__)
std::string dataPath = pm->GetSelfDir() + "/../data";
#else
std::string appdir = pm->GetSelfDir();;
std::string dataPath = vtksys::SystemTools::GetFilenameName(appdir) == "bin" ?
/* w/o shared forwarding */ appdir + "/../share/paraview-" PARAVIEW_VERSION "/data" :
/* w/ shared forwarding */ appdir + "/../../share/paraview-" PARAVIEW_VERSION "/data";
#endif
dataPath = vtksys::SystemTools::CollapseFullPath(dataPath);
if (vtksys::SystemTools::FileIsDirectory(dataPath))
{
vtkNew<vtkPVFileInformation> info;
info->SetFullPath(dataPath.c_str());
info->SetName("Examples");
info->Type = DIRECTORY;
this->Contents->AddItem(info.Get());
}
}
#if defined (_WIN32)
// Return favorite directories ...
TCHAR szPath[MAX_PATH];
if(SUCCEEDED(SHGetSpecialFolderPath(NULL, szPath, CSIDL_PERSONAL, false)))
{
vtkSmartPointer<vtkPVFileInformation> info =
vtkSmartPointer<vtkPVFileInformation>::New();
info->SetFullPath(szPath);
info->SetName("My Documents");
info->Type = DIRECTORY;
this->Contents->AddItem(info);
}
if(SUCCEEDED(SHGetSpecialFolderPath(NULL, szPath, CSIDL_DESKTOPDIRECTORY, false)))
{
vtkSmartPointer<vtkPVFileInformation> info =
vtkSmartPointer<vtkPVFileInformation>::New();
info->SetFullPath(szPath);
info->SetName("Desktop");
info->Type = DIRECTORY;
this->Contents->AddItem(info);
}
if(SUCCEEDED(SHGetSpecialFolderPath(NULL, szPath, CSIDL_FAVORITES, false)))
{
vtkSmartPointer<vtkPVFileInformation> info =
vtkSmartPointer<vtkPVFileInformation>::New();
info->SetFullPath(szPath);
info->SetName("Favorites");
info->Type = DIRECTORY;
this->Contents->AddItem(info);
}
// Return drive letters ...
DWORD n = GetLogicalDrives();
for(int i=0; i<sizeof(DWORD)*8; i++)
{
if(n & (1<<i))
{
std::string driveLetter;
driveLetter += 'A' + i;
driveLetter += ":\\";
vtkSmartPointer<vtkPVFileInformation> info =
vtkSmartPointer<vtkPVFileInformation>::New();
info->SetFullPath(driveLetter.c_str());
info->SetName(driveLetter.c_str());
info->Type = DRIVE;
this->Contents->AddItem(info);
}
}
vtkSmartPointer<vtkPVFileInformation> info =
vtkSmartPointer<vtkPVFileInformation>::New();
info->SetFullPath(WindowsNetworkRoot.c_str());
info->SetName("Windows Network");
info->Type = NETWORK_ROOT;
this->Contents->AddItem(info);
#else // _WIN32
#if defined (__APPLE__ )
// Add special directories
vtkNew<vtkPVMacFileInformationHelper> helper;
vtkSmartPointer<vtkPVFileInformation> info;
std::string homeDirectory = helper->GetHomeDirectory();
if (homeDirectory != "")
{
// Home directory
info = vtkSmartPointer<vtkPVFileInformation>::New();
info->SetFullPath(homeDirectory.c_str());
info->SetName("Home");
info->Type = DIRECTORY;
this->Contents->AddItem(info);
// Desktop directory
std::string desktop = helper->GetDesktopDirectory();
if (desktop.size() > 0)
{
info = vtkSmartPointer<vtkPVFileInformation>::New();
info->SetFullPath(desktop.c_str());
info->SetName("Desktop");
info->Type = DIRECTORY;
this->Contents->AddItem(info);
}
// Documents directory
std::string documents = helper->GetDocumentsDirectory();
if (documents.size() > 0)
{
info = vtkSmartPointer<vtkPVFileInformation>::New();
info->SetFullPath(documents.c_str());
info->SetName("Documents");
info->Type = DIRECTORY;
this->Contents->AddItem(info);
}
// Downloads directory
std::string downloads = helper->GetDownloadsDirectory();
if (downloads.size() > 0)
{
info = vtkSmartPointer<vtkPVFileInformation>::New();
info->SetFullPath(downloads.c_str());
info->SetName("Downloads");
info->Type = DIRECTORY;
this->Contents->AddItem(info);
}
}
// Get the mounted volumes
std::vector< vtkPVMacFileInformationHelper::NamePath > volumes = helper->GetMountedVolumes();
for (size_t i = 0; i < volumes.size(); ++i)
{
std::string name = volumes[i].first;
std::string path = volumes[i].second;
info = vtkSmartPointer<vtkPVFileInformation>::New();
// Filter out "home" and "net".
if (name == "home" || name == "net")
{
continue;
}
info->SetFullPath(path.c_str());
info->SetName(name.c_str());
info->Type = DRIVE;
this->Contents->AddItem(info);
}
#else
if(const char* home = getenv("HOME"))
{
vtkSmartPointer<vtkPVFileInformation> info =
vtkSmartPointer<vtkPVFileInformation>::New();
info->SetFullPath(home);
info->SetName("Home");
info->Type = DIRECTORY;
this->Contents->AddItem(info);
}
#endif
#endif // !_WIN32
}
//-----------------------------------------------------------------------------
void vtkPVFileInformation::GetWindowsDirectoryListing()
{
#if defined(_WIN32)
vtkPVFileInformationSet info_set;
if(IsNetworkPath(this->FullPath))
{
std::vector<std::string> shares;
int type;
if(getNetworkSubdirs(this->FullPath, shares, type))
{
for(unsigned int i=0; i<shares.size(); i++)
{
vtkPVFileInformation* info = vtkPVFileInformation::New();
info->SetName(shares[i].c_str());
std::string fullpath =
vtksys::SystemTools::CollapseFullPath(shares[i].c_str(), this->FullPath);
info->SetFullPath(fullpath.c_str());
info->Type = type;
info->FastFileTypeDetection = this->FastFileTypeDetection;
info_set.insert(info);
info->Delete();
}
}
this->OrganizeCollection(info_set);
for (vtkPVFileInformationSet::iterator iter = info_set.begin();
iter != info_set.end(); ++iter)
{
this->Contents->AddItem(*iter);
}
return;
}
if(IsUncPath(this->FullPath))
{
bool didListing = false;
std::vector<vtksys::String> parts =
vtksys::SystemTools::SplitString(this->FullPath+2, '\\', true);
if(parts.size() == 1)
{
// get list of all shares on server
std::vector<std::string> shares;
if(getUncSharesOnServer(parts[0], shares))
{
for(unsigned int i=0; i<shares.size(); i++)
{
vtkPVFileInformation* info = vtkPVFileInformation::New();
info->SetName(shares[i].c_str());
std::string fullpath = "\\\\" + parts[0] + "\\" + shares[i];
info->SetFullPath(fullpath.c_str());
info->Type = NETWORK_SHARE;
info->FastFileTypeDetection = this->FastFileTypeDetection;
info_set.insert(info);
info->Delete();
}
}
didListing = true;
}
if(didListing)
{
this->OrganizeCollection(info_set);
for (vtkPVFileInformationSet::iterator iter = info_set.begin();
iter != info_set.end(); ++iter)
{
this->Contents->AddItem(*iter);
}
return;
}
// fall through for normal file listing that works after shares are
// known
}
// Search for all files in the given directory.
std::string prefix = this->FullPath;
vtkPVFileInformationAddTerminatingSlash(prefix);
std::string pattern = prefix;
pattern += "*";
WIN32_FIND_DATA data;
HANDLE handle = FindFirstFile(pattern.c_str(), &data);
if(handle == INVALID_HANDLE_VALUE)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf, 0, NULL);
vtkErrorMacro("Error calling FindFirstFile : "
<< (char*)lpMsgBuf << "\nDirectory: " << prefix.c_str());
LocalFree(lpMsgBuf);
return;
}
do
{
std::string filename = data.cFileName;
if(filename == "." || filename == "..")
continue;
std::string fullpath = prefix + filename;
size_t len = filename.size();
bool isLink = false;
if(len > 4 && strncmp(filename.c_str()+len-4, ".lnk", 4) == 0)
{
fullpath = vtkPVFileInformationResolveLink(fullpath, data);
filename.resize(filename.size() - 4);
isLink = true;
}
DWORD isdir = data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
DWORD isfile = (data.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) ||
(!(data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) &&
!(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
FileTypes type = isdir ? DIRECTORY : SINGLE_FILE;
if(isLink)
{
type = type == DIRECTORY ? DIRECTORY_LINK : SINGLE_FILE_LINK;
}
if(isdir || isfile)
{
vtkPVFileInformation* infoD = vtkPVFileInformation::New();
infoD->SetName(filename.c_str());
infoD->SetFullPath(fullpath.c_str());
infoD->Type = type;
infoD->FastFileTypeDetection = this->FastFileTypeDetection;
infoD->SetHiddenFlag(); //needs full path set first
info_set.insert(infoD);
infoD->Delete();
}
// Find the next file.
} while(FindNextFile(handle, &data) != 0);
if(GetLastError() != ERROR_NO_MORE_FILES)
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf, 0, NULL);
vtkErrorMacro("Error calling FindNextFile : "
<< (char*)lpMsgBuf << "\nDirectory: " << prefix.c_str());
LocalFree(lpMsgBuf);
}
if(!FindClose(handle))
{
vtkErrorMacro("Error calling FindClose.");
}
this->OrganizeCollection(info_set);
for (vtkPVFileInformationSet::iterator iter = info_set.begin();
iter != info_set.end(); ++iter)
{
this->Contents->AddItem(*iter);
}
#else
vtkErrorMacro("GetWindowsDirectoryListing cannot be called on non-Windows systems.");
#endif
}
/* There is a problem with the Portland compiler, large file
support and glibc/Linux system headers:
http://www.pgroup.com/userforum/viewtopic.php?
p=1992&sid=f16167f51964f1a68fe5041b8eb213b6
*/
#if defined(__PGI) && defined(__USE_FILE_OFFSET64)
# define dirent dirent64
#endif
//-----------------------------------------------------------------------------
void vtkPVFileInformation::GetDirectoryListing()
{
#if defined(_WIN32)
vtkErrorMacro("GetDirectoryListing() cannot be called on Windows systems.");
return;
#else
vtkPVFileInformationSet info_set;
std::string prefix = this->FullPath;
vtkPVFileInformationAddTerminatingSlash(prefix);
// Open the directory and make sure it exists.
DIR* dir = opendir(this->FullPath);
if(!dir)
{
// Could add check of errno here.
return;
}
// Loop through the directory listing.
while(const dirent* d = readdir(dir))
{
// Skip the special directory entries.
if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
{
continue;
}
vtkPVFileInformation* info = vtkPVFileInformation::New();
info->SetName(d->d_name);
info->SetFullPath((prefix + d->d_name).c_str());
info->Type = INVALID;
info->SetHiddenFlag();
// fix to bug #09452 such that directories with trailing names can be
// shown in the file dialog
#if defined (__SVR4) && defined (__sun)
struct stat status;
int res = stat( info->FullPath, &status );
if ( res != -1 && status.st_mode & S_IFDIR )
{
info->Type = DIRECTORY;
}
#else
if ( d->d_type & DT_DIR )
{
info->Type = DIRECTORY;
}
#endif
info->FastFileTypeDetection = this->FastFileTypeDetection;
info_set.insert(info);
info->Delete();
}
closedir(dir);
this->OrganizeCollection(info_set);
// Now we detect the file types for items.
// We dissolve any groups that contain non-file items.
for (vtkPVFileInformationSet::iterator iter = info_set.begin();
iter != info_set.end(); ++iter)
{
vtkPVFileInformation* obj = (*iter);
if (obj->DetectType())
{
this->Contents->AddItem(obj);
}
else
{
// Add children to contents.
for (int cc=0; cc < obj->Contents->GetNumberOfItems(); cc++)
{
vtkPVFileInformation* child = vtkPVFileInformation::SafeDownCast(
obj->Contents->GetItemAsObject(cc));
if (child->DetectType())
{
this->Contents->AddItem(child);
}
}
}
}
#endif
}
//-----------------------------------------------------------------------------
void vtkPVFileInformation::SetHiddenFlag( )
{
#if defined(_WIN32)
if ( !this->FullPath )
{
this->Hidden = false;
return;
}
LPCSTR fp = this->FullPath;
DWORD flags= GetFileAttributes(fp);
this->Hidden =( flags & FILE_ATTRIBUTE_HIDDEN) ? true: false;
#else
if ( !this->Name )
{
this->Hidden = false;
return;
}
this->Hidden =( this->Name[0] == '.' )? true:false;
#endif
}
//-----------------------------------------------------------------------------
bool vtkPVFileInformation::DetectType()
{
if (this->Type == FILE_GROUP)
{
vtkSmartPointer<vtkCollectionIterator> iter;
iter.TakeReference(this->Contents->NewIterator());
for (iter->InitTraversal(); !iter->IsDoneWithTraversal(); iter->GoToNextItem())
{
vtkPVFileInformation* child = vtkPVFileInformation::SafeDownCast(
iter->GetCurrentObject());
if (!child->DetectType() || child->Type != SINGLE_FILE)
{
return false;
}
if (this->FastFileTypeDetection)
{
// Assume all children are same as this child.
for (; !iter->IsDoneWithTraversal(); iter->GoToNextItem())
{
vtkPVFileInformation* child2 = vtkPVFileInformation::SafeDownCast(
iter->GetCurrentObject());
child2->Type = child->Type;
}
break;
}
}
return true;
}
else if (this->Type == INVALID)
{
if (vtksys::SystemTools::FileExists(this->FullPath))
{
this-> Type =
(vtksys::SystemTools::FileIsDirectory(this->FullPath))?
DIRECTORY : SINGLE_FILE;
return true;
}
return false;
}
return true;
}
struct vtkPVFileInformation::vtkInfo
{
typedef std::map<int, vtkSmartPointer<vtkPVFileInformation> > ChildrenType;
vtkSmartPointer<vtkPVFileInformation> Group;
ChildrenType Children;
};
//-----------------------------------------------------------------------------
void vtkPVFileInformation::OrganizeCollection(vtkPVFileInformationSet& info_set)
{
typedef std::map<std::string, vtkInfo> MapOfStringToInfo;
MapOfStringToInfo fileGroups;
std::string prefix = this->FullPath;
vtkPVFileInformationAddTerminatingSlash(prefix);
for (vtkPVFileInformationSet::iterator iter = info_set.begin();
iter != info_set.end(); )
{
vtkPVFileInformation* obj = *iter;
if (obj->Type != FILE_GROUP && !IsDirectory(obj->Type))
{
bool match = false;
match = this->SequenceParser->ParseFileSequence(obj->GetName());
if (match)
{
std::string groupName = this->SequenceParser->GetSequenceName();
int groupIndex = this->SequenceParser->GetSequenceIndex();
MapOfStringToInfo::iterator iter2 = fileGroups.find(groupName);
vtkPVFileInformation* group = 0;
if (iter2 == fileGroups.end())
{
group = vtkPVFileInformation::New();
group->SetName(groupName.c_str());
group->SetFullPath((prefix + groupName).c_str());
group->Type = FILE_GROUP;
//the group inherits the hidden flag of the first item in the group
group->Hidden = obj->Hidden;
group->FastFileTypeDetection = this->FastFileTypeDetection;
//fileGroups[groupName] = group;
vtkInfo info;
info.Group = group;
fileGroups[groupName] = info;
group->Delete();
iter2 = fileGroups.find(groupName);
}
iter2->second.Children[groupIndex] = obj;
vtkPVFileInformationSet::iterator prev_iter = iter++;
info_set.erase(prev_iter);
continue;
}
}
++iter;
}
// Now scan through all created groups and dissolve trivial groups
// i.e. groups with single entries. Add all other groups to the
// results.
for (MapOfStringToInfo::iterator iter2 = fileGroups.begin();
iter2 != fileGroups.end(); ++iter2)
{
vtkInfo& info = iter2->second;
vtkPVFileInformation* group = info.Group;
if (info.Children.size() > 1)
{
vtkInfo::ChildrenType::iterator childIter = info.Children.begin();
for (; childIter != info.Children.end();++childIter)
{
group->Contents->AddItem(childIter->second.GetPointer());
}
// Build group children.
info_set.insert(group);
}
else
{
vtkInfo::ChildrenType::iterator childIter = info.Children.begin();
for (; childIter != info.Children.end();++childIter)
{
info_set.insert(
childIter->second.GetPointer());
}
}
}
}
//-----------------------------------------------------------------------------
void vtkPVFileInformation::CopyToStream(vtkClientServerStream* stream)
{
*stream << vtkClientServerStream::Reply
<< this->Name
<< this->FullPath
<< this->Type
<< this->Hidden
<< this->Contents->GetNumberOfItems();
vtkSmartPointer<vtkCollectionIterator> iter;
iter.TakeReference(this->Contents->NewIterator());
for (iter->InitTraversal(); !iter->IsDoneWithTraversal(); iter->GoToNextItem())
{
vtkClientServerStream childStream;
vtkPVFileInformation* child = vtkPVFileInformation::SafeDownCast(
iter->GetCurrentObject());
child->CopyToStream(&childStream);
*stream << childStream;
}
*stream << vtkClientServerStream::End;
}
//-----------------------------------------------------------------------------
void vtkPVFileInformation::CopyFromStream(const vtkClientServerStream* css)
{
this->Initialize();
const char* temp = 0;
if (!css->GetArgument(0, 0, &temp))
{
vtkErrorMacro("Error parsing Name.");
return;
}
this->SetName(temp);
if (!css->GetArgument(0, 1, &temp))
{
vtkErrorMacro("Error parsing FullPath.");
return;
}
this->SetFullPath(temp);
if (!css->GetArgument(0, 2, &this->Type))
{
vtkErrorMacro("Error parsing Type.");
return;
}
if (!css->GetArgument(0, 3, &this->Hidden))
{
vtkErrorMacro("Error parsing Hidden.");
return;
}
int num_of_children =0;
if (!css->GetArgument(0, 4, &num_of_children))
{
vtkErrorMacro("Error parsing Number of children.");
return;
}
for (int cc=0; cc < num_of_children; cc++)
{
vtkPVFileInformation* child = vtkPVFileInformation::New();
vtkClientServerStream childStream;
if (!css->GetArgument(0, 5+cc, &childStream))
{
vtkErrorMacro("Error parsing child #" << cc);
return;
}
child->CopyFromStream(&childStream);
this->Contents->AddItem(child);
child->Delete();
}
}
//-----------------------------------------------------------------------------
void vtkPVFileInformation::Initialize()
{
this->SetName(0);
this->SetFullPath(0);
this->Type = INVALID;
this->Hidden = false;
this->Contents->RemoveAllItems();
}
//-----------------------------------------------------------------------------
void vtkPVFileInformation::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Name: " << (this->Name? this->Name: "(null)") << endl;
os << indent << "FullPath: "
<< (this->FullPath? this->FullPath : "(null)") << endl;
os << indent << "Type: " ;
switch (this->Type)
{
case INVALID:
os << "INVALID" << endl;
break;
case SINGLE_FILE:
os << "SINGLE_FILE" << endl;
break;
case DIRECTORY:
os << "DIRECTORY" << endl;
break;
case FILE_GROUP:
os << "FILE_GROUP" << endl;
}
os << indent << "Hidden: "<< this->Hidden << endl;
os << indent << "FastFileTypeDetection: " << this->FastFileTypeDetection << endl;
for (int cc=0; cc < this->Contents->GetNumberOfItems(); cc++)
{
os << endl;
this->Contents->GetItemAsObject(cc)->PrintSelf(os, indent.GetNextIndent());
}
}
|