RemoteFiles.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 " -> "
  11. #define PARENTDIRECTORY ".."
  12. #define THISDIRECTORY "."
  13. #define ROOTDIRECTORY "/"
  14. #define FILETYPE_SYMLINK 'L'
  15. #define FILETYPE_DIRECTORY 'D'
  16. #define PARTIAL_EXT ".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 AnsiString & 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 AnsiString 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 AnsiString LogText = { read = GetLogText };
  39. __property AnsiString DisplayText = { read = GetDisplayText };
  40. private:
  41. AnsiString 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. AnsiString __fastcall GetDisplayText() const;
  48. AnsiString __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 AnsiString & Name) const;
  59. const TRemoteToken * Find(unsigned int ID) const;
  60. const TRemoteToken * Find(const AnsiString & Name) const;
  61. void __fastcall Log(TTerminal * Terminal, const char * 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<AnsiString, 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. AnsiString 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. AnsiString FLinkTo;
  90. TRights *FRights;
  91. TTerminal *FTerminal;
  92. Char FType;
  93. bool FSelected;
  94. bool FCyclicLink;
  95. AnsiString FFullFileName;
  96. int FIsHidden;
  97. AnsiString 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. AnsiString __fastcall GetModificationStr();
  104. void __fastcall SetModification(const TDateTime & value);
  105. void __fastcall SetListingStr(AnsiString value);
  106. AnsiString __fastcall GetListingStr();
  107. AnsiString __fastcall GetRightsStr();
  108. char __fastcall GetType() const;
  109. void __fastcall SetType(char AType);
  110. void __fastcall SetTerminal(TTerminal * value);
  111. void __fastcall SetRights(TRights * value);
  112. AnsiString __fastcall GetFullFileName() const;
  113. bool __fastcall GetHaveFullFileName() const;
  114. int __fastcall GetIconIndex() const;
  115. AnsiString __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. AnsiString __fastcall GetExtension();
  122. AnsiString __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 AnsiString 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 AnsiString FileName = { read = FFileName, write = FFileName };
  140. __property int INodeBlocks = { read = FINodeBlocks };
  141. __property TDateTime Modification = { read = FModification, write = SetModification };
  142. __property AnsiString ModificationStr = { read = GetModificationStr };
  143. __property AnsiString 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 AnsiString LinkTo = { read = FLinkTo, write = FLinkTo };
  150. __property AnsiString ListingStr = { read = GetListingStr, write = SetListingStr };
  151. __property TRights * Rights = { read = FRights, write = SetRights };
  152. __property TTerminal * Terminal = { read = FTerminal, write = SetTerminal };
  153. __property Char Type = { read = GetType, write = SetType };
  154. __property bool Selected = { read=FSelected, write=FSelected };
  155. __property AnsiString FullFileName = { read = GetFullFileName, write = FFullFileName };
  156. __property bool HaveFullFileName = { read = GetHaveFullFileName };
  157. __property int IconIndex = { read = GetIconIndex };
  158. __property AnsiString 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 AnsiString 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. protected:
  184. AnsiString FDirectory;
  185. TDateTime FTimestamp;
  186. TRemoteFile * __fastcall GetFiles(Integer Index);
  187. virtual void __fastcall SetDirectory(AnsiString value);
  188. AnsiString __fastcall GetFullDirectory();
  189. Boolean __fastcall GetIsRoot();
  190. TRemoteFile * __fastcall GetParentDirectory();
  191. AnsiString __fastcall GetParentPath();
  192. __int64 __fastcall GetTotalSize();
  193. virtual void __fastcall Clear();
  194. public:
  195. __fastcall TRemoteFileList();
  196. TRemoteFile * __fastcall FindFile(const AnsiString &FileName);
  197. virtual void __fastcall DuplicateTo(TRemoteFileList * Copy);
  198. virtual void __fastcall AddFile(TRemoteFile * File);
  199. __property AnsiString Directory = { read = FDirectory, write = SetDirectory };
  200. __property TRemoteFile * Files[Integer Index] = { read = GetFiles };
  201. __property AnsiString FullDirectory = { read=GetFullDirectory };
  202. __property Boolean IsRoot = { read = GetIsRoot };
  203. __property AnsiString ParentPath = { read = GetParentPath };
  204. __property __int64 TotalSize = { read = GetTotalSize };
  205. __property TDateTime Timestamp = { read = FTimestamp };
  206. };
  207. //---------------------------------------------------------------------------
  208. class TRemoteDirectory : public TRemoteFileList
  209. {
  210. friend class TSCPFileSystem;
  211. friend class TSFTPFileSystem;
  212. private:
  213. Boolean FIncludeParentDirectory;
  214. Boolean FIncludeThisDirectory;
  215. TTerminal * FTerminal;
  216. TStrings * FSelectedFiles;
  217. TRemoteFile * FParentDirectory;
  218. TRemoteFile * FThisDirectory;
  219. virtual void __fastcall SetDirectory(AnsiString value);
  220. TStrings * __fastcall GetSelectedFiles();
  221. Boolean __fastcall GetLoaded();
  222. void __fastcall SetIncludeParentDirectory(Boolean value);
  223. void __fastcall SetIncludeThisDirectory(Boolean value);
  224. protected:
  225. virtual void __fastcall Clear();
  226. public:
  227. __fastcall TRemoteDirectory(TTerminal * aTerminal, TRemoteDirectory * Template = NULL);
  228. virtual void __fastcall AddFile(TRemoteFile * File);
  229. virtual void __fastcall DuplicateTo(TRemoteFileList * Copy);
  230. __property TTerminal * Terminal = { read = FTerminal, write = FTerminal };
  231. __property TStrings * SelectedFiles = { read=GetSelectedFiles };
  232. __property Boolean IncludeParentDirectory = { read = FIncludeParentDirectory, write = SetIncludeParentDirectory };
  233. __property Boolean IncludeThisDirectory = { read = FIncludeThisDirectory, write = SetIncludeThisDirectory };
  234. __property Boolean Loaded = { read = GetLoaded };
  235. __property TRemoteFile * ParentDirectory = { read = FParentDirectory };
  236. __property TRemoteFile * ThisDirectory = { read = FThisDirectory };
  237. };
  238. //---------------------------------------------------------------------------
  239. class TCriticalSection;
  240. class TRemoteDirectoryCache : private TStringList
  241. {
  242. public:
  243. __fastcall TRemoteDirectoryCache();
  244. virtual __fastcall ~TRemoteDirectoryCache();
  245. bool __fastcall HasFileList(const AnsiString Directory);
  246. bool __fastcall HasNewerFileList(const AnsiString Directory, TDateTime Timestamp);
  247. bool __fastcall GetFileList(const AnsiString Directory,
  248. TRemoteFileList * FileList);
  249. void __fastcall AddFileList(TRemoteFileList * FileList);
  250. void __fastcall ClearFileList(AnsiString Directory, bool SubDirs);
  251. void __fastcall Clear();
  252. __property bool IsEmpty = { read = GetIsEmpty };
  253. protected:
  254. virtual void __fastcall Delete(int Index);
  255. private:
  256. TCriticalSection * FSection;
  257. bool __fastcall GetIsEmpty() const;
  258. };
  259. //---------------------------------------------------------------------------
  260. class TRemoteDirectoryChangesCache : private TStringList
  261. {
  262. public:
  263. __fastcall TRemoteDirectoryChangesCache(int MaxSize);
  264. void __fastcall AddDirectoryChange(const AnsiString SourceDir,
  265. const AnsiString Change, const AnsiString TargetDir);
  266. void __fastcall ClearDirectoryChange(AnsiString SourceDir);
  267. void __fastcall ClearDirectoryChangeTarget(AnsiString TargetDir);
  268. bool __fastcall GetDirectoryChange(const AnsiString SourceDir,
  269. const AnsiString Change, AnsiString & TargetDir);
  270. void __fastcall Clear();
  271. void __fastcall Serialize(AnsiString & Data);
  272. void __fastcall Deserialize(const AnsiString Data);
  273. __property bool IsEmpty = { read = GetIsEmpty };
  274. private:
  275. static bool __fastcall DirectoryChangeKey(const AnsiString SourceDir,
  276. const AnsiString Change, AnsiString & Key);
  277. bool __fastcall GetIsEmpty() const;
  278. void __fastcall SetValue(const AnsiString & Name, const AnsiString & Value);
  279. AnsiString __fastcall GetValue(const AnsiString & Name);
  280. int FMaxSize;
  281. };
  282. //---------------------------------------------------------------------------
  283. class TRights
  284. {
  285. public:
  286. static const int TextLen = 9;
  287. static const char UndefSymbol = '$';
  288. static const char UnsetSymbol = '-';
  289. static const char BasicSymbols[];
  290. static const char CombinedSymbols[];
  291. static const char ExtendedSymbols[];
  292. static const char ModeGroups[];
  293. enum TRight {
  294. rrUserIDExec, rrGroupIDExec, rrStickyBit,
  295. rrUserRead, rrUserWrite, rrUserExec,
  296. rrGroupRead, rrGroupWrite, rrGroupExec,
  297. rrOtherRead, rrOtherWrite, rrOtherExec,
  298. rrFirst = rrUserIDExec, rrLast = rrOtherExec };
  299. enum TFlag {
  300. rfSetUID = 04000, rfSetGID = 02000, rfStickyBit = 01000,
  301. rfUserRead = 00400, rfUserWrite = 00200, rfUserExec = 00100,
  302. rfGroupRead = 00040, rfGroupWrite = 00020, rfGroupExec = 00010,
  303. rfOtherRead = 00004, rfOtherWrite = 00002, rfOtherExec = 00001,
  304. rfRead = 00444, rfWrite = 00222, rfExec = 00111,
  305. rfNo = 00000, rfDefault = 00644, rfAll = 00777,
  306. rfSpecials = 07000, rfAllSpecials = 07777 };
  307. enum TUnsupportedFlag {
  308. rfDirectory = 040000 };
  309. enum TState { rsNo, rsYes, rsUndef };
  310. public:
  311. static TFlag __fastcall RightToFlag(TRight Right);
  312. __fastcall TRights();
  313. __fastcall TRights(const TRights & Source);
  314. __fastcall TRights(unsigned short Number);
  315. void __fastcall Assign(const TRights * Source);
  316. void __fastcall AddExecute();
  317. void __fastcall AllUndef();
  318. bool __fastcall operator ==(const TRights & rhr) const;
  319. bool __fastcall operator ==(unsigned short rhr) const;
  320. bool __fastcall operator !=(const TRights & rhr) const;
  321. TRights & __fastcall operator =(const TRights & rhr);
  322. TRights & __fastcall operator =(unsigned short rhr);
  323. TRights __fastcall operator ~() const;
  324. TRights __fastcall operator &(unsigned short rhr) const;
  325. TRights __fastcall operator &(const TRights & rhr) const;
  326. TRights & __fastcall operator &=(unsigned short rhr);
  327. TRights & __fastcall operator &=(const TRights & rhr);
  328. TRights __fastcall operator |(unsigned short rhr) const;
  329. TRights __fastcall operator |(const TRights & rhr) const;
  330. TRights & __fastcall operator |=(unsigned short rhr);
  331. TRights & __fastcall operator |=(const TRights & rhr);
  332. __fastcall operator unsigned short() const;
  333. __fastcall operator unsigned long() const;
  334. __property bool AllowUndef = { read = FAllowUndef, write = SetAllowUndef };
  335. __property bool IsUndef = { read = GetIsUndef };
  336. __property AnsiString ModeStr = { read = GetModeStr };
  337. __property AnsiString SimplestStr = { read = GetSimplestStr };
  338. __property AnsiString Octal = { read = GetOctal, write = SetOctal };
  339. __property unsigned short Number = { read = GetNumber, write = SetNumber };
  340. __property unsigned short NumberSet = { read = FSet };
  341. __property unsigned short NumberUnset = { read = FUnset };
  342. __property unsigned long NumberDecadic = { read = GetNumberDecadic };
  343. __property bool ReadOnly = { read = GetReadOnly, write = SetReadOnly };
  344. __property bool Right[TRight Right] = { read = GetRight, write = SetRight };
  345. __property TState RightUndef[TRight Right] = { read = GetRightUndef, write = SetRightUndef };
  346. __property AnsiString Text = { read = GetText, write = SetText };
  347. __property bool Unknown = { read = FUnknown };
  348. private:
  349. bool FAllowUndef;
  350. unsigned short FSet;
  351. unsigned short FUnset;
  352. AnsiString FText;
  353. bool FUnknown;
  354. bool __fastcall GetIsUndef() const;
  355. AnsiString __fastcall GetModeStr() const;
  356. AnsiString __fastcall GetSimplestStr() const;
  357. void __fastcall SetNumber(unsigned short value);
  358. AnsiString __fastcall GetText() const;
  359. void __fastcall SetText(const AnsiString & value);
  360. void __fastcall SetOctal(AnsiString value);
  361. unsigned short __fastcall GetNumber() const;
  362. unsigned short __fastcall GetNumberSet() const;
  363. unsigned short __fastcall GetNumberUnset() const;
  364. unsigned long __fastcall GetNumberDecadic() const;
  365. AnsiString __fastcall GetOctal() const;
  366. bool __fastcall GetReadOnly();
  367. bool __fastcall GetRight(TRight Right) const;
  368. TState __fastcall GetRightUndef(TRight Right) const;
  369. void __fastcall SetAllowUndef(bool value);
  370. void __fastcall SetReadOnly(bool value);
  371. void __fastcall SetRight(TRight Right, bool value);
  372. void __fastcall SetRightUndef(TRight Right, TState value);
  373. };
  374. //---------------------------------------------------------------------------
  375. enum TValidProperty { vpRights, vpGroup, vpOwner, vpModification, vpLastAccess };
  376. typedef Set<TValidProperty, vpRights, vpLastAccess> TValidProperties;
  377. class TRemoteProperties
  378. {
  379. public:
  380. TValidProperties Valid;
  381. bool Recursive;
  382. TRights Rights;
  383. bool AddXToDirectories;
  384. TRemoteToken Group;
  385. TRemoteToken Owner;
  386. __int64 Modification; // unix time
  387. __int64 LastAccess; // unix time
  388. __fastcall TRemoteProperties();
  389. __fastcall TRemoteProperties(const TRemoteProperties & rhp);
  390. bool __fastcall operator ==(const TRemoteProperties & rhp) const;
  391. bool __fastcall operator !=(const TRemoteProperties & rhp) const;
  392. void __fastcall Default();
  393. void __fastcall Load(THierarchicalStorage * Storage);
  394. void __fastcall Save(THierarchicalStorage * Storage) const;
  395. static TRemoteProperties __fastcall CommonProperties(TStrings * FileList);
  396. static TRemoteProperties __fastcall ChangedProperties(
  397. const TRemoteProperties & OriginalProperties, TRemoteProperties NewProperties);
  398. };
  399. //---------------------------------------------------------------------------
  400. AnsiString __fastcall UnixIncludeTrailingBackslash(const AnsiString Path);
  401. AnsiString __fastcall UnixExcludeTrailingBackslash(const AnsiString Path);
  402. AnsiString __fastcall UnixExtractFileDir(const AnsiString Path);
  403. AnsiString __fastcall UnixExtractFilePath(const AnsiString Path);
  404. AnsiString __fastcall UnixExtractFileName(const AnsiString Path);
  405. AnsiString __fastcall UnixExtractFileExt(const AnsiString Path);
  406. Boolean __fastcall UnixComparePaths(const AnsiString Path1, const AnsiString Path2);
  407. bool __fastcall UnixIsChildPath(AnsiString Parent, AnsiString Child);
  408. bool __fastcall ExtractCommonPath(TStrings * Files, AnsiString & Path);
  409. bool __fastcall UnixExtractCommonPath(TStrings * Files, AnsiString & Path);
  410. AnsiString __fastcall ExtractFileName(const AnsiString & Path, bool Unix);
  411. bool __fastcall IsUnixRootPath(const AnsiString Path);
  412. bool __fastcall IsUnixHiddenFile(const AnsiString Path);
  413. AnsiString __fastcall AbsolutePath(const AnsiString & Base, const AnsiString & Path);
  414. AnsiString __fastcall FromUnixPath(const AnsiString Path);
  415. AnsiString __fastcall ToUnixPath(const AnsiString Path);
  416. AnsiString __fastcall MinimizeName(const AnsiString FileName, int MaxLen, bool Unix);
  417. AnsiString __fastcall MakeFileList(TStrings * FileList);
  418. TDateTime __fastcall ReduceDateTimePrecision(TDateTime DateTime,
  419. TModificationFmt Precision);
  420. TModificationFmt __fastcall LessDateTimePrecision(
  421. TModificationFmt Precision1, TModificationFmt Precision2);
  422. AnsiString __fastcall UserModificationStr(TDateTime DateTime,
  423. TModificationFmt Precision);
  424. int __fastcall FakeFileImageIndex(AnsiString FileName, unsigned long Attrs = 0,
  425. AnsiString * TypeName = NULL);
  426. //---------------------------------------------------------------------------
  427. #endif