2015-10-08 15:01:46 +00:00
|
|
|
|
|
|
|
import sys
|
2015-10-29 20:43:44 +00:00
|
|
|
from collections import namedtuple
|
2015-10-08 15:01:46 +00:00
|
|
|
|
|
|
|
from allmydata.util.dbutil import get_db, DBError
|
|
|
|
|
|
|
|
|
|
|
|
# magic-folder db schema version 1
|
|
|
|
SCHEMA_v1 = """
|
|
|
|
CREATE TABLE version
|
|
|
|
(
|
|
|
|
version INTEGER -- contains one row, set to 1
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE local_files
|
|
|
|
(
|
2016-03-29 16:03:24 +00:00
|
|
|
path VARCHAR(1024) PRIMARY KEY, -- UTF-8 filename relative to local magic folder dir
|
|
|
|
size INTEGER, -- ST_SIZE, or NULL if the file has been deleted
|
|
|
|
mtime_ns INTEGER, -- ST_MTIME in nanoseconds
|
|
|
|
ctime_ns INTEGER, -- ST_CTIME in nanoseconds
|
2015-10-08 15:01:46 +00:00
|
|
|
version INTEGER,
|
2016-01-14 12:37:25 +00:00
|
|
|
last_uploaded_uri VARCHAR(256), -- URI:CHK:...
|
|
|
|
last_downloaded_uri VARCHAR(256), -- URI:CHK:...
|
2015-10-12 18:07:39 +00:00
|
|
|
last_downloaded_timestamp TIMESTAMP
|
2015-10-08 15:01:46 +00:00
|
|
|
);
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def get_magicfolderdb(dbfile, stderr=sys.stderr,
|
|
|
|
create_version=(SCHEMA_v1, 1), just_create=False):
|
|
|
|
# Open or create the given backupdb file. The parent directory must
|
|
|
|
# exist.
|
|
|
|
try:
|
|
|
|
(sqlite3, db) = get_db(dbfile, stderr, create_version,
|
|
|
|
just_create=just_create, dbname="magicfolderdb")
|
|
|
|
if create_version[1] in (1, 2):
|
|
|
|
return MagicFolderDB(sqlite3, db)
|
|
|
|
else:
|
|
|
|
print >>stderr, "invalid magicfolderdb schema version specified"
|
|
|
|
return None
|
|
|
|
except DBError, e:
|
|
|
|
print >>stderr, e
|
|
|
|
return None
|
|
|
|
|
2016-03-29 16:23:31 +00:00
|
|
|
PathEntry = namedtuple('PathEntry', 'size mtime_ns ctime_ns version last_uploaded_uri '
|
2016-03-29 16:03:24 +00:00
|
|
|
'last_downloaded_uri last_downloaded_timestamp')
|
2015-10-08 15:01:46 +00:00
|
|
|
|
|
|
|
class MagicFolderDB(object):
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
def __init__(self, sqlite_module, connection):
|
|
|
|
self.sqlite_module = sqlite_module
|
|
|
|
self.connection = connection
|
|
|
|
self.cursor = connection.cursor()
|
|
|
|
|
2016-02-22 16:04:19 +00:00
|
|
|
def close(self):
|
|
|
|
self.connection.close()
|
|
|
|
|
2015-10-29 20:43:44 +00:00
|
|
|
def get_db_entry(self, relpath_u):
|
|
|
|
"""
|
|
|
|
Retrieve the entry in the database for a given path, or return None
|
|
|
|
if there is no such entry.
|
2015-10-08 15:01:46 +00:00
|
|
|
"""
|
|
|
|
c = self.cursor
|
2016-03-29 16:03:24 +00:00
|
|
|
c.execute("SELECT size, mtime_ns, ctime_ns, version, last_uploaded_uri,"
|
|
|
|
" last_downloaded_uri, last_downloaded_timestamp"
|
2015-10-08 15:01:46 +00:00
|
|
|
" FROM local_files"
|
|
|
|
" WHERE path=?",
|
2015-10-29 20:43:44 +00:00
|
|
|
(relpath_u,))
|
2015-10-08 15:01:46 +00:00
|
|
|
row = self.cursor.fetchone()
|
|
|
|
if not row:
|
2015-10-29 20:43:44 +00:00
|
|
|
return None
|
2015-10-08 15:01:46 +00:00
|
|
|
else:
|
2016-03-29 16:03:24 +00:00
|
|
|
(size, mtime_ns, ctime_ns, version, last_uploaded_uri,
|
|
|
|
last_downloaded_uri, last_downloaded_timestamp) = row
|
|
|
|
return PathEntry(size=size, mtime_ns=mtime_ns, ctime_ns=ctime_ns, version=version,
|
2015-10-29 20:43:44 +00:00
|
|
|
last_uploaded_uri=last_uploaded_uri,
|
|
|
|
last_downloaded_uri=last_downloaded_uri,
|
|
|
|
last_downloaded_timestamp=last_downloaded_timestamp)
|
2015-10-08 15:01:46 +00:00
|
|
|
|
|
|
|
def get_all_relpaths(self):
|
|
|
|
"""
|
|
|
|
Retrieve a set of all relpaths of files that have had an entry in magic folder db
|
|
|
|
(i.e. that have been downloaded at least once).
|
|
|
|
"""
|
|
|
|
self.cursor.execute("SELECT path FROM local_files")
|
|
|
|
rows = self.cursor.fetchall()
|
|
|
|
return set([r[0] for r in rows])
|
|
|
|
|
2015-10-12 18:07:39 +00:00
|
|
|
def did_upload_version(self, relpath_u, version, last_uploaded_uri, last_downloaded_uri, last_downloaded_timestamp, pathinfo):
|
2015-10-08 15:01:46 +00:00
|
|
|
try:
|
2015-10-12 18:07:39 +00:00
|
|
|
self.cursor.execute("INSERT INTO local_files VALUES (?,?,?,?,?,?,?,?)",
|
2016-03-29 16:03:24 +00:00
|
|
|
(relpath_u, pathinfo.size, pathinfo.mtime_ns, pathinfo.ctime_ns,
|
|
|
|
version, last_uploaded_uri, last_downloaded_uri,
|
|
|
|
last_downloaded_timestamp))
|
2015-10-08 15:01:46 +00:00
|
|
|
except (self.sqlite_module.IntegrityError, self.sqlite_module.OperationalError):
|
|
|
|
self.cursor.execute("UPDATE local_files"
|
2016-03-29 16:03:24 +00:00
|
|
|
" SET size=?, mtime_ns=?, ctime_ns=?, version=?, last_uploaded_uri=?,"
|
|
|
|
" last_downloaded_uri=?, last_downloaded_timestamp=?"
|
2015-10-08 15:01:46 +00:00
|
|
|
" WHERE path=?",
|
2016-03-29 16:03:24 +00:00
|
|
|
(pathinfo.size, pathinfo.mtime_ns, pathinfo.ctime_ns, version,
|
|
|
|
last_uploaded_uri, last_downloaded_uri, last_downloaded_timestamp,
|
|
|
|
relpath_u))
|
2015-10-08 15:01:46 +00:00
|
|
|
self.connection.commit()
|