Exemplo n.º 1
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.º 2
0
        /// <include file='Doc/Wrappers.xml' path='docs/method[@name="Unlink"]/*'/>
        public override bool Unlink(string path, StreamUnlinkOptions options, StreamContext context)
        {
            Debug.Assert(path != null);
            Debug.Assert(Path.IsPathRooted(path));

            try
            {
                File.Delete(path);
                return(true);
            }
            catch (DirectoryNotFoundException)
            {
                PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_unlink_file_not_found",
                                                                             FileSystemUtils.StripPassword(path)));
            }
            catch (UnauthorizedAccessException)
            {
                PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_file_access_denied",
                                                                             FileSystemUtils.StripPassword(path)));
            }
            catch (IOException e)
            {
                PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_unlink_io_error",
                                                                             FileSystemUtils.StripPassword(path), PhpException.ToErrorMessage(e.Message)));
            }
            catch (Exception)
            {
                PhpException.Throw(PhpError.Warning, CoreResources.GetString("stream_unlink_error",
                                                                             FileSystemUtils.StripPassword(path)));
            }

            return(false);
        }