RemoteFiles.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. 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 };
  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();
  123. };
  124. //---------------------------------------------------------------------------
  125. class TRemoteFileList : public TObjectList
  126. {
  127. friend class TSCPFileSystem;
  128. friend class TSFTPFileSystem;
  129. protected:
  130. AnsiString FDirectory;
  131. TDateTime FTimestamp;
  132. TRemoteFile * __fastcall GetFiles(Integer Index);
  133. virtual void __fastcall SetDirectory(AnsiString value);
  134. AnsiString __fastcall GetFullDirectory();
  135. Boolean __fastcall GetIsRoot();
  136. TRemoteFile * __fastcall GetParentDirectory();
  137. AnsiString __fastcall GetParentPath();
  138. __int64 __fastcall GetTotalSize();
  139. virtual void __fastcall Clear();
  140. public:
  141. __fastcall TRemoteFileList();
  142. TRemoteFile * __fastcall FindFile(const AnsiString &FileName);
  143. virtual void __fastcall DuplicateTo(TRemoteFileList * Copy);
  144. virtual void __fastcall AddFile(TRemoteFile * File);
  145. __property AnsiString Directory = { read = FDirectory, write = SetDirectory };
  146. __property TRemoteFile * Files[Integer Index] = { read = GetFiles };
  147. __property AnsiString FullDirectory = { read=GetFullDirectory };
  148. __property Boolean IsRoot = { read = GetIsRoot };
  149. __property AnsiString ParentPath = { read = GetParentPath };
  150. __property __int64 TotalSize = { read = GetTotalSize };
  151. __property TDateTime Timestamp = { read = FTimestamp };
  152. };
  153. //---------------------------------------------------------------------------
  154. class TRemoteDirectory : public TRemoteFileList
  155. {
  156. friend class TSCPFileSystem;
  157. friend class TSFTPFileSystem;
  158. private:
  159. Boolean FIncludeParentDirectory;
  160. Boolean FIncludeThisDirectory;
  161. TTerminal * FTerminal;
  162. TStrings * FSelectedFiles;
  163. TRemoteFile * FParentDirectory;
  164. TRemoteFile * FThisDirectory;
  165. virtual void __fastcall SetDirectory(AnsiString value);
  166. TStrings * __fastcall GetSelectedFiles();
  167. Boolean __fastcall GetLoaded();
  168. void __fastcall SetIncludeParentDirectory(Boolean value);
  169. void __fastcall SetIncludeThisDirectory(Boolean value);
  170. protected:
  171. virtual void __fastcall Clear();
  172. public:
  173. __fastcall TRemoteDirectory(TTerminal * aTerminal);
  174. virtual void __fastcall AddFile(TRemoteFile * File);
  175. virtual void __fastcall DuplicateTo(TRemoteFileList * Copy);
  176. __property TTerminal * Terminal = { read = FTerminal, write = FTerminal };
  177. __property TStrings * SelectedFiles = { read=GetSelectedFiles };
  178. __property Boolean IncludeParentDirectory = { read = FIncludeParentDirectory, write = SetIncludeParentDirectory };
  179. __property Boolean IncludeThisDirectory = { read = FIncludeThisDirectory, write = SetIncludeThisDirectory };
  180. __property Boolean Loaded = { read = GetLoaded };
  181. __property TRemoteFile * ParentDirectory = { read = FParentDirectory };
  182. __property TRemoteFile * ThisDirectory = { read = FThisDirectory };
  183. };
  184. //---------------------------------------------------------------------------
  185. class TCriticalSection;
  186. class TRemoteDirectoryCache : private TStringList
  187. {
  188. public:
  189. __fastcall TRemoteDirectoryCache();
  190. virtual __fastcall ~TRemoteDirectoryCache();
  191. bool __fastcall HasFileList(const AnsiString Directory);
  192. bool __fastcall HasNewerFileList(const AnsiString Directory, TDateTime Timestamp);
  193. bool __fastcall GetFileList(const AnsiString Directory,
  194. TRemoteFileList * FileList);
  195. void __fastcall AddFileList(TRemoteFileList * FileList);
  196. void __fastcall ClearFileList(AnsiString Directory, bool SubDirs);
  197. void __fastcall Clear();
  198. __property bool IsEmpty = { read = GetIsEmpty };
  199. protected:
  200. virtual void __fastcall Delete(int Index);
  201. private:
  202. TCriticalSection * FSection;
  203. bool __fastcall GetIsEmpty() const;
  204. };
  205. //---------------------------------------------------------------------------
  206. class TRemoteDirectoryChangesCache : private TStringList
  207. {
  208. public:
  209. __fastcall TRemoteDirectoryChangesCache();
  210. void __fastcall AddDirectoryChange(const AnsiString SourceDir,
  211. const AnsiString Change, const AnsiString TargetDir);
  212. void __fastcall ClearDirectoryChange(AnsiString SourceDir);
  213. void __fastcall ClearDirectoryChangeTarget(AnsiString TargetDir);
  214. bool __fastcall GetDirectoryChange(const AnsiString SourceDir,
  215. const AnsiString Change, AnsiString & TargetDir);
  216. void __fastcall Clear();
  217. void __fastcall Serialize(AnsiString & Data);
  218. void __fastcall Deserialize(const AnsiString Data);
  219. __property bool IsEmpty = { read = GetIsEmpty };
  220. private:
  221. static bool __fastcall DirectoryChangeKey(const AnsiString SourceDir,
  222. const AnsiString Change, AnsiString & Key);
  223. bool __fastcall GetIsEmpty() const;
  224. };
  225. //---------------------------------------------------------------------------
  226. class TRights
  227. {
  228. public:
  229. static const int TextLen = 9;
  230. static const char UndefSymbol = '$';
  231. static const char UnsetSymbol = '-';
  232. static const char BasicSymbols[];
  233. static const char CombinedSymbols[];
  234. static const char ExtendedSymbols[];
  235. static const char ModeGroups[];
  236. enum TRight {
  237. rrUserIDExec, rrGroupIDExec, rrStickyBit,
  238. rrUserRead, rrUserWrite, rrUserExec,
  239. rrGroupRead, rrGroupWrite, rrGroupExec,
  240. rrOtherRead, rrOtherWrite, rrOtherExec,
  241. rrFirst = rrUserIDExec, rrLast = rrOtherExec };
  242. enum TFlag {
  243. rfSetUID = 04000, rfSetGID = 02000, rfStickyBit = 01000,
  244. rfUserRead = 00400, rfUserWrite = 00200, rfUserExec = 00100,
  245. rfGroupRead = 00040, rfGroupWrite = 00020, rfGroupExec = 00010,
  246. rfOtherRead = 00004, rfOtherWrite = 00002, rfOtherExec = 00001,
  247. rfRead = 00444, rfWrite = 00222, rfExec = 00111,
  248. rfNo = 00000, rfDefault = 00644, rfAll = 00777,
  249. rfSpecials = 07000, rfAllSpecials = 07777 };
  250. enum TUnsupportedFlag {
  251. rfDirectory = 040000 };
  252. enum TState { rsNo, rsYes, rsUndef };
  253. public:
  254. static TFlag __fastcall RightToFlag(TRight Right);
  255. __fastcall TRights();
  256. __fastcall TRights(const TRights & Source);
  257. __fastcall TRights(unsigned short Number);
  258. void __fastcall Assign(const TRights * Source);
  259. void __fastcall AddExecute();
  260. void __fastcall AllUndef();
  261. bool __fastcall operator ==(const TRights & rhr) const;
  262. bool __fastcall operator ==(unsigned short rhr) const;
  263. bool __fastcall operator !=(const TRights & rhr) const;
  264. TRights & __fastcall operator =(const TRights & rhr);
  265. TRights & __fastcall operator =(unsigned short rhr);
  266. TRights __fastcall operator ~() const;
  267. TRights __fastcall operator &(unsigned short rhr) const;
  268. TRights __fastcall operator &(const TRights & rhr) const;
  269. TRights & __fastcall operator &=(unsigned short rhr);
  270. TRights & __fastcall operator &=(const TRights & rhr);
  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. __fastcall operator unsigned short() const;
  276. __fastcall operator unsigned long() const;
  277. __property bool AllowUndef = { read = FAllowUndef, write = SetAllowUndef };
  278. __property bool IsUndef = { read = GetIsUndef };
  279. __property AnsiString ModeStr = { read = GetModeStr };
  280. __property AnsiString SimplestStr = { read = GetSimplestStr };
  281. __property AnsiString Octal = { read = GetOctal, write = SetOctal };
  282. __property unsigned short Number = { read = GetNumber, write = SetNumber };
  283. __property unsigned short NumberSet = { read = FSet };
  284. __property unsigned short NumberUnset = { read = FUnset };
  285. __property bool ReadOnly = { read = GetReadOnly, write = SetReadOnly };
  286. __property bool Right[TRight Right] = { read = GetRight, write = SetRight };
  287. __property TState RightUndef[TRight Right] = { read = GetRightUndef, write = SetRightUndef };
  288. __property AnsiString Text = { read = GetText, write = SetText };
  289. __property bool Unknown = { read = FUnknown };
  290. private:
  291. bool FAllowUndef;
  292. unsigned short FSet;
  293. unsigned short FUnset;
  294. AnsiString FText;
  295. bool FUnknown;
  296. bool __fastcall GetIsUndef() const;
  297. AnsiString __fastcall GetModeStr() const;
  298. AnsiString __fastcall GetSimplestStr() const;
  299. void __fastcall SetNumber(unsigned short value);
  300. AnsiString __fastcall GetText() const;
  301. void __fastcall SetText(const AnsiString & value);
  302. void __fastcall SetOctal(AnsiString value);
  303. unsigned short __fastcall GetNumber() const;
  304. unsigned short __fastcall GetNumberSet() const;
  305. unsigned short __fastcall GetNumberUnset() const;
  306. AnsiString __fastcall GetOctal() const;
  307. bool __fastcall GetReadOnly();
  308. bool __fastcall GetRight(TRight Right) const;
  309. TState __fastcall GetRightUndef(TRight Right) const;
  310. void __fastcall SetAllowUndef(bool value);
  311. void __fastcall SetReadOnly(bool value);
  312. void __fastcall SetRight(TRight Right, bool value);
  313. void __fastcall SetRightUndef(TRight Right, TState value);
  314. };
  315. //---------------------------------------------------------------------------
  316. enum TValidProperty { vpRights, vpGroup, vpOwner, vpModification, vpLastAccess };
  317. typedef Set<TValidProperty, vpRights, vpLastAccess> TValidProperties;
  318. class TRemoteProperties
  319. {
  320. public:
  321. TValidProperties Valid;
  322. bool Recursive;
  323. TRights Rights;
  324. bool AddXToDirectories;
  325. AnsiString Group;
  326. AnsiString Owner;
  327. __int64 Modification; // unix time
  328. __int64 LastAccess; // unix time
  329. __fastcall TRemoteProperties();
  330. bool __fastcall operator ==(const TRemoteProperties & rhp) const;
  331. bool __fastcall operator !=(const TRemoteProperties & rhp) const;
  332. void __fastcall Default();
  333. void __fastcall Load(THierarchicalStorage * Storage);
  334. void __fastcall Save(THierarchicalStorage * Storage) const;
  335. static TRemoteProperties __fastcall CommonProperties(TStrings * FileList);
  336. static TRemoteProperties __fastcall ChangedProperties(
  337. const TRemoteProperties & OriginalProperties, TRemoteProperties NewProperties);
  338. };
  339. //---------------------------------------------------------------------------
  340. AnsiString __fastcall UnixIncludeTrailingBackslash(const AnsiString Path);
  341. AnsiString __fastcall UnixExcludeTrailingBackslash(const AnsiString Path);
  342. AnsiString __fastcall UnixExtractFileDir(const AnsiString Path);
  343. AnsiString __fastcall UnixExtractFilePath(const AnsiString Path);
  344. AnsiString __fastcall UnixExtractFileName(const AnsiString Path);
  345. AnsiString __fastcall UnixExtractFileExt(const AnsiString Path);
  346. Boolean __fastcall ComparePaths(const AnsiString Path1, const AnsiString Path2);
  347. Boolean __fastcall UnixComparePaths(const AnsiString Path1, const AnsiString Path2);
  348. bool __fastcall ExtractCommonPath(TStrings * Files, AnsiString & Path);
  349. bool __fastcall UnixExtractCommonPath(TStrings * Files, AnsiString & Path);
  350. bool __fastcall IsUnixRootPath(const AnsiString Path);
  351. bool __fastcall IsUnixHiddenFile(const AnsiString Path);
  352. AnsiString __fastcall FromUnixPath(const AnsiString Path);
  353. AnsiString __fastcall ToUnixPath(const AnsiString Path);
  354. AnsiString __fastcall MinimizeName(const AnsiString FileName, int MaxLen, bool Unix);
  355. AnsiString __fastcall MakeFileList(TStrings * FileList);
  356. void __fastcall ReduceDateTimePrecision(TDateTime & DateTime,
  357. TModificationFmt Precision);
  358. AnsiString __fastcall UserModificationStr(TDateTime DateTime,
  359. TModificationFmt Precision);
  360. int __fastcall FakeFileImageIndex(AnsiString FileName, unsigned long Attrs = 0,
  361. AnsiString * TypeName = NULL);
  362. //---------------------------------------------------------------------------
  363. #endif