Changeset 280

Show
Ignore:
Timestamp:
10/26/06 21:37:35 (2 years ago)
Author:
bart
Message:

Damit, forgot to add this one to the changeset

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • eaccelerator/trunk/eaccelerator.c

    r272 r280  
    44   +----------------------------------------------------------------------+ 
    55   | Copyright (c) 2004 - 2006 eAccelerator                               | 
    6    | http://eaccelerator.net                                                                                      | 
     6   | http://eaccelerator.net                                                                                                                                                                             | 
    77   +----------------------------------------------------------------------+ 
    88   | This program is free software; you can redistribute it and/or        | 
     
    9696static HashTable eaccelerator_global_class_table; 
    9797 
    98 int binary_eaccelerator_version
    99 int binary_php_version
    100 int binary_zend_version
     98int binary_eaccelerator_version[2]
     99int binary_php_version[2]
     100int binary_zend_version[2]
    101101 
    102102#ifdef ZEND_ENGINE_2 
     
    318318} 
    319319 
    320  
    321 static int encode_version(const char *s) { 
    322   unsigned int v1 = 0; 
    323   unsigned int v2 = 0; 
    324   unsigned int v3 = 0; 
    325   unsigned int c; 
    326   char m = '.'; 
    327   sscanf(s, "%u.%u%c%u",&v1,&v2,&m,&v3); 
    328   switch (m) { 
    329     case  'a': c = 0; break; 
    330     case  'b': c = 1; break; 
    331     case  '.': c = 2; break; 
    332     case  's': c = 15; break; 
    333     default: c = 2; 
    334   } 
    335   return ((v1 & 0xf) << 20) | 
    336          ((v2 & 0xff) << 12) | 
    337          ((c & 0xf) << 8) | 
    338          (v3 & 0xff); 
    339 
    340  
    341 /* This function isn't used. So disable it for now 
    342 static void decode_version(char *version, int v) { 
    343   int t = (v & 0x000f00) >> 8; 
    344   char c; 
    345   switch (t) { 
    346     case  0: c = 'a'; break; 
    347     case  1: c = 'b'; break; 
    348     case  2: c = '.'; break; 
    349     case 15: c = 's'; break; 
    350     default: c = '.'; 
    351   } 
    352   snprintf(version, 16, "%d.%d%c%d", (v & 0xf00000) >> 20, 
    353                                      (v & 0x0ff000) >> 12, 
    354                                      c, 
    355                                      (v & 0x0000ff)); 
    356 }  
    357 */ 
     320void encode_version(const char *str, int *version, int *extra) 
     321
     322    unsigned int a = 0; 
     323    unsigned int b = 0; 
     324    unsigned int c = 0; 
     325    unsigned int d = 0; 
     326    size_t len; 
     327    char s[255]; 
     328    char buf[255]; 
     329 
     330    len = strlen(str); 
     331    memcpy(buf, str, (len > 255) ? 255 : len); 
     332    buf[255] = '\0'; 
     333 
     334    memset(s, 0, 255); 
     335    sscanf(str, "%u.%u.%u%s", &a, &b, &c, s); 
     336 
     337    if (s[0] == '.') { 
     338        sscanf(s, ".%u-%s", &d, buf); 
     339    } else if (s[0] == '-') { 
     340        memcpy(buf, &s[1], 254); 
     341    } else { 
     342        memcpy(buf, s, 255); 
     343    } 
     344 
     345    *version = ((a & 0xff) << 24) | ((b & 0xff) << 16) | ((c & 0xff) << 8) | (d & 0xff); 
     346 
     347    if (buf[0] == 0) { 
     348        a = 0; 
     349        b = 0; 
     350    } else if (strncasecmp(buf, "rev", 3) == 0) { 
     351        a = 1; 
     352        sscanf(buf, "rev%u", &b); 
     353    } else if (strncasecmp(buf, "rc", 2) == 0) { 
     354        a = 2; 
     355        sscanf(buf, "rc%u", &b); 
     356    } else if (strncasecmp(buf, "beta", 4) == 0) { 
     357        a = 3; 
     358        sscanf(buf, "beta%u", &b); 
     359    } else { 
     360        a = 0xf; 
     361        // just encode the first 4 bytes 
     362        b = ((buf[0] & 0x7f) << 21) | ((buf[1] & 0x7f) << 14) | ((buf[2] & 0x7f) << 7) | (buf[3] & 0x7f); 
     363    } 
     364 
     365    *extra = ((a & 0xf) << 28) | (0x0fffffff & b); 
     366
     367 
     368static void decode_version(int version, int extra, char *str, size_t len) 
     369
     370    int number; 
     371 
     372    if ((version & 0xff) == 0) { 
     373        number = snprintf(str, len, "%u.%u.%u", (version >> 24), ((version >> 16) & 0xff), ((version >> 8) & 0xff)); 
     374    } else { 
     375        number = snprintf(str, len, "%u.%u.%u.%u", (version >> 24), ((version >> 16) & 0xff), ((version >> 8) & 0xff), (version & 0xff)); 
     376    } 
     377 
     378    if (extra != 0) { 
     379        unsigned int type = ((extra >> 28) & 0xf); 
     380        extra = (extra & 0x0fffffff); 
     381        switch (type) { 
     382            case 1: 
     383                snprintf(&str[number], len, "-rev%u", extra); 
     384                break; 
     385            case 2: 
     386                snprintf(&str[number], len, "-rc%u", extra); 
     387                break; 
     388            case 3: 
     389                snprintf(&str[number], len, "-beta%u", extra); 
     390                break; 
     391            case 15: 
     392                if (len >= number + 5) { 
     393                    str[number] = '-'; 
     394                    str[number + 1] = (extra >> 21) & 0x7f; 
     395                    str[number + 2] = (extra >> 14) & 0x7f; 
     396                    str[number + 3] = (extra >> 7) & 0x7f; 
     397                    str[number + 4] = extra & 0x7f; 
     398                    str[number + 5] = '\0'; 
     399                } 
     400                break; 
     401            default: 
     402                break; 
     403        } 
     404    } 
     405
    358406 
    359407static char num2hex[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; 
     
    565613 
    566614/******************************************************************************/ 
    567 /* Cache file functions.                                                                                                          */ 
     615/* Cache file functions.                                                                                                               */ 
    568616/******************************************************************************/ 
    569617 
     618/* A function to check if the header of a cache file valid is. 
     619 */ 
     620inline int check_header(ea_file_header *hdr) 
     621{ 
     622#ifdef DEBUG 
     623  char current[255]; 
     624  char cache[255]; 
     625#endif 
     626         
     627  if (strncmp(hdr->magic, EA_MAGIC, 8) != 0) { 
     628#ifdef DEBUG 
     629    ea_debug_printf(EA_DEBUG, "Magic header mismatch."); 
     630#endif 
     631        return 0;        
     632  } 
     633  if (hdr->eaccelerator_version[0] != binary_eaccelerator_version[0]  
     634      || hdr->eaccelerator_version[1] != binary_eaccelerator_version[1]) { 
     635#ifdef DEBUG 
     636    decode_version(hdr->eaccelerator_version[0], hdr->eaccelerator_version[1], cache, 255); 
     637    decode_version(binary_eaccelerator_version[0], binary_eaccelerator_version[1], current, 255); 
     638    ea_debug_printf(EA_DEBUG, "eAccelerator version mismatch, cache file %s and current version %s\n", cache, current); 
     639#endif 
     640    return 0; 
     641  } 
     642  if (hdr->zend_version[0] != binary_zend_version[0]  
     643      || hdr->zend_version[1] != binary_zend_version[1]) { 
     644#ifdef DEBUG 
     645    decode_version(hdr->zend_version[0], hdr->zend_version[1], cache, 255); 
     646    decode_version(binary_zend_version[0], binary_zend_version[1], current, 255); 
     647    ea_debug_printf(EA_DEBUG, "Zend version mismatch, cache file %s and current version %s\n", cache, current); 
     648#endif 
     649    return 0; 
     650  } 
     651  if (hdr->php_version[0] != binary_php_version[0]  
     652      || hdr->php_version[1] != binary_php_version[1]) { 
     653#ifdef DEBUG 
     654    decode_version(hdr->php_version[0], hdr->php_version[1], cache, 255); 
     655    decode_version(binary_php_version[0], binary_php_version[1], current, 255); 
     656    ea_debug_printf(EA_DEBUG, "PHP version mismatch, cache file %s and current version %s\n", cache, current); 
     657#endif 
     658    return 0; 
     659  } 
     660  return 1; 
     661} 
     662 
     663/* A function to create the header for a cache file. 
     664 */ 
     665inline void init_header(ea_file_header *hdr) 
     666{ 
     667  strncpy(hdr->magic, EA_MAGIC, 8); 
     668  hdr->eaccelerator_version[0] = binary_eaccelerator_version[0]; 
     669  hdr->eaccelerator_version[1] = binary_eaccelerator_version[1]; 
     670  hdr->zend_version[0] = binary_zend_version[0]; 
     671  hdr->zend_version[1] = binary_zend_version[1]; 
     672  hdr->php_version[0] = binary_php_version[0];   
     673  hdr->php_version[1] = binary_php_version[1]; 
     674} 
    570675/* Retrieve a cache entry from the cache directory */ 
    571676static ea_cache_entry* hash_find_file(const char  *key, struct stat *buf TSRMLS_DC) { 
     
    595700      return NULL; 
    596701    } 
    597     if (strncmp(hdr.magic, EA_MAGIC, 8) != 0 || 
    598         hdr.eaccelerator_version != binary_eaccelerator_version || 
    599         hdr.zend_version != binary_zend_version || 
    600         hdr.php_version != binary_php_version) { 
     702    if (check_header(&hdr)) { 
    601703      EACCELERATOR_FLOCK(f, LOCK_UN); 
    602704      close(f); 
     
    693795  if (f > 0) { 
    694796    EACCELERATOR_FLOCK(f, LOCK_EX); 
    695     strncpy(hdr.magic, EA_MAGIC, 8); 
    696     hdr.eaccelerator_version = binary_eaccelerator_version; 
    697     hdr.zend_version    = binary_zend_version; 
    698     hdr.php_version     = binary_php_version; 
     797    init_header(&hdr); 
    699798    hdr.size  = p->size; 
    700799    hdr.mtime = p->mtime; 
     
    796895      EAG(used_entries) = (void*)used; 
    797896      EAG(mem) = op_array->filename; 
    798             /* only restore the classes and functions when we restore this script  
    799             * for the first time.  
    800             */ 
     897                       /* only restore the classes and functions when we restore this script  
     898                        * for the first time.  
     899                        */ 
    801900      if (!zend_hash_exists(&EAG(restored), p->realfilename, strlen(p->realfilename))) { 
    802               for (e = p->c_head; e!=NULL; e = e->next) { 
     901                               for (e = p->c_head; e!=NULL; e = e->next) { 
    803902          restore_class(e TSRMLS_CC); 
    804903        } 
     
    806905          restore_function(e TSRMLS_CC); 
    807906        } 
    808                     zend_hash_add(&EAG(restored), p->realfilename, strlen(p->realfilename), NULL, 0, NULL);   
    809            
    810             EAG(mem) = p->realfilename; 
     907                               zend_hash_add(&EAG(restored), p->realfilename, strlen(p->realfilename), NULL, 0, NULL);   
     908                       
     909                       EAG(mem) = p->realfilename; 
    811910    } 
    812911  } 
     
    9341033    } 
    9351034  } else if (PG(include_path) == NULL ||  
    936                        file_handle->filename[0] == '.' || 
     1035                               file_handle->filename[0] == '.' || 
    9371036             IS_SLASH(file_handle->filename[0]) || 
    9381037             IS_ABSOLUTE_PATH(file_handle->filename,strlen(file_handle->filename))) { 
     
    11791278    Bucket *function_table_tail; 
    11801279    Bucket *class_table_tail; 
    1181     HashTable* orig_function_table; 
    1182     HashTable* orig_class_table; 
    1183     HashTable* orig_eg_class_table = NULL; 
    1184     HashTable tmp_function_table; 
    1185     HashTable tmp_class_table; 
    1186     zend_function tmp_func; 
    1187     zend_class_entry tmp_class; 
    11881280    int ea_bailout; 
    11891281 
     
    17151807  REGISTER_LONG_CONSTANT("EACCELERATOR_DISK_ONLY", ea_disk_only, CONST_CS | CONST_PERSISTENT); 
    17161808  REGISTER_LONG_CONSTANT("EACCELERATOR_NONE", ea_none, CONST_CS | CONST_PERSISTENT); 
    1717   binary_eaccelerator_version = encode_version(EACCELERATOR_VERSION); 
    1718   binary_php_version = encode_version(PHP_VERSION); 
    1719   binary_zend_version = encode_version(ZEND_VERSION); 
     1809  encode_version(EACCELERATOR_VERSION, &binary_eaccelerator_version[0], &binary_eaccelerator_version[1]); 
     1810  encode_version(PHP_VERSION, &binary_php_version[0], &binary_php_version[1]); 
     1811  encode_version(ZEND_VERSION, &binary_zend_version[0], &binary_zend_version[1]); 
    17201812  eaccelerator_is_extension = 1; 
    17211813