ENGINE-451: rewrites still work. Need to add additional tests to be sure files are created as they should be. ENGINE-451
authorKrista Bennett <krista@pep-project.org>
Wed, 01 Aug 2018 11:54:36 +0200
branchENGINE-451
changeset 27970ff0fc8d9683
parent 2796 91818b883629
child 2798 e0abb44b8098
ENGINE-451: rewrites still work. Need to add additional tests to be sure files are created as they should be.
src/pgp_gpg.c
     1.1 --- a/src/pgp_gpg.c	Tue Jul 31 21:45:20 2018 +0200
     1.2 +++ b/src/pgp_gpg.c	Wed Aug 01 11:54:36 2018 +0200
     1.3 @@ -25,13 +25,81 @@
     1.4      return (int)((((str_ptr_and_bit*)(a))->key) - (((str_ptr_and_bit*)(b))->key));
     1.5  }
     1.6  
     1.7 +static bool write_old_conf_file(const char* old_fname) {
     1.8 +    if (!old_fname)
     1.9 +        return NULL;
    1.10 +        
    1.11 +    const unsigned int MAX_OPEN_TRIES = 20;
    1.12 +    const unsigned int MAX_DIGITS = 2; // cheaper than using math to sort out the MAX TRIES for a hotfix.
    1.13 +    unsigned int curr_attempt = 0;
    1.14 +    const char* old_ext = ".pep.old";    
    1.15 +    // name + . + num + old_ext + \0 - this is unoptimised for clarity.
    1.16 +    size_t buf_size = strlen(old_fname) + 1 + MAX_DIGITS + strlen(old_ext) + 1;
    1.17 +    char* buf = (char*)calloc(buf_size, 1);
    1.18 +    
    1.19 +    while (curr_attempt < MAX_OPEN_TRIES) {
    1.20 +        strlcpy(buf, old_fname, buf_size);
    1.21 +        strlcat(buf, ".", buf_size);
    1.22 +        unsigned int num_index = strlen(buf);
    1.23 +        sprintf(buf + num_index, "%d", curr_attempt);
    1.24 +        strlcat(buf, old_ext, buf_size);
    1.25 +    
    1.26 +        // Attn Claudio: Fopen vs. open is a portability choice here, consistent
    1.27 +        // with usage throughout the rest of the engine.
    1.28 +        FILE* test = Fopen(buf, "r");
    1.29 +        if (!test) 
    1.30 +            break;
    1.31 +        
    1.32 +        Fclose(test);
    1.33 +        curr_attempt++;
    1.34 +    }                
    1.35 +    
    1.36 +    if (curr_attempt >= MAX_OPEN_TRIES)
    1.37 +        return false;
    1.38 +    
    1.39 +    FILE* input = Fopen(old_fname, "r");    
    1.40 +    if (!input)
    1.41 +        return false;
    1.42 +        
    1.43 +    FILE* output = Fopen(buf, "w");
    1.44 +    if (!output) {
    1.45 +        Fclose(input);
    1.46 +        return false;
    1.47 +    }
    1.48 +    
    1.49 +    static char read_buf[MAX_LINELENGTH];
    1.50 +
    1.51 +//     const char* line_end;
    1.52 +// #ifdef WIN32
    1.53 +//     line_end = "\r\n";
    1.54 +// #else
    1.55 +//     line_end = "\n";
    1.56 +// #endif    
    1.57 +
    1.58 +    int success = 0;
    1.59 +    char* s = NULL;
    1.60 +    
    1.61 +    while ((s = Fgets(read_buf, MAX_LINELENGTH, input))) {
    1.62 +        success = Fprintf(output, "%s", s);        
    1.63 +        assert(success >= 0);
    1.64 +        if (success < 0)
    1.65 +            return false;
    1.66 +    }
    1.67 +
    1.68 +    Fclose(output);
    1.69 +    Fclose(input);
    1.70 +    return true;
    1.71 +}
    1.72 +
    1.73  // This is in response to ENGINE-427. We should NOT be aiming to keep this here.
    1.74  bool quickfix_config(stringlist_t* keys, const char* config_file_path) {
    1.75      static char buf[MAX_LINELENGTH];
    1.76      size_t num_keys = stringlist_length(keys);
    1.77  
    1.78 -    // Open file
    1.79 -
    1.80 +    if (!write_old_conf_file(config_file_path))
    1.81 +        return false;
    1.82 +        
    1.83 +    // Open old conf file
    1.84      FILE *f = Fopen(config_file_path, "r");    
    1.85         
    1.86      if (f == NULL)
    1.87 @@ -197,6 +265,12 @@
    1.88          free(found_keys);
    1.89          found_keys = NULL;
    1.90      } // End of file
    1.91 +    
    1.92 +    int ret = Fclose(f);
    1.93 +    assert(ret == 0);
    1.94 +    if (ret != 0)
    1.95 +        return false;
    1.96 +
    1.97      // then write all lines to file.
    1.98      const char* line_end;
    1.99  #ifdef WIN32
   1.100 @@ -204,25 +278,17 @@
   1.101  #else
   1.102      line_end = "\n";
   1.103  #endif    
   1.104 +
   1.105      size_t cf_path_len = strlen(config_file_path);
   1.106 -    size_t new_buf_size = cf_path_len + 5; // + .old\0
   1.107 -    char* old_config_path_fname = (char*)calloc(new_buf_size, 1);
   1.108 -    if (!old_config_path_fname)
   1.109 +    size_t new_buf_size = cf_path_len + 8; // + pep.new\0
   1.110 +    char* temp_config_path_fname = (char*)calloc(new_buf_size, 1);
   1.111 +    if (!temp_config_path_fname)
   1.112          return false;
   1.113 -    strlcpy(old_config_path_fname, config_file_path, new_buf_size);
   1.114 -    strlcat(old_config_path_fname, ".old", new_buf_size);
   1.115 -    int ret = Fclose(f);
   1.116 -    assert(ret == 0);
   1.117 -    if (ret != 0)
   1.118 -        return false;
   1.119 -    
   1.120 -    ret = rename(config_file_path, old_config_path_fname);
   1.121 -    assert(ret == 0);
   1.122 -    if (ret != 0)
   1.123 -        return false;
   1.124 -
   1.125 +    strlcpy(temp_config_path_fname, config_file_path, new_buf_size);
   1.126 +    strlcat(temp_config_path_fname, ".pep.new", new_buf_size);
   1.127 +        
   1.128      // Ok, now open the thing for writing.
   1.129 -    f = Fopen(config_file_path, "w");
   1.130 +    f = Fopen(temp_config_path_fname, "w");
   1.131      
   1.132      assert(f);
   1.133      if (f == NULL)
   1.134 @@ -243,6 +309,11 @@
   1.135      if (ret != 0)
   1.136          return false;
   1.137  
   1.138 +    ret = rename(temp_config_path_fname, config_file_path);
   1.139 +    assert(ret == 0);
   1.140 +    if (ret != 0)
   1.141 +        return false;
   1.142 +
   1.143      return true;
   1.144  }
   1.145