Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of properties files when they're in subdirs #1082

Merged
merged 5 commits into from
Jan 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 18 additions & 32 deletions python/common/src/piscsi/file_cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import logging
import asyncio
from os import path, walk
from os import walk
from functools import lru_cache
from pathlib import PurePath, Path
from zipfile import ZipFile, is_zipfile
Expand Down Expand Up @@ -54,23 +54,6 @@ def send_pb_command(self, command):

return self.sock_cmd.send_pb_command(command.SerializeToString())

# noinspection PyMethodMayBeStatic
# pylint: disable=no-self-use
def list_files(self, file_types, dir_path):
"""
Takes a (list) or (tuple) of (str) file_types - e.g. ('hda', 'hds')
Returns (list) of (list)s files_list:
index 0 is (str) file name and index 1 is (int) size in bytes
"""
files_list = []
for file_path, _dirs, files in walk(dir_path):
# Only list selected file types
files = [file for file in files if file.lower().endswith(file_types)]
files_list.extend(
[(file, path.getsize(path.join(file_path, file))) for file in files],
)
return files_list

# noinspection PyMethodMayBeStatic
def list_config_files(self):
"""
Expand Down Expand Up @@ -99,18 +82,13 @@ def list_images(self):
result = proto.PbResult()
result.ParseFromString(data)

# Get a list of all *.properties files in CFG_DIR
prop_data = self.list_files(PROPERTIES_SUFFIX, CFG_DIR)
prop_files = [PurePath(x[0]).stem for x in prop_data]

server_info = self.piscsi.get_server_info()
files = []
for file in result.image_files_info.image_files:
# Add properties meta data for the image, if applicable
if file.name in prop_files:
process = self.read_drive_properties(
Path(CFG_DIR) / f"{file.name}.{PROPERTIES_SUFFIX}"
)
prop_file_path = Path(CFG_DIR) / f"{file.name}.{PROPERTIES_SUFFIX}"
# Add properties meta data for the image, if matching prop file is found
if prop_file_path.exists():
process = self.read_drive_properties(prop_file_path)
prop = process["conf"]
else:
prop = False
Expand Down Expand Up @@ -189,6 +167,8 @@ def rename_file(self, file_path, target_path):
Returns (dict) with (bool) status, (str) msg, (dict) parameters
"""
parameters = {"target_path": target_path}
if not target_path.parent.exists():
target_path.parent.mkdir(parents=True)
if target_path.parent.exists() and not target_path.exists():
file_path.rename(target_path)
return {
Expand All @@ -211,6 +191,8 @@ def copy_file(self, file_path, target_path):
Returns (dict) with (bool) status, (str) msg, (dict) parameters
"""
parameters = {"target_path": target_path}
if not target_path.parent.exists():
target_path.parent.mkdir(parents=True)
if target_path.parent.exists() and not target_path.exists():
copyfile(str(file_path), str(target_path))
return {
Expand All @@ -224,16 +206,18 @@ def copy_file(self, file_path, target_path):
"parameters": parameters,
}

def create_empty_image(self, file_path, size):
def create_empty_image(self, target_path, size):
"""
Takes (Path) file_path and (int) size in bytes
Takes (Path) target_path and (int) size in bytes
Creates a new empty binary file to use as image
Returns (dict) with (bool) status, (str) msg, (dict) parameters
"""
parameters = {"target_path": file_path}
if file_path.parent.exists() and not file_path.exists():
parameters = {"target_path": target_path}
if not target_path.parent.exists():
target_path.parent.mkdir(parents=True)
if target_path.parent.exists() and not target_path.exists():
try:
with open(f"{file_path}", "wb") as out:
with open(f"{target_path}", "wb") as out:
out.seek(size - 1)
out.write(b"\0")
except OSError as error:
Expand Down Expand Up @@ -793,6 +777,8 @@ def write_drive_properties(self, file_name, conf):
Returns (dict) with (bool) status and (str) msg
"""
file_path = Path(CFG_DIR) / file_name
if not file_path.parent.exists():
file_path.parent.mkdir(parents=True)
try:
with open(file_path, "w") as json_file:
dump(conf, json_file, indent=4)
Expand Down
4 changes: 2 additions & 2 deletions python/web/src/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def drive_create():
Creates the image and properties file pair
"""
drive_name = request.form.get("drive_name")
file_name = Path(request.form.get("file_name")).name
file_name = Path(request.form.get("file_name"))

properties = get_properties_by_drive_name(APP.config["PISCSI_DRIVE_PROPERTIES"], drive_name)

Expand Down Expand Up @@ -431,7 +431,7 @@ def drive_cdrom():
Creates a properties file for a CD-ROM image
"""
drive_name = request.form.get("drive_name")
file_name = Path(request.form.get("file_name")).name
file_name = Path(request.form.get("file_name"))

# Creating the drive properties file
file_name = f"{file_name}.{PROPERTIES_SUFFIX}"
Expand Down