Compare commits
3 Commits
5af28d21ca
...
0.2.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
7c391b8dbc
|
|||
| aafad81bb6 | |||
|
9658f534ea
|
@@ -5,7 +5,7 @@
|
||||
# chguard
|
||||
|
||||
<div align="center">
|
||||
<img src="chguard.png" alt="chguard logo" width="256" />
|
||||
<img src="https://git.sysmd.uk/guardutils/chguard/raw/branch/main/chguard.png" alt="chguard logo" width="256" />
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
@@ -248,14 +248,19 @@ def main() -> None:
|
||||
|
||||
root = normalize_root(args.save)
|
||||
|
||||
try:
|
||||
with conn: # start transaction
|
||||
if state_exists(conn, args.name):
|
||||
if not args.overwrite:
|
||||
raise SystemExit(
|
||||
f"State '{args.name}' already exists (use --overwrite)"
|
||||
)
|
||||
delete_state(conn, args.name)
|
||||
# if the new save fails, this delete_state step will also roll back
|
||||
delete_state(conn, args.name, commit=False)
|
||||
|
||||
state_id = create_state(conn, args.name, str(root), os.getuid())
|
||||
state_id = create_state(
|
||||
conn, args.name, str(root), os.getuid(), commit=False
|
||||
)
|
||||
|
||||
# Abort early if root-owned files exist and user is not root.
|
||||
# This prevents creating snapshots that cannot be meaningfully restored.
|
||||
@@ -281,10 +286,12 @@ def main() -> None:
|
||||
),
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
console.print(f"Saved state '{args.name}' for {root}")
|
||||
return
|
||||
|
||||
except SystemExit:
|
||||
raise
|
||||
|
||||
if args.restore:
|
||||
if not args.state:
|
||||
parser.error("STATE is required with --restore")
|
||||
|
||||
@@ -61,18 +61,27 @@ def state_exists(conn: sqlite3.Connection, name: str) -> bool:
|
||||
|
||||
|
||||
def create_state(
|
||||
conn: sqlite3.Connection, name: str, root_path: str, created_by_uid: int
|
||||
conn: sqlite3.Connection,
|
||||
name: str,
|
||||
root_path: str,
|
||||
created_by_uid: int,
|
||||
*,
|
||||
commit: bool = True,
|
||||
) -> int:
|
||||
cur = conn.execute(
|
||||
"INSERT INTO states (name, root_path, created_at, created_by_uid) VALUES (?, ?, ?, ?)",
|
||||
(name, root_path, utc_now_iso(), created_by_uid),
|
||||
)
|
||||
if commit:
|
||||
conn.commit()
|
||||
return int(cur.lastrowid)
|
||||
|
||||
|
||||
def delete_state(conn: sqlite3.Connection, name: str) -> int:
|
||||
def delete_state(
|
||||
conn: sqlite3.Connection, name: str, commit: bool = True
|
||||
) -> int:
|
||||
cur = conn.execute("DELETE FROM states WHERE name = ?", (name,))
|
||||
if commit:
|
||||
conn.commit()
|
||||
return cur.rowcount
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[tool.poetry]
|
||||
name = "chguard"
|
||||
version = "0.2.1"
|
||||
version = "0.2.2"
|
||||
description = "Safety-first tool to snapshot and restore filesystem ownership and permissions."
|
||||
authors = ["Marco D'Aleo <marco@marcodaleo.com>"]
|
||||
license = "GPL-3.0-or-later"
|
||||
|
||||
Reference in New Issue
Block a user