Exemple #1
0
        protected ItemContentReaderWriterBase(CmdletProvider provider, Item item,
                                              FileSystemCmdletProviderEncoding encoding, bool raw)
        {
            Item     = item;
            Provider = provider;
            Raw      = raw;

            if (encoding == FileSystemCmdletProviderEncoding.Unknown)
            {
                encoding = FileSystemCmdletProviderEncoding.Byte;
            }
            Encoding = new FileSystemContentWriterDynamicParameters()
            {
                Encoding = encoding
            }.EncodingType;
        }
Exemple #2
0
        public ContentReaderWriterBase(CacheNode item, ContentReaderWriterDynamicParametersBase parameters, FtpClient client)
        {
            Item = item;

            Client = client;

                        #if NETCOREAPP
            Encoding = parameters?.Encoding;
                        #else
            var encoding = parameters?.Encoding ?? FileSystemCmdletProviderEncoding.Byte;

            if (encoding != FileSystemCmdletProviderEncoding.Byte)
            {
                Encoding = new FileSystemContentWriterDynamicParameters()
                {
                    Encoding = encoding
                }.EncodingType;
            }
                        #endif
        }
        /// <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;
            bool     suppressNewline     = false;

            // Get the dynamic parameters
            if (DynamicParameters != null)
            {
                // [BUG] Regardless of override DynamicParameters is of type FileSystemContentWriterDynamicParameters
                // StreamContentWriterDynamicParameters dynParams = DynamicParameters as StreamContentWriterDynamicParameters;
                FileSystemContentWriterDynamicParameters dynParams = DynamicParameters as FileSystemContentWriterDynamicParameters;

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

                    if (usingByteEncoding && streamTypeSpecified)
                    {
                        WriteWarning(FileSystemProviderStrings.EncodingNotUsed);
                    }

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

                    suppressNewline = dynParams.NoNewline.IsPresent;
                }
            }

            StreamContentReaderWriter stream = null;

            ZipFileItemInfo archiveFile;

            if (ItemExists(path))
            {
                archiveFile = GetItemHelper(path);
            }
            else
            {
                // Set-Item should create an item if not exists.
                archiveFile = NewItemHelper(path);
            }

            try
            {
                stream = new ZipFileContentStream(archiveFile, FileMode.Append, encoding, usingByteEncoding, 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);
        }