Files
drobboxordinacebackup/indexer/db.py
2026-02-08 12:28:54 +01:00

92 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import pymysql
import hashlib
from indexer.config import DB_CONFIG, ROOT_NAME
def get_connection():
return pymysql.connect(**DB_CONFIG)
def preload_mark_all_missing():
"""
Na začátku běhu:
označí všechny soubory jako neexistující.
Ty, které skener znovu najde, se přepnou zpět na exists_now = 1.
"""
conn = get_connection()
try:
with conn.cursor() as cur:
cur.execute("UPDATE files SET exists_now = 0")
conn.commit()
finally:
conn.close()
def path_hash(path: str) -> bytes:
"""
MD5 hash cesty pouze identifikátor, ne bezpečnostní hash
"""
return hashlib.md5(path.encode("utf-8")).digest()
def find_file_by_path(cur, path_hash_bytes):
cur.execute(
"""
SELECT id, file_size, mtime, content_hash
FROM files
WHERE path_hash = %s
""",
(path_hash_bytes,)
)
return cur.fetchone()
def insert_file(cur, file):
cur.execute(
"""
INSERT INTO files (
root_name, full_path, path_hash,
file_name, directory,
file_size, mtime, content_hash,
first_seen, last_seen, exists_now
)
VALUES (
%s, %s, %s,
%s, %s,
%s, %s, %s,
NOW(), NOW(), 1
)
""",
(
ROOT_NAME,
file["full_path"],
path_hash(file["full_path"]),
file["file_name"],
file["directory"],
file["size"],
file["mtime"],
file["content_hash"],
)
)
return cur.lastrowid
def update_file(cur, file_id, file):
cur.execute(
"""
UPDATE files
SET file_size = %s,
mtime = %s,
content_hash = %s,
last_seen = NOW(),
exists_now = 1
WHERE id = %s
""",
(
file["size"],
file["mtime"],
file["content_hash"],
file_id,
)
)