/// <summary> /// Creates a new file entry. /// </summary> /// <param name="device">The device.</param> /// <param name="parent">parent entry or null if entry is root</param> /// <param name="name">name of the entry.</param> /// <param name="type">entry type.</param> /// <param name="isRoot">if set to <c>true</c> [is root].</param> internal FileEntry(Device device, FileEntry parent, string name, FileListingService.FileTypes type, bool isRoot) { this.FetchTime = 0; this.Parent = parent; this.Name = name; this.Type = type; this.IsRoot = isRoot; Children = new List <FileEntry>(); CheckAppPackageStatus(); this.Exists = true; this.Device = device; }
/// <summary> /// Creates a new file entry. /// </summary> /// <param name="device">The device.</param> /// <param name="parent">parent entry or null if entry is root</param> /// <param name="name">name of the entry.</param> /// <param name="type">entry type.</param> /// <param name="isRoot">if set to <see langword="true"/> [is root].</param> public FileEntry(FileSystem fileSystem, FileEntry parent, String name, FileListingService.FileTypes type, bool isRoot) { this.FetchTime = 0; this.Parent = parent; this.Name = name; this.Type = type; this.IsRoot = isRoot; Children = new List <FileEntry>(); CheckAppPackageStatus(); this.Exists = true; this.fileSystem = fileSystem; }
/// <summary> /// compute the recursive file size of all the files in the list. Folder have a weight of 1. /// </summary> /// <param name="entries">The remote files</param> /// <param name="fls">The FileListingService</param> /// <returns>The total number of bytes of the specified remote files</returns> private long GetTotalRemoteFileSize(IEnumerable <FileEntry> entries, FileListingService fls) { long count = 0; foreach (FileEntry e in entries) { FileListingService.FileTypes type = e.Type; if (type == FileListingService.FileTypes.Directory) { // get the children IEnumerable <FileEntry> children = fls.GetChildren(e, false, null); count += GetTotalRemoteFileSize(children, fls) + 1; } else if (type == FileListingService.FileTypes.File) { count += e.Size; } } return(count); }
/// <summary> /// /// </summary> /// <param name="entries"></param> /// <param name="localPath"></param> /// <param name="fls"></param> /// <param name="monitor"></param> /// <returns></returns> /// <exception cref="System.IO.IOException">Throws if unable to create a file or folder</exception> /// <exception cref="System.ArgumentNullException">Throws if the ISyncProgressMonitor is null</exception> private SyncResult DoPull(IEnumerable <FileEntry> entries, string localPath, FileListingService fileListingService, ISyncProgressMonitor monitor) { if (monitor == null) { throw new ArgumentNullException("monitor", "Monitor cannot be null"); } // check if we're cancelled if (monitor.IsCanceled) { return(new SyncResult(ErrorCodeHelper.RESULT_CANCELED)); } // check if we need to create the local directory DirectoryInfo localDir = new DirectoryInfo(localPath); if (!localDir.Exists) { localDir.Create( ); } foreach (FileEntry e in entries) { // check if we're canceled if (monitor.IsCanceled) { return(new SyncResult(ErrorCodeHelper.RESULT_CANCELED)); } // the destination item (folder or file) String dest = Path.Combine(localPath, e.Name); // get type (we only pull directory and files for now) FileListingService.FileTypes type = e.Type; if (type == FileListingService.FileTypes.Directory) { monitor.StartSubTask(e.FullPath, dest); // then recursively call the content. Since we did a ls command // to get the number of files, we can use the cache FileEntry[] children = fileListingService.GetChildren(e, true, null); SyncResult result = DoPull(children, dest, fileListingService, monitor); if (result.Code != ErrorCodeHelper.RESULT_OK) { return(result); } monitor.Advance(1); } else if (type == FileListingService.FileTypes.File) { monitor.StartSubTask(e.FullPath, dest); SyncResult result = DoPullFile(e.FullPath, dest, monitor); if (result.Code != ErrorCodeHelper.RESULT_OK) { return(result); } } else if (type == FileListingService.FileTypes.Link) { monitor.StartSubTask(e.FullPath, dest); SyncResult result = DoPullFile(e.FullResolvedPath, dest, monitor); if (result.Code != ErrorCodeHelper.RESULT_OK) { return(result); } } else { Log.d("ddms-sync", String.Format("unknown type to transfer: {0}", type)); } } return(new SyncResult(ErrorCodeHelper.RESULT_OK)); }
/// <summary> /// Processes the new lines. /// </summary> /// <param name="lines">The lines.</param> protected override void ProcessNewLines(string[] lines) { foreach (String line in lines) { // no need to handle empty lines. if (line.Length == 0) { continue; } // run the line through the regexp var m = line.Trim( ).Match(FileListingService.LS_PATTERN_EX, RegexOptions.Compiled); if (!m.Success) { Log.v("madb", "no match on file pattern: {0}", line); continue; } // get the name String name = m.Groups[9].Value; if (String.Compare(name, ".", true) == 0 || String.Compare(name, "..", true) == 0) { // we don't care if the entry is a "." or ".." continue; } // get the rest of the groups String permissions = m.Groups[1].Value; String owner = m.Groups[2].Value; String group = m.Groups[3].Value; bool isExec = String.Compare(m.Groups[10].Value, "*", true) == 0; long size = 0; String sizeData = m.Groups[4].Value.Trim( ); long.TryParse(String.IsNullOrEmpty(sizeData) ? "0" : sizeData, out size); String date1 = m.Groups[5].Value.Trim( ); String date2 = m.Groups[6].Value.Trim( ); String date3 = m.Groups[7].Value.Trim( ); DateTime date = DateTime.Now.GetEpoch( ); String time = m.Groups[8].Value.Trim(); if (String.IsNullOrEmpty(time)) { time = date.ToString("HH:mm"); } if (date1.Length == 3) { // check if we don't have a year and use current if we don't String tyear = String.IsNullOrEmpty(date3) ? DateTime.Now.Year.ToString( ) : date3; date = DateTime.ParseExact(String.Format("{0}-{1}-{2} {3}", date1, date2.PadLeft(2, '0'), tyear, time), "MMM-dd-yyyy HH:mm", CultureInfo.CurrentCulture); } else if (date1.Length == 4) { date = DateTime.ParseExact(String.Format("{0}-{1}-{2} {3}", date1, date2.PadLeft(2, '0'), date3, time), "yyyy-MM-dd HH:mm", CultureInfo.CurrentCulture); } String info = null; String linkName = null; // and the type FileListingService.FileTypes objectType = FileListingService.FileTypes.Other; switch (permissions[0]) { case '-': objectType = FileListingService.FileTypes.File; break; case 'b': objectType = FileListingService.FileTypes.Block; break; case 'c': objectType = FileListingService.FileTypes.Character; break; case 'd': objectType = FileListingService.FileTypes.Directory; break; case 'l': objectType = FileListingService.FileTypes.Link; break; case 's': objectType = FileListingService.FileTypes.Socket; break; case 'p': objectType = FileListingService.FileTypes.FIFO; break; } // now check what we may be linking to if (objectType == FileListingService.FileTypes.Link) { String[] segments = name.Split(new string[] { " -> " }, StringSplitOptions.RemoveEmptyEntries); // we should have 2 segments if (segments.Length == 2) { // update the entry name to not contain the link name = segments[0]; // and the link name info = segments[1]; // now get the path to the link String[] pathSegments = info.Split(new String[] { FileListingService.FILE_SEPARATOR }, StringSplitOptions.RemoveEmptyEntries); if (pathSegments.Length == 1) { // the link is to something in the same directory, // unless the link is .. if (String.Compare("..", pathSegments[0], false) == 0) { // set the type and we're done. objectType = FileListingService.FileTypes.DirectoryLink; } else { // either we found the object already // or we'll find it later. } } } else { } linkName = info; // add an arrow in front to specify it's a link. info = String.Format(LINK_FORMAT, info); } // get the entry, either from an existing one, or a new one FileEntry entry = GetExistingEntry(name); if (entry == null) { entry = new FileEntry(Parent.Device, Parent, name, objectType, false /* isRoot */); } // add some misc info entry.Permissions = new FilePermissions(permissions); entry.Size = size; entry.Date = date; entry.Owner = owner; entry.Group = group; entry.IsExecutable = isExec; entry.LinkName = linkName; if (objectType == FileListingService.FileTypes.Link) { entry.Info = info; } Entries.Add(entry); } }