Skip to content

Commit 81cc244

Browse files
committed
fix: change user for every mkdir command (fixes #729)
1 parent 118085b commit 81cc244

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

pikaur/build.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
dirname,
2323
interactive_spawn,
2424
joined_spawn,
25+
mkdir,
2526
open_file,
2627
remove_dir,
2728
replace_file,
@@ -80,7 +81,7 @@ def _shell(cmds: list[str]) -> "InteractiveSpawn":
8081
return interactive_spawn(isolate_root_cmd(wrap_proxy_env(cmds)))
8182

8283

83-
def _mkdir(to_path: Path) -> None:
84+
def isolated_mkdir(to_path: Path) -> None:
8485
mkdir_result = spawn(isolate_root_cmd(["mkdir", "-p", str(to_path)]))
8586
if mkdir_result.returncode != 0:
8687
print_stdout(mkdir_result.stdout_text)
@@ -92,7 +93,7 @@ def copy_aur_repo(from_path: Path, to_path: Path) -> None:
9293
from_path = from_path.resolve()
9394
to_path = to_path.resolve()
9495
if not to_path.exists():
95-
_mkdir(to_path)
96+
isolated_mkdir(to_path)
9697

9798
from_paths = []
9899
for src_path_str in glob(f"{from_path}/*") + glob(f"{from_path}/.*"):
@@ -107,7 +108,7 @@ def copy_aur_repo(from_path: Path, to_path: Path) -> None:
107108
if result.returncode != 0:
108109
if to_path.exists():
109110
remove_dir(to_path)
110-
_mkdir(to_path)
111+
isolated_mkdir(to_path)
111112
result = interactive_spawn(cmd_args)
112113
if result.returncode != 0:
113114
raise RuntimeError(translate(f"Can't copy '{from_path}' to '{to_path}'."))
@@ -201,7 +202,7 @@ def __init__( # pylint: disable=super-init-not-called
201202
else:
202203
self.clone = True
203204
else:
204-
self.repo_path.mkdir(parents=True)
205+
isolated_mkdir(self.repo_path)
205206
self.clone = True
206207

207208
self.reviewed = self.current_hash == self.last_installed_hash
@@ -482,8 +483,7 @@ def _set_built_package_path(self) -> None:
482483
new_package_sig_path = new_package_path.parent / (
483484
new_package_path.name + ".sig"
484485
)
485-
if not PACKAGE_CACHE_PATH.exists():
486-
PACKAGE_CACHE_PATH.mkdir(parents=True)
486+
mkdir(PACKAGE_CACHE_PATH)
487487
replace_file(pkg_path, new_package_path)
488488
replace_file(pkg_sig_path, new_package_sig_path)
489489
pkg_path = new_package_path

pikaur/config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ def write_config(config: configparser.ConfigParser | None = None) -> None:
368368
config[section_name][option_name] = option_schema["default"]
369369
need_write = True
370370
if need_write:
371-
if not CONFIG_ROOT.exists():
372-
CONFIG_ROOT.mkdir(parents=True)
371+
from .core import mkdir # pylint: disable=import-outside-toplevel
372+
mkdir(CONFIG_ROOT)
373373
with get_config_path().open("w", encoding=DEFAULT_CONFIG_ENCODING) as configfile:
374374
config.write(configfile)
375375

pikaur/core.py

+11
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,14 @@ def check_runtime_deps(dep_names: list[str] | None = None) -> None:
393393
translate("executable not found"),
394394
))
395395
sys.exit(2)
396+
397+
398+
def mkdir(path: Path) -> None:
399+
if not path.exists():
400+
path.mkdir(parents=True)
401+
args = parse_args()
402+
user_id = args.user_id
403+
if not isinstance(user_id, int):
404+
raise TypeError
405+
if user_id:
406+
os.chown(path, user_id, user_id)

pikaur/main.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
DEFAULT_INPUT_ENCODING,
3030
check_runtime_deps,
3131
interactive_spawn,
32+
mkdir,
3233
spawn,
3334
sudo,
3435
)
@@ -351,8 +352,7 @@ def migrate_old_aur_repos_dir() -> None:
351352
_OLD_AUR_REPOS_CACHE_PATH.exists() and not AUR_REPOS_CACHE_PATH.exists()
352353
):
353354
return
354-
if not DATA_ROOT.exists():
355-
DATA_ROOT.mkdir(parents=True)
355+
mkdir(DATA_ROOT)
356356
shutil.move(_OLD_AUR_REPOS_CACHE_PATH, AUR_REPOS_CACHE_PATH)
357357

358358
print_stderr()
@@ -377,13 +377,10 @@ def create_dirs() -> None:
377377
# Chown the private CacheDirectory to root to signal systemd that
378378
# it needs to recursively chown it to the correct user
379379
os.chown(os.path.realpath(CACHE_ROOT), 0, 0)
380-
if not _USER_CACHE_ROOT.exists():
381-
_USER_CACHE_ROOT.mkdir(parents=True)
382-
if not CACHE_ROOT.exists():
383-
CACHE_ROOT.mkdir(parents=True)
380+
mkdir(_USER_CACHE_ROOT)
381+
mkdir(CACHE_ROOT)
384382
migrate_old_aur_repos_dir()
385-
if not AUR_REPOS_CACHE_PATH.exists():
386-
AUR_REPOS_CACHE_PATH.mkdir(parents=True)
383+
mkdir(AUR_REPOS_CACHE_PATH)
387384

388385

389386
def restore_tty() -> None:

0 commit comments

Comments
 (0)