Skip to content
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

Add ability to provide custom stream file names #751

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 17 additions & 15 deletions Mono.Cecil.Cil/PortablePdb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName)
return GetSymbolReader (module, Disposable.Owned (file as Stream), file.Name);
}

public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream)
public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream, string symbolFileName)
{
Mixin.CheckModule (module);
Mixin.CheckStream (symbolStream);

return GetSymbolReader (module, Disposable.NotOwned (symbolStream), symbolStream.GetFileName ());
return GetSymbolReader (module, Disposable.NotOwned (symbolStream), symbolStream.GetFileName (symbolFileName));
}

ISymbolReader GetSymbolReader (ModuleDefinition module, Disposable<Stream> symbolStream, string fileName)
Expand Down Expand Up @@ -163,7 +163,7 @@ public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName)
throw new InvalidOperationException ();

return new EmbeddedPortablePdbReader (
(PortablePdbReader) new PortablePdbReaderProvider ().GetSymbolReader (module, GetPortablePdbStream (entry)));
(PortablePdbReader) new PortablePdbReaderProvider ().GetSymbolReader (module, GetPortablePdbStream (entry), fileName));
}

static Stream GetPortablePdbStream (ImageDebugHeaderEntry entry)
Expand All @@ -180,7 +180,7 @@ static Stream GetPortablePdbStream (ImageDebugHeaderEntry entry)
return decompressed_stream;
}

public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream)
public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream, string symbolFileName)
{
throw new NotSupportedException ();
}
Expand Down Expand Up @@ -227,23 +227,23 @@ public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName)
Mixin.CheckFileName (fileName);

var file = File.OpenWrite (Mixin.GetPdbFileName (fileName));
return GetSymbolWriter (module, Disposable.Owned (file as Stream));
return GetSymbolWriter (module, Disposable.Owned (file as Stream), file.Name);
}

public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream)
public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream, string symbolFileName)
{
Mixin.CheckModule (module);
Mixin.CheckStream (symbolStream);

return GetSymbolWriter (module, Disposable.NotOwned (symbolStream));
return GetSymbolWriter (module, Disposable.NotOwned (symbolStream), symbolFileName);
}

ISymbolWriter GetSymbolWriter (ModuleDefinition module, Disposable<Stream> stream)
ISymbolWriter GetSymbolWriter (ModuleDefinition module, Disposable<Stream> stream, string fileName)
{
var metadata = new MetadataBuilder (module, this);
var writer = ImageWriter.CreateDebugWriter (module, metadata, stream);

return new PortablePdbWriter (metadata, module, writer);
return new PortablePdbWriter (metadata, module, writer, fileName);
}
}

Expand All @@ -252,15 +252,17 @@ public sealed class PortablePdbWriter : ISymbolWriter {
readonly MetadataBuilder pdb_metadata;
readonly ModuleDefinition module;
readonly ImageWriter writer;
readonly string file_name;

MetadataBuilder module_metadata;

bool IsEmbedded { get { return writer == null; } }

internal PortablePdbWriter (MetadataBuilder pdb_metadata, ModuleDefinition module)
internal PortablePdbWriter (MetadataBuilder pdb_metadata, ModuleDefinition module, string fileName)
{
this.pdb_metadata = pdb_metadata;
this.module = module;
this.file_name = fileName;

this.module_metadata = module.metadata_builder;

Expand All @@ -270,8 +272,8 @@ internal PortablePdbWriter (MetadataBuilder pdb_metadata, ModuleDefinition modul
pdb_metadata.AddCustomDebugInformations (module);
}

internal PortablePdbWriter (MetadataBuilder pdb_metadata, ModuleDefinition module, ImageWriter writer)
: this (pdb_metadata, module)
internal PortablePdbWriter (MetadataBuilder pdb_metadata, ModuleDefinition module, ImageWriter writer, string fileName)
: this (pdb_metadata, module, fileName)
{
this.writer = writer;
}
Expand Down Expand Up @@ -301,7 +303,7 @@ public ImageDebugHeader GetDebugHeader ()
// PDB Age
buffer.WriteUInt32 (1);
// PDB Path
var fileName = writer.BaseStream.GetFileName ();
var fileName = writer.BaseStream.GetFileName (file_name);
if (string.IsNullOrEmpty (fileName)) {
fileName = module.Assembly.Name.Name + ".pdb";
}
Expand Down Expand Up @@ -401,11 +403,11 @@ public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName)
Mixin.CheckFileName (fileName);

var stream = new MemoryStream ();
var pdb_writer = (PortablePdbWriter) new PortablePdbWriterProvider ().GetSymbolWriter (module, stream);
var pdb_writer = (PortablePdbWriter) new PortablePdbWriterProvider ().GetSymbolWriter (module, stream, fileName);
return new EmbeddedPortablePdbWriter (stream, pdb_writer);
}

public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream)
public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream, string symbolFileName)
{
throw new NotSupportedException ();
}
Expand Down
14 changes: 7 additions & 7 deletions Mono.Cecil.Cil/Symbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ public interface ISymbolReader : IDisposable {

public interface ISymbolReaderProvider {
ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName);
ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream);
ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream, string symbolFileName);
}

#if !NET_CORE
Expand Down Expand Up @@ -903,7 +903,7 @@ public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName)
return null;
}

public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream)
public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream, string symbolFileName)
{
if (module.Image.HasDebugTables ())
return null;
Expand All @@ -927,7 +927,7 @@ public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStre
symbolStream.Position = position;

if (intHeader == portablePdbHeader) {
return new PortablePdbReaderProvider ().GetSymbolReader (module, symbolStream);
return new PortablePdbReaderProvider ().GetSymbolReader (module, symbolStream, symbolFileName);
}

const string nativePdbHeader = "Microsoft C/C++ MSF 7.00";
Expand All @@ -945,7 +945,7 @@ public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStre

if (isNativePdb) {
try {
return SymbolProvider.GetReaderProvider (SymbolKind.NativePdb).GetSymbolReader (module, symbolStream);
return SymbolProvider.GetReaderProvider (SymbolKind.NativePdb).GetSymbolReader (module, symbolStream, symbolFileName);
} catch (Exception) {
// We might not include support for native pdbs.
}
Expand All @@ -958,7 +958,7 @@ public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStre

if (longHeader == mdbHeader) {
try {
return SymbolProvider.GetReaderProvider (SymbolKind.Mdb).GetSymbolReader (module, symbolStream);
return SymbolProvider.GetReaderProvider (SymbolKind.Mdb).GetSymbolReader (module, symbolStream, symbolFileName);
} catch (Exception) {
// We might not include support for mdbs.
}
Expand Down Expand Up @@ -1070,7 +1070,7 @@ public interface ISymbolWriter : IDisposable {
public interface ISymbolWriterProvider {

ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName);
ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream);
ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream, string symbolFileName);
}

public class DefaultSymbolWriterProvider : ISymbolWriterProvider {
Expand All @@ -1087,7 +1087,7 @@ public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName)
return reader.GetWriterProvider ().GetSymbolWriter (module, fileName);
}

public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream)
public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream, string symbolFileName)
{
throw new NotSupportedException ();
}
Expand Down
4 changes: 2 additions & 2 deletions Mono.Cecil.PE/ImageReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ public static Image ReadImage (Disposable<Stream> stream, string file_name)
reader.ReadImage ();
return reader.image;
} catch (EndOfStreamException e) {
throw new BadImageFormatException (stream.value.GetFileName (), e);
throw new BadImageFormatException (file_name, e);
}
}

Expand All @@ -786,7 +786,7 @@ public static Image ReadPortablePdb (Disposable<Stream> stream, string file_name
reader.ReadMetadata ();
return reader.image;
} catch (EndOfStreamException e) {
throw new BadImageFormatException (stream.value.GetFileName (), e);
throw new BadImageFormatException (file_name, e);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Mono.Cecil/AssemblyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static void ReadSymbols (ModuleDefinition module, ReaderParameters parameters)
module.SymbolReaderProvider = symbol_reader_provider;

var reader = parameters.SymbolStream != null
? symbol_reader_provider.GetSymbolReader (module, parameters.SymbolStream)
? symbol_reader_provider.GetSymbolReader (module, parameters.SymbolStream, parameters.SymbolFileName)
: symbol_reader_provider.GetSymbolReader (module, module.FileName);

if (reader != null) {
Expand Down
6 changes: 3 additions & 3 deletions Mono.Cecil/AssemblyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static void Write (ModuleDefinition module, Disposable<Stream> stream, WriterPar
module.symbol_reader.Dispose ();

var name = module.assembly != null && module.kind != ModuleKind.NetModule ? module.assembly.Name : null;
var fq_name = stream.value.GetFileName ();
var fq_name = stream.value.GetFileName (parameters.FileName);
var timestamp = parameters.Timestamp ?? module.timestamp;
var symbol_writer_provider = parameters.SymbolWriterProvider;

Expand Down Expand Up @@ -152,7 +152,7 @@ static ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fq_name, I
return null;

if (parameters.SymbolStream != null)
return symbol_writer_provider.GetSymbolWriter (module, parameters.SymbolStream);
return symbol_writer_provider.GetSymbolWriter (module, parameters.SymbolStream, parameters.SymbolFileName);

return symbol_writer_provider.GetSymbolWriter (module, fq_name);
}
Expand Down Expand Up @@ -956,7 +956,7 @@ public void SetSymbolWriter (ISymbolWriter writer)
symbol_writer = writer;

if (symbol_writer == null && module.HasImage && module.Image.HasDebugTables ())
symbol_writer = new PortablePdbWriter (this, module);
symbol_writer = new PortablePdbWriter (this, module, "");
}

TextMap CreateTextMap ()
Expand Down
31 changes: 29 additions & 2 deletions Mono.Cecil/ModuleDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,26 @@ public enum ReadingMode {

public sealed class ReaderParameters {

string file_name;
ReadingMode reading_mode;
internal IAssemblyResolver assembly_resolver;
internal IMetadataResolver metadata_resolver;
internal IMetadataImporterProvider metadata_importer_provider;
internal IReflectionImporterProvider reflection_importer_provider;
Stream symbol_stream;
string symbol_file_name;
ISymbolReaderProvider symbol_reader_provider;
bool read_symbols;
bool throw_symbols_mismatch;
bool projections;
bool in_memory;
bool read_write;

public string FileName {
get { return file_name; }
set { file_name = value; }
}

public ReadingMode ReadingMode {
get { return reading_mode; }
set { reading_mode = value; }
Expand Down Expand Up @@ -76,6 +83,11 @@ public Stream SymbolStream {
set { symbol_stream = value; }
}

public string SymbolFileName {
get { return symbol_file_name; }
set { symbol_file_name = value; }
}

public ISymbolReaderProvider SymbolReaderProvider {
get { return symbol_reader_provider; }
set { symbol_reader_provider = value; }
Expand Down Expand Up @@ -179,14 +191,21 @@ static TargetRuntime GetCurrentRuntime ()

public sealed class WriterParameters {

string file_name;
uint? timestamp;
Stream symbol_stream;
string symbol_file_name;
ISymbolWriterProvider symbol_writer_provider;
bool write_symbols;
byte [] key_blob;
string key_container;
SR.StrongNameKeyPair key_pair;

public string FileName {
get { return file_name; }
set { file_name = value; }
}

public uint? Timestamp {
get { return timestamp; }
set { timestamp = value; }
Expand All @@ -197,6 +216,11 @@ public Stream SymbolStream {
set { symbol_stream = value; }
}

public string SymbolFileName {
get { return symbol_file_name; }
set { symbol_file_name = value; }
}

public ISymbolWriterProvider SymbolWriterProvider {
get { return symbol_writer_provider; }
set { symbol_writer_provider = value; }
Expand Down Expand Up @@ -1131,7 +1155,7 @@ public static ModuleDefinition ReadModule (Stream stream, ReaderParameters param
Mixin.CheckStream (stream);
Mixin.CheckReadSeek (stream);

return ReadModule (Disposable.NotOwned (stream), stream.GetFileName (), parameters);
return ReadModule (Disposable.NotOwned (stream), stream.GetFileName (parameters.FileName), parameters);
}

static ModuleDefinition ReadModule (Disposable<Stream> stream, string fileName, ReaderParameters parameters)
Expand Down Expand Up @@ -1287,8 +1311,11 @@ public static bool HasImage (this ModuleDefinition self)
return self != null && self.HasImage;
}

public static string GetFileName (this Stream self)
public static string GetFileName (this Stream self, string file_name)
{
if (!string.IsNullOrEmpty (file_name))
return file_name;

var file_stream = self as FileStream;
if (file_stream == null)
return string.Empty;
Expand Down
4 changes: 2 additions & 2 deletions Test/Mono.Cecil.Tests/PortablePdbTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,9 @@ public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName)
return new SymbolWriter (writer_provider.GetSymbolWriter (module, fileName));
}

public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream)
public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream, string symbolFileName)
{
return new SymbolWriter (writer_provider.GetSymbolWriter (module, symbolStream));
return new SymbolWriter (writer_provider.GetSymbolWriter (module, symbolStream, symbolFileName));
}
}

Expand Down
2 changes: 1 addition & 1 deletion symbols/mdb/Mono.Cecil.Mdb/MdbReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName)
return new MdbReader (module, MonoSymbolFile.ReadSymbolFile (Mixin.GetMdbFileName (fileName)));
}

public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream)
public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream, string symbolFileName)
{
Mixin.CheckModule (module);
Mixin.CheckStream (symbolStream);
Expand Down
2 changes: 1 addition & 1 deletion symbols/mdb/Mono.Cecil.Mdb/MdbWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ISymbolWriter GetSymbolWriter (ModuleDefinition module, string fileName)
return new MdbWriter (module, fileName);
}

public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream)
public ISymbolWriter GetSymbolWriter (ModuleDefinition module, Stream symbolStream, string symbolFileName)
{
throw new NotImplementedException ();
}
Expand Down
Loading