RemoteFiles.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. //---------------------------------------------------------------------------
  2. #ifndef RemoteFilesH
  3. #define RemoteFilesH
  4. //---------------------------------------------------------------------------
  5. #include <vector>
  6. #include <map>
  7. //---------------------------------------------------------------------------
  8. enum TModificationFmt { mfNone, mfMDHM, mfMDY, mfFull };
  9. //---------------------------------------------------------------------------
  10. #define SYMLINKSTR L" -> "
  11. #define PARENTDIRECTORY L".."
  12. #define THISDIRECTORY L"."
  13. #define ROOTDIRECTORY L"/"
  14. #define FILETYPE_SYMLINK L'L'
  15. #define FILETYPE_DIRECTORY L'D'
  16. #define PARTIAL_EXT L".filepart"
  17. //---------------------------------------------------------------------------
  18. class TTerminal;
  19. class TRights;
  20. class TRemoteFileList;
  21. class THierarchicalStorage;
  22. //---------------------------------------------------------------------------
  23. class TRemoteToken
  24. {
  25. public:
  26. __fastcall TRemoteToken();
  27. explicit __fastcall TRemoteToken(const UnicodeString & Name);
  28. void __fastcall Clear();
  29. bool __fastcall operator ==(const TRemoteToken & rht) const;
  30. bool __fastcall operator !=(const TRemoteToken & rht) const;
  31. TRemoteToken & __fastcall operator =(const TRemoteToken & rht);
  32. int __fastcall Compare(const TRemoteToken & rht) const;
  33. __property UnicodeString Name = { read = FName, write = FName };
  34. __property bool NameValid = { read = GetNameValid };
  35. __property unsigned int ID = { read = FID, write = SetID };
  36. __property bool IDValid = { read = FIDValid };
  37. __property bool IsSet = { read = GetIsSet };
  38. __property UnicodeString LogText = { read = GetLogText };
  39. __property UnicodeString DisplayText = { read = GetDisplayText };
  40. private:
  41. UnicodeString FName;
  42. unsigned int FID;
  43. bool FIDValid;
  44. void __fastcall SetID(unsigned int value);
  45. bool __fastcall GetNameValid() const;
  46. bool __fastcall GetIsSet() const;
  47. UnicodeString __fastcall GetDisplayText() const;
  48. UnicodeString __fastcall GetLogText() const;
  49. };
  50. //---------------------------------------------------------------------------
  51. class TRemoteTokenList
  52. {
  53. public:
  54. TRemoteTokenList * __fastcall Duplicate() const;
  55. void __fastcall Clear();
  56. void __fastcall Add(const TRemoteToken & Token);
  57. void __fastcall AddUnique(const TRemoteToken & Token);
  58. bool __fastcall Exists(const UnicodeString & Name) const;
  59. const TRemoteToken * Find(unsigned int ID) const;
  60. const TRemoteToken * Find(const UnicodeString & Name) const;
  61. void __fastcall Log(TTerminal * Terminal, const wchar_t * Title);
  62. int __fastcall Count() const;
  63. const TRemoteToken * __fastcall Token(int Index) const;
  64. private:
  65. typedef std::vector<TRemoteToken> TTokens;
  66. typedef std::map<UnicodeString, size_t> TNameMap;
  67. typedef std::map<unsigned int, size_t> TIDMap;
  68. TTokens FTokens;
  69. TNameMap FNameMap;
  70. TIDMap FIDMap;
  71. };
  72. //---------------------------------------------------------------------------
  73. class TRemoteFile : public TPersistent
  74. {
  75. private:
  76. TRemoteFileList * FDirectory;
  77. TRemoteToken FOwner;
  78. TModificationFmt FModificationFmt;
  79. __int64 FSize;
  80. UnicodeString FFileName;
  81. Integer FINodeBlocks;
  82. TDateTime FModification;
  83. TDateTime FLastAccess;
  84. TRemoteToken FGroup;
  85. Integer FIconIndex;
  86. Boolean FIsSymLink;
  87. TRemoteFile * FLinkedFile;
  88. TRemoteFile * FLinkedByFile;
  89. UnicodeString FLinkTo;
  90. TRights *FRights;
  91. TTerminal *FTerminal;
  92. wchar_t FType;
  93. bool FSelected;
  94. bool FCyclicLink;
  95. UnicodeString FFullFileName;
  96. int FIsHidden;
  97. UnicodeString FTypeName;
  98. int __fastcall GetAttr();
  99. bool __fastcall GetBrokenLink();
  100. bool __fastcall GetIsDirectory() const;
  101. TRemoteFile * __fastcall GetLinkedFile();
  102. void __fastcall SetLinkedFile(TRemoteFile * value);
  103. UnicodeString __fastcall GetModificationStr();
  104. void __fastcall SetModification(const TDateTime & value);
  105. void __fastcall SetListingStr(UnicodeString value);
  106. UnicodeString __fastcall GetListingStr();
  107. UnicodeString __fastcall GetRightsStr();
  108. wchar_t __fastcall GetType() const;
  109. void __fastcall SetType(wchar_t AType);
  110. void __fastcall SetTerminal(TTerminal * value);
  111. void __fastcall SetRights(TRights * value);
  112. UnicodeString __fastcall GetFullFileName() const;
  113. bool __fastcall GetHaveFullFileName() const;
  114. int __fastcall GetIconIndex() const;
  115. UnicodeString __fastcall GetTypeName();
  116. bool __fastcall GetIsHidden();
  117. void __fastcall SetIsHidden(bool value);
  118. bool __fastcall GetIsParentDirectory() const;
  119. bool __fastcall GetIsThisDirectory() const;
  120. bool __fastcall GetIsInaccesibleDirectory() const;
  121. UnicodeString __fastcall GetExtension();
  122. UnicodeString __fastcall GetUserModificationStr();
  123. void __fastcall LoadTypeInfo();
  124. protected:
  125. void __fastcall FindLinkedFile();
  126. public:
  127. __fastcall TRemoteFile(TRemoteFile * ALinkedByFile = NULL);
  128. virtual __fastcall ~TRemoteFile();
  129. TRemoteFile * __fastcall Duplicate(bool Standalone = true) const;
  130. void __fastcall ShiftTime(const TDateTime & Difference);
  131. void __fastcall Complete();
  132. __property int Attr = { read = GetAttr };
  133. __property bool BrokenLink = { read = GetBrokenLink };
  134. __property TRemoteFileList * Directory = { read = FDirectory, write = FDirectory };
  135. __property UnicodeString RightsStr = { read = GetRightsStr };
  136. __property __int64 Size = { read = FSize, write = FSize };
  137. __property TRemoteToken Owner = { read = FOwner, write = FOwner };
  138. __property TRemoteToken Group = { read = FGroup, write = FGroup };
  139. __property UnicodeString FileName = { read = FFileName, write = FFileName };
  140. __property int INodeBlocks = { read = FINodeBlocks };
  141. __property TDateTime Modification = { read = FModification, write = SetModification };
  142. __property UnicodeString ModificationStr = { read = GetModificationStr };
  143. __property UnicodeString UserModificationStr = { read = GetUserModificationStr };
  144. __property TModificationFmt ModificationFmt = { read = FModificationFmt, write = FModificationFmt };
  145. __property TDateTime LastAccess = { read = FLastAccess, write = FLastAccess };
  146. __property bool IsSymLink = { read = FIsSymLink };
  147. __property bool IsDirectory = { read = GetIsDirectory };
  148. __property TRemoteFile * LinkedFile = { read = GetLinkedFile, write = SetLinkedFile };
  149. __property UnicodeString LinkTo = { read = FLinkTo, write = FLinkTo };
  150. __property UnicodeString ListingStr = { read = GetListingStr, write = SetListingStr };
  151. __property TRights * Rights = { read = FRights, write = SetRights };
  152. __property TTerminal * Terminal = { read = FTerminal, write = SetTerminal };
  153. __property wchar_t Type = { read = GetType, write = SetType };
  154. __property bool Selected = { read=FSelected, write=FSelected };
  155. __property UnicodeString FullFileName = { read = GetFullFileName, write = FFullFileName };
  156. __property bool HaveFullFileName = { read = GetHaveFullFileName };
  157. __property int IconIndex = { read = GetIconIndex };
  158. __property UnicodeString TypeName = { read = GetTypeName };
  159. __property bool IsHidden = { read = GetIsHidden, write = SetIsHidden };
  160. __property bool IsParentDirectory = { read = GetIsParentDirectory };
  161. __property bool IsThisDirectory = { read = GetIsThisDirectory };
  162. __property bool IsInaccesibleDirectory = { read=GetIsInaccesibleDirectory };
  163. __property UnicodeString Extension = { read=GetExtension };
  164. };
  165. //---------------------------------------------------------------------------
  166. class TRemoteDirectoryFile : public TRemoteFile
  167. {
  168. public:
  169. __fastcall TRemoteDirectoryFile();
  170. };
  171. //---------------------------------------------------------------------------
  172. class TRemoteParentDirectory : public TRemoteDirectoryFile
  173. {
  174. public:
  175. __fastcall TRemoteParentDirectory(TTerminal * Terminal);
  176. };
  177. //---------------------------------------------------------------------------
  178. class TRemoteFileList : public TObjectList
  179. {
  180. friend class TSCPFileSystem;
  181. friend class TSFTPFileSystem;
  182. friend class TFTPFileSystem;
  183. friend class TWebDAVFileSystem;
  184. protected:
  185. UnicodeString FDirectory;
  186. TDateTime FTimestamp;
  187. TRemoteFile * __fastcall GetFiles(Integer Index);
  188. virtual void __fastcall SetDirectory(UnicodeString value);
  189. UnicodeString __fastcall GetFullDirectory();
  190. Boolean __fastcall GetIsRoot();
  191. TRemoteFile * __fastcall GetParentDirectory();
  192. UnicodeString __fastcall GetParentPath();
  193. __int64 __fastcall GetTotalSize();
  194. public:
  195. __fastcall TRemoteFileList();
  196. virtual void __fastcall Reset();
  197. TRemoteFile * __fastcall FindFile(const UnicodeString &FileName);
  198. virtual void __fastcall DuplicateTo(TRemoteFileList * Copy);
  199. virtual void __fastcall AddFile(TRemoteFile * File);
  200. __property UnicodeString Directory = { read = FDirectory, write = SetDirectory };
  201. __property TRemoteFile * Files[Integer Index] = { read = GetFiles };
  202. __property UnicodeString FullDirectory = { read=GetFullDirectory };
  203. __property Boolean IsRoot = { read = GetIsRoot };
  204. __property UnicodeString ParentPath = { read = GetParentPath };
  205. __property __int64 TotalSize = { read = GetTotalSize };
  206. __property TDateTime Timestamp = { read = FTimestamp };
  207. };
  208. //---------------------------------------------------------------------------
  209. class TRemoteDirectory : public TRemoteFileList
  210. {
  211. friend class TSCPFileSystem;
  212. friend class TSFTPFileSystem;
  213. private:
  214. Boolean FIncludeParentDirectory;
  215. Boolean FIncludeThisDirectory;
  216. TTerminal * FTerminal;
  217. TStrings * FSelectedFiles;
  218. TRemoteFile * FParentDirectory;
  219. TRemoteFile * FThisDirectory;
  220. virtual void __fastcall SetDirectory(UnicodeString value);
  221. TStrings * __fastcall GetSelectedFiles();
  222. Boolean __fastcall GetLoaded();
  223. void __fastcall SetIncludeParentDirectory(Boolean value);
  224. void __fastcall SetIncludeThisDirectory(Boolean value);
  225. void __fastcall ReleaseRelativeDirectories();
  226. public:
  227. __fastcall TRemoteDirectory(TTerminal * aTerminal, TRemoteDirectory * Template = NULL);
  228. virtual __fastcall ~TRemoteDirectory();
  229. virtual void __fastcall AddFile(TRemoteFile * File);
  230. virtual void __fastcall DuplicateTo(TRemoteFileList * Copy);
  231. virtual void __fastcall Reset();
  232. __property TTerminal * Terminal = { read = FTerminal, write = FTerminal };
  233. __property TStrings * SelectedFiles = { read=GetSelectedFiles };
  234. __property Boolean IncludeParentDirectory = { read = FIncludeParentDirectory, write = SetIncludeParentDirectory };
  235. __property Boolean IncludeThisDirectory = { read = FIncludeThisDirectory, write = SetIncludeThisDirectory };
  236. __property Boolean Loaded = { read = GetLoaded };
  237. __property TRemoteFile * ParentDirectory = { read = FParentDirectory };
  238. __property TRemoteFile * ThisDirectory = { read = FThisDirectory };
  239. };
  240. //---------------------------------------------------------------------------
  241. class TRemoteDirectoryCache : private TStringList
  242. {
  243. public:
  244. __fastcall TRemoteDirectoryCache();
  245. virtual __fastcall ~TRemoteDirectoryCache();
  246. bool __fastcall HasFileList(const UnicodeString Directory);
  247. bool __fastcall HasNewerFileList(const UnicodeString Directory, TDateTime Timestamp);
  248. bool __fastcall GetFileList(const UnicodeString Directory,
  249. TRemoteFileList * FileList);
  250. void __fastcall AddFileList(TRemoteFileList * FileList);
  251. void __fastcall ClearFileList(UnicodeString Directory, bool SubDirs);
  252. void __fastcall Clear();
  253. __property bool IsEmpty = { read = GetIsEmpty };
  254. protected:
  255. virtual void __fastcall Delete(int Index);
  256. private:
  257. TCriticalSection * FSection;
  258. bool __fastcall GetIsEmpty() const;
  259. void __fastcall DoClearFileList(UnicodeString Directory, bool SubDirs);
  260. };
  261. //---------------------------------------------------------------------------
  262. class TRemoteDirectoryChangesCache : private TStringList
  263. {
  264. public:
  265. __fastcall TRemoteDirectoryChangesCache(int MaxSize);
  266. void __fastcall AddDirectoryChange(const UnicodeString SourceDir,
  267. const UnicodeString Change, const UnicodeString TargetDir);
  268. void __fastcall ClearDirectoryChange(UnicodeString SourceDir);
  269. void __fastcall ClearDirectoryChangeTarget(UnicodeString TargetDir);
  270. bool __fastcall GetDirectoryChange(const UnicodeString SourceDir,
  271. const UnicodeString Change, UnicodeString & TargetDir);
  272. void __fastcall Clear();
  273. void __fastcall Serialize(UnicodeString & Data);
  274. void __fastcall Deserialize(const UnicodeString Data);
  275. __property bool IsEmpty = { read = GetIsEmpty };
  276. private:
  277. static bool __fastcall DirectoryChangeKey(const UnicodeString SourceDir,
  278. const UnicodeString Change, UnicodeString & Key);
  279. bool __fastcall GetIsEmpty() const;
  280. void __fastcall SetValue(const UnicodeString & Name, const UnicodeString & Value);
  281. UnicodeString __fastcall GetValue(const UnicodeString & Name);
  282. int FMaxSize;
  283. };
  284. //---------------------------------------------------------------------------
  285. class TRights
  286. {
  287. public:
  288. static const int TextLen = 9;
  289. static const wchar_t UndefSymbol = L'$';
  290. static const wchar_t UnsetSymbol = L'-';
  291. static const wchar_t BasicSymbols[];
  292. static const wchar_t CombinedSymbols[];
  293. static const wchar_t ExtendedSymbols[];
  294. static const wchar_t ModeGroups[];
  295. enum TRight {
  296. rrUserIDExec, rrGroupIDExec, rrStickyBit,
  297. rrUserRead, rrUserWrite, rrUserExec,
  298. rrGroupRead, rrGroupWrite, rrGroupExec,
  299. rrOtherRead, rrOtherWrite, rrOtherExec,
  300. rrFirst = rrUserIDExec, rrLast = rrOtherExec };
  301. enum TFlag {
  302. rfSetUID = 04000, rfSetGID = 02000, rfStickyBit = 01000,
  303. rfUserRead = 00400, rfUserWrite = 00200, rfUserExec = 00100,
  304. rfGroupRead = 00040, rfGroupWrite = 00020, rfGroupExec = 00010,
  305. rfOtherRead = 00004, rfOtherWrite = 00002, rfOtherExec = 00001,
  306. rfRead = 00444, rfWrite = 00222, rfExec = 00111,
  307. rfNo = 00000, rfDefault = 00644, rfAll = 00777,
  308. rfSpecials = 07000, rfAllSpecials = 07777 };
  309. enum TUnsupportedFlag {
  310. rfDirectory = 040000 };
  311. enum TState { rsNo, rsYes, rsUndef };
  312. public:
  313. static TFlag __fastcall RightToFlag(TRight Right);
  314. __fastcall TRights();
  315. __fastcall TRights(const TRights & Source);
  316. __fastcall TRights(unsigned short Number);
  317. void __fastcall Assign(const TRights * Source);
  318. void __fastcall AddExecute();
  319. void __fastcall AllUndef();
  320. bool __fastcall operator ==(const TRights & rhr) const;
  321. bool __fastcall operator ==(unsigned short rhr) const;
  322. bool __fastcall operator !=(const TRights & rhr) const;
  323. TRights & __fastcall operator =(const TRights & rhr);
  324. TRights & __fastcall operator =(unsigned short rhr);
  325. TRights __fastcall operator ~() const;
  326. TRights __fastcall operator &(unsigned short rhr) const;
  327. TRights __fastcall operator &(const TRights & rhr) const;
  328. TRights & __fastcall operator &=(unsigned short rhr);
  329. TRights & __fastcall operator &=(const TRights & rhr);
  330. TRights __fastcall operator |(unsigned short rhr) const;
  331. TRights __fastcall operator |(const TRights & rhr) const;
  332. TRights & __fastcall operator |=(unsigned short rhr);
  333. TRights & __fastcall operator |=(const TRights & rhr);
  334. __fastcall operator unsigned short() const;
  335. __fastcall operator unsigned long() const;
  336. __property bool AllowUndef = { read = FAllowUndef, write = SetAllowUndef };
  337. __property bool IsUndef = { read = GetIsUndef };
  338. __property UnicodeString ModeStr = { read = GetModeStr };
  339. __property UnicodeString SimplestStr = { read = GetSimplestStr };
  340. __property UnicodeString Octal = { read = GetOctal, write = SetOctal };
  341. __property unsigned short Number = { read = GetNumber, write = SetNumber };
  342. __property unsigned short NumberSet = { read = FSet };
  343. __property unsigned short NumberUnset = { read = FUnset };
  344. __property unsigned long NumberDecadic = { read = GetNumberDecadic };
  345. __property bool ReadOnly = { read = GetReadOnly, write = SetReadOnly };
  346. __property bool Right[TRight Right] = { read = GetRight, write = SetRight };
  347. __property TState RightUndef[TRight Right] = { read = GetRightUndef, write = SetRightUndef };
  348. __property UnicodeString Text = { read = GetText, write = SetText };
  349. __property bool Unknown = { read = FUnknown };
  350. private:
  351. bool FAllowUndef;
  352. unsigned short FSet;
  353. unsigned short FUnset;
  354. UnicodeString FText;
  355. bool FUnknown;
  356. bool __fastcall GetIsUndef() const;
  357. UnicodeString __fastcall GetModeStr() const;
  358. UnicodeString __fastcall GetSimplestStr() const;
  359. void __fastcall SetNumber(unsigned short value);
  360. UnicodeString __fastcall GetText() const;
  361. void __fastcall SetText(const UnicodeString & value);
  362. void __fastcall SetOctal(UnicodeString value);
  363. unsigned short __fastcall GetNumber() const;
  364. unsigned short __fastcall GetNumberSet() const;
  365. unsigned short __fastcall GetNumberUnset() const;
  366. unsigned long __fastcall GetNumberDecadic() const;
  367. UnicodeString __fastcall GetOctal() const;
  368. bool __fastcall GetReadOnly();
  369. bool __fastcall GetRight(TRight Right) const;
  370. TState __fastcall GetRightUndef(TRight Right) const;
  371. void __fastcall SetAllowUndef(bool value);
  372. void __fastcall SetReadOnly(bool value);
  373. void __fastcall SetRight(TRight Right, bool value);
  374. void __fastcall SetRightUndef(TRight Right, TState value);
  375. };
  376. //---------------------------------------------------------------------------
  377. enum TValidProperty { vpRights, vpGroup, vpOwner, vpModification, vpLastAccess };
  378. typedef Set<TValidProperty, vpRights, vpLastAccess> TValidProperties;
  379. class TRemoteProperties
  380. {
  381. public:
  382. TValidProperties Valid;
  383. bool Recursive;
  384. TRights Rights;
  385. bool AddXToDirectories;
  386. TRemoteToken Group;
  387. TRemoteToken Owner;
  388. __int64 Modification; // unix time
  389. __int64 LastAccess; // unix time
  390. __fastcall TRemoteProperties();
  391. __fastcall TRemoteProperties(const TRemoteProperties & rhp);
  392. bool __fastcall operator ==(const TRemoteProperties & rhp) const;
  393. bool __fastcall operator !=(const TRemoteProperties & rhp) const;
  394. void __fastcall Default();
  395. void __fastcall Load(THierarchicalStorage * Storage);
  396. void __fastcall Save(THierarchicalStorage * Storage) const;
  397. static TRemoteProperties __fastcall CommonProperties(TStrings * FileList);
  398. static TRemoteProperties __fastcall ChangedProperties(
  399. const TRemoteProperties & OriginalProperties, TRemoteProperties NewProperties);
  400. };
  401. //---------------------------------------------------------------------------
  402. UnicodeString __fastcall UnixIncludeTrailingBackslash(const UnicodeString Path);
  403. UnicodeString __fastcall UnixExcludeTrailingBackslash(const UnicodeString Path);
  404. UnicodeString __fastcall UnixExtractFileDir(const UnicodeString Path);
  405. UnicodeString __fastcall UnixExtractFilePath(const UnicodeString Path);
  406. UnicodeString __fastcall UnixExtractFileName(const UnicodeString Path);
  407. UnicodeString __fastcall UnixExtractFileExt(const UnicodeString Path);
  408. Boolean __fastcall UnixComparePaths(const UnicodeString Path1, const UnicodeString Path2);
  409. bool __fastcall UnixIsChildPath(UnicodeString Parent, UnicodeString Child);
  410. bool __fastcall ExtractCommonPath(TStrings * Files, UnicodeString & Path);
  411. bool __fastcall UnixExtractCommonPath(TStrings * Files, UnicodeString & Path);
  412. UnicodeString __fastcall ExtractFileName(const UnicodeString & Path, bool Unix);
  413. bool __fastcall IsUnixRootPath(const UnicodeString Path);
  414. bool __fastcall IsUnixHiddenFile(const UnicodeString Path);
  415. UnicodeString __fastcall AbsolutePath(const UnicodeString & Base, const UnicodeString & Path);
  416. UnicodeString __fastcall FromUnixPath(const UnicodeString Path);
  417. UnicodeString __fastcall ToUnixPath(const UnicodeString Path);
  418. UnicodeString __fastcall MinimizeName(const UnicodeString FileName, int MaxLen, bool Unix);
  419. UnicodeString __fastcall MakeFileList(TStrings * FileList);
  420. TDateTime __fastcall ReduceDateTimePrecision(TDateTime DateTime,
  421. TModificationFmt Precision);
  422. TModificationFmt __fastcall LessDateTimePrecision(
  423. TModificationFmt Precision1, TModificationFmt Precision2);
  424. UnicodeString __fastcall UserModificationStr(TDateTime DateTime,
  425. TModificationFmt Precision);
  426. UnicodeString __fastcall ModificationStr(TDateTime DateTime,
  427. TModificationFmt Precision);
  428. int __fastcall FakeFileImageIndex(UnicodeString FileName, unsigned long Attrs = 0,
  429. UnicodeString * TypeName = NULL);
  430. //---------------------------------------------------------------------------
  431. #endif