From c992bb4c7cc230eff246f5368b7fc765750eec5b Mon Sep 17 00:00:00 2001 From: Joursoir Date: Wed, 28 Oct 2020 10:17:47 +0000 Subject: feature: move command --- easydir.c | 7 ++++--- main.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/easydir.c b/easydir.c index 9337fe1..162bae7 100644 --- a/easydir.c +++ b/easydir.c @@ -31,11 +31,12 @@ int checkFileExist(char *path_to_file) { FILE *pFile; - pFile = fopen(path_to_file, "r"); + pFile = fopen(path_to_file, "r+"); // r+ so that errno can equal EISDIR if(pFile == NULL) { - if(errno == ENOENT) { // file doesn't exist + if(errno == ENOENT) // file doesn't exist return 0; - } + if(errno == EISDIR) // it's directory + return 2; else callError(120); } fclose(pFile); diff --git a/main.c b/main.c index 1381e5a..4848606 100644 --- a/main.c +++ b/main.c @@ -52,6 +52,7 @@ #define STR_EDITUSE "Use: lpass edit [-t=text-editor] passname\n" #define STR_GENERATEUSE "Use: lpass generate [-l=pass-length] [-f] passname\n" #define STR_REMOVEUSE "Use: lpass remove/rm/delete passname\n" +#define STR_MOVEUSE "Use: lpass move/mv [-f] old-path new-path" // == global var == char *gPath_rootdir; // /home/[username]/.lockpassword/ @@ -200,13 +201,59 @@ static void cmd_edit(int argc, char *argv[]) static void cmd_move(int argc, char *argv[]) { - char *old_path = argv[2]; - char *new_path = argv[argc-1]; + /* we have a two situation: + 1) mv file file + 2) mv file directory */ - if(old_path == new_path) - printError("Use: lpass move/mv [old path] [new path]"); + const struct option long_options[] = { + {"force", no_argument, NULL, 'f'}, + {NULL, 0, NULL, 0} + }; + + int result, flag_force = 0; + while((result = getopt_long(argc, argv, "f", long_options, NULL)) != -1) { + switch(result) { + case 'f': { flag_force = 1; break; } + default: printError(STR_MOVEUSE); + } + } + + if(optind < argc) optind++; // for skip "move" + #if defined(DEBUG) + for(int i=0; i < argc; i++) printf("arg: %s\n", argv[i]); + printf("old-path: %s\n", argv[optind]); + if(argv[optind] != NULL) printf("new-path: %s\n", argv[optind+1]); + #endif + + if(argv[optind] == NULL) printError(STR_MOVEUSE); + if(argv[optind+1] == NULL) printError(STR_MOVEUSE); + + char *old_path = argv[optind]; + checkForbiddenPaths(old_path); globalSplitPath(old_path); + if(checkFileExist(gPath_pass) != 1) printError("Error: No such old-path exists\n"); + + char *old_path_gpg = gPath_pass; + char *old_path_subdir = gPath_subdir; + + char *new_path = argv[optind+1]; + checkForbiddenPaths(new_path); globalSplitPath(new_path); + + if(checkFileExist(new_path) == 2) // if new-path = dir + ; + else if(checkFileExist(gPath_pass) == 1) { // if new-path = file + if(!flag_force) { + if(getOverwriteAnswer(new_path) != 1) + return; + } + new_path = gPath_pass; + } + else printError("Error: No such new-path exists\n"); + + char *arguments[] = {"mv", "-f", old_path_gpg, new_path, NULL}; + easyFork("mv", arguments); - // soon ... + deleteEmptyDir(old_path_subdir); + free(old_path_subdir); free(old_path_gpg); } static void cmd_generate(int argc, char *argv[]) -- cgit v1.2.3-18-g5258