Skip to content

Commit 39fbb85

Browse files
authored
Merge pull request #222 from jakub-nt/ENT-12739
ENT-12739: Added detection of the user providing wrong policy set path in cfbs analyze
2 parents baaefef + 1681abc commit 39fbb85

1 file changed

Lines changed: 52 additions & 2 deletions

File tree

cfbs/analyze.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from cfbs.utils import (
1111
FetchError,
1212
cfbs_dir,
13+
deduplicate_list,
1314
fetch_url,
1415
file_sha256,
1516
get_json,
@@ -25,14 +26,20 @@ def path_components(path):
2526
2627
The first component is `""` for a path starting with a separator. On Windows, if `path` begins with n backslashes, the first n components will be `""`.
2728
28-
The last component is the filename, trailing separators do not affect the result."""
29+
The last component is the name of the file or directory, trailing separators do not affect the result.
30+
"""
2931
norm_path = os.path.normpath(path)
3032

3133
dir_components = norm_path.split(os.sep)
3234

3335
return dir_components
3436

3537

38+
def name(path):
39+
"""Returns the name of the path to file or directory."""
40+
return path_components(path)[-1]
41+
42+
3643
def is_path_component(path, component):
3744
"""Returns whether `component` is a path component of `path`."""
3845
p_components = path_components(path)
@@ -551,8 +558,51 @@ def analyze_policyset(
551558
if reference_version is None:
552559
reference_version = versions_data.version_counter.most_common_version()
553560

554-
# if not a single file in the analyzed policyset has an MPF-known checksum, and a specific `reference_version` was not given, `reference_version` will still be `None`
561+
# if not a single file in the analyzed policyset has an MPF-known (checksum, filepath), and a specific `reference_version` was not given, `reference_version` will still be `None`
555562
if reference_version is None:
563+
# try to detect whether the user provided a wrong policy set path
564+
# gather all possible policy set paths, by detecting promises.cf or update.cf
565+
possible_policyset_relpaths = []
566+
mpfnorm_path = mpf_normalized_path(path, is_parentpath, masterfiles_dir)
567+
for filepath in files_dict:
568+
file_name = name(filepath)
569+
if file_name in ("promises.cf", "update.cf"):
570+
relpath = os.path.relpath(filepath, mpfnorm_path)
571+
relpath = relpath.replace(os.sep, "/")
572+
573+
if "/" not in relpath:
574+
# `"."`, not `path`, as the list collects relative paths
575+
possible_policyset_relpaths.append(".")
576+
if relpath.endswith("/update.cf"):
577+
possible_policyset_relpaths.append(relpath[:-10])
578+
if relpath.endswith("/promises.cf"):
579+
possible_policyset_relpaths.append(relpath[:-12])
580+
581+
# for drive root, the path's parent is the path itself, so only check the parent path if this is not the case
582+
if os.path.realpath(path) != os.path.realpath(os.path.join(path, "..")):
583+
if os.path.exists(os.path.join(path, "..", "update.cf")) or os.path.exists(
584+
os.path.join(path, "..", "promises.cf")
585+
):
586+
possible_policyset_relpaths.append("..")
587+
588+
possible_policyset_relpaths = deduplicate_list(possible_policyset_relpaths)
589+
590+
# check whether the policy set contains update.cf or promises.cf directly in masterfiles
591+
if not (
592+
(masterfiles_dir if is_parentpath else ".") in possible_policyset_relpaths
593+
):
594+
extra_error_text = ""
595+
if len(possible_policyset_relpaths) > 0:
596+
extra_error_text = (
597+
"Did you mean to provide one of the following paths (or their parent paths), relative to the provided path, instead?:\n "
598+
+ "\n ".join(possible_policyset_relpaths)
599+
+ "\n"
600+
)
601+
user_error(
602+
"There doesn't seem to be a valid policy set in the supplied path.\n Usage: cfbs analyze path/to/policy-set\n"
603+
+ extra_error_text
604+
)
605+
556606
reference_version_files = []
557607
reference_version_checksums = {}
558608
else:

0 commit comments

Comments
 (0)