Browse Source

feat: 添加删除旧密码操作

可以删除旧密码项
增加文件写入缓冲
SongZihuan 3 years ago
parent
commit
71a1093d40
3 changed files with 64 additions and 9 deletions
  1. 13 3
      main.c
  2. 2 1
      main.h
  3. 49 5
      passwd_file.c

+ 13 - 3
main.c

@@ -16,14 +16,15 @@ struct arg_define arg[] = {
         {.ch='h', .name="help", .flat='h', .argument=no_argument},
         {.ch='s', .name="set-pw", .flat='s', .argument=no_argument},
         {.ch='g', .name="get-pw", .flat='g', .argument=no_argument},
+        {.ch='t', .name="tips", .flat='t', .argument=no_argument},
 #ifdef INCLUDE_KEY
         {.ch='c', .name="check-key", .flat='c', .argument=must_argument},
 #else
         {.ch='i', .name="in-file", .flat='i', .argument=no_argument},
-        {.ch='t', .name="tips", .flat='t', .argument=no_argument},
         {.ch='?', .name="set-tips", .flat='w', .argument=no_argument},
         {.ch='p', .name="print-label", .flat='p', .argument=no_argument},
         {.ch='d', .name="delete", .flat='d', .argument=no_argument},
+        {.ch='?', .name="delete-old", .flat='o', .argument=no_argument},
 #endif
         {.ch=0},
 };
@@ -35,6 +36,7 @@ enum {
 } work = no;
 bool in_file = false;  // 是否在文件中工作
 bool print_passwd = false;  // 是否打印content
+bool del_old = false;
 
 void printVersion(void);
 void printHelp(void);
@@ -87,6 +89,9 @@ int main(int argc, char **argv) {
                     goto what_do;
                 work = del_pw;
                 break;
+            case 'o':
+                del_old = true;
+                break;
             case 'i':
                 in_file = true;
                 break;
@@ -184,8 +189,10 @@ int main(int argc, char **argv) {
     if (!status)
         return EXIT_FAILURE;
 
-    if (in_file)
-        writePasswdFile();  // 写入数据
+    if (in_file) {
+        if (!writePasswdFile())  // 写入数据
+            return EXIT_FAILURE;
+    }
     return EXIT_SUCCESS;
 
     little_exit:
@@ -276,6 +283,9 @@ bool setPassWd(void) {
     printPasswdStr(account, passwd, note, passwd_str);
 
     if (in_file) {
+        if (del_old)
+            delContentByName(in_file_name);
+
         addContent(in_file_name, passwd_str);
         printf("The label has been written to the file. (name: %s)\n", in_file_name);
         if (print_passwd)

+ 2 - 1
main.h

@@ -53,7 +53,8 @@ char *findContent(char *name);
 void printFileTips(void);
 void setFileTips(char *tips);
 void printContent(void);
-void writePasswdFile(void);
+bool writePasswdFile(void);
 bool delContent(size_t del_index[], size_t size);
+void delContentByName(char *name);
 
 #endif //H_PASSWD_MAIN_H

+ 49 - 5
passwd_file.c

@@ -224,8 +224,32 @@ static h_char *getContentMD5(void) {
     return md5str;
 }
 
-void writePasswdFile(void) {  // 写入数据
-    FILE *fp = fopen(path, "wb");
+static bool writePasswdFileFinal(void) {
+    FILE *from = fopen("tmp.bak.hdp", "rb");
+    FILE *to = fopen(path, "wb");
+    size_t ret1, ret2;
+    char buf[1024];
+
+    do {
+        ret1 = fread(buf, sizeof(char), 1024, from);
+        ret2 = fwrite(buf, sizeof(char), ret1, to);
+
+        if (ret1 != ret2) {
+            fclose(from);
+            fclose(to);
+            fprintf(stderr, "Write file error.\n");
+            return false;
+        }
+
+    } while (ret1 == 1024);
+
+    fclose(from);
+    fclose(to);
+    return true;
+}
+
+bool writePasswdFile(void) {  // 写入数据
+    FILE *fp = fopen("tmp.bak.hdp", "wb");
     h_char *md5 = getContentMD5();
 
     if (!writeFileHead(fp, md5))
@@ -242,20 +266,29 @@ void writePasswdFile(void) {  // 写入数据
 
     free(md5);
     fclose(fp);
-    return;
+
+    if (!writePasswdFileFinal()) {
+        remove("tmp.bak.hdp");  // 删除文件
+        return false;
+    }
+
+    remove("tmp.bak.hdp");  // 删除文件
+    return true;
 
     error:
     free(md5);
     fclose(fp);
+    remove("tmp.bak.hdp");  // 删除文件
     fprintf(stderr, "File writing error occurred.\n");
+    return false;
 }
 
 void printContent(void) {
     struct Content *con = content;
     printf("********************\n");
     printf("Print all label in file.\n");
-    printf("total: %d\n", content_size);
-    printf("--------------------\n", content_size);
+    printf("total: %lu\n", content_size);
+    printf("--------------------\n");
     for (int i = 0; i < content_size; i++, con = con->next)
         printf("%d. %s : name: %s, label: %s\n", i, con->date, con->name, con->passwd_str);
     printf("********************\n");
@@ -379,3 +412,14 @@ bool delContent(size_t del_index[], size_t size) {  // del_index按从小到大
 
     return true;
 }
+
+void delContentByName(char *name) {
+    struct Content **con = &content;
+    while(*con != NULL) {
+        if (!strcmp((*con)->name, name)) {
+            *con = freeOneConten(*con);
+            content_size--;
+        } else
+            con = &((*con)->next);
+    }
+}