public static DateTime StringToFtpDate(String value) { //String[] lParts = Regex.Split(value, @"\w+"); String[] lParts = ParseLine(value, 3); return(FtpListingItem.StringToFtpDate(lParts[0], lParts[1], lParts[2])); }
public void Parse(String list, Boolean includeUpDir) { this.Clear(); Boolean lFoundUpDir = false; //String[] lItems = Regex.Split(list, @"(?:\r\n|\r|\n)"); String[] lItems = SplitLines(list); for (Int32 i = 0; i < lItems.Length; i++) { String lItem = lItems[i].Trim(); if (String.IsNullOrEmpty(lItem)) { continue; } if (lItem.ToLower().StartsWith("total")) { continue; } try { FtpListingItem lNewItem = new FtpListingItem(); lNewItem.Parse(lItem); this.Add(lNewItem); if (lNewItem.Directory && lNewItem.FileName == "..") { lFoundUpDir = true; } } catch (Exception ex) { if (!(ex is FormatException)) { if (defined("ECHOES") && (!(ex is IndexOutOfRangeException))) { throw ex; } else if (!defined("ECHOES") && (!(ex is RTLException))) { throw ex; } } } } if (includeUpDir && !lFoundUpDir) { FtpListingItem lUpItem = new FtpListingItem(); lUpItem.Directory = true; lUpItem.FileName = ".."; this.Insert(0, lUpItem); } }
public FtpListingItem Add() { FtpListingItem lResult = new FtpListingItem(); this.Add(lResult); return(lResult); }
public override String ToString() { Char[] lRights = new Char[] { 'd', 'r', 'w', 'x', 'r', 'w', 'x', 'r', 'w', 'x' }; if (!this.Directory) { lRights[0] = '-'; } if (!this.UserRead) { lRights[1] = '-'; } if (!this.UserWrite) { lRights[2] = '-'; } if (!this.UserExec) { lRights[3] = '-'; } if (!this.GroupRead) { lRights[4] = '-'; } if (!this.GroupWrite) { lRights[5] = '-'; } if (!this.GroupExec) { lRights[6] = '-'; } if (!this.OtherRead) { lRights[7] = '-'; } if (!this.OtherWrite) { lRights[8] = '-'; } if (!this.OtherExec) { lRights[9] = '-'; } return(String.Format("{0} {1,3} {2,8} {3,8} {4,7} {5} {6}", new String(lRights), this.SubItemCount, this.User, this.Group, this.Size, FtpListingItem.FtpDateToString(this.FileDate), this.FileName)); }
protected void AddListingItems(FtpListing listing, IEnumerable ftpItems) { if (ftpItems == null) { return; } foreach (IFtpItem item in ftpItems) { FtpListingItem lListingItem = listing.Add(); item.FillFtpListingItem(lListingItem); } }
public override void DoListFolderItems(FtpListing listing) { String[] lNames = Directory.GetDirectories(LocalPath); foreach (String name in lNames) { FtpListingItem lListingItem = listing.Add(); lListingItem.Directory = true; lListingItem.FileName = Path.GetFileName(name); lListingItem.FileDate = Directory.GetLastWriteTime(Path.Combine(LocalPath, name)); lListingItem.Size = 0; lListingItem.User = "******"; lListingItem.Group = "system"; lListingItem.UserRead = UserRead; lListingItem.UserWrite = UserWrite; lListingItem.UserExec = UserRead; lListingItem.GroupRead = GroupRead; lListingItem.GroupWrite = GroupWrite; lListingItem.GroupExec = GroupRead; lListingItem.OtherRead = WorldRead; lListingItem.OtherWrite = WorldWrite; lListingItem.OtherExec = WorldRead; } DirectoryInfo lDirectory = new DirectoryInfo(LocalPath); FileInfo[] lFiles = lDirectory.GetFiles(); foreach (FileInfo file in lFiles) { FtpListingItem lListingItem = listing.Add(); lListingItem.Directory = false; lListingItem.FileName = file.Name; lListingItem.FileDate = file.LastWriteTime; lListingItem.Size = file.Length; lListingItem.User = "******"; lListingItem.Group = "system"; lListingItem.UserRead = UserRead; lListingItem.UserWrite = UserWrite; lListingItem.UserExec = false; lListingItem.GroupRead = GroupRead; lListingItem.GroupWrite = GroupWrite; lListingItem.GroupExec = false; lListingItem.OtherRead = WorldRead; lListingItem.OtherWrite = WorldWrite; lListingItem.OtherExec = false; } AddListingItems(listing, SubFolders); AddListingItems(listing, Files); }
private bool fComplete; public bool Complete { get { return fComplete; } set { fComplete = value; } } public override void FillFtpListingItem(FtpListingItem aItem) { base.FillFtpListingItem(aItem); aItem.Directory = false; aItem.Size = Size; aItem.UserRead = Complete && UserRead; aItem.UserWrite = Complete && UserWrite;
public override void FillFtpListingItem(FtpListingItem item) { base.FillFtpListingItem(item); item.Directory = false; item.Size = Size; item.UserRead = Complete && UserRead; item.UserWrite = Complete && UserWrite; item.UserExec = false; item.GroupRead = Complete && GroupRead; item.GroupWrite = Complete && GroupWrite; item.GroupExec = false; item.OtherRead = Complete && WorldRead; item.OtherWrite = Complete && WorldWrite; item.OtherExec = false; }
public void ListFolderItems(FtpListing listing) { /* Add this folder (.) */ FtpListingItem lListingItem = listing.Add(); FillFtpListingItem(lListingItem, "."); /* Add parent folder (..) */ if (Parent != null) { lListingItem = listing.Add(); Parent.FillFtpListingItem(lListingItem, ".."); } DoListFolderItems(listing); }
public virtual void FillFtpListingItem(FtpListingItem item) { item.Directory = (this is IFtpFolder); item.FileName = Name; item.FileDate = Date; item.Size = Size; item.User = OwningUser; item.Group = OwningGroup; item.UserRead = UserRead; item.UserWrite = UserWrite; item.UserExec = item.Directory && UserRead; item.GroupRead = GroupRead; item.GroupWrite = GroupWrite; item.GroupExec = item.Directory && GroupRead; item.OtherRead = WorldRead; item.OtherWrite = WorldWrite; item.OtherExec = item.Directory && WorldRead; }
if (aSession.Username == OwningUser && UserRead) return true; if (WorldRead) return true; return false; } public virtual bool AllowWrite(VirtualFtpSession aSession) { if (Invalid) return false; if (aSession.IsFileAdmin) return true; if (aSession.Username == OwningUser && UserWrite) return true; if (WorldWrite) return true; return false; } private bool[] fRights = new bool[6]; public virtual bool UserRead { get { return fRights[0]; } set { fRights[0] = value; } } public virtual bool UserWrite { get { return fRights[1]; } set { fRights[1] = value; } } public virtual bool GroupRead { get { return fRights[2]; } set { fRights[2] = value; } } public virtual bool GroupWrite { get { return fRights[3]; } set { fRights[3] = value; } } public virtual bool WorldRead { get { return fRights[4]; } set { fRights[4] = value; } }
public virtual void FillFtpListingItem(FtpListingItem item, String name) { FillFtpListingItem(item); item.FileName = name; }
public void SyncDirectory(string aLocalDirectory, string aRemoteDirectory) { string lOriginalLocalDirectory = Directory.GetCurrentDirectory(); string lOriginalRemoteDirectory = fFtpClient.GetCurrentDirectory(); try { #region Step into synchronized directories ClientLog(String.Format("Local change directory to {0}", aLocalDirectory)); Directory.SetCurrentDirectory(aLocalDirectory); ClientLog(String.Format("Remote change directory to {0}", aRemoteDirectory)); fFtpClient.ChangeDirectory(aRemoteDirectory); #endregion #region Retrieve content ClientLog("Retrieving directory contents"); fFtpClient.List(); #endregion #region Get Local Folders List Dictionary <String, DirectoryInfo> lLocalFolders = new Dictionary <String, DirectoryInfo>(); if (fRecursiveSync) { foreach (String lName in Directory.GetDirectories(aLocalDirectory)) { DirectoryInfo d = new DirectoryInfo(lName); lLocalFolders.Add(d.Name, d); } } #endregion #region Get Local Files List Dictionary <String, FileInfo> lLocalFiles = new Dictionary <String, FileInfo>(); foreach (String lName in Directory.GetFiles(aLocalDirectory)) { FileInfo f = new FileInfo(lName); lLocalFiles.Add(f.Name, f); } #endregion #region Get Remote Files and Directories List Dictionary <String, FtpListingItem> lRemoteFolders = new Dictionary <string, FtpListingItem>(); Dictionary <String, FtpListingItem> lRemoteFiles = new Dictionary <string, FtpListingItem>(); foreach (FtpListingItem lRemoteItem in fFtpClient.CurrentDirectoryContents) { if (lRemoteItem.Directory) { if (fRecursiveSync) { if (lRemoteItem.FileName != "..") { lRemoteFolders.Add(lRemoteItem.FileName, lRemoteItem); } } } else { lRemoteFiles.Add(lRemoteItem.FileName, lRemoteItem); } } #endregion #region Synchronization... switch (fSyncMode) { #region Local Synchronization. Master: ftp; slave: local; case SyncMode.Local: #region Folders synchronization foreach (String lName in lRemoteFolders.Keys) { ClientLog(String.Format("Synchronizing folder '{0}'...", lName)); String lLocalItemName = Path.Combine(aLocalDirectory, lName); if (!Directory.Exists(lLocalItemName)) { Directory.CreateDirectory(lLocalItemName); } SyncDirectory(lLocalItemName, lName); ClientLog(String.Format("Folder '{0}' has been synchronized", lName)); lLocalFolders.Remove(lName); } #region Delete local folders that doesn't exists on FTP if (!fDoNotDeleteMissingItems) { foreach (DirectoryInfo toDelete in lLocalFolders.Values) { toDelete.Delete(true); //delete recursive } lLocalFolders.Clear(); } #endregion #endregion #region Files synchronization foreach (String lName in lRemoteFiles.Keys) { FtpListingItem lRemoteItem = lRemoteFiles[lName]; Boolean lNeedSync = true; if (lLocalFiles.ContainsKey(lName)) { FileInfo lLocalItem = lLocalFiles[lName]; lNeedSync = ( (lLocalItem.Length != lRemoteItem.Size) || (lLocalItem.LastWriteTime != lRemoteItem.FileDate)); } ClientLog( String.Format( "File {0} {1}", lRemoteItem.FileName, lNeedSync ? "requires synchronization." : "doesn't require synchronization.")); if (lNeedSync) { ClientLog(String.Format("Downloading {0}...", lRemoteItem.FileName)); using (Stream lStream = File.Open(Path.Combine(aLocalDirectory, lName), FileMode.Create)) { fFtpClient.Retrieve(lRemoteItem, lStream); lStream.Close(); File.SetLastWriteTime(lRemoteItem.FileName, lRemoteItem.FileDate); } ClientLog(String.Format("File {0} has been downloaded.", lRemoteItem.FileName)); } lLocalFiles.Remove(lName); } #region Delete local files that doesn't exists on FTP if (!fDoNotDeleteMissingItems) { foreach (FileInfo toDelete in lLocalFiles.Values) { toDelete.Delete(); } lLocalFiles.Clear(); } #endregion #endregion break; #endregion #region Remote Synchronization. Master: local; slave: ftp; case SyncMode.Remote: #region Folders Synchronization. foreach (String lName in lLocalFolders.Keys) { ClientLog(String.Format("Synchronizing folder '{0}'...", lName)); String lLocalItemName = Path.Combine(aLocalDirectory, lName); if (!lRemoteFolders.ContainsKey(lName)) { fFtpClient.MakeDirectory(lName); } SyncDirectory(lLocalItemName, lName); ClientLog(String.Format("Folder '{0}' has been synchronized", lName)); lRemoteFolders.Remove(lName); } #region Delete FTP folders that doesn't exists locally if (!fDoNotDeleteMissingItems) { foreach (FtpListingItem toDelete in lRemoteFolders.Values) { fFtpClient.RemoveDirectory(toDelete.FileName); } lRemoteFolders.Clear(); } #endregion #endregion #region Files Synchronization. foreach (String lName in lLocalFiles.Keys) { String lLocalItemName = Path.Combine(aLocalDirectory, lName); Boolean lNeedSync = true; if (lRemoteFiles.ContainsKey(lName)) { FileInfo lLocalItem = new FileInfo(lLocalItemName); FtpListingItem lRemoteItem = lRemoteFiles[lName]; lNeedSync = ( (lLocalItem.Length != lRemoteItem.Size) || (lLocalItem.LastWriteTime != lRemoteItem.FileDate)); } ClientLog( String.Format( "File {0} {1}", lName, lNeedSync ? "requires synchronization." : "doesn't require synchronization.")); if (lNeedSync) { ClientLog(String.Format("Uploading {0}...", lName)); if (File.Exists(lLocalItemName)) { using (FileStream fs = new FileStream(lLocalItemName, FileMode.Open, FileAccess.Read)) { fFtpClient.Store(lName, fs); } } ClientLog(String.Format("File {0} has been uploaded.", lName)); } lRemoteFiles.Remove(lName); } #region Delete FTP files that doesn't exist locally if (!fDoNotDeleteMissingItems) { foreach (FtpListingItem toDelete in lRemoteFiles.Values) { fFtpClient.Delete(toDelete.FileName); } lRemoteFiles.Clear(); } #endregion #endregion break; #endregion } #endregion } finally { #region Step out of synchronized directories Directory.SetCurrentDirectory(lOriginalLocalDirectory); ClientLog(String.Format("Local change directory to {0}", lOriginalLocalDirectory)); fFtpClient.ChangeDirectory(lOriginalRemoteDirectory); ClientLog(String.Format("Remote change directory to {0}", lOriginalRemoteDirectory)); #endregion } }
public void Parse(String item) { //Regex lRegEx = new Regex(@"\s+"); // there is two modes possible Unix mode or MS-DOS mode if (item.StartsWith("d") || item.StartsWith("-")) { /* * Unix Mode * ====================================================================== * drwxr-xr-x 3 65025 100 4096 Dec 10 12:13 1 1 * drwxr-xr-x 2 65025 100 4096 Dec 10 12:13 2 * -rw-r--r-- 1 65025 100 35 Dec 10 12:33 root.txt * -rw-r--r-- 1 65025 100 43 Dec 10 12:33 root2.txt * * * where * 0 - access * 1 - sub item count * 2 - owner * 3 - group * 4 - size * 5 - Month * 6 - day * 7 - Time or Year * 8 - Filename */ //String[] lSplittedData = lRegEx.Split(item, 9); String[] lSplittedData = ParseLine(item, 9); // Copy splitted data to result // Problem is that at least one FTP server doesn;t return Group segment // So we have to compensate this String[] lSegments = new String[9]; for (Int32 i = 0; i < 3; i++) { lSegments[i] = lSplittedData[i]; } for (Int32 i = 1; i <= 6; i++) { lSegments[9 - i] = lSplittedData[lSplittedData.Length - i]; } this.Directory = lSegments[0][0] != '-'; this.UserRead = lSegments[0][1] != '-'; this.UserWrite = lSegments[0][2] != '-'; this.UserExec = lSegments[0][3] != '-'; this.GroupRead = lSegments[0][4] != '-'; this.GroupWrite = lSegments[0][5] != '-'; this.GroupExec = lSegments[0][6] != '-'; this.OtherRead = lSegments[0][7] != '-'; this.OtherWrite = lSegments[0][8] != '-'; this.OtherExec = lSegments[0][9] != '-'; this.SubItemCount = Convert.ToInt32(lSegments[1]); this.User = lSegments[2]; this.Group = lSegments[3]; this.Size = Convert.ToInt64(lSegments[4]); String lMonthShortName = lSegments[5]; String lDay = lSegments[6]; String lTimeOrYear = lSegments[7]; this.FileDate = FtpListingItem.StringToFtpDate(lMonthShortName, lDay, lTimeOrYear); this.FileName = lSegments[8]; } else { /* * MS-DOS Mode * ====================================================================== * 01-14-08 01:35PM <DIR> 1 1 * 01-14-08 01:35PM <DIR> 2 * 01-14-08 01:36PM 35 root.txt * 01-14-08 01:36PM 43 root2.txt * * where * * 0 - date * 1 - time * 2 - Size or IsDir * 3 - Filename */ //String[] lSegments = lRegEx.Split(item, 4); String[] lSegments = ParseLine(item, 4); this.Directory = (lSegments[2] == "<DIR>"); this.Size = this.Directory ? 0 : Convert.ToInt64(lSegments[2]); String lDateStr = lSegments[0]; String lTimeStr = lSegments[1]; this.FileDate = FtpListingItem.StringToFtpDate(lDateStr, lTimeStr); this.FileName = lSegments[3]; } }
public virtual string OwningGroup { get { return fOwningGroup; } set { fOwningGroup = value; } } public virtual bool AllowRead(VirtualFtpSession aSession) { if (Invalid) return false;