Bug 34414 - System.IO.Packaging problem with readonly files
Summary: System.IO.Packaging problem with readonly files
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: WindowsBase (show other bugs)
Version: 4.0.0
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-09-29 16:53 UTC by Maury Markowitz
Modified: 2015-09-29 16:56 UTC (History)
1 user (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description Maury Markowitz 2015-09-29 16:53:08 UTC
My code reads XLSX files using System.IO.Packaging: 

    FStream = new FileStream(ConStr, FileMode.Open, FileAccess.Read, FileShare.Read);
    using (Package xlPackage = Package.Open(FStream, FileMode.Open, FileAccess.Read)) {
	foreach (PackageRelationship PackRel in xlPackage.GetRelationshipsByType(cXl07WorkbookRelSchema)) {
            WBPart = xlPackage.GetPart(PackUriHelper.ResolvePartUri(PackRel.SourceUri, PackRel.TargetUri));
	    RD = new XmlTextReader(WBPart.GetStream());
	    while (RD.Read()) {
//look for names of the worksheets inside
            }
         }

         foreach (PackageRelationship PackRel in WBPart.GetRelationshipsByType(cXl07SharedStringsRelSchema)) {
            ...
         }

The purpose of the first loop is to find the WBPart and then read its tags looking for sheets. If we don't find the WBPart or there's no sheets, we fail out as the file is malformed. Then we loop again, this time looking in WBPart for various other things, in this case the sharedstrings.

This code runs fine on the PC under .Net 4.5.x/VS2015. On Xamarin/Mac it fails at the second instance of GetRelationshipsByType if the file is read-only. Changing the stream and Package.Open to ReadWrite fixed the problem. As this instruction should certainly be available for read-only files, I am filing this as a bug.

The confusing part here is that the first call to GetRelationshipsByType worked fine, it was only as part of WBPart that there was a problem. 

I have /Library/Frameworks/Mono.framework/Versions/4.0.4/lib/mono/gac/WindowsBase/4.0.0.0__31bf3856ad364e35/WindowsBase.dll

The stack trace is:

  at System.IO.Packaging.Package.CheckIsReadOnly () [0x0000c] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr4/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.4/mcs/class/WindowsBase/System.IO.Packaging/Package.cs:113 
  at System.IO.Packaging.PackagePart.CreateRelationship (System.Uri targetUri, TargetMode targetMode, System.String relationshipType, System.String id, Boolean loading) [0x00000] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr4/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.4/mcs/class/WindowsBase/System.IO.Packaging/PackagePart.cs:128 
  at System.IO.Packaging.PackagePart.LoadRelationships (System.Collections.Generic.Dictionary`2 relationships, System.IO.Stream stream) [0x0008f] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr4/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.4/mcs/class/WindowsBase/System.IO.Packaging/PackagePart.cs:168 
  at System.IO.Packaging.PackagePart.get_Relationships () [0x00048] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr4/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.4/mcs/class/WindowsBase/System.IO.Packaging/PackagePart.cs:50 
  at System.IO.Packaging.PackagePart.GetRelationshipsByType (System.String relationshipType) [0x0000c] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr4/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.4/mcs/class/WindowsBase/System.IO.Packaging/PackagePart.cs:200 
  at DtaProject.ReadProjectFromStream (System.IO.Stream pDBStream) [0x00218] in /PC/ClassLibrary1/ClassLibrary1/DBData.cs:1538

Note You need to log in before you can comment on or make changes to this bug.