implemented link subcommand

master
BodgeMaster 2021-12-20 11:48:54 +01:00
parent b9dc47d0e9
commit b5c9d92ca8
1 changed files with 44 additions and 5 deletions

View File

@ -200,6 +200,8 @@ if __name__ == "__main__":
#TODO: clarification of <> and []
#TODO: subcommand to change container settings
VALID_COMMANDS=["create", "add", "remove", "search", "lookup", "link", "check", "update", "fix", "help"]
#TODO: (*fully) implemented subcommands: *create, *add, *lookup, *help
#TODO: unimplemented subcommands: remove, search, link, check, update, fix
try:
command = sys.argv[1].split("+")
@ -212,11 +214,6 @@ if __name__ == "__main__":
print(USAGE, file=sys.stderr)
sys.exit(USAGE_ERROR)
# help subcommand
if command[0] == "help":
print(USAGE)
sys.exit(0)
# create subcommand: create a new directory containing a folder for stored objects, one for parity files and one for
# arguments: <storage directory> [parity=<on|off>] [parity-bytes=<number of bytes for each parity byte>] [checksum-algorithm=<algorithm>] [compress=<on|off>]
if command[0] == "create":
@ -400,6 +397,48 @@ if __name__ == "__main__":
else:
print(json.dumps(lookup_results))
# link subcommand: add a symlink in <location> that points to the referenced file
# arguments:
# <storage directory> <hash> <location>
if command[0] == "link":
if len(sys.argv)<5:
print("Too few arguments!", file=sys.stderr)
print(USAGE, file=sys.stderr)
sys.exit(USAGE_ERROR)
storage_directory = sys.argv[2]
file_hash = sys.argv[3]
link_location = sys.argv[4]
status, parity, parity_bytes, checksum_algorithm, compress = load_container_settings(storage_directory)
if not status==0:
if status==PATH_ERROR:
print("Invalid storage directory!", file=sys.stderr)
print(USAGE, file=sys.stderr)
if status==GENERAL_ERROR:
print("Verifying container settings failed.", file=sys.stderr)
sys.exit(status)
if file_hash_or_path_is_known_hash(storage_directory, file_hash, compress):
if os.path.isdir(os.sep.join(link_location.split(os.sep)[:-1])):
if os.path.exists(link_location):
print(link_location+": file already exists.", file=sys.stderr)
sys.exit(GENERAL_ERROR)
else:
suffix = ""
if compress:
suffix = ".xz"
object_path = os.path.join(storage_directory, "objects", file_hash+suffix)
os.symlink(object_path, link_location)
print(link_location+" -> "+object_path)
else:
print("Parent directory "+os.sep.join(link_location.split(os.sep)[:-1])+" does not exist.", file=sys.stderr)
sys.exit(GENERAL_ERROR)
# help subcommand
if command[0] == "help":
print(USAGE)
sys.exit(0)
# this line is here to work around a bug in Xed