RemoteFiles.h 16 KB

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