private bool SeekPositionForTail(IContentReader reader)
        {
            bool flag;
            FileSystemContentReaderWriter fileSystemContentReaderWriter = reader as FileSystemContentReaderWriter;

            try
            {
                fileSystemContentReaderWriter.SeekItemsBackward(this.Tail);
                flag = true;
            }
            catch (BackReaderEncodingNotSupportedException backReaderEncodingNotSupportedException)
            {
                fileSystemContentReaderWriter.Seek((long)0, SeekOrigin.Begin);
                flag = false;
            }
            return(flag);
        }
Exemple #2
0
 public IContentWriter GetContentWriter(string path)
 {
     if (string.IsNullOrEmpty(path))
     {
         throw PSTraceSource.NewArgumentException("path");
     }
     path = NormalizePath(path);
     bool usingByteEncoding = false;
     Encoding encodingType = Encoding.Default;
     FileMode openOrCreate = FileMode.OpenOrCreate;
     string streamName = null;
     if (base.DynamicParameters != null)
     {
         FileSystemContentWriterDynamicParameters dynamicParameters = base.DynamicParameters as FileSystemContentWriterDynamicParameters;
         if (dynamicParameters != null)
         {
             usingByteEncoding = dynamicParameters.UsingByteEncoding;
             if (dynamicParameters.WasStreamTypeSpecified)
             {
                 encodingType = dynamicParameters.EncodingType;
             }
             streamName = dynamicParameters.Stream;
         }
     }
     int index = path.IndexOf(':');
     int startIndex = path.IndexOf(':', index + 1);
     if (startIndex > 0)
     {
         streamName = path.Substring(startIndex + 1);
         path = path.Remove(startIndex);
     }
     FileSystemContentReaderWriter writer = null;
     try
     {
         writer = new FileSystemContentReaderWriter(path, streamName, openOrCreate, FileAccess.Write, FileShare.Write, encodingType, usingByteEncoding, false, this, false);
     }
     catch (PathTooLongException exception)
     {
         base.WriteError(new ErrorRecord(exception, "GetContentWriterPathTooLongError", ErrorCategory.InvalidArgument, path));
     }
     catch (FileNotFoundException exception2)
     {
         base.WriteError(new ErrorRecord(exception2, "GetContentWriterFileNotFoundError", ErrorCategory.ObjectNotFound, path));
     }
     catch (DirectoryNotFoundException exception3)
     {
         base.WriteError(new ErrorRecord(exception3, "GetContentWriterDirectoryNotFoundError", ErrorCategory.ObjectNotFound, path));
     }
     catch (ArgumentException exception4)
     {
         base.WriteError(new ErrorRecord(exception4, "GetContentWriterArgumentError", ErrorCategory.InvalidArgument, path));
     }
     catch (IOException exception5)
     {
         base.WriteError(new ErrorRecord(exception5, "GetContentWriterIOError", ErrorCategory.WriteError, path));
     }
     catch (SecurityException exception6)
     {
         base.WriteError(new ErrorRecord(exception6, "GetContentWriterSecurityError", ErrorCategory.PermissionDenied, path));
     }
     catch (UnauthorizedAccessException exception7)
     {
         base.WriteError(new ErrorRecord(exception7, "GetContentWriterUnauthorizedAccessError", ErrorCategory.PermissionDenied, path));
     }
     return writer;
 }
Exemple #3
0
 public IContentReader GetContentReader(string path)
 {
     if (string.IsNullOrEmpty(path))
     {
         throw PSTraceSource.NewArgumentException("path");
     }
     path = NormalizePath(path);
     string delimiter = "\n";
     Encoding encodingType = Encoding.Default;
     bool waitForChanges = false;
     bool usingByteEncoding = false;
     bool delimiterSpecified = false;
     bool isRawStream = false;
     string streamName = null;
     if (base.DynamicParameters != null)
     {
         FileSystemContentReaderDynamicParameters dynamicParameters = base.DynamicParameters as FileSystemContentReaderDynamicParameters;
         if (dynamicParameters != null)
         {
             this.ValidateParameters((bool) dynamicParameters.Raw);
             isRawStream = (bool) dynamicParameters.Raw;
             delimiterSpecified = dynamicParameters.DelimiterSpecified;
             if (delimiterSpecified)
             {
                 delimiter = dynamicParameters.Delimiter;
             }
             usingByteEncoding = dynamicParameters.UsingByteEncoding;
             if (dynamicParameters.WasStreamTypeSpecified)
             {
                 encodingType = dynamicParameters.EncodingType;
             }
             waitForChanges = (bool) dynamicParameters.Wait;
             streamName = dynamicParameters.Stream;
         }
     }
     int index = path.IndexOf(':');
     int startIndex = path.IndexOf(':', index + 1);
     if (startIndex > 0)
     {
         streamName = path.Substring(startIndex + 1);
         path = path.Remove(startIndex);
     }
     FileSystemContentReaderWriter writer = null;
     try
     {
         if (delimiterSpecified)
         {
             if (usingByteEncoding)
             {
                 Exception exception = new ArgumentException(FileSystemProviderStrings.DelimiterError, "delimiter");
                 base.WriteError(new ErrorRecord(exception, "GetContentReaderArgumentError", ErrorCategory.InvalidArgument, path));
                 return writer;
             }
             return new FileSystemContentReaderWriter(path, streamName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, delimiter, encodingType, waitForChanges, this, isRawStream);
         }
         writer = new FileSystemContentReaderWriter(path, streamName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, encodingType, usingByteEncoding, waitForChanges, this, isRawStream);
     }
     catch (PathTooLongException exception2)
     {
         base.WriteError(new ErrorRecord(exception2, "GetContentReaderPathTooLongError", ErrorCategory.InvalidArgument, path));
     }
     catch (FileNotFoundException exception3)
     {
         base.WriteError(new ErrorRecord(exception3, "GetContentReaderFileNotFoundError", ErrorCategory.ObjectNotFound, path));
     }
     catch (DirectoryNotFoundException exception4)
     {
         base.WriteError(new ErrorRecord(exception4, "GetContentReaderDirectoryNotFoundError", ErrorCategory.ObjectNotFound, path));
     }
     catch (ArgumentException exception5)
     {
         base.WriteError(new ErrorRecord(exception5, "GetContentReaderArgumentError", ErrorCategory.InvalidArgument, path));
     }
     catch (IOException exception6)
     {
         base.WriteError(new ErrorRecord(exception6, "GetContentReaderIOError", ErrorCategory.ReadError, path));
     }
     catch (SecurityException exception7)
     {
         base.WriteError(new ErrorRecord(exception7, "GetContentReaderSecurityError", ErrorCategory.PermissionDenied, path));
     }
     catch (UnauthorizedAccessException exception8)
     {
         base.WriteError(new ErrorRecord(exception8, "GetContentReaderUnauthorizedAccessError", ErrorCategory.PermissionDenied, path));
     }
     return writer;
 }
        private bool ScanForwardsForTail(ContentCommandBase.ContentHolder holder, CmdletProviderContext currentContext)
        {
            FileSystemContentReaderWriter reader = holder.Reader as FileSystemContentReaderWriter;
            Queue <object> objs        = new Queue <object>();
            IList          lists       = null;
            ErrorRecord    errorRecord = null;

            do
            {
                try
                {
                    lists = reader.ReadWithoutWaitingChanges(this.ReadCount);
                }
                catch (Exception exception1)
                {
                    Exception exception = exception1;
                    CommandsCommon.CheckForSevereException(this, exception);
                    ProviderInvocationException providerInvocationException = new ProviderInvocationException("ProviderContentReadError", SessionStateStrings.ProviderContentReadError, holder.PathInfo.Provider, holder.PathInfo.Path, exception);
                    MshLog.LogProviderHealthEvent(base.Context, holder.PathInfo.Provider.Name, providerInvocationException, Severity.Warning);
                    errorRecord = new ErrorRecord(providerInvocationException.ErrorRecord, providerInvocationException);
                    break;
                }
                if (lists == null || lists.Count <= 0)
                {
                    continue;
                }
                foreach (object obj in lists)
                {
                    if (objs.Count == this.Tail)
                    {
                        objs.Dequeue();
                    }
                    objs.Enqueue(obj);
                }
            }while (lists != null && lists.Count > 0);
            if (objs.Count > 0)
            {
                int count = 0;
                if (this.ReadCount <= (long)0 || this.ReadCount >= (long)objs.Count && this.ReadCount != (long)1)
                {
                    count = objs.Count;
                    ArrayList arrayLists = new ArrayList();
                    while (objs.Count > 0)
                    {
                        arrayLists.Add(objs.Dequeue());
                    }
                    base.WriteContentObject(arrayLists.ToArray(), (long)count, holder.PathInfo, currentContext);
                }
                else
                {
                    if (this.ReadCount != (long)1)
                    {
                        while ((long)objs.Count >= this.ReadCount)
                        {
                            ArrayList arrayLists1 = new ArrayList();
                            int       num         = 0;
                            while ((long)num < this.ReadCount)
                            {
                                arrayLists1.Add(objs.Dequeue());
                                num++;
                                count++;
                            }
                            base.WriteContentObject(arrayLists1.ToArray(), (long)count, holder.PathInfo, currentContext);
                        }
                        int count1 = objs.Count;
                        if (count1 > 0)
                        {
                            ArrayList arrayLists2 = new ArrayList();
                            while (count1 > 0)
                            {
                                arrayLists2.Add(objs.Dequeue());
                                count1--;
                                count++;
                            }
                            base.WriteContentObject(arrayLists2.ToArray(), (long)count, holder.PathInfo, currentContext);
                        }
                    }
                    else
                    {
                        while (objs.Count > 0)
                        {
                            int num1 = count;
                            count = num1 + 1;
                            base.WriteContentObject(objs.Dequeue(), (long)num1, holder.PathInfo, currentContext);
                        }
                    }
                }
            }
            if (errorRecord == null)
            {
                return(true);
            }
            else
            {
                base.WriteError(errorRecord);
                return(false);
            }
        }
        /// <summary>
        /// Creates an instance of the FileSystemContentStream class, opens
        /// the specified file for writing, and returns the IContentReader interface
        /// to it.
        /// </summary>
        /// 
        /// <param name="path">
        /// The path of the file to be opened for writing.
        /// </param>
        /// 
        /// <returns>
        /// An IContentWriter for the specified file.
        /// </returns>
        /// 
        /// <exception cref="System.ArgumentException">
        ///     path is null or empty.
        /// </exception>
        public IContentWriter GetContentWriter(string path)
        {
            if (String.IsNullOrEmpty(path))
            {
                throw PSTraceSource.NewArgumentException("path");
            }

            path = NormalizePath(path);

            // If this is true, then the content will be read as bytes
            bool usingByteEncoding = false;
            bool streamTypeSpecified = false;
            Encoding encoding = ClrFacade.GetDefaultEncoding();
            FileMode filemode = FileMode.OpenOrCreate;
            string streamName = null;
            bool suppressNewline = false;

            // Get the dynamic parameters

            if (DynamicParameters != null)
            {
                FileSystemContentWriterDynamicParameters dynParams =
                    DynamicParameters as FileSystemContentWriterDynamicParameters;

                if (dynParams != null)
                {
                    usingByteEncoding = dynParams.UsingByteEncoding;
                    streamTypeSpecified = dynParams.WasStreamTypeSpecified;

                    if (streamTypeSpecified)
                    {
                        encoding = dynParams.EncodingType;
                    }

                    streamName = dynParams.Stream;
                    suppressNewline = dynParams.NoNewline.IsPresent;
                } // dynParams != null
            }

            // See if they've used the inline stream syntax. They have more than one colon.
            int firstColon = path.IndexOf(':');
            int secondColon = path.IndexOf(':', firstColon + 1);
            if (secondColon > 0)
            {
                streamName = path.Substring(secondColon + 1);
                path = path.Remove(secondColon);
            }

            FileSystemContentReaderWriter stream = null;

            try
            {
                stream = new FileSystemContentReaderWriter(path, streamName, filemode, FileAccess.Write, FileShare.Write, encoding, usingByteEncoding, false, this, false, suppressNewline);
            }
            catch (PathTooLongException pathTooLong)
            {
                WriteError(new ErrorRecord(pathTooLong, "GetContentWriterPathTooLongError", ErrorCategory.InvalidArgument, path));
            }
            catch (FileNotFoundException fileNotFound)
            {
                WriteError(new ErrorRecord(fileNotFound, "GetContentWriterFileNotFoundError", ErrorCategory.ObjectNotFound, path));
            }
            catch (DirectoryNotFoundException directoryNotFound)
            {
                WriteError(new ErrorRecord(directoryNotFound, "GetContentWriterDirectoryNotFoundError", ErrorCategory.ObjectNotFound, path));
            }
            catch (ArgumentException argException)
            {
                WriteError(new ErrorRecord(argException, "GetContentWriterArgumentError", ErrorCategory.InvalidArgument, path));
            }
            catch (IOException ioException)
            {
                //IOException contains specific message about the error occured and so no need for errordetails.
                WriteError(new ErrorRecord(ioException, "GetContentWriterIOError", ErrorCategory.WriteError, path));
            }
            catch (System.Security.SecurityException securityException)
            {
                WriteError(new ErrorRecord(securityException, "GetContentWriterSecurityError", ErrorCategory.PermissionDenied, path));
            }
            catch (UnauthorizedAccessException unauthorizedAccess)
            {
                WriteError(new ErrorRecord(unauthorizedAccess, "GetContentWriterUnauthorizedAccessError", ErrorCategory.PermissionDenied, path));
            }

            return stream;
        } // GetContentWriter
        /// <summary>
        /// Creates an instance of the FileSystemContentStream class, opens
        /// the specified file for reading, and returns the IContentReader interface
        /// to it.
        /// </summary>
        /// 
        /// <param name="path">
        /// The path of the file to be opened for reading.
        /// </param>
        /// 
        /// <returns>
        /// An IContentReader for the specified file.
        /// </returns>
        /// 
        /// <exception cref="System.ArgumentException">
        ///     path is null or empty.
        /// </exception>
        public IContentReader GetContentReader(string path)
        {
            if (String.IsNullOrEmpty(path))
            {
                throw PSTraceSource.NewArgumentException("path");
            }

            path = NormalizePath(path);

            // Defaults for the file read operation
            string delimiter = "\n";
            Encoding encoding = ClrFacade.GetDefaultEncoding();
            bool waitForChanges = false;

            bool streamTypeSpecified = false;
            bool usingByteEncoding = false;
            bool delimiterSpecified = false;
            bool isRawStream = false;
            string streamName = null;

            // Get the dynamic parameters.
            // They override the defaults specified above.
            if (DynamicParameters != null)
            {
                FileSystemContentReaderDynamicParameters dynParams =
                    DynamicParameters as FileSystemContentReaderDynamicParameters;

                if (dynParams != null)
                {
                    // -raw is not allowed when -first,-last or -wait is specified
                    // this call will validate that and throws.
                    ValidateParameters(dynParams.Raw);

                    isRawStream = dynParams.Raw;

                    // Get the delimiter
                    delimiterSpecified = dynParams.DelimiterSpecified;
                    if (delimiterSpecified)
                        delimiter = dynParams.Delimiter;

                    // Get the stream type
                    usingByteEncoding = dynParams.UsingByteEncoding;
                    streamTypeSpecified = dynParams.WasStreamTypeSpecified;

                    if (streamTypeSpecified)
                    {
                        encoding = dynParams.EncodingType;
                    }

                    // Get the wait value
                    waitForChanges = dynParams.Wait;

                    // Get the stream name
                    streamName = dynParams.Stream;
                } // dynParams != null
            } // DynamicParameters != null

            // See if they've used the inline stream syntax. They have more than one colon.
            int firstColon = path.IndexOf(':');
            int secondColon = path.IndexOf(':', firstColon + 1);
            if (secondColon > 0)
            {
                streamName = path.Substring(secondColon + 1);
                path = path.Remove(secondColon);
            }

            FileSystemContentReaderWriter stream = null;

            try
            {
                // Users can't both read as bytes, and specify a delimiter
                if (delimiterSpecified)
                {
                    if (usingByteEncoding)
                    {
                        Exception e =
                            new ArgumentException(FileSystemProviderStrings.DelimiterError, "delimiter");
                        WriteError(new ErrorRecord(
                            e,
                            "GetContentReaderArgumentError",
                            ErrorCategory.InvalidArgument,
                            path));
                    }
                    else
                    {
                        // Initialize the file reader
                        stream = new FileSystemContentReaderWriter(path, streamName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, delimiter, encoding, waitForChanges, this, isRawStream);
                    }
                }
                else
                {
                    stream = new FileSystemContentReaderWriter(path, streamName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, encoding, usingByteEncoding, waitForChanges, this, isRawStream);
                }
            }
            catch (PathTooLongException pathTooLong)
            {
                WriteError(new ErrorRecord(pathTooLong, "GetContentReaderPathTooLongError", ErrorCategory.InvalidArgument, path));
            }
            catch (FileNotFoundException fileNotFound)
            {
                WriteError(new ErrorRecord(fileNotFound, "GetContentReaderFileNotFoundError", ErrorCategory.ObjectNotFound, path));
            }
            catch (DirectoryNotFoundException directoryNotFound)
            {
                WriteError(new ErrorRecord(directoryNotFound, "GetContentReaderDirectoryNotFoundError", ErrorCategory.ObjectNotFound, path));
            }
            catch (ArgumentException argException)
            {
                WriteError(new ErrorRecord(argException, "GetContentReaderArgumentError", ErrorCategory.InvalidArgument, path));
            }
            catch (IOException ioException)
            {
                //IOException contains specific message about the error occured and so no need for errordetails.
                WriteError(new ErrorRecord(ioException, "GetContentReaderIOError", ErrorCategory.ReadError, path));
            }
            catch (System.Security.SecurityException securityException)
            {
                WriteError(new ErrorRecord(securityException, "GetContentReaderSecurityError", ErrorCategory.PermissionDenied, path));
            }
            catch (UnauthorizedAccessException unauthorizedAccess)
            {
                WriteError(new ErrorRecord(unauthorizedAccess, "GetContentReaderUnauthorizedAccessError", ErrorCategory.PermissionDenied, path));
            }

            return stream;
        } // GetContentReader