Skip to content

Commit

Permalink
Introduce flags & recursive protection
Browse files Browse the repository at this point in the history
 - `protect` now has options: R
 - `rm-p` now can protect you from removing files inside a protected
directories.
  • Loading branch information
alanzchen committed Feb 13, 2017
1 parent c635719 commit 8bfcded
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 19 deletions.
12 changes: 9 additions & 3 deletions rm_protection/protect.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@

def protect(protect_args=None):
c = Config()
flags = ''
option_end = False
if not protect_args:
protect_args = argv[1:]
for arg in protect_args:
if arg in c.invalid:
print("\".\" and \"..\" may not be protected")
if arg == '--':
option_end = True
elif (arg.startswith("-") and not option_end):
flags = flags + arg[arg.rfind('-') + 1:]
elif arg in c.invalid:
print('protect: "." and ".." may not be protected')
else:
path = abspath(expv(expu(arg)))
evalpath = dirname(path) + "/." + basename(path) + c.suffix
Expand All @@ -20,7 +26,7 @@ def protect(protect_args=None):
with open(evalpath, "w") as f:
question = input("Question for " + path + ": ")
answer = input("Answer: ")
f.write(question + "\n" + answer)
f.write(question + "\n" + answer + "\n" + flags.upper())


if __name__ == "__main__":
Expand Down
81 changes: 65 additions & 16 deletions rm_protection/rm_p.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,76 @@
from rm_protection.config import Config


def ask(evalpath, path=""):
c = Config()


def ask(evalpath, parent=False):
with open(evalpath, "r") as f:
if path:
print(path + ": " + f.readline().rstrip("\n"))
else:
print("A file is protected by " + evalpath)
print(f.readline().rstrip("\n"))
if input("Answer: ") == f.readline().rstrip("\n"):
question = f.readline().rstrip("\n")
answer = f.readline().rstrip("\n")
try:
flags = f.readline().rstrip("\n")
except:
flags = ''
if parent and 'R' not in flags:
print(original_path(evalpath) + ' is protected but flag "R" is missing.')
return True
elif path:
print("Wrong answer! " + path + " will not be removed")
print("The answer is stored in " + evalpath)
return False
else:
print("Wrong answer! File/directory protected by " + evalpath + " will not be removed")
print(original_path(evalpath) + ": " + question)
if input("Answer: ") == answer:
return True
else:
print("Wrong answer! " + original_path(evalpath) + " will not be removed")
print("The answer is stored in " + evalpath)
return False


def original_path(evalpath):
global c
basepath = dirname(evalpath)
filename = basename(evalpath)[1:-len(c.suffix)]
if basepath == '/':
return basepath + filename
else:
return basepath + '/' + filename

def ask_in(q, a):
return bool(input(q) in a)


def gen_evalpaths(path):
paths = {}
path = dirname(path)
while path != '/':
evalpath = gen_eval(path)
paths[path] = evalpath
path = dirname(path)
return paths


def gen_eval(path):
global c
basedir = dirname(path)
if basedir == '/':
basedir = ''
return basedir + "/." + basename(path) + c.suffix


def parent_clear(file_evalpaths):
for filepath in file_evalpaths:
parent_eval = file_evalpaths[filepath]
if exists(parent_eval):
print('The parent directory ' + filepath + ' is protected')
result = ask(parent_eval, parent=True)
if not result:
print(filepath + ' will not be removed')
return False
return True


def rm(rm_args=None):
global c
args = ''
c = Config()
paths = []
evalpaths = []
option_end = False
Expand All @@ -43,7 +88,8 @@ def rm(rm_args=None):
pass
else:
path = abspath(expv(expu(arg)))
evalpath = dirname(path) + "/." + basename(path) + c.suffix
file_evalpaths = gen_evalpaths(path)
evalpath = gen_eval(path)
if c.suffix in arg:
print(path + " is a protection file")
if ask_in(q="Do you want to remove it? (y/n) ", a="Yesyes"):
Expand All @@ -52,15 +98,18 @@ def rm(rm_args=None):
print(path + " will not be removed")
continue
if exists(evalpath):
if ask(evalpath, path):
if ask(evalpath):
paths.append(path)
evalpaths.append(evalpath)
else:
continue
elif isdir(path):
if not parent_clear(file_evalpaths):
continue
if isdir(path):
find_exec = "find " + path + " -name " + "\".*" + c.suffix + "\"" + " -print"
out, err = Popen(find_exec, shell=True, stdout=PIPE, stderr=PIPE, universal_newlines=True).communicate()
for pfile in iter(out.splitlines()):
print("A protected file or directory is found inside " + path)
if not ask(pfile):
print("Terminated due to potentially dangerous action")
exit(1)
Expand Down

1 comment on commit 8bfcded

@alanzchen
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit is related to #6.

Please sign in to comment.