obshashrange: avoid crash when 'cache/' directory is missing
authorPierre-Yves David <pierre-yves.david@octobus.net>
Wed, 17 May 2017 18:40:48 +0200
changeset 2399 70275407a31e
parent 2398 612c17c8ab69
child 2400 9a53ed7e5540
obshashrange: avoid crash when 'cache/' directory is missing If the directory is missing, we create it.
hgext3rd/evolve/obsdiscovery.py
--- a/hgext3rd/evolve/obsdiscovery.py	Wed May 17 17:16:59 2017 +0200
+++ b/hgext3rd/evolve/obsdiscovery.py	Wed May 17 18:40:48 2017 +0200
@@ -425,6 +425,7 @@
 
     def __init__(self, repo):
         super(_obshashcache, self).__init__()
+        self._vfs = repo.vfs
         self._path = repo.vfs.join('cache/evoext_obshashrange_v1.sqlite')
         self._new = set()
         self._valid = True
@@ -524,6 +525,15 @@
             self._ondiskcachekey = self.emptykey
         assert self._cachekey is not None
 
+    def _db(self):
+        try:
+            util.makedirs(self._vfs.dirname(self._path))
+        except OSError:
+            return None
+        con = sqlite3.connect(self._path)
+        con.text_factory = str
+        return con
+
     @util.propertycache
     def _con(self):
         if not self._valid:
@@ -531,8 +541,9 @@
         repo = self._repo()
         if repo is None:
             return None
-        con = sqlite3.connect(self._path)
-        con.text_factory = str
+        con = self._db()
+        if con is None:
+            return None
         cur = con.execute(_queryexist)
         if cur.fetchone() is None:
             self._valid = False
@@ -566,8 +577,11 @@
             if '_con' in vars(self):
                 del self._con
 
-            con = sqlite3.connect(self._path)
-            con.text_factory = str
+            con = self._db()
+            if con is None:
+                repo.ui.log('evoext-cache', 'unable to write obshashrange cache'
+                            ' - cannot create database')
+                return
             with con:
                 for req in _sqliteschema:
                     con.execute(req)