RemoteFiles.h 17 KB

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