Ticket #1027: lighttpd-1.4.18-compress-mkdir.patch

File lighttpd-1.4.18-compress-mkdir.patch, 2.8 kB (added by spillgroup, 4 months ago)

Optimized version of patch for 1.4.18.

  • b/src/mod_compress.c

    old new  
    102102        return HANDLER_GO_ON; 
    103103} 
    104104 
     105// 0 on success, -1 for error 
     106int mkdir_recursive(char *dir) { 
     107        char *p = dir; 
     108 
     109        if (!dir || !dir[0]) 
     110                return 0; 
     111 
     112        while ((p = strchr(p + 1, '/')) != NULL) { 
     113 
     114                *p = '\0'; 
     115                if ((mkdir(dir, 0700) != 0) && (errno != EEXIST)) { 
     116                        *p = '/'; 
     117                        return -1; 
     118                } 
     119 
     120                *p++ = '/'; 
     121                if (!*p) return 0; // Ignore trailing slash 
     122        } 
     123 
     124        return (mkdir(dir, 0700) != 0) && (errno != EEXIST) ? -1 : 0; 
     125} 
     126 
     127// 0 on success, -1 for error 
     128int mkdir_for_file(char *filename) { 
     129        char *p = filename; 
     130 
     131        if (!filename || !filename[0]) 
     132                return -1; 
     133 
     134        while ((p = strchr(p + 1, '/')) != NULL) { 
     135 
     136                *p = '\0'; 
     137                if ((mkdir(filename, 0700) != 0) && (errno != EEXIST)) { 
     138                        *p = '/'; 
     139                        return -1; 
     140                } 
     141 
     142                *p++ = '/'; 
     143                if (!*p) return -1; // Unexpected trailing slash in filename 
     144        } 
     145 
     146        return 0; 
     147} 
     148 
    105149SETDEFAULTS_FUNC(mod_compress_setdefaults) { 
    106150        plugin_data *p = p_d; 
    107151        size_t i = 0; 
     
    134178                } 
    135179 
    136180                if (!buffer_is_empty(s->compress_cache_dir)) { 
     181                        mkdir_recursive(s->compress_cache_dir->ptr); 
     182                 
    137183                        struct stat st; 
    138184                        if (0 != stat(s->compress_cache_dir->ptr, &st)) { 
    139185                                log_error_write(srv, __FILE__, __LINE__, "sbs", "can't stat compress.cache-dir", 
     
    342388        BUFFER_APPEND_SLASH(p->ofn); 
    343389 
    344390        if (0 == strncmp(con->physical.path->ptr, con->physical.doc_root->ptr, con->physical.doc_root->used-1)) { 
    345                 size_t offset = p->ofn->used - 1; 
    346                 char *dir, *nextdir; 
    347  
    348391                buffer_append_string(p->ofn, con->physical.path->ptr + con->physical.doc_root->used - 1); 
    349  
    350392                buffer_copy_string_buffer(p->b, p->ofn); 
    351  
    352                 /* mkdir -p ... */ 
    353                 for (dir = p->b->ptr + offset; NULL != (nextdir = strchr(dir, '/')); dir = nextdir + 1) { 
    354                         *nextdir = '\0'; 
    355  
    356                         if (-1 == mkdir(p->b->ptr, 0700)) { 
    357                                 if (errno != EEXIST) { 
    358                                         log_error_write(srv, __FILE__, __LINE__, "sbss", "creating cache-directory", p->b, "failed", strerror(errno)); 
    359  
    360                                         return -1; 
    361                                 } 
    362                         } 
    363  
    364                         *nextdir = '/'; 
    365                 } 
    366393        } else { 
    367394                buffer_append_string_buffer(p->ofn, con->uri.path); 
    368395        } 
     
    395422                        return 0; 
    396423                } 
    397424 
    398                 log_error_write(srv, __FILE__, __LINE__, "sbss", "creating cachefile", p->ofn, "failed", strerror(errno)); 
     425                if (-1 == mkdir_for_file(p->ofn->ptr)) { 
     426                        log_error_write(srv, __FILE__, __LINE__, "sb", "couldn't create directory for file", p->ofn); 
     427                        return -1; 
     428                } 
    399429 
    400                 return -1; 
     430                if (-1 == (ofd = open(p->ofn->ptr, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0600))) { 
     431                        log_error_write(srv, __FILE__, __LINE__, "sbss", "creating cachefile", p->ofn, "failed", strerror(errno)); 
     432                        return -1; 
     433                } 
    401434        } 
    402435#if 0 
    403436        log_error_write(srv, __FILE__, __LINE__, "bs", p->ofn, "compress-cache miss");