/// <summary> /// Overload of <see cref="ParseMode(string, StreamOpenOptions, out FileMode, out FileAccess, out StreamAccessOptions)"/> without the <c>out</c> arguments. /// </summary> /// <param name="mode">Mode as passed to <c>fopen()</c>.</param> /// <param name="options">The <see cref="StreamOpenOptions"/> passed to <c>fopen()</c>.</param> /// <param name="accessOptions">Resulting <see cref="StreamAccessOptions"/> giving /// additional information to the stream opener.</param> /// <returns><c>true</c> if the given mode was a valid file opening mode, otherwise <c>false</c>.</returns> /// <exception cref="ArgumentException">If the <paramref name="mode"/> is not valid.</exception> internal bool ParseMode(string mode, StreamOpenOptions options, out StreamAccessOptions accessOptions) { FileMode fileMode; FileAccess fileAccess; return(ParseMode(mode, options, out fileMode, out fileAccess, out accessOptions)); }
/// <summary> /// Checks whether the supported read/write access matches the reqiured one. /// </summary> /// <param name="accessOptions">The access options specified by the user.</param> /// <param name="supportedAccess">The read/write access options supported by the stream.</param> /// <param name="path">The path given by user to report errors.</param> /// <returns><c>false</c> if the stream does not support any of the required modes, <c>true</c> otherwise.</returns> internal bool CheckOptions(StreamAccessOptions accessOptions, FileAccess supportedAccess, string path) { FileAccess requiredAccess = (FileAccess)accessOptions & FileAccess.ReadWrite; FileAccess faultyAccess = requiredAccess & ~supportedAccess; if ((faultyAccess & FileAccess.Read) > 0) { PhpException.Throw(PhpError.Warning, ErrResources.stream_open_read_unsupported, FileSystemUtils.StripPassword(path)); return(false); } else if ((faultyAccess & FileAccess.Write) > 0) { PhpException.Throw(PhpError.Warning, ErrResources.stream_open_write_unsupported, FileSystemUtils.StripPassword(path)); return(false); } return(true); }
/// <summary> /// Starts a process and creates a pipe to its standard input or output. /// </summary> /// <param name="ctx">Runtime context.</param> /// <param name="command">The command.</param> /// <param name="mode">Pipe open mode (<c>"r"</c> or <c>"w"</c>).</param> /// <returns>Opened pipe or <B>null</B> on error.</returns> public static PhpResource popen(Context ctx, string command, string mode) { if (String.IsNullOrEmpty(mode)) { PhpException.Throw(PhpError.Warning, Core.Resources.ErrResources.invalid_file_mode, mode); return(null); } bool read = mode[0] == 'r'; bool write = mode[0] == 'w' || mode[0] == 'a' || mode[0] == 'x'; if (!read && !write) { PhpException.Throw(PhpError.Warning, Core.Resources.ErrResources.invalid_file_mode, mode); return(null); } Process process = CreateProcessExecutingCommand(ctx, ref command, false); if (process == null) { return(null); } process.StartInfo.RedirectStandardOutput = read; process.StartInfo.RedirectStandardInput = write; if (!StartProcess(process, true)) { return(null); } Stream stream = (read) ? process.StandardOutput.BaseStream : process.StandardInput.BaseStream; StreamAccessOptions access = (read) ? StreamAccessOptions.Read : StreamAccessOptions.Write; ProcessWrapper wrapper = new ProcessWrapper(process); PhpStream php_stream = new NativeStream(ctx, stream, wrapper, access, String.Empty, StreamContext.Default); return(php_stream); }
public static bool BeginIO(Stream stream, PhpStream phpStream, StreamAccessOptions access, int desc_no) { if (access == StreamAccessOptions.Read && !phpStream.CanWrite || access == StreamAccessOptions.Write && !phpStream.CanRead) { PhpException.Throw(PhpError.Warning, Resources.LibResources.descriptor_item_invalid_mode, desc_no.ToString()); return(false); } var pipe = new ActivePipe { stream = stream, phpStream = phpStream, access = access }; pipe.callback = new AsyncCallback(pipe.Callback); if (access == StreamAccessOptions.Read) { var buffer = new byte[BufferSize]; stream.BeginRead(buffer, 0, buffer.Length, pipe.callback, null); pipe.buffer = buffer; } else { pipe.buffer = phpStream.ReadBytes(BufferSize); if (pipe.buffer != null) { stream.BeginWrite(pipe.buffer, 0, pipe.buffer.Length, pipe.callback, null); } else { stream.Dispose(); } } return(true); }
public static bool BeginIO(Stream stream, PhpStream phpStream, StreamAccessOptions access, int desc_no) { if (access == StreamAccessOptions.Read && !phpStream.CanWrite || access == StreamAccessOptions.Write && !phpStream.CanRead) { PhpException.Throw(PhpError.Warning, LibResources.GetString("descriptor_item_invalid_mode", desc_no)); return(false); } ActivePipe pipe = new ActivePipe(); pipe.stream = stream; pipe.phpStream = phpStream; pipe.access = access; pipe.callback = new AsyncCallback(pipe.Callback); if (access == StreamAccessOptions.Read) { var buffer = new byte[BufferSize]; stream.BeginRead(buffer, 0, buffer.Length, pipe.callback, null); pipe.buffer = new PhpBytes(buffer); } else { pipe.buffer = phpStream.ReadBytes(BufferSize); if (pipe.buffer != null) { stream.BeginWrite(pipe.buffer.ReadonlyData, 0, pipe.buffer.Length, pipe.callback, null); } else { stream.Close(); } } return(true); }
public PhpUserStream(UserStreamWrapper/*!*/openingWrapper, StreamAccessOptions accessOptions, string openedPath, StreamContext context) : base(openingWrapper, accessOptions, openedPath, context) { }
public NativeStream(Stream nativeStream, StreamWrapper openingWrapper, StreamAccessOptions accessOptions, string openedPath, StreamContext context) : base(openingWrapper, accessOptions, openedPath, context) { Debug.Assert(nativeStream != null); this.stream = nativeStream; }
public static bool BeginIO(Stream stream, PhpStream phpStream, StreamAccessOptions access, int desc_no) { if (access == StreamAccessOptions.Read && !phpStream.CanWrite || access == StreamAccessOptions.Write && !phpStream.CanRead) { PhpException.Throw(PhpError.Warning, LibResources.GetString("descriptor_item_invalid_mode", desc_no)); return false; } ActivePipe pipe = new ActivePipe(); pipe.stream = stream; pipe.phpStream = phpStream; pipe.access = access; pipe.callback = new AsyncCallback(pipe.Callback); if (access == StreamAccessOptions.Read) { var buffer = new byte[BufferSize]; stream.BeginRead(buffer, 0, buffer.Length, pipe.callback, null); pipe.buffer = new PhpBytes(buffer); } else { pipe.buffer = phpStream.ReadBytes(BufferSize); if (pipe.buffer != null) stream.BeginWrite(pipe.buffer.ReadonlyData, 0, pipe.buffer.Length, pipe.callback, null); else stream.Close(); } return true; }
/// <summary> /// Overload of <see cref="ParseMode(string, StreamOpenOptions, out FileMode, out FileAccess, out StreamAccessOptions)"/> without the <c>out</c> arguments. /// </summary> /// <param name="mode">Mode as passed to <c>fopen()</c>.</param> /// <param name="options">The <see cref="StreamOpenOptions"/> passed to <c>fopen()</c>.</param> /// <param name="accessOptions">Resulting <see cref="StreamAccessOptions"/> giving /// additional information to the stream opener.</param> /// <returns><c>true</c> if the given mode was a valid file opening mode, otherwise <c>false</c>.</returns> /// <exception cref="ArgumentException">If the <paramref name="mode"/> is not valid.</exception> internal bool ParseMode(string mode, StreamOpenOptions options, out StreamAccessOptions accessOptions) { FileMode fileMode; FileAccess fileAccess; return (ParseMode(mode, options, out fileMode, out fileAccess, out accessOptions)); }
public NativeStream(IEncodingProvider enc_provider, Stream nativeStream, StreamWrapper openingWrapper, StreamAccessOptions accessOptions, string openedPath, StreamContext context) : base(enc_provider, openingWrapper, accessOptions, openedPath, context) { Debug.Assert(nativeStream != null); this.stream = nativeStream; }
public NativeStream(Context ctx, Stream nativeStream, StreamWrapper openingWrapper, StreamAccessOptions accessOptions, string openedPath, StreamContext context) : base(ctx, openingWrapper, accessOptions, openedPath, context) { Debug.Assert(nativeStream != null); this.stream = nativeStream; }
/// <summary> /// Creates a new <see cref="ExternalStream"/> with the given proxy object. /// </summary> /// <param name="proxy">Instance of a class derived from <see cref="MarshalByRefObject"/> that should /// serve as a proxy for this <see cref="ExternalStream"/>.</param> /// <param name="openingWrapper">The parent instance.</param> /// <param name="accessOptions">The additional options parsed from the <c>fopen()</c> mode.</param> /// <param name="openedPath">The absolute path to the opened resource.</param> /// <param name="context">The stream context passed to fopen().</param> internal ExternalStream(IExternalStream proxy, StreamWrapper openingWrapper, StreamAccessOptions accessOptions, string openedPath, StreamContext context) : base(openingWrapper, accessOptions, openedPath, context) { this.proxy = proxy; }
public PhpUserStream(Context ctx, UserStreamWrapper /*!*/ openingWrapper, StreamAccessOptions accessOptions, string openedPath, StreamContext context) : base(ctx, openingWrapper, accessOptions, openedPath, context) { }
/// <summary> /// Checks whether the supported read/write access matches the reqiured one. /// </summary> /// <param name="accessOptions">The access options specified by the user.</param> /// <param name="supportedAccess">The read/write access options supported by the stream.</param> /// <param name="path">The path given by user to report errors.</param> /// <returns><c>false</c> if the stream does not support any of the required modes, <c>true</c> otherwise.</returns> internal bool CheckOptions(StreamAccessOptions accessOptions, FileAccess supportedAccess, string path) { FileAccess requiredAccess = (FileAccess)accessOptions & FileAccess.ReadWrite; FileAccess faultyAccess = requiredAccess & ~supportedAccess; if ((faultyAccess & FileAccess.Read) > 0) { PhpException.Throw(PhpError.Warning, ErrResources.stream_open_read_unsupported, FileSystemUtils.StripPassword(path)); return false; } else if ((faultyAccess & FileAccess.Write) > 0) { PhpException.Throw(PhpError.Warning, ErrResources.stream_open_write_unsupported, FileSystemUtils.StripPassword(path)); return false; } return true; }
/// <summary> /// PhpStream is created by a StreamWrapper together with the /// encapsulated RawStream (the actual file opening is handled /// by the wrapper). /// </summary> /// <remarks> /// This class newly implements the auto-remove behavior too /// (see <see cref="StreamAccessOptions.Temporary"/>). /// </remarks> /// <param name="ctx">Runtime context.</param> /// <param name="openingWrapper">The parent instance.</param> /// <param name="accessOptions">The additional options parsed from the <c>fopen()</c> mode.</param> /// <param name="openedPath">The absolute path to the opened resource.</param> /// <param name="context">The stream context passed to fopen().</param> public PhpStream(Context ctx, StreamWrapper openingWrapper, StreamAccessOptions accessOptions, string openedPath, StreamContext context) : base(PhpStreamTypeName) { Debug.Assert(ctx != null); Debug.Assert(context != null); _ctx = ctx; _context = context; this.Wrapper = openingWrapper; this.OpenedPath = openedPath; // Stream modifiers (defined in open-time). this.Options = accessOptions; // Allocate the text conversion filters for this stream. if ((accessOptions & StreamAccessOptions.UseText) > 0) { if ((accessOptions & StreamAccessOptions.Read) > 0) { textReadFilter = new TextReadFilter(); } if ((accessOptions & StreamAccessOptions.Write) > 0) { textWriteFilter = new TextWriteFilter(); } } // this.readTimeout = ScriptContext.CurrentContext.Config.FileSystem.DefaultSocketTimeout; }
/// <summary> /// Parse the <paramref name="mode"/> argument passed to <c>fopen()</c> /// and make the appropriate <see cref="FileMode"/> and <see cref="FileAccess"/> /// combination. /// Integrate the relevant options from <see cref="StreamOpenOptions"/> too. /// </summary> /// <param name="mode">Mode as passed to <c>fopen()</c>.</param> /// <param name="options">The <see cref="StreamOpenOptions"/> passed to <c>fopen()</c>.</param> /// <param name="fileMode">Resulting <see cref="FileMode"/> specifying opening mode.</param> /// <param name="fileAccess">Resulting <see cref="FileAccess"/> specifying read/write access options.</param> /// <param name="accessOptions">Resulting <see cref="StreamAccessOptions"/> giving /// additional information to the stream opener.</param> /// <returns><c>true</c> if the given mode was a valid file opening mode, otherwise <c>false</c>.</returns> public bool ParseMode(string mode, StreamOpenOptions options, out FileMode fileMode, out FileAccess fileAccess, out StreamAccessOptions accessOptions) { accessOptions = StreamAccessOptions.Empty; bool forceBinary = false; // The user requested a text stream bool forceText = false; // Use text access to the stream (default is binary) // First check for relevant options in StreamOpenOptions: // Search for the file only if mode=='[ra]*' and use_include_path==true. // StreamAccessOptions findFile = 0; if ((options & StreamOpenOptions.UseIncludePath) > 0) { // findFile = StreamAccessOptions.FindFile; accessOptions |= StreamAccessOptions.FindFile; } // Copy the AutoRemove option. if ((options & StreamOpenOptions.Temporary) > 0) { accessOptions |= StreamAccessOptions.Temporary; } // Now do the actual mode parsing: fileMode = FileMode.Open; fileAccess = FileAccess.Write; if (String.IsNullOrEmpty(mode)) { PhpException.Throw(PhpError.Warning, ErrResources.empty_file_mode); return(false); } switch (mode[0]) { case 'r': // flags = 0; // fileMode is already set to Open fileAccess = FileAccess.Read; //accessOptions |= findFile; break; case 'w': // flags = O_TRUNC|O_CREAT; // fileAccess is set to Write fileMode = FileMode.Create; //accessOptions |= findFile; // EX: Note that use_include_path is applicable to all access methods. // Create truncates the existing file to zero length break; case 'a': // flags = O_CREAT|O_APPEND; // fileAccess is set to Write fileMode = FileMode.Append; //accessOptions |= findFile; // Note: .NET does not support the "a+" mode, use "r+" and Seek() break; case 'x': // flags = O_CREAT|O_EXCL; // fileAccess is set to Write fileMode = FileMode.CreateNew; accessOptions |= StreamAccessOptions.Exclusive; break; default: PhpException.Throw(PhpError.Warning, ErrResources.invalid_file_mode, mode); return(false); } if (mode.IndexOf('+') > -1) { // flags |= O_RDWR; fileAccess = FileAccess.ReadWrite; } if ((fileMode == FileMode.Append) && (fileAccess == FileAccess.ReadWrite)) { // Note: .NET does not support the "a+" mode, use "r+" and Seek() fileMode = FileMode.OpenOrCreate; fileAccess = FileAccess.ReadWrite; accessOptions |= StreamAccessOptions.SeekEnd; } if (mode.IndexOf('b') > -1) { // flags |= O_BINARY; forceBinary = true; } if (mode.IndexOf('t') > -1) { // flags |= _O_TEXT; forceText = true; } // Exactly one of these options is required. if ((forceBinary && forceText) || (!forceBinary && !forceText)) { //LocalConfiguration config = Configuration.Local; //// checks whether default mode is applicable: //if (config.FileSystem.DefaultFileOpenMode == "b") //{ // forceBinary = true; //} //else if (config.FileSystem.DefaultFileOpenMode == "t") //{ // forceText = true; //} //else //{ // PhpException.Throw(PhpError.Warning, ErrResources.ambiguous_file_mode, mode); //} throw new NotImplementedException("Configuration.FileSystem.DefaultFileOpenMode"); // Binary mode is assumed } else if (forceText) { // Default mode is binary (unless the text mode is specified). accessOptions |= StreamAccessOptions.UseText; } // Store the two file-access flags into the access options too. accessOptions |= (StreamAccessOptions)fileAccess; return(true); }
/// <summary> /// Parse the <paramref name="mode"/> argument passed to <c>fopen()</c> /// and make the appropriate <see cref="FileMode"/> and <see cref="FileAccess"/> /// combination. /// Integrate the relevant options from <see cref="StreamOpenOptions"/> too. /// </summary> /// <param name="mode">Mode as passed to <c>fopen()</c>.</param> /// <param name="options">The <see cref="StreamOpenOptions"/> passed to <c>fopen()</c>.</param> /// <param name="fileMode">Resulting <see cref="FileMode"/> specifying opening mode.</param> /// <param name="fileAccess">Resulting <see cref="FileAccess"/> specifying read/write access options.</param> /// <param name="accessOptions">Resulting <see cref="StreamAccessOptions"/> giving /// additional information to the stream opener.</param> /// <returns><c>true</c> if the given mode was a valid file opening mode, otherwise <c>false</c>.</returns> public bool ParseMode(string mode, StreamOpenOptions options, out FileMode fileMode, out FileAccess fileAccess, out StreamAccessOptions accessOptions) { accessOptions = StreamAccessOptions.Empty; bool forceBinary = false; // The user requested a text stream bool forceText = false; // Use text access to the stream (default is binary) // First check for relevant options in StreamOpenOptions: // Search for the file only if mode=='[ra]*' and use_include_path==true. // StreamAccessOptions findFile = 0; if ((options & StreamOpenOptions.UseIncludePath) > 0) { // findFile = StreamAccessOptions.FindFile; accessOptions |= StreamAccessOptions.FindFile; } // Copy the AutoRemove option. if ((options & StreamOpenOptions.Temporary) > 0) { accessOptions |= StreamAccessOptions.Temporary; } // Now do the actual mode parsing: fileMode = FileMode.Open; fileAccess = FileAccess.Write; if (String.IsNullOrEmpty(mode)) { PhpException.Throw(PhpError.Warning, ErrResources.empty_file_mode); return false; } switch (mode[0]) { case 'r': // flags = 0; // fileMode is already set to Open fileAccess = FileAccess.Read; //accessOptions |= findFile; break; case 'w': // flags = O_TRUNC|O_CREAT; // fileAccess is set to Write fileMode = FileMode.Create; //accessOptions |= findFile; // EX: Note that use_include_path is applicable to all access methods. // Create truncates the existing file to zero length break; case 'a': // flags = O_CREAT|O_APPEND; // fileAccess is set to Write fileMode = FileMode.Append; //accessOptions |= findFile; // Note: .NET does not support the "a+" mode, use "r+" and Seek() break; case 'x': // flags = O_CREAT|O_EXCL; // fileAccess is set to Write fileMode = FileMode.CreateNew; accessOptions |= StreamAccessOptions.Exclusive; break; default: PhpException.Throw(PhpError.Warning, ErrResources.invalid_file_mode, mode); return false; } if (mode.IndexOf('+') > -1) { // flags |= O_RDWR; fileAccess = FileAccess.ReadWrite; } if ((fileMode == FileMode.Append) && (fileAccess == FileAccess.ReadWrite)) { // Note: .NET does not support the "a+" mode, use "r+" and Seek() fileMode = FileMode.OpenOrCreate; fileAccess = FileAccess.ReadWrite; accessOptions |= StreamAccessOptions.SeekEnd; } if (mode.IndexOf('b') > -1) { // flags |= O_BINARY; forceBinary = true; } if (mode.IndexOf('t') > -1) { // flags |= _O_TEXT; forceText = true; } // Exactly one of these options is required. if ((forceBinary && forceText) || (!forceBinary && !forceText)) { //LocalConfiguration config = Configuration.Local; //// checks whether default mode is applicable: //if (config.FileSystem.DefaultFileOpenMode == "b") //{ // forceBinary = true; //} //else if (config.FileSystem.DefaultFileOpenMode == "t") //{ // forceText = true; //} //else //{ // PhpException.Throw(PhpError.Warning, ErrResources.ambiguous_file_mode, mode); //} throw new NotImplementedException("Configuration.FileSystem.DefaultFileOpenMode"); // Binary mode is assumed } else if (forceText) { // Default mode is binary (unless the text mode is specified). accessOptions |= StreamAccessOptions.UseText; } // Store the two file-access flags into the access options too. accessOptions |= (StreamAccessOptions)fileAccess; return true; }