Changeset 249 for trunk/lib


Ignore:
Timestamp:
05/04/11 10:55:22 (13 years ago)
Author:
tim
Message:

reorganized iterator code for simpler locking and easier future key caching

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/regfi.c

    r233 r249  
    18231823  if(ret_val == NULL)
    18241824    return NULL;
     1825 
     1826  ret_val->cur = talloc(ret_val, REGFI_ITER_POSITION);
     1827  if(ret_val->cur == NULL)
     1828  {
     1829    talloc_free(ret_val);
     1830    return NULL;
     1831  }
     1832
     1833  ret_val->key_positions = void_stack_new(REGFI_MAX_DEPTH);
     1834  if(ret_val->key_positions == NULL)
     1835  {
     1836    talloc_free(ret_val);
     1837    return NULL;
     1838  }
     1839  talloc_reparent(NULL, ret_val, ret_val->key_positions);
    18251840
    18261841  root = (REGFI_NK*)regfi_get_rootkey(file);
     
    18301845    return NULL;
    18311846  }
    1832   ret_val->cur_key = root;
    1833   talloc_reparent(NULL, ret_val, root);
    1834 
    1835   ret_val->key_positions = void_stack_new(REGFI_MAX_DEPTH);
    1836   if(ret_val->key_positions == NULL)
    1837   {
    1838     talloc_free(ret_val);
    1839     return NULL;
    1840   }
    1841   talloc_reparent(NULL, ret_val, ret_val->key_positions);
    1842 
     1847
     1848  ret_val->cur->offset = root->offset;
     1849  if(root->subkeys_off == REGFI_OFFSET_NONE)
     1850    ret_val->cur->num_subkeys = 0;
     1851  else
     1852    ret_val->cur->num_subkeys = regfi_fetch_num_subkeys(root);
     1853 
     1854  if(root->values_off == REGFI_OFFSET_NONE)
     1855    ret_val->cur->num_values = 0;
     1856  else
     1857    ret_val->cur->num_values = regfi_fetch_num_values(root);
     1858
     1859  ret_val->cur->cur_subkey = 0;
     1860  ret_val->cur->cur_value = 0;
    18431861  ret_val->f = file;
    1844   ret_val->cur_subkey = 0;
    1845   ret_val->cur_value = 0;
    1846    
     1862
     1863  regfi_free_record(ret_val->f, root);
    18471864  return ret_val;
    18481865}
     
    18551872  talloc_unlink(NULL, i);
    18561873}
    1857 
    18581874
    18591875
     
    18641880{
    18651881  REGFI_NK* subkey;
    1866   REGFI_ITER_POSITION* pos;
    1867 
    1868   pos = talloc(i->key_positions, REGFI_ITER_POSITION);
     1882  REGFI_ITER_POSITION* pos = talloc(i, REGFI_ITER_POSITION);
    18691883  if(pos == NULL)
    18701884    return false;
     
    18731887  if(subkey == NULL)
    18741888  {
     1889    regfi_log_add(REGFI_LOG_WARN, "Could not obtain cur_subkey during"
     1890                  " iterator_down with subkey index (%d) and key offset=%.8X\n",
     1891                  i->cur->cur_subkey, i->cur->offset);
    18751892    talloc_free(pos);
    18761893    return false;
    18771894  }
    18781895
    1879   pos->nk = i->cur_key;
    1880   pos->cur_subkey = i->cur_subkey;
    1881   if(!void_stack_push(i->key_positions, pos))
     1896  if(!void_stack_push(i->key_positions, i->cur))
    18821897  {
    18831898    talloc_free(pos);
    1884     talloc_unlink(NULL, subkey);
     1899    regfi_free_record(i->f, subkey);
    18851900    return false;
    1886   } 
    1887   talloc_reparent(NULL, i, subkey);
    1888 
    1889   i->cur_key = subkey;
    1890   i->cur_subkey = 0;
    1891   i->cur_value = 0;
    1892 
     1901  }
     1902
     1903  pos->offset = subkey->offset;
     1904  if(subkey->subkeys_off == REGFI_OFFSET_NONE)
     1905    pos->num_subkeys = 0;
     1906  else
     1907    pos->num_subkeys = regfi_fetch_num_subkeys(subkey);
     1908
     1909  if(subkey->values_off == REGFI_OFFSET_NONE)
     1910    pos->num_values = 0;
     1911  else
     1912    pos->num_values = regfi_fetch_num_values(subkey);
     1913
     1914  pos->cur_subkey = 0;
     1915  pos->cur_value = 0;
     1916  i->cur = pos;
     1917
     1918  regfi_free_record(i->f, subkey);
    18931919  return true;
    18941920}
     
    19081934    return false;
    19091935 
    1910   talloc_unlink(i, i->cur_key);
     1936  talloc_unlink(i, i->cur);
     1937
    19111938  regfi_unlock(i->f, &i->f->mem_lock, "regfi_iterator_up");
    19121939
    1913   i->cur_key = pos->nk;
    1914   i->cur_subkey = pos->cur_subkey;
    1915   i->cur_value = 0;
    1916   talloc_free(pos);
    1917 
     1940  i->cur = pos;
    19181941  return true;
    19191942}
     
    19351958bool regfi_iterator_find_subkey(REGFI_ITERATOR* i, const char* name)
    19361959{
     1960  const REGFI_NK* cur_key;
    19371961  uint32_t new_index;
    1938 
    1939   if(regfi_find_subkey(i->f, i->cur_key, name, &new_index))
    1940   {
    1941     i->cur_subkey = new_index;
    1942     return true;
    1943   }
    1944 
    1945   return false;
     1962  bool ret_val = false;
     1963
     1964  cur_key = regfi_iterator_cur_key(i);
     1965  if(cur_key == NULL)
     1966    /* XXX: report error */
     1967    return ret_val;
     1968
     1969  if(regfi_find_subkey(i->f, cur_key, name, &new_index))
     1970  {
     1971    i->cur->cur_subkey = new_index;
     1972    ret_val = true;
     1973  }
     1974
     1975  regfi_free_record(i->f, cur_key);
     1976  return ret_val;
    19461977}
    19471978
     
    19822013    return ret_val;
    19832014
    1984   ret_val = talloc_reference(NULL, i->cur_key);
     2015  ret_val = regfi_load_key(i->f, i->cur->offset, i->f->string_encoding, true);
    19852016
    19862017  regfi_unlock(i->f, &i->f->mem_lock, "regfi_iterator_cur_key"); 
     
    20042035bool regfi_iterator_first_subkey(REGFI_ITERATOR* i)
    20052036{
    2006   i->cur_subkey = 0;
    2007  
    2008   return ((i->cur_key != NULL) && (i->cur_key->subkeys_off!=REGFI_OFFSET_NONE)
    2009           && (i->cur_subkey < regfi_fetch_num_subkeys(i->cur_key)));
     2037  i->cur->cur_subkey = 0;
     2038  return (i->cur->cur_subkey < i->cur->num_subkeys);
    20102039}
    20112040
     
    20152044const REGFI_NK* regfi_iterator_cur_subkey(REGFI_ITERATOR* i)
    20162045{
    2017   return regfi_get_subkey(i->f, i->cur_key, i->cur_subkey);
     2046  const REGFI_NK* cur_key;
     2047  const REGFI_NK* ret_val;
     2048 
     2049  cur_key = regfi_iterator_cur_key(i);
     2050  if(cur_key == NULL)
     2051    /* XXX: report error */
     2052    return NULL;
     2053
     2054  ret_val = regfi_get_subkey(i->f, cur_key, i->cur->cur_subkey);
     2055
     2056  regfi_free_record(i->f, cur_key);
     2057  return ret_val;
    20182058}
    20192059
     
    20232063bool regfi_iterator_next_subkey(REGFI_ITERATOR* i)
    20242064{
    2025   i->cur_subkey++;
    2026 
    2027   return ((i->cur_key != NULL) && (i->cur_key->subkeys_off!=REGFI_OFFSET_NONE)
    2028           && (i->cur_subkey < regfi_fetch_num_subkeys(i->cur_key)));
     2065  i->cur->cur_subkey++;
     2066  return (i->cur->cur_subkey < i->cur->num_subkeys);
    20292067}
    20302068
     
    20342072bool regfi_iterator_find_value(REGFI_ITERATOR* i, const char* name)
    20352073{
     2074  const REGFI_NK* cur_key;
    20362075  uint32_t new_index;
    2037 
    2038   if(regfi_find_value(i->f, i->cur_key, name, &new_index))
    2039   {
    2040     i->cur_value = new_index;
    2041     return true;
    2042   }
    2043 
    2044   return false;
     2076  bool ret_val = false;
     2077
     2078  cur_key = regfi_iterator_cur_key(i);
     2079  if(cur_key == NULL)
     2080    /* XXX: report error */
     2081    return ret_val;
     2082
     2083  if(regfi_find_value(i->f, cur_key, name, &new_index))
     2084  {
     2085    i->cur->cur_value = new_index;
     2086    ret_val = true;
     2087  }
     2088
     2089  regfi_free_record(i->f, cur_key);
     2090  return ret_val;
    20452091}
    20462092
     
    20502096bool regfi_iterator_first_value(REGFI_ITERATOR* i)
    20512097{
    2052   i->cur_value = 0;
    2053   return (i->cur_key->values != NULL && i->cur_key->values->elements != NULL
    2054           && (i->cur_value < regfi_fetch_num_values(i->cur_key)));
     2098  i->cur->cur_value = 0;
     2099  return (i->cur->cur_value < i->cur->num_values);
    20552100}
    20562101
     
    20602105const REGFI_VK* regfi_iterator_cur_value(REGFI_ITERATOR* i)
    20612106{
    2062   return regfi_get_value(i->f, i->cur_key, i->cur_value);
     2107  const REGFI_NK* cur_key;
     2108  const REGFI_VK* ret_val = NULL;
     2109
     2110  cur_key = regfi_iterator_cur_key(i);
     2111  if(cur_key == NULL)
     2112    /* XXX: report error */
     2113    return ret_val;
     2114
     2115  ret_val = regfi_get_value(i->f, cur_key, i->cur->cur_value);
     2116 
     2117  regfi_free_record(i->f, cur_key);
     2118  return ret_val;
    20632119}
    20642120
     
    20682124bool regfi_iterator_next_value(REGFI_ITERATOR* i)
    20692125{
    2070   i->cur_value++;
    2071   return (i->cur_key->values != NULL && i->cur_key->values->elements != NULL
    2072           && (i->cur_value < regfi_fetch_num_values(i->cur_key)));
     2126  i->cur->cur_value++;
     2127  return (i->cur->cur_value < i->cur->num_values);
     2128}
     2129
     2130
     2131
     2132
     2133/******************************************************************************
     2134 *****************************************************************************/
     2135const REGFI_NK** regfi_iterator_cur_path(REGFI_ITERATOR* i)
     2136{
     2137  REGFI_NK** ret_val;
     2138  void_stack_iterator* iter;
     2139  const REGFI_ITER_POSITION* cur;
     2140  uint16_t k;
     2141
     2142  ret_val = talloc_array(NULL, REGFI_NK*, void_stack_size(i->key_positions)+2);
     2143  if(ret_val == NULL)
     2144    return NULL;
     2145
     2146  iter = void_stack_iterator_new(i->key_positions);
     2147  if (iter == NULL)
     2148  {
     2149    talloc_free(ret_val);
     2150    return NULL;
     2151  }
     2152 
     2153  if(!regfi_lock(i->f, &i->f->mem_lock, "regfi_cur_path"))
     2154  {
     2155    talloc_free(ret_val);
     2156    return NULL;
     2157  }
     2158
     2159  for(cur=void_stack_iterator_next(iter), k=0;
     2160      cur != NULL; cur=void_stack_iterator_next(iter), k++)
     2161  {
     2162    ret_val[k] = regfi_load_key(i->f, cur->offset, i->f->string_encoding, true);
     2163    talloc_reparent(NULL, ret_val, ret_val[k]);
     2164  }
     2165  ret_val[k] = regfi_load_key(i->f, i->cur->offset, i->f->string_encoding, true);
     2166  talloc_reparent(NULL, ret_val, ret_val[k]);
     2167
     2168  regfi_unlock(i->f, &i->f->mem_lock, "regfi_cur_path");
     2169  void_stack_iterator_free(iter);
     2170
     2171  ret_val[++k] = NULL;
     2172  return (const REGFI_NK**)ret_val;
    20732173}
    20742174
Note: See TracChangeset for help on using the changeset viewer.