refactor: set permission
This commit is contained in:
+77
-12
@@ -8,10 +8,17 @@ from tools.helper import download_file, get_download_dir, host
|
||||
from tools import container
|
||||
from tools.logger import Logger
|
||||
|
||||
|
||||
class General:
|
||||
files = []
|
||||
|
||||
@property
|
||||
def arch(self):
|
||||
return host()
|
||||
|
||||
@property
|
||||
def skip_extract(self):
|
||||
return False
|
||||
return False
|
||||
|
||||
@property
|
||||
def download_loc(self):
|
||||
@@ -25,7 +32,8 @@ class General:
|
||||
return "/tmp/waydroid"
|
||||
|
||||
def download(self):
|
||||
Logger.info("Downloading {} now to {} .....".format(self.dl_file_name, self.download_loc))
|
||||
Logger.info("Downloading {} now to {} .....".format(
|
||||
self.dl_file_name, self.download_loc))
|
||||
loc_md5 = ""
|
||||
if os.path.isfile(self.download_loc):
|
||||
with open(self.download_loc, "rb") as f:
|
||||
@@ -37,7 +45,7 @@ class General:
|
||||
Logger.warning(
|
||||
"md5 mismatches, redownloading now ....")
|
||||
loc_md5 = download_file(self.dl_link, self.download_loc)
|
||||
|
||||
|
||||
def remove(self):
|
||||
for f in self.files:
|
||||
file = os.path.join(self.copy_dir, self.partition, f)
|
||||
@@ -59,25 +67,29 @@ class General:
|
||||
z.extractall(self.extract_to)
|
||||
|
||||
def add_props(self):
|
||||
arch = host()[0]
|
||||
bin_dir = os.path.join(self.copy_dir, "system", "bin")
|
||||
resetprop_rc=os.path.join(self.copy_dir, "system/etc/init/resetprop.rc")
|
||||
resetprop_rc = os.path.join(
|
||||
self.copy_dir, "system/etc/init/resetprop.rc")
|
||||
if not os.path.isfile(os.path.join(bin_dir, "_resetprop")):
|
||||
if not os.path.exists(bin_dir):
|
||||
os.makedirs(bin_dir)
|
||||
shutil.copy(os.path.join("./bin",arch,"_resetprop"), bin_dir)
|
||||
shutil.copy(os.path.join(
|
||||
"./bin", self.arch[0], "_resetprop"), bin_dir)
|
||||
os.chmod(os.path.join(bin_dir, "_resetprop"), 0o755)
|
||||
if not os.path.isfile(os.path.join(bin_dir, "resetprop.sh")):
|
||||
with open(os.path.join(bin_dir, "resetprop.sh"), "w") as f:
|
||||
f.write("#!/system/bin/sh\n")
|
||||
f.write("temp_dir=$(mktemp -d);ln -s /system/bin/_resetprop \"${temp_dir}/resetprop\"\n")
|
||||
f.write("while read line; do \"${temp_dir}/resetprop\" ${line%=*} ${line#*=}; done < /vendor/waydroid.prop\n")
|
||||
f.write(
|
||||
"temp_dir=$(mktemp -d);ln -s /system/bin/_resetprop \"${temp_dir}/resetprop\"\n")
|
||||
f.write(
|
||||
"while read line; do \"${temp_dir}/resetprop\" ${line%=*} ${line#*=}; done < /vendor/waydroid.prop\n")
|
||||
os.chmod(os.path.join(bin_dir, "resetprop.sh"), 0o755)
|
||||
if not os.path.isfile(resetprop_rc):
|
||||
if not os.path.exists(os.path.dirname(resetprop_rc)):
|
||||
os.makedirs(os.path.dirname(resetprop_rc))
|
||||
with open(resetprop_rc, "w") as f:
|
||||
f.write("on post-fs-data\n exec u:r:su:s0 root root -- /system/bin/sh /system/bin/resetprop.sh")
|
||||
f.write(
|
||||
"on post-fs-data\n exec u:r:su:s0 root root -- /system/bin/sh /system/bin/resetprop.sh")
|
||||
os.chmod(resetprop_rc, 0o644)
|
||||
|
||||
cfg = configparser.ConfigParser()
|
||||
@@ -86,17 +98,68 @@ class General:
|
||||
for key in self.apply_props.keys():
|
||||
if self.apply_props[key]:
|
||||
cfg.set('properties', key, self.apply_props[key])
|
||||
|
||||
|
||||
with open("/var/lib/waydroid/waydroid.cfg", "w") as f:
|
||||
cfg.write(f)
|
||||
|
||||
|
||||
def extract_app_lib(self, apk_file_path):
|
||||
lib_dest_dir = os.path.dirname(apk_file_path)
|
||||
with zipfile.ZipFile(apk_file_path, "r") as apk:
|
||||
for file_info in apk.infolist():
|
||||
file_name = file_info.filename
|
||||
file_path = os.path.join(lib_dest_dir, file_name)
|
||||
if file_info.filename.startswith(f"lib/{self.arch[0]}/") and file_name.endswith(".so"):
|
||||
os.makedirs(os.path.dirname(
|
||||
file_path), exist_ok=True)
|
||||
with apk.open(file_info.filename) as src_file, open(file_path, "wb") as dest_file:
|
||||
# Logger.info(f"{src_file} -> {dest_file}")
|
||||
shutil.copyfileobj(src_file, dest_file)
|
||||
|
||||
def set_path_perm(self, path):
|
||||
if "bin" in path.split("/"):
|
||||
perms = [0, 2000, 0o755, 0o777]
|
||||
else:
|
||||
perms = [0, 0, 0o755, 0o644]
|
||||
|
||||
mode = os.stat(path).st_mode
|
||||
|
||||
if os.path.isdir(path):
|
||||
mode |= perms[2]
|
||||
else:
|
||||
mode |= perms[3]
|
||||
|
||||
os.chown(path, perms[0], perms[1])
|
||||
os.chmod(path, mode)
|
||||
|
||||
def set_perm2(self, path, recursive=False):
|
||||
if not os.path.exists(path):
|
||||
return
|
||||
|
||||
if recursive and os.path.isdir(path):
|
||||
for root, dirs, files in os.walk(path):
|
||||
for dir in dirs:
|
||||
self.set_path_perm(os.path.join(root, dir))
|
||||
for file in files:
|
||||
self.set_path_perm(os.path.join(root, file))
|
||||
else:
|
||||
self.set_path_perm(path)
|
||||
|
||||
def set_perm(self):
|
||||
for f in self.files:
|
||||
path = os.path.join(self.copy_dir, self.partition, f)
|
||||
if "*" in path:
|
||||
for wildcard_file in glob.glob(path):
|
||||
self.set_perm2(wildcard_file, recursive=True)
|
||||
else:
|
||||
self.set_perm2(path, recursive=True)
|
||||
|
||||
def remove_props(self):
|
||||
cfg = configparser.ConfigParser()
|
||||
cfg.read("/var/lib/waydroid/waydroid.cfg")
|
||||
|
||||
for key in self.apply_props.keys():
|
||||
cfg.remove_option('properties', key)
|
||||
|
||||
|
||||
with open("/var/lib/waydroid/waydroid.cfg", "w") as f:
|
||||
cfg.write(f)
|
||||
|
||||
@@ -117,6 +180,7 @@ class General:
|
||||
self.extra1()
|
||||
if hasattr(self, "apply_props"):
|
||||
self.add_props()
|
||||
self.set_perm()
|
||||
Logger.info(f"{self.id} installation finished")
|
||||
|
||||
def uninstall(self):
|
||||
@@ -124,4 +188,5 @@ class General:
|
||||
if hasattr(self, "apply_props"):
|
||||
self.remove_props()
|
||||
self.extra2()
|
||||
self.set_perm()
|
||||
Logger.info("Uninstallation finished")
|
||||
|
||||
Reference in New Issue
Block a user