[rygel] core, media-export: Handle invalid database files



commit ea17a7a912c538ace5664b1e4bf35219bebfd2a5
Author: Jens Georg <mail jensge org>
Date:   Wed Aug 12 13:11:30 2009 +0200

    core, media-export: Handle invalid database files
    
    This dummy container is used if the database file
    cannot be opened.

 src/plugins/media-export/Makefile.am               |    1 +
 .../rygel-media-export-null-container.vala         |   58 ++++++++++++++++++++
 .../rygel-media-export-root-container.vala         |   16 ++++--
 src/rygel/rygel-media-db.vala                      |   42 ++++++++++++---
 4 files changed, 104 insertions(+), 13 deletions(-)
---
diff --git a/src/plugins/media-export/Makefile.am b/src/plugins/media-export/Makefile.am
index 164ec29..83045f5 100644
--- a/src/plugins/media-export/Makefile.am
+++ b/src/plugins/media-export/Makefile.am
@@ -10,6 +10,7 @@ AM_CFLAGS = $(LIBGUPNP_CFLAGS) \
 	    -I$(top_srcdir)/src/rygel -DDATA_DIR='"$(datadir)"'
 
 librygel_media_export_la_SOURCES = rygel-media-export-plugin.vala \
+				   rygel-media-export-null-container.vala \
 				   rygel-media-export-root-container.vala \
 				   rygel-media-export-recursive-file-monitor.vala \
 				   rygel-media-export-harvester.vala \
diff --git a/src/plugins/media-export/rygel-media-export-null-container.vala b/src/plugins/media-export/rygel-media-export-null-container.vala
new file mode 100644
index 0000000..86d8ae8
--- /dev/null
+++ b/src/plugins/media-export/rygel-media-export-null-container.vala
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2009 Jens Georg <mail jensge org>.
+ *
+ * This file is part of Rygel.
+ *
+ * Rygel is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Rygel is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+using Rygel;
+using Gee;
+
+/**
+ * This is an empty container used to satisfy rygel if no mediadb could be
+ * created
+ */
+internal class Rygel.NullContainer : MediaContainer {
+    public NullContainer () {
+        base.root ("MediaExport", 0);
+    }
+
+    public override void get_children (uint               offset,
+                                       uint               max_count,
+                                       Cancellable?       cancellable,
+                                       AsyncReadyCallback callback) {
+        var res = new SimpleAsyncResult<int> (this, callback);
+        res.complete_in_idle ();
+    }
+
+    public override Gee.List<MediaObject>? get_children_finish (AsyncResult res)
+                                                                 throws Error {
+        return new Gee.ArrayList<MediaObject>();
+    }
+
+    public override void find_object (string             id,
+                                      Cancellable?       cancellable,
+                                      AsyncReadyCallback callback) {
+        var res = new SimpleAsyncResult<int> (this, callback);
+        res.complete_in_idle ();
+    }
+
+    public override MediaObject? find_object_finish (AsyncResult res)
+                                                                 throws Error {
+        return null;
+    }
+
+}
diff --git a/src/plugins/media-export/rygel-media-export-root-container.vala b/src/plugins/media-export/rygel-media-export-root-container.vala
index c4b34f8..3e13e8d 100644
--- a/src/plugins/media-export/rygel-media-export-root-container.vala
+++ b/src/plugins/media-export/rygel-media-export-root-container.vala
@@ -28,7 +28,7 @@ public class Rygel.MediaExportRootContainer : Rygel.MediaDBContainer {
     private HashMap<File, MediaExportHarvester> harvester;
     private MediaExportRecursiveFileMonitor monitor;
 
-    private static MediaExportRootContainer instance = null;
+    private static MediaContainer instance = null;
 
     private ArrayList<string> get_uris () {
         ArrayList<string> uris;
@@ -60,9 +60,16 @@ public class Rygel.MediaExportRootContainer : Rygel.MediaDBContainer {
         return uris;
     }
 
-    public static MediaExportRootContainer get_instance() {
+    public static MediaContainer get_instance() {
         if (MediaExportRootContainer.instance == null) {
-            MediaExportRootContainer.instance = new MediaExportRootContainer ();
+            try {
+                var db = MediaDB.create("media-export");
+                MediaExportRootContainer.instance =
+                                             new MediaExportRootContainer (db);
+            } catch (MediaDBError err) {
+                warning("Failed to create instance of database");
+                MediaExportRootContainer.instance = new NullContainer ();
+            }
         }
 
         return MediaExportRootContainer.instance;
@@ -71,8 +78,7 @@ public class Rygel.MediaExportRootContainer : Rygel.MediaDBContainer {
     /**
      * Create a new root container.
      */
-    private MediaExportRootContainer () {
-        var db = new MediaDB("media-export");
+    private MediaExportRootContainer (MediaDB db) {
         base (db, "0", "MediaExportRoot");
 
         this.extractor = new MetadataExtractor ();
diff --git a/src/rygel/rygel-media-db.vala b/src/rygel/rygel-media-db.vala
index 752fe6b..52f6307 100644
--- a/src/rygel/rygel-media-db.vala
+++ b/src/rygel/rygel-media-db.vala
@@ -218,8 +218,17 @@ public class Rygel.MediaDB : Object {
                 if (schema_info[1] == schema_version) {
                     debug ("Media DB schema has current version");
                 } else {
-                    debug ("Schema version differs... checking for upgrade");
-                    // FIXME implement if necessary
+                    int old_version = schema_info[1].to_int();
+                    int new_version = schema_version.to_int();
+                    if (schema_info[1].to_int() < schema_version.to_int()) {
+                        debug ("Older schema detected. Upgrading...");
+                    } else {
+                        // FIXME implement if necessary
+                        warning("The version \"%d\" of the detected database" +
+                                " is newer than our supported version \"%d\"",
+                                old_version, new_version);
+                        db = null;
+                    }
                 }
             } else {
                 warning ("Incompatible schema... cannot proceed");
@@ -236,7 +245,7 @@ public class Rygel.MediaDB : Object {
             if (rc != Sqlite.OK) {
                 warning ("Something weird going on: %s",
                          db.errmsg ());
-                db = null;
+                this.db = null;
                 return;
             }
 
@@ -244,23 +253,40 @@ public class Rygel.MediaDB : Object {
                 debug ("Empty database, creating new schema version %s",
                        schema_version);
                 if (!create_schema ()) {
+                    this.db = null;
                     return;
                 }
             } else {
                 warning ("Incompatible schema... cannot proceed");
+                this.db = null;
                 return;
             }
         }
     }
 
-    public MediaDB (string name) {
+    private MediaDB (string name, MediaDBObjectFactory factory) {
         open_db (name);
-        this.factory = new MediaDBObjectFactory ();
+        this.factory = factory;
     }
 
-    public MediaDB.with_factory (string name, MediaDBObjectFactory factory) {
-        open_db (name);
-        this.factory = factory;
+    public static MediaDB? create (string name) throws MediaDBError {
+        var instance = new MediaDB (name, new MediaDBObjectFactory());
+        if (instance.db != null) {
+            return instance;
+        }
+
+        throw new MediaDBError.GENERAL_ERROR("Invalid database");
+    }
+
+    public static MediaDB? create_with_factory (string               name,
+                                                MediaDBObjectFactory factory)
+                                                throws MediaDBError          {
+        var instance = new MediaDB (name, new MediaDBObjectFactory());
+        if (instance.db != null) {
+            return instance;
+        }
+
+        throw new MediaDBError.GENERAL_ERROR("Invalid database");
     }
 
     private bool sweeper () {



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