Changeset 2135

Show
Ignore:
Timestamp:
03/21/2008 03:22:50 PM (4 months ago)
Author:
stbuehler
Message:

Fix mod_compress bug (#1027)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/NEWS

    r2134 r2135  
    2626  * fix auth-ldap configuration in tests 
    2727  * fcgi-stat-accel: Fix unused var / indentation 
     28  * fix mod_compress bug (#1027) 
    2829 
    2930- 1.5.0-r19.. - 
  • trunk/src/mod_compress.c

    r2131 r2135  
    105105} 
    106106 
    107 void mkdir_recursive(const char *dir) { 
    108  
    109         char dir_copy[256]; 
    110         char *p = dir_copy; 
     107// 0 on success, -1 for error 
     108static int mkdir_recursive(char *dir) { 
     109        char *p = dir; 
    111110 
    112111        if (!dir || !dir[0]) 
    113                 return; 
    114  
    115         strncpy(dir_copy, dir, sizeof(dir_copy) / sizeof(dir_copy[0])); 
     112                return 0; 
    116113 
    117114        while ((p = strchr(p + 1, '/')) != NULL) { 
    118115 
    119116                *p = '\0'; 
    120                 if ((mkdir(dir_copy, 0700) != 0) && (errno != EEXIST)) 
    121                         return; 
     117                if ((mkdir(dir, 0700) != 0) && (errno != EEXIST)) { 
     118                        *p = '/'; 
     119                        return -1; 
     120                } 
    122121 
    123122                *p++ = '/'; 
    124         } 
    125  
    126         mkdir(dir, 0700); 
     123                if (!*p) return 0; // Ignore trailing slash 
     124        } 
     125 
     126        return (mkdir(dir, 0700) != 0) && (errno != EEXIST) ? -1 : 0; 
     127
     128 
     129// 0 on success, -1 for error 
     130static int mkdir_for_file(char *filename) { 
     131        char *p = filename; 
     132 
     133        if (!filename || !filename[0]) 
     134                return -1; 
     135 
     136        while ((p = strchr(p + 1, '/')) != NULL) { 
     137 
     138                *p = '\0'; 
     139                if ((mkdir(filename, 0700) != 0) && (errno != EEXIST)) { 
     140                        ERROR("creating cache-directory \"%s\" failed: %s", filename, strerror(errno)); 
     141                        *p = '/'; 
     142                        return -1; 
     143                } 
     144 
     145                *p++ = '/'; 
     146                if (!*p) { 
     147                        ERROR("unexpected trailing slash for filename \"%s\"", filename); 
     148                        return -1; 
     149                } 
     150        } 
     151 
     152        return 0; 
    127153} 
    128154 
     
    162188                        if (0 != stat(s->compress_cache_dir->ptr, &st)) { 
    163189 
    164                                 log_error_write(srv, __FILE__, __LINE__, "sbs", "can't stat compress.cache-dir, attempting to create", 
    165                                                 s->compress_cache_dir, strerror(errno)); 
     190                                ERROR("can't stat compress.cache-dir (%s), attempting to create '%s'", strerror(errno),SAFE_BUF_STR(s->compress_cache_dir)); 
    166191                                mkdir_recursive(s->compress_cache_dir->ptr); 
    167192 
    168193                                if (0 != stat(s->compress_cache_dir->ptr, &st)) { 
    169  
    170                                         log_error_write(srv, __FILE__, __LINE__, "sbs", "can't stat compress.cache-dir, create failed", 
    171                                                                         s->compress_cache_dir, strerror(errno)); 
    172  
     194                                        ERROR("can't stat compress.cache-dir (%s), failed to create '%s'", strerror(errno),SAFE_BUF_STR(s->compress_cache_dir)); 
    173195                                        return HANDLER_ERROR; 
    174196                                } 
     
    378400 
    379401        if (0 == strncmp(con->physical.path->ptr, con->physical.doc_root->ptr, con->physical.doc_root->used-1)) { 
    380                 size_t offset = p->ofn->used - 1; 
    381                 char *dir, *nextdir; 
    382  
    383402                buffer_append_string(p->ofn, con->physical.path->ptr + con->physical.doc_root->used - 1); 
    384  
    385403                buffer_copy_string_buffer(p->b, p->ofn); 
    386  
    387                 /* mkdir -p ... */ 
    388                 for (dir = p->b->ptr + offset; NULL != (nextdir = strchr(dir, '/')); dir = nextdir + 1) { 
    389                         *nextdir = '\0'; 
    390  
    391                         if (-1 == mkdir(p->b->ptr, 0700)) { 
    392                                 if (errno != EEXIST) { 
    393                                         log_error_write(srv, __FILE__, __LINE__, "sbss", "creating cache-directory", p->b, "failed", strerror(errno)); 
    394  
    395                                         return -1; 
    396                                 } 
    397                         } 
    398  
    399                         *nextdir = '/'; 
    400                 } 
    401404        } else { 
    402405                buffer_append_string_buffer(p->ofn, con->uri.path); 
     
    414417                break; 
    415418        default: 
    416                 log_error_write(srv, __FILE__, __LINE__, "sd", "unknown compression type", type); 
     419                ERROR("unknown compression type %d", type); 
    417420                return -1; 
    418421        } 
     
    423426        if (HANDLER_ERROR != stat_cache_get_entry(srv, con, p->ofn, &compressed_sce)) { 
    424427                /* file exists */ 
    425                 if (con->conf.log_request_handling) TRACE("file exists in the cache (%s), sending it", BUF_STR(p->ofn)); 
     428                if (con->conf.log_request_handling) TRACE("file exists in the cache (%s), sending it", SAFE_BUF_STR(p->ofn)); 
    426429 
    427430                chunkqueue_reset(con->send); 
     
    433436 
    434437        if (-1 == (ofd = open(p->ofn->ptr, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0600))) { 
    435                 if (errno == EEXIST) { 
    436                         /* cache-entry exists */ 
    437  
    438                 } 
    439  
    440                 log_error_write(srv, __FILE__, __LINE__, "sbss", 
    441                                 "creating cachefile", p->ofn, 
    442                                 "failed", strerror(errno)); 
    443  
    444                 return -1; 
     438                if (-1 == mkdir_for_file(p->ofn->ptr)) { 
     439                        return -1; // error message in mkdir_for_file 
     440                } else if (-1 == (ofd = open(p->ofn->ptr, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0600))) { 
     441                        ERROR("creating cachefile '%s' failed: %s", SAFE_BUF_STR(p->ofn), strerror(errno)); 
     442                        return -1; 
     443                } 
    445444        } 
    446445 
    447446        if (-1 == (ifd = open(filename, O_RDONLY | O_BINARY))) { 
    448                 log_error_write(srv, __FILE__, __LINE__, "sbss", 
    449                                 "opening plain-file", fn, 
    450                                 "failed", strerror(errno)); 
    451  
     447                ERROR("opening plain-file '%s' failed: %s", SAFE_BUF_STR(fn), strerror(errno)); 
    452448                close(ofd); 
    453  
    454449                return -1; 
    455450        } 
     
    457452 
    458453        if (MAP_FAILED == (start = mmap(NULL, sce->st.st_size, PROT_READ, MAP_SHARED, ifd, 0))) { 
    459                 log_error_write(srv, __FILE__, __LINE__, "sbss", 
    460                                 "mmaping", fn, 
    461                                 "failed", strerror(errno)); 
    462  
     454                ERROR("mmaping '%s' failed: %s", SAFE_BUF_STR(fn), strerror(errno)); 
    463455                close(ofd); 
    464456                close(ifd); 
     
    526518 
    527519        if (-1 == (ifd = open(fn->ptr, O_RDONLY | O_BINARY))) { 
    528                 log_error_write(srv, __FILE__, __LINE__, "sbss", "opening plain-file", fn, "failed", strerror(errno)); 
     520                ERROR("opening plain-file '%s' failed: %s", SAFE_BUF_STR(fn), strerror(errno)); 
    529521 
    530522                return -1; 
     
    536528 
    537529        if (MAP_FAILED == start) { 
    538                 log_error_write(srv, __FILE__, __LINE__, "sbss", "mmaping", fn, "failed", strerror(errno)); 
     530                ERROR("mmaping '%s' failed: %s", SAFE_BUF_STR(fn), strerror(errno)); 
    539531 
    540532                return -1; 
     
    644636 
    645637        if (HANDLER_GO_ON != stat_cache_get_entry(srv, con, con->physical.path, &sce)) { 
    646                 if (con->conf.log_request_handling) TRACE("file '%s' not found", BUF_STR(con->physical.path)); 
     638                if (con->conf.log_request_handling) TRACE("file '%s' not found", SAFE_BUF_STR(con->physical.path)); 
    647639                return HANDLER_GO_ON; 
    648640        } 
     
    651643        if (max_fsize && (sce->st.st_size >> 10) > max_fsize) { 
    652644                if (con->conf.log_request_handling) TRACE("file '%s' is too large: %jd",  
    653                                 BUF_STR(con->physical.path),  
     645                                SAFE_BUF_STR(con->physical.path),  
    654646                                (intmax_t) sce->st.st_size); 
    655647 
     
    660652        if (sce->st.st_size < 128) { 
    661653                if (con->conf.log_request_handling) TRACE("file '%s' is too small: %jd",  
    662                                 BUF_STR(con->physical.path),  
     654                                SAFE_BUF_STR(con->physical.path),  
    663655                                (intmax_t) sce->st.st_size); 
    664656 
     
    671663 
    672664                if (!compress_ds) { 
    673                         ERROR("evil: %s .. %s", BUF_STR(con->physical.path), BUF_STR(con->uri.path)); 
     665                        ERROR("evil: %s .. %s", SAFE_BUF_STR(con->physical.path), SAFE_BUF_STR(con->uri.path)); 
    674666 
    675667                        return HANDLER_GO_ON; 
     
    748740         * current version */ 
    749741        if (HANDLER_FINISHED == http_response_handle_cachable(srv, con, mtime)) { 
    750                 if (con->conf.log_request_handling) TRACE("%s is still the same, caching", BUF_STR(con->physical.path)); 
     742                if (con->conf.log_request_handling) TRACE("%s is still the same, caching", SAFE_BUF_STR(con->physical.path)); 
    751743                return HANDLER_FINISHED; 
    752744        } 
     
    780772                                CONST_BUF_LEN(sce->content_type)); 
    781773 
    782                 if (con->conf.log_request_handling) TRACE("looks like %s could be compressed", BUF_STR(con->physical.path)); 
     774                if (con->conf.log_request_handling) TRACE("looks like %s could be compressed", SAFE_BUF_STR(con->physical.path)); 
    783775                return HANDLER_FINISHED; 
    784776        }