public FileSystemObject(string path, string filename, string owner, string group, long size, FileSystemObjectPermissions permissions, bool isDirectory, bool isLink, DateTime lastEdit) { this.path = path; this.filename = filename; this.owner = owner; this.group = group; this.size = size; this.permissions = permissions; this.isDirectory = isDirectory; this.isLink = isLink; this.lastEdit = lastEdit; }
/// <summary> /// Gets the objects from the given path. Recommended to end the path with "/" /// </summary> /// <param name="dir">The path</param> /// <returns>A list ordered by name</returns> public InteractionResult <List <FileSystemObject> > GetObjectsFromDir(string dir) { List <FileSystemObject> result = new List <FileSystemObject>(); //Safety check if (mDevice.ConnectionStatus != DeviceState.Online) { return(new InteractionResult <List <FileSystemObject> >(result, false, new Exception("Device not online"))); } //Execute command Shell shell = mDevice.CommandShell; string output = shell.Exec("ls -l -a \"" + dir + "\"", mHasRoot); //Check for errors if (output.Contains("opendir failed")) { return(new InteractionResult <List <FileSystemObject> >(result, false, new Exception(output.After("opendir failed, ")))); } //Split and prepare regex string[] lines = output.Split(new string[] { "\r\r\n" }, StringSplitOptions.RemoveEmptyEntries); //Regex regex = new Regex(@"^(?P<permissions>[dl\-][rwx\-]+) (?P<owner>\w+)\W+(?P<group>[\w_]+)\W*(?P<size>\d+)?\W+(?P<datetime>\d{4}-\d{2}-\d{2} \d{2}:\d{2}) (?P<name>.+)$"); Regex regex = new Regex(@"^(?<permissions>[dl\-][rwx\-]+) (?<owner>\w+)\W+(?<group>[\w_]+)\W*(?<size>\d+)?\W+(?<datetime>\d{4}-\d{2}-\d{2} \d{2}:\d{2}) (?<name>.+)$"); //Go thorough each line foreach (string line in lines) { Match match = regex.Match(line); if (!match.Success) { continue; } //Basic variables bool isDir = false; bool isLink = false; long size = 0L; string path = ""; //Parse filname string filename = match.Groups["name"].Value; //Determining type if (match.Groups["permissions"].Value.StartsWith("l")) { filename = filename.BeforeFirst(" -> "); isLink = true; } else if (match.Groups["permissions"].Value.StartsWith("d")) { isDir = true; } //Building of path if (dir.EndsWith("/")) { path = dir + filename; } else { path = dir + "/" + filename; } //Parse of last edit date DateTime lastEdit = DateTime.ParseExact(match.Groups["datetime"].Value, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture); //Parse of owner string owner = match.Groups["owner"].Value; //Parse of group string group = match.Groups["group"].Value; //Parse of size if it is a file long.TryParse(match.Groups["size"].Value, out size); //Parse of permissions char[] pl = match.Groups["permissions"].Value.ToCharArray(); FileSystemObjectPermissionGroup userGroup = new FileSystemObjectPermissionGroup(pl[1] == 'r', pl[2] == 'w', pl[3] == 'x'); FileSystemObjectPermissionGroup ownerGroup = new FileSystemObjectPermissionGroup(pl[4] == 'r', pl[5] == 'w', pl[6] == 'x'); FileSystemObjectPermissionGroup otherGroup = new FileSystemObjectPermissionGroup(pl[7] == 'r', pl[8] == 'w', pl[9] == 'x'); FileSystemObjectPermissions permissions = new FileSystemObjectPermissions(userGroup, ownerGroup, otherGroup); //Add to results result.Add(new FileSystemObject(path, filename, owner, group, size, permissions, isDir, isLink, lastEdit)); } return(new InteractionResult <List <FileSystemObject> >(result, true, null));; }