public Stream GetTimestepLogStream(Guid sessionGuid) { EnsureIsInitialized(); string filename = GetTimestepLogPath(sessionGuid); return(sftp.Open(filename, FileMode.Open, FileAccess.Read)); }
/// <inheritdoc/> /// <remarks>SFTP offers something called symbolic links, but they are not always enabled and are not actually a real copy.</remarks> public void CopyFile(string fileName, string newFileName) { HandleExceptions(() => { if (_sftpClient.Exists(newFileName)) { throw new RepositoryConfigurationException(ConfigurationExceptionCategory.FileExists, this, String.Format(RepositoryExceptionMessage.FileAlreadyExists_1, newFileName)); } using (Stream inputFileStream = _sftpClient.OpenRead(fileName)) using (Stream outputFileStream = _sftpClient.Open(newFileName, FileMode.CreateNew, FileAccess.Write)) { inputFileStream.CopyTo(outputFileStream); } _sftpClient.RenameFile(fileName, newFileName); }, fileName); }
public async Task WriteFile(SftpClient sftp, string path, ReadOnlyMemory <byte> contents) { logger.LogDebug("Opening {path} for writing", path); await using var writeFs = sftp.Open(path, FileMode.Create, FileAccess.Write); logger.LogDebug("Writing {bytes}B", contents.Length); await writeFs.WriteAsync(contents); await writeFs.FlushAsync(); }
private static void DeleteRecoursively(string path, SftpClient client) { var files = client.ListDirectory(path).Where(d => d.Name != "." && d.Name != ".."); foreach (var fileSystemInfo in files) { if (fileSystemInfo.IsDirectory) { client.Open(fileSystemInfo.FullName, FileMode.Open); DeleteRecoursively(fileSystemInfo.FullName, client); } else { client.Delete(fileSystemInfo.FullName); } } client.Delete(path); }
private async Task <string> ReadData(SftpClient client, SftpFile actualFile, GameLogFile known) { _logger.LogInformation("Read {0} from offset {1}", actualFile.FullName, known.ReadSize); using (var stream = client.Open(actualFile.FullName, FileMode.Open, FileAccess.Read)) { if (known.ReadSize != 0) { stream.Seek(known.ReadSize, SeekOrigin.Begin); } var data = known.UnreadData + await ReadData(stream); var dataEnd = data.LastIndexOf("\r\n"); known.UnreadData = data.Substring(dataEnd + 2); known.ReadSize = stream.Position; return(data.Substring(0, dataEnd)); } }
public NtStatus CreateFile(string fileName, FileAccess access, FileShare share, FileMode mode, FileOptions options, FileAttributes attributes, DokanFileInfo info) { //Debug.WriteLine(@"{0} : {1} {2}", fileName, mode.ToString(), access.ToString()); fileName = ToUnixStylePath(fileName); if (fileName.EndsWith("desktop.ini", StringComparison.OrdinalIgnoreCase) || fileName.EndsWith("autorun.inf", StringComparison.OrdinalIgnoreCase)) { return(DokanResult.FileNotFound); } bool exists = sftpClient.Exists(fileName); // todo : add to memory cache if (info.IsDirectory) { try { switch (mode) { case FileMode.Open: if (!exists) { return(DokanResult.PathNotFound); } break; case FileMode.CreateNew: if (exists) { return(DokanResult.FileExists); } sftpClient.CreateDirectory(fileName); break; } } catch (Renci.SshNet.Common.SshException) { return(DokanResult.AccessDenied); } } else { bool readWriteAttributes = (access & DataAccess) == 0; bool readAccess = (access & DataWriteAccess) == 0; System.IO.FileAccess acs = readAccess ? System.IO.FileAccess.Read : System.IO.FileAccess.ReadWrite; switch (mode) { case FileMode.Open: if (!exists) { return(DokanResult.FileNotFound); } SftpFileAttributes attr = sftpClient.GetAttributes(fileName); if (readWriteAttributes || attr.IsDirectory) { info.IsDirectory = attr.IsDirectory; info.Context = new object(); return(DokanResult.Success); } break; case FileMode.CreateNew: if (exists) { return(DokanResult.FileExists); } // cache invalidate break; case FileMode.Truncate: if (!exists) { return(DokanResult.FileNotFound); } // cache invalidate break; default: // cache invalidate break; } try { info.Context = sftpClient.Open(fileName, mode, acs) as SftpFileStream; } catch (Renci.SshNet.Common.SshException) { return(DokanResult.AccessDenied); } } return(DokanResult.Success); }
public void OpenTest1() { ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value SftpClient target = new SftpClient(connectionInfo); // TODO: Initialize to an appropriate value string path = string.Empty; // TODO: Initialize to an appropriate value FileMode mode = new FileMode(); // TODO: Initialize to an appropriate value FileAccess access = new FileAccess(); // TODO: Initialize to an appropriate value SftpFileStream expected = null; // TODO: Initialize to an appropriate value SftpFileStream actual; actual = target.Open(path, mode, access); Assert.AreEqual(expected, actual); Assert.Inconclusive("Verify the correctness of this test method."); }
static bool FTP_to_SFTP() { long alreadyCopied = 0; byte[] buffer = new byte[BufferSize]; try { using (var _ftpClient = new FtpClient(FtpProtocol.Ftp, FTP_Server, FTP_Port, FTP_Credentials)) { _sftpClient.Connect(); var filesToCopy = _ftpClient.ListEntries(FTP_PublishedFilesDirectory); long totalSize = filesToCopy.Sum(_ => _.Size ?? 0); int totalFiles = filesToCopy.Count(); int copiedFiles = 0; foreach (var fileToCopy in filesToCopy) { var sftpFileName = $"{SFTP_PublishedFilesDirectory}/{Path.GetFileName(fileToCopy.Name.Replace("-", ""))}"; HashAlgorithm murmur128 = MurmurHash.Create128(managed: false); string ftpHash = null; string sftpHash = null; long ftpSize = fileToCopy.Size.Value; long sftpSize = 0; string tempFile = Path.GetTempFileName(); using (var ftpFileStream = _ftpClient.Retr(fileToCopy.Path)) using (var tempFileStream = File.Open(tempFile, FileMode.Create, FileAccess.Write, FileShare.None)) { int readBytes = ftpFileStream.Read(buffer, 0, buffer.Length); while (readBytes > 0) { tempFileStream.Write(buffer, 0, readBytes); readBytes = ftpFileStream.Read(buffer, 0, buffer.Length); } } Log.WriteLine($"Copy {fileToCopy.Name} from FTP to {tempFile}"); using (var tempFileStream = File.Open(tempFile, FileMode.Open, FileAccess.Read, FileShare.Read)) { ftpHash = ToHashString(murmur128.ComputeHash(tempFileStream)); } if (_sftpClient.Exists(sftpFileName)) { sftpSize = _sftpClient.Get(sftpFileName).Length; using (var sftpFileStream = _sftpClient.OpenRead(sftpFileName)) { sftpHash = ToHashString(murmur128.ComputeHash(sftpFileStream)); } } if (ftpHash == sftpHash) { alreadyCopied += fileToCopy.Size.Value; copiedFiles++; ConsoleProgress.Write("({1}/{2}) {0}%", alreadyCopied * 100 / totalSize, copiedFiles, totalFiles); File.Delete(tempFile); continue; } Log.WriteLine($"{fileToCopy.Name} - {ftpHash} ({ftpSize}) - {sftpHash} ({(string.IsNullOrEmpty(sftpHash) ? 0 : sftpSize)})"); using (var tempFileStream = File.Open(tempFile, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var sftpFileStream = _sftpClient.Open(sftpFileName, FileMode.Create, FileAccess.Write)) { int readBytes = tempFileStream.Read(buffer, 0, buffer.Length); while (readBytes > 0) { sftpFileStream.Write(buffer, 0, readBytes); alreadyCopied += readBytes; ConsoleProgress.Write("({1}/{2}) {0}%", alreadyCopied * 100 / totalSize, copiedFiles, totalFiles); readBytes = tempFileStream.Read(buffer, 0, buffer.Length); } } File.Delete(tempFile); copiedFiles++; sftpSize = _sftpClient.Get(sftpFileName).Length; using (var sftpFileStream = _sftpClient.OpenRead(sftpFileName)) { sftpHash = ToHashString(murmur128.ComputeHash(sftpFileStream)); } Log.WriteLine($"Verify {fileToCopy.Name} - {ftpHash} ({ftpSize}) - {sftpHash} ({sftpSize})"); } _sftpClient.Disconnect(); return(true); } } catch (Exception e) { Log.WriteLine(e.Message); return(false); } }
public Stream Open(string path, FileMode mode, FileAccess access) { return(new Remote.SshFileStream(_sftp.Open(path, mode, access))); }
public bool downloadFile(string remoteDirectory, string remoteFileName, DirectoryInfo targetDirectory) { bool success = true; Directory.CreateDirectory(targetDirectory.FullName); sftp.ConnectionInfo.Timeout = TimeSpan.FromMinutes(30); sftp.Connect(); var remoteFiles = sftp.ListDirectory(remoteDirectory); //for each file in FTP directory if (remoteFiles.Count() > 0) { var remoteFile = remoteFiles.Where(x => x.Name == remoteFileName).OrderByDescending(x => x.Name).First(); if (remoteFile != null) { bool retry = false; bool continueDownload = false; int retryCnt = 0; //if file name matches if (remoteFile.Name.Contains(remoteFileName)) { Console.WriteLine("Downloading: " + remoteFile.Name); FileInfo localFile = new FileInfo(Path.Combine(targetDirectory.FullName, remoteFile.Name)); //delete file if already exists in local directory if (localFile.Exists) { Console.WriteLine("Deleting local file: " + localFile.FullName); localFile.Delete(); } do { //download file from ftp Stream destinationStream; if (continueDownload) { Console.WriteLine("Resuming download..."); destinationStream = new FileStream(localFile.FullName, FileMode.Append); } else { Console.WriteLine("Starting download..."); destinationStream = new FileStream(localFile.FullName, FileMode.Create); } using (destinationStream) using (var sourceStream = sftp.Open(remoteFile.FullName, FileMode.Open)) { sourceStream.Seek(destinationStream.Length, SeekOrigin.Begin); byte[] buffer = new byte[81920]; int read; ulong total = (ulong)destinationStream.Length; while ((read = sourceStream.Read(buffer, 0, buffer.Length)) != 0) { destinationStream.Write(buffer, 0, read); // report progress //total = total + (ulong)read; //printActionDel(total); } localFile = new FileInfo(localFile.FullName); } if (localFile.Exists) { if (remoteFile.Length == localFile.Length) { Console.WriteLine("Download complete!"); continueDownload = false; retry = false; success = true; break; } else if (localFile.Length > remoteFile.Length) { Console.WriteLine("Invalid Download. Disconnecting..."); sftp.Disconnect(); success = false; continueDownload = false; retry = false; break; } else if (localFile.Length < remoteFile.Length) { Console.WriteLine("Continuing..."); continueDownload = true; retry = false; sftp.Disconnect(); continue; } } else { Console.WriteLine("Retrying..."); retry = true; retryCnt++; } } while (retry && retryCnt < 3); } } else { Console.WriteLine("Empty file: '" + remoteFileName + "'"); throw new FileNotFoundException(); } } else { Console.WriteLine("File '" + remoteFileName + "' not found."); } Console.WriteLine("Disconnecting..."); sftp.Disconnect(); return(success); }