diff options
author | Joursoir <chat@joursoir.net> | 2020-10-28 10:17:47 +0000 |
---|---|---|
committer | Joursoir <chat@joursoir.net> | 2020-10-28 10:17:47 +0000 |
commit | c992bb4c7cc230eff246f5368b7fc765750eec5b (patch) | |
tree | 5ac830807bf9698123979e48512af9f4327b305e | |
parent | 101dd376b32c2cd12640e81b93c2f30975baac7f (diff) | |
download | lock-password-c992bb4c7cc230eff246f5368b7fc765750eec5b.tar.gz lock-password-c992bb4c7cc230eff246f5368b7fc765750eec5b.tar.bz2 lock-password-c992bb4c7cc230eff246f5368b7fc765750eec5b.zip |
feature: move command
-rw-r--r-- | easydir.c | 7 | ||||
-rw-r--r-- | main.c | 57 |
2 files changed, 56 insertions, 8 deletions
@@ -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); @@ -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[]) |