RemoteFiles.h 15 KB

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