]> git.ipfire.org Git - ddns.git/blobdiff - src/ddns/database.py
Update translations
[ddns.git] / src / ddns / database.py
index 5d4ffc98ff85372fda120bb84845c230b83484e8..70a73635740211e356e2f68144dc4475f3390dea 100644 (file)
@@ -20,7 +20,7 @@
 ###############################################################################
 
 import datetime
-import os.path
+import os
 import sqlite3
 
 # Initialize the logger.
@@ -31,9 +31,11 @@ logger.propagate = 1
 class DDNSDatabase(object):
        def __init__(self, core, path):
                self.core = core
+               self.path = path
 
-               # Open the database file
-               self._db = self._open_database(path)
+               # We won't open the connection to the database directly
+               # so that we do not do it unnecessarily.
+               self._db = None
 
        def __del__(self):
                self._close_database()
@@ -46,7 +48,7 @@ class DDNSDatabase(object):
                conn = sqlite3.connect(path, detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
                conn.isolation_level = None
 
-               if not exists:
+               if not exists and self.is_writable():
                        logger.debug("Initialising database layout")
                        c = conn.cursor()
                        c.executescript("""
@@ -68,12 +70,25 @@ class DDNSDatabase(object):
 
                return conn
 
+       def is_writable(self):
+               # Check if the database file exists and is writable.
+               ret = os.access(self.path, os.W_OK)
+               if ret:
+                       return True
+
+               # If not, we check if we are able to write to the directory.
+               # In that case the database file will be created in _open_database().
+               return os.access(os.path.dirname(self.path), os.W_OK)
+
        def _close_database(self):
                if self._db:
                        self._db_close()
                        self._db = None
 
        def _execute(self, query, *parameters):
+               if self._db is None:
+                       self._db = self._open_database(self.path)
+
                c = self._db.cursor()
                try:
                        c.execute(query, parameters)
@@ -81,6 +96,10 @@ class DDNSDatabase(object):
                        c.close()
 
        def add_update(self, hostname, status, message=None):
+               if not self.is_writable():
+                       logger.warning("Could not log any updates because the database is not writable")
+                       return
+
                self._execute("INSERT INTO updates(hostname, status, message, timestamp) \
                        VALUES(?, ?, ?, ?)", hostname, status, message, datetime.datetime.utcnow())
 
@@ -103,6 +122,9 @@ class DDNSDatabase(object):
                """
                        Returns the timestamp of the last update (with the given status code).
                """
+               if self._db is None:
+                       self._db = self._open_database(self.path)
+
                c = self._db.cursor()
 
                try:
@@ -122,6 +144,9 @@ class DDNSDatabase(object):
                """
                        Returns the update status of the last update.
                """
+               if self._db is None:
+                       self._db = self._open_database(self.path)
+
                c = self._db.cursor()
 
                try:
@@ -137,6 +162,9 @@ class DDNSDatabase(object):
                """
                        Returns the reason string for the last failed update (if any).
                """
+               if self._db is None:
+                       self._db = self._open_database(self.path)
+
                c = self._db.cursor()
 
                try: