[banshee] [Banshee.Gio] Fix some issues/add tests for Demux



commit 5d11d8ff3271614c82d1233c60a1a33c94647704
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Thu Nov 12 13:41:46 2009 -0800

    [Banshee.Gio] Fix some issues/add tests for Demux

 .../Banshee.Gio/Banshee.IO.Gio/DemuxVfs.cs         |   21 +++++--
 src/Backends/Banshee.Gio/Banshee.IO.Gio/Tests.cs   |   59 ++++++++++++++++++++
 2 files changed, 75 insertions(+), 5 deletions(-)
---
diff --git a/src/Backends/Banshee.Gio/Banshee.IO.Gio/DemuxVfs.cs b/src/Backends/Banshee.Gio/Banshee.IO.Gio/DemuxVfs.cs
index 2d27927..4cf9469 100644
--- a/src/Backends/Banshee.Gio/Banshee.IO.Gio/DemuxVfs.cs
+++ b/src/Backends/Banshee.Gio/Banshee.IO.Gio/DemuxVfs.cs
@@ -37,11 +37,16 @@ namespace Banshee.IO.Gio
     {
         private GLib.File file;
         private GLib.FileInfo file_info;
+        private string path;
 
         public DemuxVfs (string path)
         {
-            file = FileFactory.NewForPath (path);
-            file_info = file.QueryInfo ("access::can-read,access::can-write", FileQueryInfoFlags.None, null);
+            this.path = path;
+            file = path.StartsWith ("/") ? FileFactory.NewForPath (path) : FileFactory.NewForUri (path);
+
+            if (file.Exists) {
+                file_info = file.QueryInfo ("etag::value,access::can-read,access::can-write", FileQueryInfoFlags.None, null);
+            }
         }
 
         public void CloseStream (System.IO.Stream stream)
@@ -58,15 +63,21 @@ namespace Banshee.IO.Gio
         }
 
         public System.IO.Stream WriteStream {
-            get { return new GioStream (file.Create (FileCreateFlags.None, null)); }
+            // FIXME we really need GFileIOStream here, but that depends on glib 2.22 (and a binding for it in gio#)
+            // as-is, this stream is write-only (not readable) which breaks taglib-sharp
+            get { return new GioStream (file.Exists
+                    ? file.Replace (file_info.Etag, false, FileCreateFlags.None, null)
+                    : file.Create (FileCreateFlags.None, null)
+                );
+            }
         }
 
         public bool IsReadable {
-            get { return file_info.GetAttributeBoolean ("access::can-read"); }
+            get { return file_info == null ? true : file_info.GetAttributeBoolean ("access::can-read"); }
         }
 
         public bool IsWritable {
-            get { return file_info.GetAttributeBoolean ("access::can-write"); }
+            get { return file_info == null ? true : file_info.GetAttributeBoolean ("access::can-write"); }
         }
     }
 }
diff --git a/src/Backends/Banshee.Gio/Banshee.IO.Gio/Tests.cs b/src/Backends/Banshee.Gio/Banshee.IO.Gio/Tests.cs
index 4ef9c85..8becf44 100644
--- a/src/Backends/Banshee.Gio/Banshee.IO.Gio/Tests.cs
+++ b/src/Backends/Banshee.Gio/Banshee.IO.Gio/Tests.cs
@@ -128,6 +128,65 @@ namespace Banshee.IO.Gio
         }
 
         [Test]
+        public void DemuxCreateFile ()
+        {
+            var newf = Uri ("newfile");
+            var newp = Path ("newfile");
+            Assert.IsFalse (file.Exists (newf));
+
+            var demux = new DemuxVfs (newp);
+            Assert.IsTrue (demux.IsWritable);
+            Assert.IsTrue (demux.IsReadable);
+
+            var stream = demux.WriteStream;
+            Assert.IsTrue (stream.CanWrite);
+            stream.WriteByte (0xAB);
+            demux.CloseStream (stream);
+
+            Assert.IsTrue (file.Exists (newf));
+        }
+
+        [Test]
+        public void DemuxOverwriteFile ()
+        {
+            Assert.IsTrue (file.Exists (foo));
+            Assert.AreEqual (3, file.GetSize (foo));
+
+            var demux = new DemuxVfs (foo.AbsoluteUri);
+            Assert.IsTrue (demux.IsWritable);
+            Assert.IsTrue (demux.IsReadable);
+            var stream = demux.WriteStream;
+            Assert.IsTrue (stream.CanWrite);
+
+            // Make sure can read from WriteStream - required by TagLib#
+            // FIXME - depends on glib 2.22 and new gio# - see gio DemuxVfs.cs
+            Assert.AreEqual ((byte)'b', stream.ReadByte (), "Known failure, bug in Gio backend, depends on glib 2.22 for fix");
+            stream.Position = 0;
+
+            stream.WriteByte (0xAB);
+
+            demux.CloseStream (stream);
+            Assert.IsTrue (file.Exists (foo));
+            Assert.AreEqual (1, file.GetSize (foo));
+        }
+
+        [Test]
+        public void DemuxReadFile ()
+        {
+            Assert.IsTrue (file.Exists (foo));
+
+            var demux = new DemuxVfs (foo.AbsoluteUri);
+            var stream = demux.ReadStream;
+
+            // foo contains 'bar'
+            Assert.AreEqual ((byte)'b', stream.ReadByte ());
+            Assert.AreEqual ((byte)'a', stream.ReadByte ());
+            Assert.AreEqual ((byte)'r', stream.ReadByte ());
+
+            demux.CloseStream (stream);
+        }
+
+        [Test]
         public void GetFileProperties ()
         {
             Assert.AreEqual (3, file.GetSize (foo));



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]