RemoteFiles.h 16 KB

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