/// <summary> /// Initializes a new instance of the <see cref="SftpFile"/> class. /// </summary> /// <param name="sftpSession">The SFTP session.</param> /// <param name="fullName">Full path of the directory or file.</param> /// <param name="attributes">Attributes of the directory or file.</param> /// <exception cref="ArgumentNullException"><paramref name="sftpSession"/> or <paramref name="fullName"/> is null.</exception> internal SftpFile(SftpSession sftpSession, string fullName, SftpFileAttributes attributes) { if (sftpSession == null) { throw new SshConnectionException("Client not connected."); } if (attributes == null) { throw new ArgumentNullException("attributes"); } if (fullName == null) { throw new ArgumentNullException("fullName"); } this._sftpSession = sftpSession; this.Attributes = attributes; this.Name = fullName.Substring(fullName.LastIndexOf('/') + 1); this.FullName = fullName; }
internal SftpFileStream(SftpSession session, string path, FileMode mode, FileAccess access, int bufferSize) : this(session, path, mode, access, bufferSize, false) { // Nothing to do here. }
internal SftpFileStream(SftpSession session, string path, FileMode mode, FileAccess access, int bufferSize, bool useAsync) { // Validate the parameters. if (session == null) { throw new SshConnectionException("Client not connected."); } if (path == null) { throw new ArgumentNullException("path"); } if (bufferSize <= 0 || bufferSize > 16 * 1024) { throw new ArgumentOutOfRangeException("bufferSize"); } if (access < FileAccess.Read || access > FileAccess.ReadWrite) { throw new ArgumentOutOfRangeException("access"); } if (mode < FileMode.CreateNew || mode > FileMode.Append) { throw new ArgumentOutOfRangeException("mode"); } this.Timeout = TimeSpan.FromSeconds(30); this.Name = path; // Initialize the object state. this._session = session; this._access = access; this._ownsHandle = true; this._isAsync = useAsync; this._path = path; this._bufferSize = bufferSize; this._buffer = new byte[bufferSize]; this._bufferPosn = 0; this._bufferLen = 0; this._bufferOwnedByWrite = false; this._canSeek = true; this._position = 0; this._serverFilePosition = 0; this._session.Disconnected += Session_Disconnected; var flags = Flags.None; switch (access) { case FileAccess.Read: flags |= Flags.Read; break; case FileAccess.Write: flags |= Flags.Write; break; case FileAccess.ReadWrite: flags |= Flags.Read; flags |= Flags.Write; break; default: break; } switch (mode) { case FileMode.Append: flags |= Flags.Append; break; case FileMode.Create: this._handle = this._session.RequestOpen(path, flags | Flags.Truncate, true); if (this._handle == null) { flags |= Flags.CreateNew; } else { flags |= Flags.Truncate; } break; case FileMode.CreateNew: flags |= Flags.CreateNew; break; case FileMode.Open: break; case FileMode.OpenOrCreate: flags |= Flags.CreateNewOrOpen; break; case FileMode.Truncate: flags |= Flags.Truncate; break; default: break; } if (this._handle == null) { this._handle = this._session.RequestOpen(this._path, flags); } this._attributes = this._session.RequestFStat(this._handle); if (mode == FileMode.Append) { this._position = this._attributes.Size; this._serverFilePosition = (ulong)this._attributes.Size; } }
internal SftpFileStream(SftpSession session, string path, FileMode mode, FileAccess access) : this(session, path, mode, access, 4096, false) { // Nothing to do here. }