Exemple #1
0
sharefile_handle
sharefile_init (const char *mode)
{
  struct Hash_tuning;

  struct sharefile *p = malloc (sizeof (struct sharefile));
  if (p)
    {
      p->mode = strdup (mode);
      if (p->mode)
	{
	  p->table = hash_initialize (DefaultHashTableSize, NULL,
				      entry_hashfunc,
				      entry_comparator,
				      entry_free);
	  if (p->table)
	    {
	      return p;
	    }
	  else
	    {
	      free (p->mode);
	      free (p);
	    }
	}
      else
	{
	  free (p);
	}
    }
  return NULL;
}
Exemple #2
0
/* Create new exclude segment of given TYPE and OPTIONS, and attach it
   to the head of EX.  */
static void
new_exclude_segment (struct exclude *ex, enum exclude_type type, int options)
{
  struct exclude_segment *sp = xzalloc (sizeof (struct exclude_segment));
  sp->type = type;
  sp->options = options;
  switch (type)
    {
    case exclude_pattern:
      break;

    case exclude_hash:
      sp->v.table = hash_initialize (0, NULL,
                                     (options & FNM_CASEFOLD) ?
                                       string_hasher_ci
                                       : string_hasher,
                                     (options & FNM_CASEFOLD) ?
                                       string_compare_ci
                                       : string_compare,
                                     string_free);
      break;
    }
  sp->next = ex->head;
  ex->head = sp;
}
Exemple #3
0
/* Return zero if TABLE contains a LEN-character long prefix of STRING,
   otherwise, insert a newly allocated copy of this prefix to TABLE and
   return 1.  If RETURN_PREFIX is not NULL, point it to the allocated
   copy. */
static bool
hash_string_insert_prefix (Hash_table **table, char const *string, size_t len,
			   const char **return_prefix)
{
  Hash_table *t = *table;
  char *s;
  char *e;

  if (len)
    {
      s = xmalloc (len + 1);
      memcpy (s, string, len);
      s[len] = 0;
    }
  else
    s = xstrdup (string);
  
  if (! ((t
	  || (*table = t = hash_initialize (0, 0, hash_string_hasher,
					    hash_string_compare, 0)))
	 && (e = hash_insert (t, s))))
    xalloc_die ();

  if (e == s)
    {
      if (return_prefix)
	*return_prefix = s;
      return 1;
    }
  else
    {
      free (s);
      return 0;
    }
}
Exemple #4
0
static bool
init_html_ucs2 (RECODE_STEP step,
		RECODE_CONST_REQUEST request,
		RECODE_CONST_OPTION_LIST before_options,
		RECODE_CONST_OPTION_LIST after_options,
		unsigned mask)
{
  Hash_table *table;
  struct ucs2_to_string const *cursor;

  if (before_options || after_options)
    return false;

  table = hash_initialize (0, NULL, string_hash, string_compare, NULL);
  if (!table)
    return false;

  for (cursor = translations; cursor->code; cursor++)
    if (cursor->flags & mask
	&& (!request->diacritics_only || cursor->code > 128))
      if (!hash_insert (table, cursor))
	return false;

  step->step_type = RECODE_STRING_TO_UCS2;
  step->step_table = table;
  return true;
}
Exemple #5
0
int
alias_install(const char *kw, int argc, char **argv, grecs_locus_t *ploc)
{
    struct alias *sample = xmalloc(sizeof(*sample)),
                  *ap;

    sample->kw = xstrdup(kw);
    sample->argc = argc;
    sample->argv = argv;
    sample->locus = *ploc;

    if (! ((alias_table
            || (alias_table = hash_initialize(0, 0,
                              alias_hasher,
                              alias_compare,
                              NULL)))
            && (ap = hash_insert(alias_table, sample))))
        xalloc_die();

    if (ap != sample) {
        free(sample->kw);
        free(sample);
        grecs_error(ploc, 0, _("alias `%s' already defined"), kw);
        grecs_error(&ap->locus, 0,
                    _("this is the location of the previous definition"));
        return 1; /* Found */
    }
    return 0;
}
Exemple #6
0
void
init_event_handlers (void)
{
  assert (event_handlers == NULL);
  event_handlers =
    hash_initialize (64, NULL, entry_hash, entry_compare, entry_free);
}
Exemple #7
0
static Hash_table * mnt_new(size_t n)
{
	Hash_table *rv = hash_initialize(n, NULL, me_hash, me_cmp, me_free);

	if (!rv)
		mog_oom();
	return rv;
}
Exemple #8
0
void
init_backup_hash_table (void)
{
  file_id_table = hash_initialize (0, NULL, file_id_hasher,
				   file_id_comparator, free);
  if (!file_id_table)
    xalloc_die ();
}
Exemple #9
0
bool
init_combine (RECODE_STEP step,
	      RECODE_CONST_REQUEST request,
	      RECODE_CONST_OPTION_LIST before_options,
	      RECODE_CONST_OPTION_LIST after_options)
{
  const unsigned short *data = (const unsigned short *) step->step_table;
  Hash_table *table;

  if (before_options || after_options)
    return false;

  table = hash_initialize (0, NULL, state_hash, state_compare, state_free);

  if (!table)
    return false;
  step->step_type = RECODE_COMBINE_STEP;
  step->step_table = table;

  if (!data)
    return true;

  while (*data != DONE)
    {
      unsigned short result = *data++;
      struct state *state = NULL;

      while (*data != DONE)
	if (*data == ELSE)
	  {
	    if (state)
	      {
		if (state->result != NOT_A_CHARACTER)
		  abort ();
		state->result = result;
		state = NULL;
	      }
	    data++;
	  }
	else
	  {
	    state = prepare_shifted_state (state, *data++, step);
	    if (!state)
	      return false;
	  }

      if (state)
	{
	  if (state->result != NOT_A_CHARACTER
	      && state->result != state->character)
	    abort ();
	  state->result = result;
	}
      data++;
    }

  return true;
}
Exemple #10
0
/* Initialize the hash table implementing a set of F_triple entries
   corresponding to destination files.  */
void
dest_info_init (struct cp_options *x)
{
  x->dest_info
    = hash_initialize (DEST_INFO_INITIAL_CAPACITY,
		       NULL,
		       triple_hash,
		       triple_compare,
		       triple_free);
}
Exemple #11
0
/* Initialize the hash table.  */
extern void
hash_init (void)
{
  src_to_dest = hash_initialize (INITIAL_TABLE_SIZE, NULL,
				 src_to_dest_hash,
				 src_to_dest_compare,
				 src_to_dest_free);
  if (src_to_dest == NULL)
    xalloc_die ();
}
Exemple #12
0
void
muscle_init (void)
{
  /* Initialize the muscle obstack.  */
  obstack_init (&muscle_obstack);

  muscle_table = hash_initialize (HT_INITIAL_CAPACITY, NULL, hash_muscle,
                                  hash_compare_muscles, muscle_entry_free);

  /* Version and input file.  */
  MUSCLE_INSERT_STRING ("version", VERSION);
}
Exemple #13
0
/* Create and link a new directory entry for directory NAME, having a
   device number DEV and an inode number INO, with NFS indicating
   whether it is an NFS device and FOUND indicating whether we have
   found that the directory exists.  */
static struct directory *
note_directory (char const *name, struct timespec mtime,
		dev_t dev, ino_t ino, bool nfs, bool found,
		const char *contents)
{
  struct directory *directory = attach_directory (name);

  directory->mtime = mtime;
  directory->device_number = dev;
  directory->inode_number = ino;
  directory->children = CHANGED_CHILDREN;
  if (nfs)
    DIR_SET_FLAG (directory, DIRF_NFS);
  if (found)
    DIR_SET_FLAG (directory, DIRF_FOUND);
  if (contents)
    directory->dump = dumpdir_create (contents);
  else
    directory->dump = NULL;

  if (! ((directory_table
	  || (directory_table = hash_initialize (0, 0,
						 hash_directory_canonical_name,
						 compare_directory_canonical_names,
						 0)))
	 && hash_insert (directory_table, directory)))
    xalloc_die ();

  if (! ((directory_meta_table
	  || (directory_meta_table = hash_initialize (0, 0,
						      hash_directory_meta,
						      compare_directory_meta,
						      0)))
	 && hash_insert (directory_meta_table, directory)))
    xalloc_die ();

  return directory;
}
Exemple #14
0
/*****************************************************************************
 * Cache_Create
 *****************************************************************************/
Cache* 
Cache_Create (void* talloc_context, size_t size, time_t max_age,
	      Cache_FreeExpiredData free_expired_data)
{
	if (size < 1) {
		Log_Printf (LOG_ERROR, "Cache: invalid size : %ld\n", 
			    (long) size);
		return NULL; // ---------->
	}
	Cache* cache = talloc (talloc_context, Cache);
	if (cache == NULL)
		return NULL; // ---------->
	*cache = (Cache) { 
		.size    	   = size,
		.max_age 	   = max_age,
		.next_clean	   = 0,
		.free_expired_data = free_expired_data,
		// other data initialized to 0
	};  
#if CACHE_FIXED_SIZE
	cache->table = talloc_array (cache, Entry, size);
	if (cache->table) {
		int i;
		for (i = 0; i < size; i++)
			cache->table[i] = (Entry) { 
				.key  = NULL, 
				.rip  = 0,
				.data = NULL
			};
	}
#else
	cache->table = hash_initialize (size, NULL,
					cache_hasher, cache_comparator,
					NULL);
#endif
	if (cache->table == NULL) {
		Log_Printf (LOG_ERROR, "Cache: can't create table");
		talloc_free (cache);
		return NULL; // ---------->
	}
	
	talloc_set_destructor (cache, cache_destroy);
	return cache;
}
Exemple #15
0
/* Initialize the hash table implementing a set of F_triple entries
   corresponding to source files listed on the command line.  */
void
src_info_init (struct cp_options *x)
{

  /* Note that we use triple_hash_no_name here.
     Contrast with the use of triple_hash above.
     That is necessary because a source file may be specified
     in many different ways.  We want to warn about this
       cp a a d/
     as well as this:
       cp a ./a d/
  */
  x->src_info
    = hash_initialize (DEST_INFO_INITIAL_CAPACITY,
		       NULL,
		       triple_hash_no_name,
		       triple_compare,
		       triple_free);
}
Exemple #16
0
/* Set up the cycle-detection machinery:  */
static bool
setup_dir(FTS *fts)
{
  if (fts->fts_options & (FTS_TIGHT_CYCLE_CHECK | FTS_LOGICAL)) {
      enum { HT_INITIAL_SIZE = 31 };
      fts->fts_cycle.ht = hash_initialize((size_t)HT_INITIAL_SIZE, NULL,
										  AD_hash, AD_compare, free);
      if (! fts->fts_cycle.ht) {
		  return false;
	  }
  } else {
      fts->fts_cycle.state = (struct cycle_check_state *)malloc(sizeof *fts->fts_cycle.state);
      if (! fts->fts_cycle.state) {
		  return false;
	  }
      cycle_check_init(fts->fts_cycle.state);
  }

  return true;
}
Exemple #17
0
/* Return true if we've already seen the triple, <FILENAME, dev, ino>.
   If *HT is not initialized, initialize it.  */
static bool
seen_triple (Hash_table **ht, char const *filename, struct stat const *st)
{
  if (*ht == NULL)
    {
      size_t initial_capacity = 7;
      *ht = hash_initialize (initial_capacity,
                            NULL,
                            triple_hash,
                            triple_compare_ino_str,
                            triple_free);
      if (*ht == NULL)
        xalloc_die ();
    }

  if (seen_file (*ht, filename, st))
    return true;

  record_file (*ht, filename, st);
  return false;
}
Exemple #18
0
void
guestfs_set_private (guestfs_h *g, const char *key, void *data)
{
  if (g->pda == NULL) {
    g->pda = hash_initialize (16, NULL, hasher, comparator, freer);
    if (g->pda == NULL)
      g->abort_cb ();
  }

  struct pda_entry *new_entry = safe_malloc (g, sizeof *new_entry);
  new_entry->key = safe_strdup (g, key);
  new_entry->data = data;

  struct pda_entry *old_entry = hash_delete (g->pda, new_entry);
  freer (old_entry);

  struct pda_entry *entry = hash_insert (g->pda, new_entry);
  if (entry == NULL)
    g->abort_cb ();
  assert (entry == new_entry);
}
  ac_rtld_config::ac_rtld_config() {
    int mapfile_fd;

    config_loaded = false;
    hashtable = NULL;

    /* Our config file exists? */
    mapfile_fd = find_config_file ("ac_rtld.relmap");
    
    if (mapfile_fd >= 0) {      
    
      if (hash_initialize(&hashtable) == FINE) {

	if (process_map_file(mapfile_fd, hashtable) == FINE) {
	  config_loaded = true;
	} else {
	  hash_delete(&hashtable);
	}
      }

      close(mapfile_fd);
    }
  }
Exemple #20
0
int main(int argc ,char ** argv)
{

    Hash_table * myhash=hash_initialize (100,NULL,hash_fun,hash_str_cmp,free);
    char str[256];
    struct tl * ptl=NULL ;
    FILE * fp=fopen("c:\\test.log","r");
    while(NULL != fgets(str,256,fp))
    {
        ptl=make_tl(str);
        hash_insert(myhash,ptl);
        //      printf("%s 44444 %s  ",ptl->s1,ptl->s2);
    }
    fclose(fp);
	hash_print(myhash);
    printf("-----------------------------------------------------\n");
    struct tl dd ;
    struct tl* s;
    strcpy(dd.s1,"888033");
    s=hash_lookup (myhash,&dd);
    printf("%s the value %s  ",dd.s1,s->s2);
    hash_print_statistics(myhash,stdout);
    return 0;
}
Exemple #21
0
int
main (int argc, char **argv)
{
  unsigned int mapfile_fd;
  hash_node **hashtable;
  char reverse_mode;

  if (hash_initialize(&hashtable) == ACRELCONVERT_FUNC_ERROR)
    exit(EXIT_FAILURE);
    
  if (process_parameters(argc, argv, &reverse_mode) == ACRELCONVERT_FUNC_ERROR)
    exit(EXIT_FAILURE);

  if ((mapfile_fd = open(argv[2], 0)) == -1) {
    fprintf(stderr, "Fatal error while opening map file \"%s\"\n", argv[2]);
    exit(EXIT_FAILURE);
  } 

  if (process_map_file(mapfile_fd, hashtable, reverse_mode) == ACRELCONVERT_FUNC_ERROR)
    {
      close(mapfile_fd);
      exit(EXIT_FAILURE);
    }
  close(mapfile_fd);

  if (process_elf(argv[3], hashtable) == ACRELCONVERT_FUNC_ERROR)
    {
      hash_delete (&hashtable);
      exit(EXIT_FAILURE);
    }

  hash_delete (&hashtable);

  exit(EXIT_SUCCESS);

}
Exemple #22
0
int main(int argc , char **argv ) 
{ int optc ;
  _Bool ok ;
  _Bool delim_specified ;
  char *spec_list_string ;
  char *tmp ;
  char *tmp___0 ;
  char *tmp___1 ;
  size_t tmp___2 ;
  char *tmp___3 ;
  char *tmp___4 ;
  char *tmp___5 ;
  char *tmp___6 ;
  char *tmp___7 ;
  _Bool tmp___8 ;
  _Bool tmp___9 ;
  int *tmp___10 ;
  int tmp___11 ;
  int tmp___12 ;

  {
  delim_specified = (_Bool)0;
  set_program_name((char const   *)*(argv + 0));
  setlocale(6, "");
  bindtextdomain("coreutils", "/usr/local/share/locale");
  textdomain("coreutils");
  atexit(& close_stdout);
  operating_mode = (enum operating_mode )0;
  suppress_non_delimited = (_Bool)0;
  delim = (unsigned char )'\000';
  have_read_stdin = (_Bool)0;
  while (1) {
    optc = getopt_long(argc, (char * const  *)argv, "b:c:d:f:ns", longopts, (int *)((void *)0));
    if (! (optc != -1)) {
      break;
    } else {

    }
    switch (optc) {
    case 98: 
    case 99: 
    if ((unsigned int )operating_mode != 0U) {
      while (1) {
        tmp = gettext("only one type of list may be specified");
        error(0, 0, (char const   *)tmp);
        usage(1);
        break;
      }
    } else {

    }
    operating_mode = (enum operating_mode )1;
    spec_list_string = optarg;
    break;
    case 102: 
    if ((unsigned int )operating_mode != 0U) {
      while (1) {
        tmp___0 = gettext("only one type of list may be specified");
        error(0, 0, (char const   *)tmp___0);
        usage(1);
        break;
      }
    } else {

    }
    operating_mode = (enum operating_mode )2;
    spec_list_string = optarg;
    break;
    case 100: 
    if ((int )*(optarg + 0) != 0) {
      if ((int )*(optarg + 1) != 0) {
        while (1) {
          tmp___1 = gettext("the delimiter must be a single character");
          error(0, 0, (char const   *)tmp___1);
          usage(1);
          break;
        }
      } else {

      }
    } else {

    }
    delim = (unsigned char )*(optarg + 0);
    delim_specified = (_Bool)1;
    break;
    case 128: 
    output_delimiter_specified = (_Bool)1;
    if ((int )*(optarg + 0) == 0) {
      output_delimiter_length = 1UL;
    } else {
      tmp___2 = strlen((char const   *)optarg);
      output_delimiter_length = tmp___2;
    }
    output_delimiter_string = xstrdup((char const   *)optarg);
    break;
    case 110: 
    break;
    case 115: 
    suppress_non_delimited = (_Bool)1;
    break;
    case 129: 
    complement = (_Bool)1;
    break;
    case -130: 
    usage(0);
    break;
    case -131: 
    version_etc(stdout, "cut", "GNU coreutils", Version, "David M. Ihnat", "David MacKenzie", "Jim Meyering", (char *)((void *)0));
    exit(0);
    break;
    default: 
    usage(1);
    }
  }
  if ((unsigned int )operating_mode == 0U) {
    while (1) {
      tmp___3 = gettext("you must specify a list of bytes, characters, or fields");
      error(0, 0, (char const   *)tmp___3);
      usage(1);
      break;
    }
  } else {

  }
  if ((int )delim != 0) {
    if ((unsigned int )operating_mode != 2U) {
      while (1) {
        tmp___4 = gettext("an input delimiter may be specified only when operating on fields");
        error(0, 0, (char const   *)tmp___4);
        usage(1);
        break;
      }
    } else {

    }
  } else {

  }
  if (suppress_non_delimited) {
    if ((unsigned int )operating_mode != 2U) {
      while (1) {
        tmp___5 = gettext("suppressing non-delimited lines makes sense\n\tonly when operating on fields");
        error(0, 0, (char const   *)tmp___5);
        usage(1);
        break;
      }
    } else {

    }
  } else {

  }
  if (output_delimiter_specified) {
    range_start_ht = hash_initialize(31UL, (Hash_tuning const   *)((void *)0), & hash_int, & hash_compare_ints, (void (*)(void * ))((void *)0));
    if ((unsigned long )range_start_ht == (unsigned long )((void *)0)) {
      xalloc_die();
    } else {

    }
  } else {

  }
  tmp___8 = set_fields((char const   *)spec_list_string);
  if (! tmp___8) {
    if ((unsigned int )operating_mode == 2U) {
      while (1) {
        tmp___6 = gettext("missing list of fields");
        error(0, 0, (char const   *)tmp___6);
        usage(1);
        break;
      }
    } else {
      while (1) {
        tmp___7 = gettext("missing list of positions");
        error(0, 0, (char const   *)tmp___7);
        usage(1);
        break;
      }
    }
  } else {

  }
  if (! delim_specified) {
    delim = (unsigned char )'\t';
  } else {

  }
  if ((unsigned long )output_delimiter_string == (unsigned long )((void *)0)) {
    dummy[0] = (char )delim;
    dummy[1] = (char )'\000';
    output_delimiter_string = dummy;
    output_delimiter_length = 1UL;
  } else {

  }
  if (optind == argc) {
    ok = cut_file("-");
  } else {
    ok = (_Bool)1;
    while (optind < argc) {
      tmp___9 = cut_file((char const   *)*(argv + optind));
      ok = (_Bool )((int )ok & (int )tmp___9);
      optind ++;
    }
  }
  if (range_start_ht) {
    hash_free(range_start_ht);
  } else {

  }
  if (have_read_stdin) {
    tmp___11 = fclose(stdin);
    if (tmp___11 == -1) {
      tmp___10 = __errno_location();
      error(0, *tmp___10, "-");
      ok = (_Bool)0;
    } else {

    }
  } else {

  }
  if (ok) {
    tmp___12 = 0;
  } else {
    tmp___12 = 1;
  }
  exit(tmp___12);
}
}
int
utimecmp (char const *dst_name,
	  struct stat const *dst_stat,
	  struct stat const *src_stat,
	  int options)
{
  /* Things to watch out for:

     The code uses a static hash table internally and is not safe in the
     presence of signals, multiple threads, etc.

     int and long int might be 32 bits.  Many of the calculations store
     numbers up to 2 billion, and multiply by 10; they have to avoid
     multiplying 2 billion by 10, as this exceeds 32-bit capabilities.

     time_t might be unsigned.  */

  verify (TYPE_IS_INTEGER (time_t));
  verify (TYPE_TWOS_COMPLEMENT (int));

  /* Destination and source time stamps.  */
  time_t dst_s = dst_stat->st_mtime;
  time_t src_s = src_stat->st_mtime;
  int dst_ns = get_stat_mtime_ns (dst_stat);
  int src_ns = get_stat_mtime_ns (src_stat);

  if (options & UTIMECMP_TRUNCATE_SOURCE)
    {
      /* Look up the time stamp resolution for the destination device.  */

      /* Hash table for devices.  */
      static Hash_table *ht;

      /* Information about the destination file system.  */
      static struct fs_res *new_dst_res;
      struct fs_res *dst_res;

      /* Time stamp resolution in nanoseconds.  */
      int res;

      if (! ht)
	ht = hash_initialize (16, NULL, dev_info_hash, dev_info_compare, free);
      if (! new_dst_res)
	{
	  new_dst_res = xmalloc (sizeof *new_dst_res);
	  new_dst_res->resolution = 2 * BILLION;
	  new_dst_res->exact = false;
	}
      new_dst_res->dev = dst_stat->st_dev;
      dst_res = hash_insert (ht, new_dst_res);
      if (! dst_res)
	xalloc_die ();

      if (dst_res == new_dst_res)
	{
	  /* NEW_DST_RES is now in use in the hash table, so allocate a
	     new entry next time.  */
	  new_dst_res = NULL;
	}

      res = dst_res->resolution;

      if (! dst_res->exact)
	{
	  /* This file system's resolution is not known exactly.
	     Deduce it, and store the result in the hash table.  */

	  time_t dst_a_s = dst_stat->st_atime;
	  time_t dst_c_s = dst_stat->st_ctime;
	  time_t dst_m_s = dst_s;
	  int dst_a_ns = get_stat_atime_ns (dst_stat);
	  int dst_c_ns = get_stat_ctime_ns (dst_stat);
	  int dst_m_ns = dst_ns;

	  /* Set RES to an upper bound on the file system resolution
	     (after truncation due to SYSCALL_RESOLUTION) by inspecting
	     the atime, ctime and mtime of the existing destination.
	     We don't know of any file system that stores atime or
	     ctime with a higher precision than mtime, so it's valid to
	     look at them too.  */
	  {
	    bool odd_second = (dst_a_s | dst_c_s | dst_m_s) & 1;

	    if (SYSCALL_RESOLUTION == BILLION)
	      {
		if (odd_second | dst_a_ns | dst_c_ns | dst_m_ns)
		  res = BILLION;
	      }
	    else
	      {
		int a = dst_a_ns;
		int c = dst_c_ns;
		int m = dst_m_ns;

		/* Write it this way to avoid mistaken GCC warning
		   about integer overflow in constant expression.  */
		int SR10 = SYSCALL_RESOLUTION;  SR10 *= 10;

		if ((a % SR10 | c % SR10 | m % SR10) != 0)
		  res = SYSCALL_RESOLUTION;
		else
		  for (res = SR10, a /= SR10, c /= SR10, m /= SR10;
		       (res < dst_res->resolution
			&& (a % 10 | c % 10 | m % 10) == 0);
		       res *= 10, a /= 10, c /= 10, m /= 10)
		    if (res == BILLION)
		      {
			if (! odd_second)
			  res *= 2;
			break;
		      }
	      }

	    dst_res->resolution = res;
	  }

	  if (SYSCALL_RESOLUTION < res)
	    {
	      struct timespec timespec[2];
	      struct stat dst_status;

	      /* Ignore source time stamp information that must necessarily
		 be lost when filtered through utimens.  */
	      src_ns -= src_ns % SYSCALL_RESOLUTION;

	      /* If the time stamps disagree widely enough, there's no need
		 to interrogate the file system to deduce the exact time
		 stamp resolution; return the answer directly.  */
	      {
		time_t s = src_s & ~ (res == 2 * BILLION);
		if (src_s < dst_s || (src_s == dst_s && src_ns <= dst_ns))
		  return 1;
		if (dst_s < s
		    || (dst_s == s && dst_ns < src_ns - src_ns % res))
		  return -1;
	      }

	      /* Determine the actual time stamp resolution for the
		 destination file system (after truncation due to
		 SYSCALL_RESOLUTION) by setting the access time stamp of the
		 destination to the existing access time, except with
		 trailing nonzero digits.  */

	      timespec[0].tv_sec = dst_a_s;
	      timespec[0].tv_nsec = dst_a_ns;
	      timespec[1].tv_sec = dst_m_s | (res == 2 * BILLION);
	      timespec[1].tv_nsec = dst_m_ns + res / 9;

	      /* Set the modification time.  But don't try to set the
		 modification time of symbolic links; on many hosts this sets
		 the time of the pointed-to file.  */
	      if (S_ISLNK (dst_stat->st_mode)
		  || utimens (dst_name, timespec) != 0)
		return -2;

	      /* Read the modification time that was set.  It's safe to call
		 'stat' here instead of worrying about 'lstat'; either the
		 caller used 'stat', or the caller used 'lstat' and found
		 something other than a symbolic link.  */
	      {
		int stat_result = stat (dst_name, &dst_status);

		if (stat_result
		    | (dst_status.st_mtime ^ dst_m_s)
		    | (get_stat_mtime_ns (&dst_status) ^ dst_m_ns))
		  {
		    /* The modification time changed, or we can't tell whether
		       it changed.  Change it back as best we can.  */
		    timespec[1].tv_sec = dst_m_s;
		    timespec[1].tv_nsec = dst_m_ns;
		    utimens (dst_name, timespec);
		  }

		if (stat_result != 0)
		  return -2;
	      }

	      /* Determine the exact resolution from the modification time
		 that was read back.  */
	      {
		int old_res = res;
		int a = (BILLION * (dst_status.st_mtime & 1)
			 + get_stat_mtime_ns (&dst_status));

		res = SYSCALL_RESOLUTION;

		for (a /= res; a % 10 != 0; a /= 10)
		  {
		    if (res == BILLION)
		      {
			res *= 2;
			break;
		      }
		    res *= 10;
		    if (res == old_res)
		      break;
		  }
	      }
	    }

	  dst_res->resolution = res;
	  dst_res->exact = true;
	}

      /* Truncate the source's time stamp according to the resolution.  */
      src_s &= ~ (res == 2 * BILLION);
      src_ns -= src_ns % res;
    }

  /* Compare the time stamps and return -1, 0, 1 accordingly.  */
  return (dst_s < src_s ? -1
	  : dst_s > src_s ? 1
	  : dst_ns < src_ns ? -1
	  : dst_ns > src_ns);
}
Exemple #24
0
int
main (int argc, char **argv)
{
  unsigned int i;
  unsigned int k;
  unsigned int table_size[] = {1, 2, 3, 4, 5, 23, 53};
  Hash_table *ht;
  Hash_tuning tuning;

  hash_reset_tuning (&tuning);
  tuning.shrink_threshold = 0.3;
  tuning.shrink_factor = 0.707;
  tuning.growth_threshold = 1.5;
  tuning.growth_factor = 2.0;
  tuning.is_n_buckets = true;

  if (1 < argc)
    {
      unsigned int seed;
      if (get_seed (argv[1], &seed) != 0)
        {
          fprintf (stderr, "invalid seed: %s\n", argv[1]);
          exit (EXIT_FAILURE);
        }

      srand (seed);
    }

  for (i = 0; i < ARRAY_CARDINALITY (table_size); i++)
    {
      size_t sz = table_size[i];
      ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL);
      ASSERT (ht);
      insert_new (ht, "a");
      {
        char *str1 = strdup ("a");
        char *str2;
        ASSERT (str1);
        str2 = hash_insert (ht, str1);
        ASSERT (str1 != str2);
        ASSERT (STREQ (str1, str2));
        free (str1);
      }
      insert_new (ht, "b");
      insert_new (ht, "c");
      i = 0;
      ASSERT (hash_do_for_each (ht, walk, &i) == 3);
      ASSERT (i == 7);
      {
        void *buf[5] = { NULL };
        ASSERT (hash_get_entries (ht, NULL, 0) == 0);
        ASSERT (hash_get_entries (ht, buf, 5) == 3);
        ASSERT (STREQ (buf[0], "a") || STREQ (buf[0], "b") || STREQ (buf[0], "c"));
      }
      ASSERT (hash_delete (ht, "a"));
      ASSERT (hash_delete (ht, "a") == NULL);
      ASSERT (hash_delete (ht, "b"));
      ASSERT (hash_delete (ht, "c"));

      ASSERT (hash_rehash (ht, 47));
      ASSERT (hash_rehash (ht, 467));

      /* Free an empty table. */
      hash_clear (ht);
      hash_free (ht);

      ht = hash_initialize (sz, NULL, hash_pjw, hash_compare_strings, NULL);
      ASSERT (ht);

      insert_new (ht, "z");
      insert_new (ht, "y");
      insert_new (ht, "x");
      insert_new (ht, "w");
      insert_new (ht, "v");
      insert_new (ht, "u");

      hash_clear (ht);
      ASSERT (hash_get_n_entries (ht) == 0);
      hash_free (ht);

      /* Test pointer hashing.  */
      ht = hash_initialize (sz, NULL, NULL, NULL, NULL);
      ASSERT (ht);
      {
        char *str = strdup ("a");
        ASSERT (str);
        insert_new (ht, "a");
        insert_new (ht, str);
        ASSERT (hash_lookup (ht, str) == str);
        free (str);
      }
      hash_free (ht);
    }

  hash_reset_tuning (&tuning);
  tuning.shrink_threshold = 0.3;
  tuning.shrink_factor = 0.707;
  tuning.growth_threshold = 1.5;
  tuning.growth_factor = 2.0;
  tuning.is_n_buckets = true;
  /* Invalid tuning.  */
  ht = hash_initialize (4651, &tuning, hash_pjw, hash_compare_strings,
                        hash_freer);
  ASSERT (!ht);

  /* Alternate tuning.  */
  tuning.growth_threshold = 0.89;

  /* Run with default tuning, then with custom tuning settings.  */
  for (k = 0; k < 2; k++)
    {
      Hash_tuning const *tune = (k == 0 ? NULL : &tuning);
      /* Now, each entry is malloc'd.  */
      ht = hash_initialize (4651, tune, hash_pjw,
                            hash_compare_strings, hash_freer);
      ASSERT (ht);
      for (i = 0; i < 10000; i++)
        {
          unsigned int op = rand () % 10;
          switch (op)
            {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
              {
                char buf[50];
                char const *p = uinttostr (i, buf);
                char *p_dup = strdup (p);
                ASSERT (p_dup);
                insert_new (ht, p_dup);
              }
              break;

            case 6:
              {
                size_t n = hash_get_n_entries (ht);
                ASSERT (hash_rehash (ht, n + rand () % 20));
              }
              break;

            case 7:
              {
                size_t n = hash_get_n_entries (ht);
                size_t delta = rand () % 20;
                if (delta < n)
                  ASSERT (hash_rehash (ht, n - delta));
              }
              break;

            case 8:
            case 9:
              {
                /* Delete a random entry.  */
                size_t n = hash_get_n_entries (ht);
                if (n)
                  {
                    size_t kk = rand () % n;
                    void const *p;
                    void *v;
                    for (p = hash_get_first (ht); kk;
                         --kk, p = hash_get_next (ht, p))
                      {
                        /* empty */
                      }
                    ASSERT (p);
                    v = hash_delete (ht, p);
                    ASSERT (v);
                    free (v);
                  }
                break;
              }
            }
          ASSERT (hash_table_ok (ht));
        }

      hash_free (ht);
    }

  return 0;
}
Exemple #25
0
void
collect_and_sort_names (void)
{
  struct name *name;
  struct name *next_name, *prev_name;
  int num_names;
  struct stat statbuf;
  Hash_table *nametab;
  
  name_gather ();

  if (!namelist)
    addname (".", 0, false, NULL);

  if (listed_incremental_option)
    {
      switch (chdir_count ())
	{
	case 0:
	  break;

	case 1:
	  if (namelist->change_dir == 0)
	    USAGE_ERROR ((0, 0,
			  _("Using -C option inside file list is not "
			    "allowed with --listed-incremental")));
	  break;

	default:
	  USAGE_ERROR ((0, 0,
			_("Only one -C option is allowed with "
			  "--listed-incremental")));
	}

      read_directory_file ();
    }
  
  num_names = 0;
  for (name = namelist; name; name = name->next, num_names++)
    {
      if (name->found_count || name->directory)
	continue;
      if (name->matching_flags & EXCLUDE_WILDCARDS)
	/* NOTE: EXCLUDE_ANCHORED is not relevant here */
	/* FIXME: just skip regexps for now */
	continue;
      chdir_do (name->change_dir);

      if (name->name[0] == 0)
	continue;

      if (deref_stat (dereference_option, name->name, &statbuf) != 0)
	{
	  stat_diag (name->name);
	  continue;
	}
      if (S_ISDIR (statbuf.st_mode))
	{
	  name->found_count++;
	  add_hierarchy_to_namelist (name, statbuf.st_dev, true);
	}
    }

  namelist = merge_sort (namelist, num_names, compare_names);

  num_names = 0;
  nametab = hash_initialize (0, 0,
			     name_hash,
			     name_compare, NULL);
  for (name = namelist; name; name = next_name)
    {
      next_name = name->next;
      name->caname = normalize_filename (name->name);
      if (prev_name)
	{
	  struct name *p = hash_lookup (nametab, name);
	  if (p)
	    {
	      /* Keep the one listed in the command line */
	      if (!name->parent)
		{
		  if (p->child)
		    rebase_child_list (p->child, name);
		  /* FIXME: remove_directory (p->caname); ? */
		  remname (p);
		  free_name (p);
		  num_names--;
		}
	      else
		{
		  if (name->child)
		    rebase_child_list (name->child, p);
		  /* FIXME: remove_directory (name->caname); ? */
		  remname (name);
		  free_name (name);
		  continue;
		}
	    }
	}
      name->found_count = 0;
      if (!hash_insert (nametab, name))
	xalloc_die ();
      prev_name = name;
      num_names++;
    }
  nametail = prev_name;
  hash_free (nametab);

  namelist = merge_sort (namelist, num_names, compare_names_found);

  if (listed_incremental_option)
    {
      for (name = namelist; name && name->name[0] == 0; name++)
	;
      if (name)
	append_incremental_renames (name->directory);
    }
}
Exemple #26
0
static sparse_map *
sparse_new (size_t size_hint)
{
  return hash_initialize (size_hint, NULL, sparse_hash_, sparse_cmp_, free);
}