8000 Unify file encoding when a cmdlet creates a file by JamesWTruher · Pull Request #4119 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Unify file encoding when a cmdlet creates a file #4119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Changed PowerShellEncoding class name to EncodingUtils
made file encoding probe method internal
  • Loading branch information
JamesWTruher committed Aug 2, 2017
commit bf2be6fd369ceec7f3178b9649ee4ba5df09dae9
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ private void ProcessObjectContent(PSObject inputObject)
else if (obj is string)
{
string inputString = obj.ToString();
Encoding resolvedEncoding = PowerShellEncoding.GetEncoding(this, Encoding);
Encoding resolvedEncoding = EncodingUtils.GetEncoding(this, Encoding);
inputBytes = resolvedEncoding.GetBytes(inputString);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected override void BeginProcessing()
List<string> generatedFiles = GenerateProxyModule(
tempDirectory,
Path.GetFileName(directory.FullName),
PowerShellEncoding.GetEncoding(this, Encoding),
EncodingUtils.GetEncoding(this, Encoding),
_force,
listOfCommandMetadata,
alias2resolvedCommandName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1274,7 +1274,7 @@ protected override void BeginProcessing()
// Process encoding switch.
if (Encoding != FileEncoding.Unspecified )
{
_textEncoding = PowerShellEncoding.GetEncoding(this, Encoding);
_textEncoding = EncodingUtils.GetEncoding(this, Encoding);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ protected override void EndProcessing()
/// <summary>
/// To make it easier to specify -Encoding parameter, we add an ArgumentTransformationAttribute here.
/// When the input data is of type string and is valid to be converted to System.Text.Encoding
/// via PowerShellEncoding.GetEncoding(), we do the conversion and return the converted value.
/// via EncodingUtils.GetEncoding(), we do the conversion and return the converted value.
/// Otherwise, we just return the input data.
/// </summary>
internal sealed class ArgumentToEncodingNameTransformationAttribute : ArgumentTransformationAttribute
Expand All @@ -504,7 +504,7 @@ public override object Transform(EngineIntrinsics engineIntrinsics, object input
FileEncoding encoding;
if (LanguagePrimitives.TryConvertTo<FileEncoding>(inputData, out encoding))
{
return PowerShellEncoding.GetEncoding(encoding);
return EncodingUtils.GetEncoding(encoding);
}
return inputData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ private void ReadScriptContents()
{
using (FileStream readerStream = new FileStream(_path, FileMode.Open, FileAccess.Read))
{
Encoding defaultEncoding = PowerShellEncoding.GetDefaultEncoding();
Encoding defaultEncoding = EncodingUtils.GetDefaultEncoding();
Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = readerStream.SafeFileHandle;

using (StreamReader scriptReader = new StreamReader(readerStream, defaultEncoding))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ internal static string ReadScript(string path)
{
using (FileStream readerStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
Encoding defaultEncoding = PowerShellEncoding.GetDefaultEncoding();
Encoding defaultEncoding = EncodingUtils.GetDefaultEncoding();
Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = readerStream.SafeFileHandle;

using (StreamReader scriptReader = new StreamReader(readerStream, defaultEncoding))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1071,11 +1071,11 @@ internal string Path
_path = value;

Encoding = Encoding.UTF8;
FileEncoding fileEncoding = PowerShellEncoding.GetFileEncodingFromFile(value);
FileEncoding fileEncoding = EncodingUtils.GetFileEncodingFromFile(value);

if (fileEncoding != FileEncoding.Default)
{
Encoding = PowerShellEncoding.GetEncoding(fileEncoding);
Encoding = EncodingUtils.GetEncoding(fileEncoding);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2180,20 +2180,20 @@ internal static int StartSSHProcess(
{
Debug.Assert(stdinFd >= 0, "Invalid Fd");
standardInput = new StreamWriter(OpenStream(stdinFd, FileAccess.Write),
PowerShellEncoding.utf8NoBom, StreamBufferSize)
EncodingUtils.utf8NoBom, StreamBufferSize)
{ AutoFlush = true };
}
if (startInfo.RedirectStandardOutput)
{
Debug.Assert(stdoutFd >= 0, "Invalid Fd");
standardOutput = new StreamReader(OpenStream(stdoutFd, FileAccess.Read),
startInfo.StandardOutputEncoding ?? PowerShellEncoding.utf8NoBom, true, StreamBufferSize);
startInfo.StandardOutputEncoding ?? EncodingUtils.utf8NoBom, true, StreamBufferSize);
}
if (startInfo.RedirectStandardError)
{
Debug.Assert(stderrFd >= 0, "Invalid Fd");
standardError = new StreamReader(OpenStream(stderrFd, FileAccess.Read),
startInfo.StandardErrorEncoding ?? PowerShellEncoding.utf8NoBom, true, StreamBufferSize);
startInfo.StandardErrorEncoding ?? EncodingUtils.utf8NoBom, true, StreamBufferSize);
}

return childPid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ private bool ReadByteEncoded(bool waitChanges, ArrayList blocks, bool readBack)
// the changes
if (waitChanges)
{
WaitForChanges(_path, _mode, _access, _share, PowerShellEncoding.GetDefaultEncoding());
WaitForChanges(_path, _mode, _access, _share, EncodingUtils.GetDefaultEncoding());
byteRead = _stream.ReadByte();
}
}
Expand Down Expand Up @@ -1161,8 +1161,8 @@ internal FileStreamBackReader(FileStream fileStream, Encoding encoding)
_currentPosition = _stream.Position;

// Get the oem encoding and system current ANSI code page
_oemEncoding = PowerShellEncoding.GetEncoding(FileEncoding.Oem);
_defaultAnsiEncoding = PowerShellEncoding.GetEncoding(FileEncoding.Default);
_oemEncoding = EncodingUtils.GetEncoding(FileEncoding.Oem);
_defaultAnsiEncoding = EncodingUtils.GetEncoding(FileEncoding.Default);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6558,7 +6558,7 @@ public IContentReader GetContentReader(string path)

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

bool streamTypeSpecified = false;
Expand Down Expand Up @@ -6593,7 +6593,7 @@ public IContentReader GetContentReader(string path)

if (streamTypeSpecified)
{
encoding = PowerShellEncoding.GetProviderEncoding(this, dynParams.Encoding);
encoding = EncodingUtils.GetProviderEncoding(this, dynParams.Encoding);
}

// Get the wait value
Expand Down Expand Up @@ -6722,7 +6722,7 @@ public IContentWriter GetContentWriter(string path)
bool usingByteEncoding = false;
bool streamTypeSpecified = false;
// we need to discover the encoding
Encoding encoding = PowerShellEncoding.GetProviderEncoding(this, FileEncoding.Unspecified);
Encoding encoding = EncodingUtils.GetProviderEncoding(this, FileEncoding.Unspecified);
FileMode filemode = FileMode.OpenOrCreate;
string streamName = null;
bool suppressNewline = false;
Expand All @@ -6741,7 +6741,7 @@ public IContentWriter GetContentWriter(string path)

if (streamTypeSpecified)
{
encoding = PowerShellEncoding.GetProviderEncoding(this, dynParams.Encoding);
encoding = EncodingUtils.GetProviderEncoding(this, dynParams.Encoding);
}

streamName = dynParams.Stream;
Expand Down
8 changes: 4 additions & 4 deletions src/System.Management.Automation/utils/ClrFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ internal static Encoding GetDefaultEncoding()
EncodingRegisterProvider();

uint currentAnsiCp = NativeMethods.GetACP();
s_defaultEncoding = Encoding.GetEncoding((int)currentAnsiCp);
s_defaultEncoding = EncodingUtils.GetDefaultEncoding();
#endif
}
return s_defaultEncoding;
Expand All @@ -136,11 +136,11 @@ internal static Encoding GetOEMEncoding()
{
#if UNIX // PowerShell Core on Unix
s_oemEncoding = GetDefaultEncoding();
#else // PowerShell Core on Windows
#else // PowerShell Core on Windows, which needs provider registration
EncodingRegisterProvider();

uint oemCp = NativeMethods.GetOEMCP();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this line is no longer needed.

s_oemEncoding = Encoding.GetEncoding((int)oemCp);
s_oemEncoding = EncodingUtils.GetDefaultEncoding();
#endif
}
return s_oemEncoding;
Expand Down Expand Up @@ -275,7 +275,7 @@ private static SecurityZone ReadFromZoneIdentifierDataStream(string filePath)
FileAccess.Read, FileShare.Read);

// If we successfully get the zone data stream, try to read the ZoneId information
// use the method in this class not PowerShellEncoding.
// use the method in this class not EncodingUtils.
using (StreamReader zoneDataReader = new StreamReader(zoneDataSteam, GetDefaultEncoding()))
{
string line = null;
Expand Down
4 changes: 2 additions & 2 deletions src/System.Management.Automation/utils/Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public enum FileEncoding
/// <summary>
/// the helper class for determining encodings for PowerShell
/// </summary>
public static class PowerShellEncoding
public static class EncodingUtils
{

/// <summary>
Expand Down Expand Up @@ -232,7 +232,7 @@ public static Encoding GetEncoding(Cmdlet cmdlet, FileEncoding encoding)
/// <param name="path">The path to a file to inspect for an encoding</param>
/// <returns>System.Text.Encoding</returns>
/// </summary>
public static FileEncoding GetFileEncodingFromFile(string path)
internal static FileEncoding GetFileEncodingFromFile(string path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this should be GetEncodingFromFile to keep the naming consistent

{
if (!File.Exists(path))
{
Expand Down
4 changes: 2 additions & 2 deletions src/System.Management.Automation/utils/PathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal static void MasterStreamOpen(
bool isLiteralPath
)
{
Encoding resolvedEncoding = PowerShellEncoding.GetEncoding(cmdlet, encoding);
Encoding resolvedEncoding = EncodingUtils.GetEncoding(cmdlet, encoding);

MasterStreamOpen(cmdlet, filePath, resolvedEncoding, defaultEncoding, Append, Force, NoClobber, out fileStream, out streamWriter, out readOnlyFileInfo, isLiteralPath);
}
Expand Down Expand Up @@ -193,7 +193,7 @@ internal static void ReportFileOpenFailure(Cmdlet cmdlet, string filePath, Excep
internal static StreamReader OpenStreamReader(PSCmdlet command, string filePath, FileEncoding encoding, bool isLiteralPath)
{
FileStream fileStream = OpenFileStream(filePath, command, isLiteralPath);
return new StreamReader(fileStream, PowerShellEncoding.GetEncoding(command, encoding));
return new StreamReader(fileStream, EncodingUtils.GetEncoding(command, encoding));
}

internal static FileStream OpenFileStream(string filePath, PSCmdlet command, bool isLiteralPath)
Expand Down
0