From d56d0e18ae98ee210f7aba6d8901f77793d6bbc4 Mon Sep 17 00:00:00 2001 From: Alexandra Drazilov Date: Tue, 7 Dec 2021 20:29:19 -0500 Subject: [PATCH 1/5] Added in Chmod functionality Signed-off-by: Alexandra Drazilov --- mock/server.py | 54 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/mock/server.py b/mock/server.py index d2a3f212b..180d5db30 100644 --- a/mock/server.py +++ b/mock/server.py @@ -518,23 +518,45 @@ def unixfile_rename(subpath): dir = dir["contents"][newNames[i]] return {"msg": "File Successfully Renamed"} - -@app.route('/unixfile/copy/', methods=['POST']) -def unixfile_copy(subpath): +@app.route('/unixfile/chmod/', methods=['POST']) +def unixfile_chmod(dir): if request.method == 'POST': - newNames = request.get_json()['newName'].split('/') - paths = subpath.split('/') - directory = global_directory - for i in range(len(paths)-1): - directory = directory["contents"][paths[i]] - newDirectory = directory['contents'][paths[len(paths)-1]].copy() - dir = global_directory - for i in range(len(newNames)): - if i == len(newNames)-1: - dir['contents'][newNames[i]] = newDirectory - dir = dir["contents"][newNames[i]] - return {"msg": "File Successfully Copied"} + try: + directory = global_directory["contents"] + dirPaths = dir.split("/") + mode = request.args.get('mode') + recursive = False + pattern = request.args.get('pattern') + if(request.args.get('recursive') is not None): + recursive = request.args.get('recursive').lower() == "true" + if(pattern == ""): + pattern = None + try: + int(mode, 8) + if(mode[:2] == "0o"): + mode = int(mode, 8) + else: + mode = int(mode) + except: + return {"msg": "Failed to change mode, mode not octal"}, 400 + currPath = directory + for x in range(0, len(dirPaths) - 1): + if(dirPaths[x] not in currPath): + return {"msg": "Directory/File not found."}, 404 + currPath = currPath[dirPaths[x]]["contents"] + if (dirPaths[len(dirPaths) - 1] not in currPath): + return {"msg": "Directory/File not found."}, 404 + if(pattern is None or pattern in dirPaths[len(dirPaths)-1]): + currPath[dirPaths[len(dirPaths) - 1]]["permissions"] = mode + if(currPath[dirPaths[len(dirPaths)-1]]["type"] == "folder" and len(currPath[dirPaths[len(dirPaths)-1]]["contents"]) > 0 and recursive): + for child, value in currPath[dirPaths[len(dirPaths)-1]]["contents"].items(): + if (value["type"] == "folder"): + print(unixfile_chmod(dir + "/" + child)) + if (pattern is None or pattern in child): + value["permissions"] = mode + return {"msg": "Successfully Modified Modes"}, 200 + except: + return {"msg": "Failed to modify file modes"}, 500 if __name__ == '__main__': app.run(debug=True) - From 1e45a3dcb1709172969e67e6df2babd7e63ac826 Mon Sep 17 00:00:00 2001 From: Alexandra Drazilov Date: Tue, 7 Dec 2021 20:40:10 -0500 Subject: [PATCH 2/5] Added in comments and returned if recursion does not work Signed-off-by: Alexandra Drazilov --- mock/server.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/mock/server.py b/mock/server.py index 180d5db30..a8b4f5c2c 100644 --- a/mock/server.py +++ b/mock/server.py @@ -531,6 +531,7 @@ def unixfile_chmod(dir): recursive = request.args.get('recursive').lower() == "true" if(pattern == ""): pattern = None + #Check if the input is an octal number try: int(mode, 8) if(mode[:2] == "0o"): @@ -540,18 +541,24 @@ def unixfile_chmod(dir): except: return {"msg": "Failed to change mode, mode not octal"}, 400 currPath = directory + #Get the contents of the layer above the directory for x in range(0, len(dirPaths) - 1): if(dirPaths[x] not in currPath): return {"msg": "Directory/File not found."}, 404 currPath = currPath[dirPaths[x]]["contents"] + #Get the directory of the call if (dirPaths[len(dirPaths) - 1] not in currPath): return {"msg": "Directory/File not found."}, 404 + #Change mode of the directory provided if(pattern is None or pattern in dirPaths[len(dirPaths)-1]): currPath[dirPaths[len(dirPaths) - 1]]["permissions"] = mode + #Make a recursive call to this function if it is recursive if(currPath[dirPaths[len(dirPaths)-1]]["type"] == "folder" and len(currPath[dirPaths[len(dirPaths)-1]]["contents"]) > 0 and recursive): for child, value in currPath[dirPaths[len(dirPaths)-1]]["contents"].items(): if (value["type"] == "folder"): - print(unixfile_chmod(dir + "/" + child)) + recursiveResult = unixfile_chmod(dir + "/" + child) + if(recursiveResult[1] != 200): + return recursiveResult if (pattern is None or pattern in child): value["permissions"] = mode return {"msg": "Successfully Modified Modes"}, 200 From c89c9f746b0d95421b2c9736c6de83df517e0240 Mon Sep 17 00:00:00 2001 From: Alexandra Drazilov Date: Tue, 7 Dec 2021 20:43:38 -0500 Subject: [PATCH 3/5] Added back in copy Signed-off-by: Alexandra Drazilov --- mock/server.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/mock/server.py b/mock/server.py index a8b4f5c2c..6b955db93 100644 --- a/mock/server.py +++ b/mock/server.py @@ -518,6 +518,22 @@ def unixfile_rename(subpath): dir = dir["contents"][newNames[i]] return {"msg": "File Successfully Renamed"} +@app.route('/unixfile/copy/', methods=['POST']) +def unixfile_copy(subpath): + if request.method == 'POST': + newNames = request.get_json()['newName'].split('/') + paths = subpath.split('/') + directory = global_directory + for i in range(len(paths)-1): + directory = directory["contents"][paths[i]] + newDirectory = directory['contents'][paths[len(paths)-1]].copy() + dir = global_directory + for i in range(len(newNames)): + if i == len(newNames)-1: + dir['contents'][newNames[i]] = newDirectory + dir = dir["contents"][newNames[i]] + return {"msg": "File Successfully Copied"} + @app.route('/unixfile/chmod/', methods=['POST']) def unixfile_chmod(dir): if request.method == 'POST': From f0450af1a646c6d8ba0b4a69b66da55b774c1a1c Mon Sep 17 00:00:00 2001 From: Alexandra Drazilov Date: Tue, 7 Dec 2021 20:44:28 -0500 Subject: [PATCH 4/5] Added back in line Signed-off-by: Alexandra Drazilov --- mock/server.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mock/server.py b/mock/server.py index 6b955db93..690e0408b 100644 --- a/mock/server.py +++ b/mock/server.py @@ -518,6 +518,7 @@ def unixfile_rename(subpath): dir = dir["contents"][newNames[i]] return {"msg": "File Successfully Renamed"} + @app.route('/unixfile/copy/', methods=['POST']) def unixfile_copy(subpath): if request.method == 'POST': From eae8f05c3a325e1649508b6428216f619f276eaf Mon Sep 17 00:00:00 2001 From: Alexandra Drazilov Date: Mon, 20 Dec 2021 20:49:42 -0500 Subject: [PATCH 5/5] Changed the contents command to send the actual mode back instead of just 777 Signed-off-by: Alexandra Drazilov --- mock/server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mock/server.py b/mock/server.py index 690e0408b..a3201901b 100644 --- a/mock/server.py +++ b/mock/server.py @@ -207,7 +207,7 @@ def unix_contents(subpath): "size": 9, "ccsid": 0, "createdAt": "2018-11-03T14:18:27", - "mode": 777 + "mode": global_directory['contents'][key]['permissions'] } ) else: @@ -231,7 +231,7 @@ def unix_contents(subpath): "size": 9, "ccsid": 0, "createdAt": "2018-11-03T14:18:27", - "mode": 777 + "mode": directory['contents'][key]['permissions'] } ) return test