From 25e98489c0300dd14daef120e9e1cd28148e8dba Mon Sep 17 00:00:00 2001 From: xenofem Date: Mon, 1 Apr 2024 21:50:25 -0400 Subject: [PATCH] make extracted files and directories readonly --- dlibrary/dlibrary.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/dlibrary/dlibrary.py b/dlibrary/dlibrary.py index 330baef..dca0fbc 100755 --- a/dlibrary/dlibrary.py +++ b/dlibrary/dlibrary.py @@ -6,12 +6,13 @@ import importlib_resources as resources from io import BytesIO from pathlib import Path import os -from os.path import relpath, splitext +from os.path import relpath, splitext, join import random import re import readline import shutil import sqlite3 +import stat import textwrap import unicodedata from urllib.parse import urlparse @@ -96,6 +97,9 @@ PDF_INLINE_IMAGE_REGEX = re.compile(r'(^|\s)(BI|ID|EI)($|\s)') SUGGESTED_WORKS_COUNT = 10 +READONLY_FILE = stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH +READONLY_DIR = READONLY_FILE | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH + debug_mode = False def debug(s): if debug_mode: @@ -120,6 +124,12 @@ def open_rarfile_with_encoding(path): print(f'{path} contains filenames with unknown character encoding!') exit(1) +def readonly(path): + for parentdir, dirs, files in os.walk(path, topdown=False): + for f in files: + os.chmod(join(parentdir, f), READONLY_FILE, follow_symlinks=False) + os.chmod(parentdir, READONLY_DIR, follow_symlinks=False) + def extract(args): absolute_archive_paths = set(path.resolve(strict=True) for path in args.archives) @@ -136,6 +146,8 @@ def extract(args): work_extract_path.mkdir(parents=True) z.extractall(path=work_extract_path) + readonly(work_extract_path) + if args.remove: archive_path.unlink() @@ -154,6 +166,8 @@ def extract(args): work_extract_path.mkdir(parents=True) r.extractall(path=work_extract_path) + readonly(work_extract_path) + if args.remove: for vol in volumes: vol.unlink()