Exemplo n.º 1
0
        /// <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));
        }
Exemplo n.º 2
0
        public override PhpStream Open(Context ctx, ref string path, string mode, StreamOpenOptions options, StreamContext context)
        {
            int level       = -1;
            var deflateMode = DeflateFilterMode.Normal;

            #region Parse mode options
            // PHP just looks whether there are mode flags in the mode string (skip the first character)
            // last flag is the valid one

            for (int i = 1; i < mode.Length; i++)
            {
                if (Char.IsDigit(mode[i]))
                {
                    level = mode[i] - '0';
                }
                else if (mode[i] == 'f')
                {
                    deflateMode = DeflateFilterMode.Filter;
                }
                else if (mode[i] == 'h')
                {
                    deflateMode = DeflateFilterMode.Huffman;
                }
            }

            #endregion

            #region Path correction

            if (path.StartsWith("compress.zlib://"))
            {
                path = path.Substring(16);
            }
            else if (path.StartsWith("zlib:"))
            {
                path = path.Substring(5);
            }

            #endregion

            var stream = PhpStream.Open(ctx, path, mode, options);

            if (stream != null && stream.CanRead)
            {
                stream.AddFilter(new GzipUncompressionFilter(), FilterChainOptions.Read);
            }

            if (stream != null && stream.CanWrite)
            {
                stream.AddFilter(new GzipCompresionFilter(level, deflateMode), FilterChainOptions.Write);
            }

            return(stream);
        }
Exemplo n.º 3
0
        public override PhpStream Open(ref string path, string mode, StreamOpenOptions options, StreamContext context)
        {
            int level = -1;
            DeflateFilterMode deflateMode = DeflateFilterMode.Normal;

            #region Parse mode options
            // PHP just looks whether there are mode flags in the mode string (skip the first character)
            // last flag is the valid one

            for (int i = 1; i < mode.Length; i++)
            {
                if (Char.IsDigit(mode[i]))
                {
                    level = mode[i] - '0';
                }
                else if (mode[i] == 'f')
                {
                    deflateMode = DeflateFilterMode.Filter;
                }
                else if (mode[i] == 'h')
                {
                    deflateMode = DeflateFilterMode.Huffman;
                }
            }

            #endregion

            #region Path correction

            if (path.StartsWith("compress.zlib://"))
            {
                path = path.Substring(16);
            }
            else if (path.StartsWith("zlib:"))
            {
                path = path.Substring(5);
            }

            #endregion

            PhpStream stream = PhpStream.Open(path, mode, options);

            if (stream != null && stream.CanRead)
            {
                stream.AddFilter(new GzipUncompressionFilter(), FilterChainOptions.Read);
            }

            if (stream != null && stream.CanWrite)
            {
                stream.AddFilter(new GzipCompresionFilter(level, deflateMode), FilterChainOptions.Write);
            }

            return stream;
        }
Exemplo n.º 4
0
        /// <include file='Doc/Wrappers.xml' path='docs/method[@name="Open"]/*'/>
        public override PhpStream Open(ref string path, string mode, StreamOpenOptions options, StreamContext context)
        {
            StreamAccessOptions accessOptions;

            if (!ParseMode(mode, options, out accessOptions))
            {
                return(null);
            }
            string          opened_path;
            IExternalStream stream = proxy.Open(path, mode, (int)options, out opened_path, null);

            path = opened_path;
            return(stream == null ? null :
                   new ExternalStream(stream, this, accessOptions, opened_path, context));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Prevents invalid options from the the options argument for StreamWrapper.Open().
        /// </summary>
        /// <param name="ctx">Runtime context.</param>
        /// <param name="flags">Flags passed to stream opening functions.</param>
        /// <returns>The StreamOpenFlags combination for the given arguments.</returns>
        static StreamOpenOptions ProcessOptions(Context ctx, FileOpenOptions flags)
        {
            StreamOpenOptions options = 0;

            if ((flags & FileOpenOptions.UseIncludePath) > 0)
            {
                options |= StreamOpenOptions.UseIncludePath;
            }

            if (!ctx.ErrorReportingDisabled)
            {
                options |= StreamOpenOptions.ReportErrors;
            }

            return(options);
        }
Exemplo n.º 6
0
			public override PhpStream Open(ref string path, string mode, StreamOpenOptions options, StreamContext context)
			{
				return null;
			}
Exemplo n.º 7
0
        public override PhpStream Open(Context ctx, ref string path, string mode, StreamOpenOptions options, StreamContext context)
        {
            Debug.Assert(path != null);
            //Debug.Assert(PhpPath.IsLocalFile(path));

            // Get the File.Open modes from the mode string
            FileMode            fileMode;
            FileAccess          fileAccess;
            StreamAccessOptions ao;

            if (!ParseMode(mode, options, out fileMode, out fileAccess, out ao))
            {
                return(null);
            }

            // Open the native stream
            FileStream stream = null;

            try
            {
                // stream = File.Open(path, fileMode, fileAccess, FileShare.ReadWrite);
                stream = new FileStream(path, fileMode, fileAccess, FileShare.ReadWrite | FileShare.Delete);
            }
            catch (FileNotFoundException)
            {
                // Note: There may still be an URL in the path here.
                PhpException.Throw(PhpError.Warning, ErrResources.stream_file_not_exists, FileSystemUtils.StripPassword(path));

                return(null);
            }
            catch (IOException e)
            {
                if ((ao & StreamAccessOptions.Exclusive) > 0)
                {
                    PhpException.Throw(PhpError.Warning, ErrResources.stream_file_exists, FileSystemUtils.StripPassword(path));
                }
                else
                {
                    PhpException.Throw(PhpError.Warning, ErrResources.stream_file_io_error, FileSystemUtils.StripPassword(path), PhpException.ToErrorMessage(e.Message));
                }
                return(null);
            }
            catch (UnauthorizedAccessException)
            {
                PhpException.Throw(PhpError.Warning, ErrResources.stream_file_access_denied, FileSystemUtils.StripPassword(path));
                return(null);
            }
            catch (Exception)
            {
                PhpException.Throw(PhpError.Warning, ErrResources.stream_file_invalid, FileSystemUtils.StripPassword(path));
                return(null);
            }

            if ((ao & StreamAccessOptions.SeekEnd) > 0)
            {
                // Read/Write Append is not supported. Seek to the end of file manually.
                stream.Seek(0, SeekOrigin.End);
            }

            if ((ao & StreamAccessOptions.Temporary) > 0)
            {
                // Set the file attributes to Temporary too.
                File.SetAttributes(path, FileAttributes.Temporary);
            }

            return(new NativeStream(ctx, stream, this, ao, path, context));
        }
Exemplo n.º 8
0
 public abstract PhpStream Open(Context ctx, ref string path, string mode, StreamOpenOptions options, StreamContext context);
Exemplo n.º 9
0
        /// <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));
        }
Exemplo n.º 10
0
        /// <summary>
        /// Openes a PhpStream using the appropriate StreamWrapper.
        /// </summary>
        /// <param name="ctx">Current runtime context.</param>
        /// <param name="path">URI or filename of the resource to be opened.</param>
        /// <param name="mode">A file-access mode as passed to the PHP function.</param>
        /// <param name="options">A combination of <see cref="StreamOpenOptions"/>.</param>
        /// <param name="context">A valid StreamContext. Must not be <c>null</c>.</param>
        /// <returns></returns>
        public static PhpStream Open(Context ctx, string path, string mode, StreamOpenOptions options, StreamContext context)
        {
            if (context == null)
                throw new ArgumentNullException("context");

            Debug.Assert(ctx != null);

            StreamWrapper wrapper;
            if (!PhpStream.ResolvePath(ctx, ref path, out wrapper, CheckAccessMode.FileMayExist, (CheckAccessOptions)options))
                return null;

            return wrapper.Open(ctx, ref path, mode, options, context);

        }
Exemplo n.º 11
0
        public override PhpStream Open(ref string path, string mode, StreamOpenOptions options, StreamContext context)
        {
            //From filestreamwrapper
            Debug.Assert(path != null);
            //Debug.Assert(PhpPath.IsLocalFile(path));

            // Get the File.Open modes from the mode string
            FileMode            fileMode;
            FileAccess          fileAccess;
            StreamAccessOptions ao;

            if (!ParseMode(mode, options, out fileMode, out fileAccess, out ao))
            {
                return(null);
            }

            string[] arr     = path.Split('#');
            string   archive = arr[0];
            string   entry   = arr[1];

            // Open the native stream
            ZipFile zip = null;

            try
            {
                // stream = File.Open(path, fileMode, fileAccess, FileShare.ReadWrite);
                zip = new ZipFile(File.Open(archive, FileMode.Open, FileAccess.Read, FileShare.Read));
            }
            catch (FileNotFoundException)
            {
                // Note: There may still be an URL in the path here.
                PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_not_exists",
                                                                             FileSystemUtils.StripPassword(path)));

                return(null);
            }
            catch (IOException e)
            {
                if ((ao & StreamAccessOptions.Exclusive) > 0)
                {
                    PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_exists",
                                                                                 FileSystemUtils.StripPassword(path)));
                }
                else
                {
                    PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_io_error",
                                                                                 FileSystemUtils.StripPassword(path), PhpException.ToErrorMessage(e.Message)));
                }
                return(null);
            }
            catch (UnauthorizedAccessException)
            {
                PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_access_denied",
                                                                             FileSystemUtils.StripPassword(path)));
                return(null);
            }
            catch (Exception)
            {
                PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_invalid",
                                                                             FileSystemUtils.StripPassword(path)));
                return(null);
            }

            if ((ao & StreamAccessOptions.SeekEnd) > 0)
            {
                throw new NotSupportedException();
            }

            if ((ao & StreamAccessOptions.Temporary) > 0)
            {
                // Set the file attributes to Temporary too.
                throw new NotSupportedException();
            }

            if (zip == null)
            {
                return(null);
            }
            ZipEntry zEntry = zip.GetEntry(entry);

            if (zEntry == null)
            {
                return(null);
            }
            Stream s = zip.GetInputStream(zEntry);

            return(new NativeStream(s, this, ao, path, context));
        }
Exemplo n.º 12
0
        public override PhpStream Open(ref string path, string mode, StreamOpenOptions options, StreamContext context)
        {
            //From filestreamwrapper
            Debug.Assert(path != null);
            //Debug.Assert(PhpPath.IsLocalFile(path));

            // Get the File.Open modes from the mode string
            FileMode fileMode;
            FileAccess fileAccess;
            StreamAccessOptions ao;

            if (!ParseMode(mode, options, out fileMode, out fileAccess, out ao)) return null;

            string[] arr = path.Split('#');
            string archive = arr[0];
            string entry = arr[1];

            // Open the native stream
            ZipFile zip = null;
            try
            {
                // stream = File.Open(path, fileMode, fileAccess, FileShare.ReadWrite);
                zip = new ZipFile(File.Open(archive, FileMode.Open, FileAccess.Read, FileShare.Read));
            }
            catch (FileNotFoundException)
            {
                // Note: There may still be an URL in the path here.
                PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_not_exists",
                    FileSystemUtils.StripPassword(path)));

                return null;
            }
            catch (IOException e)
            {
                if ((ao & StreamAccessOptions.Exclusive) > 0)
                {
                    PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_exists",
                        FileSystemUtils.StripPassword(path)));
                }
                else
                {
                    PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_io_error",
                        FileSystemUtils.StripPassword(path), PhpException.ToErrorMessage(e.Message)));
                }
                return null;
            }
            catch (UnauthorizedAccessException)
            {
                PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_access_denied",
                    FileSystemUtils.StripPassword(path)));
                return null;
            }
            catch (Exception)
            {
                PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_invalid",
                    FileSystemUtils.StripPassword(path)));
                return null;
            }

            if ((ao & StreamAccessOptions.SeekEnd) > 0)
            {
                throw new NotSupportedException();
            }

            if ((ao & StreamAccessOptions.Temporary) > 0)
            {
                // Set the file attributes to Temporary too.
                throw new NotSupportedException();
            }

            if (zip == null)
            {
                return null;
            }
            ZipEntry zEntry = zip.GetEntry(entry);
            if (zEntry == null)
            {
                return null;
            }
            Stream s = zip.GetInputStream(zEntry);
            return new NativeStream(s, this, ao, path, context);
        }
Exemplo n.º 13
0
            public override PhpStream Open(Context ctx, ref string path, string mode, StreamOpenOptions options, StreamContext context)
            {
                if (TryResolvePhar(ctx, path.AsSpan(), out var phar, out var entry))
                {
                    // Template: phar://alias/entryName
                    var resource = PharExtensions.GetResourceStream(phar, entry);

                    if (resource != null)
                    {
                        return(new NativeStream(ctx, resource, this, StreamAccessOptions.UseText | StreamAccessOptions.Read, path, context));
                    }
                    else
                    {
                        // TODO: entry not found
                    }
                }
Exemplo n.º 14
0
            public override PhpStream Open(Context ctx, ref string path, string mode, StreamOpenOptions options, StreamContext context)
            {
                if (FileSystemUtils.TryGetScheme(path, out var schemespan))
                {
                    var schemeends = schemespan.Length + 3;
                    var sep        = path.IndexOfAny(PathUtils.DirectorySeparatorChars, schemeends);
                    if (sep >= 0)
                    {
                        Stream resource = null;

                        var alias    = path.Substring(schemeends, sep - schemeends);
                        var pharFile = PharExtensions.AliasToPharFile(ctx, alias);
                        if (pharFile != null)
                        {
                            // Template: phar://alias/entryName
                            resource = PharExtensions.GetResourceStream(pharFile, path.Substring(sep + 1));
                        }
                        else
                        {
                            // Template: phar://path_phar_file/entryName
                            var pharExt = path.IndexOfOrdinal(PharExtensions.PharExtension, schemeends, path.Length - schemeends);
                            if (pharExt >= 0 && pharExt + PharExtensions.PharExtension.Length + 1 < path.Length)
                            {
                                // path_phar_file:
                                var pharPath = path.Substring(schemeends, pharExt + PharExtensions.PharExtension.Length - schemeends);

                                // entryName:
                                var entryName = path.Substring(pharExt + PharExtensions.PharExtension.Length + 1);

                                // ensure phar is loaded
                                // TODO: locate pharPath and get containing System.Reflection.Assembly
                                throw new NotImplementedException();
                            }
                        }

                        if (resource != null)
                        {
                            return(new NativeStream(ctx, resource, this, StreamAccessOptions.UseText | StreamAccessOptions.Read, path, context));
                        }
                    }
                }

                return(null);
            }
Exemplo n.º 15
0
 public abstract PhpStream Open(Context ctx, ref string path, string mode, StreamOpenOptions options, StreamContext context);
Exemplo n.º 16
0
 public override PhpStream Open(Context ctx, ref string path, string mode, StreamOpenOptions options, StreamContext context)
 {
     return(null);
 }
Exemplo n.º 17
0
 public static PhpStream Open(Context ctx, string path, string mode, StreamOpenOptions options)
 {
     return Open(ctx, path, mode, options, StreamContext.Default);
 }
Exemplo n.º 18
0
        /// <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);
        }
Exemplo n.º 19
0
		/// <include file='Doc/Wrappers.xml' path='docs/method[@name="Open"]/*'/>
		public override PhpStream Open(ref string path, string mode, StreamOpenOptions options, StreamContext context)
		{
			Debug.Assert(path != null);
			//Debug.Assert(PhpPath.IsLocalFile(path));

			// Get the File.Open modes from the mode string
			FileMode fileMode;
			FileAccess fileAccess;
			StreamAccessOptions ao;

			if (!ParseMode(mode, options, out fileMode, out fileAccess, out ao)) return null;

			// Open the native stream
			this.storageFile = IsolatedStorageFile.GetUserStoreForApplication();
			FileStream stream = null;
			try
			{
				stream = new IsolatedStorageFileStream(path, fileMode, fileAccess, FileShare.ReadWrite | FileShare.Delete, storageFile);
			}
			catch (FileNotFoundException)
			{
				// Note: There may still be an URL in the path here.
				PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_not_exists",
					FileSystemUtils.StripPassword(path)));
				return null;
			}
			catch (IOException e)
			{
				if ((ao & StreamAccessOptions.Exclusive) > 0)
				{
					PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_exists",
						FileSystemUtils.StripPassword(path)));
				}
				else
				{
					PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_io_error",
						FileSystemUtils.StripPassword(path), PhpException.ToErrorMessage(e.Message)));
				}
				return null;
			}
			catch (UnauthorizedAccessException)
			{
				PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_access_denied",
					FileSystemUtils.StripPassword(path)));
				return null;
			}
			catch (Exception)
			{
				PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_invalid",
					FileSystemUtils.StripPassword(path)));
				return null;
			}

			if ((ao & StreamAccessOptions.SeekEnd) > 0)
			{
				// Read/Write Append is not supported. Seek to the end of file manually.
				stream.Seek(0, SeekOrigin.End);
			}

			if ((ao & StreamAccessOptions.Temporary) > 0)
			{
				// Set the file attributes to Temporary too.
				File.SetAttributes(path, FileAttributes.Temporary);
			}

			return new NativeStream(stream, this, ao, path, context);
		}
Exemplo n.º 20
0
        /// <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;
        }