RemoteFiles.h 15 KB

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