forked from DMOJ/judge-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change regex to filesystem policy matcher, first step of DMOJ#871
- Loading branch information
Showing
17 changed files
with
259 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
from typing import List, Union | ||
|
||
|
||
class ACCESS_MODE: | ||
NONE = 0 | ||
EXACT = 1 | ||
RECURSIVE = 2 | ||
|
||
|
||
class Dir: | ||
def __init__(self): | ||
self.access_mode = ACCESS_MODE.NONE | ||
|
||
self.map = {} | ||
|
||
|
||
class File: | ||
pass | ||
|
||
|
||
class ExactFile: | ||
def __init__(self, path: str): | ||
self.path = path | ||
|
||
|
||
class ExactDir: | ||
access_mode = ACCESS_MODE.EXACT | ||
|
||
def __init__(self, path: str): | ||
self.path = path | ||
|
||
|
||
class RecursiveDir: | ||
access_mode = ACCESS_MODE.RECURSIVE | ||
|
||
def __init__(self, path: str): | ||
self.path = path | ||
|
||
|
||
Rule = Union[ExactFile, ExactDir, RecursiveDir] | ||
|
||
|
||
class FilesystemPolicy: | ||
def __init__(self, rules: List[Rule]): | ||
self.rules = rules | ||
|
||
self.built = False | ||
|
||
def __add__(self, other): | ||
assert not self.built and not other.built, "Refusing to merge built policies: merge before building" | ||
|
||
return FilesystemPolicy(self.rules + other.rules) | ||
|
||
def build(self): | ||
self.root = Dir() | ||
|
||
for rule in self.rules: | ||
self._add_rule(rule) | ||
|
||
self.built = True | ||
|
||
def _add_rule(self, rule: Rule): | ||
if rule.path == "/": | ||
return self._finalize_root_rule(rule) | ||
|
||
node = self.root | ||
path = rule.path | ||
|
||
assert path[0] == '/', "Rule must specify an absolute path to rule" | ||
|
||
*directory_path, final_component = path[1:].split("/") | ||
|
||
for component in directory_path: | ||
assert component != "", "Must not have empty components in rule to add" | ||
|
||
new_node = node.map.setdefault(component, Dir()) | ||
|
||
assert isinstance(new_node, Dir), "Cannot descend into non-directory" | ||
|
||
node = new_node | ||
|
||
self._finalize_rule(node, final_component, rule) | ||
|
||
def _finalize_root_rule(self, rule: Rule): | ||
assert not isinstance(rule, ExactFile), "Root is not a file" | ||
|
||
self._finalize_directory_rule(self.root, rule) | ||
|
||
def _finalize_rule(self, node: Dir, final_component: str, rule: Rule): | ||
assert final_component != "", "Must not have trailing slashes in rule path" | ||
|
||
if isinstance(rule, ExactFile): | ||
new_node = node.map.setdefault(final_component, File()) | ||
|
||
assert isinstance(new_node, File), "Can't add ExactFile: Dir rule exists" | ||
|
||
else: | ||
new_node = node.map.setdefault(final_component, Dir()) | ||
|
||
assert isinstance(new_node, Dir), "Can't add rule: File rule exists" | ||
|
||
self._finalize_directory_rule(new_node, rule) | ||
|
||
def _finalize_directory_rule(self, node: Dir, rule: Union[ExactDir, RecursiveDir]): | ||
node.access_mode = max(node.access_mode, rule.access_mode) # Allow the more permissive rule | ||
|
||
# `path` should be a canonical path. The output of `realpath` is appropriate | ||
def check(self, path: str) -> bool: | ||
if path == "/": | ||
return self._check_final_node(self.root) | ||
|
||
assert self.built, "Must build the policy object before checking" | ||
assert path[0] == '/', "Must pass an absolute path to check" | ||
|
||
path_split = path[1:].split("/") | ||
node = self.root | ||
|
||
for component in path_split: | ||
assert component != "", "Must not have empty components in path to check" | ||
|
||
if isinstance(node, File): | ||
return False | ||
|
||
elif node.access_mode == ACCESS_MODE.RECURSIVE: | ||
return True | ||
|
||
else: | ||
node = node.map.get(component) | ||
|
||
if node is None: | ||
return False | ||
|
||
return self._check_final_node(node) | ||
|
||
def _check_final_node(self, node: Union[Dir, File]) -> bool: | ||
return isinstance(node, File) or node.access_mode != ACCESS_MODE.NONE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.