/// <inheritdoc/> public override async Task<FtpResponse> Process(FtpCommand command, CancellationToken cancellationToken) { await Connection.WriteAsync(new FtpResponse(150, "Opening data connection."), cancellationToken); ITcpSocketClient responseSocket; try { responseSocket = await Connection.CreateResponseSocket(); } catch (Exception) { return new FtpResponse(425, "Can't open data connection."); } try { // Parse arguments in a way that's compatible with broken FTP clients var argument = new ListArguments(command.Argument); var showHidden = argument.All; // Instantiate the formatter IListFormatter formatter; if (string.Equals(command.Name, "NLST", StringComparison.OrdinalIgnoreCase)) { formatter = new ShortListFormatter(); } else if (string.Equals(command.Name, "LS", StringComparison.OrdinalIgnoreCase)) { formatter = new LongListFormatter(); } else { formatter = new LongListFormatter(); } // Parse the given path to determine the mask (e.g. when information about a file was requested) var directoriesToProcess = new Queue<DirectoryQueueItem>(); // Use braces to avoid the definition of mask and path in the following parts // of this function. { var mask = "*"; var path = Data.Path.Clone(); if (!string.IsNullOrEmpty(argument.Path)) { var foundEntry = await Data.FileSystem.SearchEntryAsync(path, argument.Path, cancellationToken); if (foundEntry?.Directory == null) return new FtpResponse(550, "File system entry not found."); var dirEntry = foundEntry.Entry as IUnixDirectoryEntry; if (dirEntry == null) { mask = foundEntry.FileName; } else if (!dirEntry.IsRoot) { path.Push(dirEntry); } } directoriesToProcess.Enqueue(new DirectoryQueueItem(path, mask)); } var encoding = Data.NlstEncoding ?? Connection.Encoding; using (var stream = await Connection.CreateEncryptedStream(responseSocket.WriteStream)) { using (var writer = new StreamWriter(stream, encoding, 4096, true) { NewLine = "\r\n", }) { while (directoriesToProcess.Count != 0) { var queueItem = directoriesToProcess.Dequeue(); var currentPath = queueItem.Path; var mask = queueItem.Mask; var currentDirEntry = currentPath.Count != 0 ? currentPath.Peek() : Data.FileSystem.Root; if (argument.Recursive) { var line = currentPath.ToDisplayString() + ":"; Connection.Log?.Debug(line); await writer.WriteLineAsync(line); } var mmOptions = new Options() { IgnoreCase = Data.FileSystem.FileSystemEntryComparer.Equals("a", "A"), NoGlobStar = true, Dot = true, }; var mm = new Minimatcher(mask, mmOptions); var entries = await Data.FileSystem.GetEntriesAsync(currentDirEntry, cancellationToken); var enumerator = new DirectoryListingEnumerator(entries, Data.FileSystem, currentPath, true); while (enumerator.MoveNext()) { var name = enumerator.Name; if (!enumerator.IsDotEntry) { if (!mm.IsMatch(name)) continue; if (name.StartsWith(".") && !showHidden) continue; } var entry = enumerator.Entry; if (argument.Recursive && !enumerator.IsDotEntry) { var dirEntry = entry as IUnixDirectoryEntry; if (dirEntry != null) { var subDirPath = currentPath.Clone(); subDirPath.Push(dirEntry); directoriesToProcess.Enqueue(new DirectoryQueueItem(subDirPath, "*")); } } var line = formatter.Format(entry, name); Connection.Log?.Debug(line); await writer.WriteLineAsync(line); } } } } } finally { responseSocket.Dispose(); } // Use 250 when the connection stays open. return new FtpResponse(250, "Closing data connection."); }
/// <inheritdoc/> public override async Task<FtpResponse> Process(FtpCommand command, CancellationToken cancellationToken) { await Connection.WriteAsync(new FtpResponse(150, "Opening data connection."), cancellationToken); ITcpSocketClient responseSocket; try { responseSocket = await Connection.CreateResponseSocket(); } catch (Exception) { return new FtpResponse(425, "Can't open data connection."); } try { var argument = command.Argument; IListFormatter formatter; if (string.Equals(command.Name, "NLST", StringComparison.OrdinalIgnoreCase)) { formatter = new ShortListFormatter(); } else { formatter = new LongListFormatter(); } var mask = (string.IsNullOrEmpty(argument) || argument.StartsWith("-")) ? "*" : argument; var encoding = Data.NlstEncoding ?? Connection.Encoding; using (var stream = await Connection.CreateEncryptedStream(responseSocket.WriteStream)) { using (var writer = new StreamWriter(stream, encoding, 4096, true) { NewLine = "\r\n", }) { foreach (var line in formatter.GetPrefix(Data.CurrentDirectory)) { Connection.Log?.Debug(line); await writer.WriteLineAsync(line); } var mmOptions = new Options() { IgnoreCase = Data.FileSystem.FileSystemEntryComparer.Equals("a", "A"), NoGlobStar = true, Dot = true, }; var mm = new Minimatcher(mask, mmOptions); foreach (var entry in (await Data.FileSystem.GetEntriesAsync(Data.CurrentDirectory, cancellationToken)).Where(x => mm.IsMatch(x.Name))) { var line = formatter.Format(entry); Connection.Log?.Debug(line); await writer.WriteLineAsync(line); } foreach (var line in formatter.GetSuffix(Data.CurrentDirectory)) { Connection.Log?.Debug(line); await writer.WriteLineAsync(line); } } } } finally { responseSocket.Dispose(); } // Use 250 when the connection stays open. return new FtpResponse(250, "Closing data connection."); }