bool Creature_tracker::update_pos( const monster &critter, const tripoint &new_pos )
{
    const auto old_pos = critter.pos();
    if( critter.is_dead() ) {
        // mon_at ignores dead critters anyway, changing their position in the
        // monsters_by_location map is useless.
        remove_from_location_map( critter );
        return true;
    }

    const int critter_id = mon_at( old_pos );
    const int new_critter_id = mon_at( new_pos );
    if( new_critter_id >= 0 ) {
        auto &othermon = *monsters_list[new_critter_id];
        if( othermon.is_hallucination() ) {
            othermon.die( nullptr );
        } else {
            debugmsg( "update_zombie_pos: wanted to move %s to %d,%d,%d, but new location already has %s",
                      critter.disp_name().c_str(),
                      new_pos.x, new_pos.y, new_pos.z, othermon.disp_name().c_str() );
            return false;
        }
    }

    if( critter_id >= 0 ) {
        if( &critter == monsters_list[critter_id] ) {
            monsters_by_location.erase( old_pos );
            monsters_by_location[new_pos] = critter_id;
            return true;
        } else {
            const auto &othermon = *monsters_list[critter_id];
            debugmsg( "update_zombie_pos: wanted to move %s from old location %d,%d,%d, but it had %s instead",
                      critter.disp_name().c_str(),
                      old_pos.x, old_pos.y, old_pos.z, othermon.disp_name().c_str() );
            return false;
        }
    } else {
        // We're changing the x/y/z coordinates of a zombie that hasn't been added
        // to the game yet. add_zombie() will update monsters_by_location for us.
        debugmsg( "update_zombie_pos: no %s at %d,%d,%d (moving to %d,%d,%d)",
                  critter.disp_name().c_str(),
                  old_pos.x, old_pos.y, old_pos.z, new_pos.x, new_pos.y, new_pos.z );
        // Rebuild cache in case the monster actually IS in the game, just bugged
        rebuild_cache();
        return false;
    }

    return false;
}
Esempio n. 2
0
int user_belongs_to_group(uid_t uid, gid_t gid)
{
    int ret = 0;
    int i;
    uid_t *uids;

    pthread_rwlock_rdlock(&cache_lock);

    if (cache_rebuild_requested) {
        pthread_rwlock_unlock(&cache_lock);

        pthread_rwlock_wrlock(&cache_lock);
        if (cache_rebuild_requested) {
            DPRINTF("Building user/group cache");
            cache_rebuild_requested = 0;
            rebuild_cache();
        }
        pthread_rwlock_unlock(&cache_lock);

        pthread_rwlock_rdlock(&cache_lock);
    }

    struct uid_cache_entry *uent = uid_cache_lookup(uid);
    if (uent && uent->main_gid == gid) {
        ret = 1;
        goto done;
    }

    struct gid_cache_entry *gent = gid_cache_lookup(gid);
    if (gent) {
        uids = (uid_t*)ARENA_GET(cache_arena, gent->uids_offset);
        for (i = 0; i < gent->uid_count; ++i) {
            if (uids[i] == uid) {
                ret = 1;
                goto done;
            }
        }
    }

done:
    pthread_rwlock_unlock(&cache_lock);
    return ret;
}
bool Creature_tracker::update_pos( const monster &critter, const tripoint &new_pos )
{
    if( critter.is_dead() ) {
        // find ignores dead critters anyway, changing their position in the
        // monsters_by_location map is useless.
        remove_from_location_map( critter );
        return true;
    }

    if( const std::shared_ptr<monster> new_critter_ptr = find( new_pos ) ) {
        auto &othermon = *new_critter_ptr;
        if( othermon.is_hallucination() ) {
            othermon.die( nullptr );
        } else {
            debugmsg( "update_zombie_pos: wanted to move %s to %d,%d,%d, but new location already has %s",
                      critter.disp_name(),
                      new_pos.x, new_pos.y, new_pos.z, othermon.disp_name() );
            return false;
        }
    }

    const auto iter = std::find_if( monsters_list.begin(), monsters_list.end(),
    [&]( const std::shared_ptr<monster> &ptr ) {
        return ptr.get() == &critter;
    } );
    if( iter != monsters_list.end() ) {
        monsters_by_location.erase( critter.pos() );
        monsters_by_location[new_pos] = *iter;
        return true;
    } else {
        const tripoint &old_pos = critter.pos();
        // We're changing the x/y/z coordinates of a zombie that hasn't been added
        // to the game yet. add_zombie() will update monsters_by_location for us.
        debugmsg( "update_zombie_pos: no %s at %d,%d,%d (moving to %d,%d,%d)",
                  critter.disp_name(),
                  old_pos.x, old_pos.y, old_pos.z, new_pos.x, new_pos.y, new_pos.z );
        // Rebuild cache in case the monster actually IS in the game, just bugged
        rebuild_cache();
        return false;
    }
}
void Creature_tracker::swap_positions( monster &first, monster &second )
{
    const int first_mdex = mon_at( first.pos() );
    const int second_mdex = mon_at( second.pos() );
    remove_from_location_map( first );
    remove_from_location_map( second );
    bool ok = true;
    if( first_mdex == -1 || second_mdex == -1 || first_mdex == second_mdex ) {
        debugmsg( "Tried to swap monsters with invalid positions" );
        ok = false;
    }

    tripoint temp = second.pos();
    second.spawn( first.pos() );
    first.spawn( temp );
    if( ok ) {
        monsters_by_location[first.pos()] = first_mdex;
        monsters_by_location[second.pos()] = second_mdex;
    } else {
        // Try to avoid spamming error messages if something weird happens
        rebuild_cache();
    }
}
bool Creature_tracker::update_pos(const monster &critter, const tripoint &new_pos)
{
    const auto old_pos = critter.pos3();
    if( critter.is_dead() ) {
        // mon_at ignores dead critters anyway, changing their position in the
        // monsters_by_location map is useless.
        remove_from_location_map( critter );
        return true;
    }

    bool success = false;
    const int critter_id = mon_at( old_pos );
    const int new_critter_id = mon_at( new_pos );
    if( new_critter_id >= 0 ) {
        debugmsg("update_zombie_pos: new location %d,%d,%d already has zombie %d",
                 new_pos.x, new_pos.y, new_pos.z, new_critter_id);
    } else if( critter_id >= 0 ) {
        if( &critter == monsters_list[critter_id] ) {
            monsters_by_location.erase( old_pos );
            monsters_by_location[new_pos] = critter_id;
            success = true;
        } else {
            debugmsg("update_zombie_pos: old location %d,%d had zombie %d instead",
                     old_pos.x, old_pos.y, critter_id);
        }
    } else {
        // We're changing the x/y/z coordinates of a zombie that hasn't been added
        // to the game yet. add_zombie() will update monsters_by_location for us.
        debugmsg("update_zombie_pos: no such zombie at %d,%d,%d (moving to %d,%d,%d)",
                 old_pos.x, old_pos.y, old_pos.z, new_pos.x, new_pos.y, new_pos.z );
        // Rebuild cache in case the monster actually IS in the game, just bugged
        rebuild_cache();
    }

    return success;
}
static void
process_one_file (const char *filename,
                  GError    **err)
{
  char *new_filename;
  char *dirname;
  char *basename;
  GKeyFile *kf = NULL;
  GError *rebuild_error;
  GSList *tmp;
  
  kf = g_key_file_new ();
  if (!g_key_file_load_from_file (kf, filename,
			          G_KEY_FILE_KEEP_COMMENTS|
				  G_KEY_FILE_KEEP_TRANSLATIONS,
				  err)) {
    g_key_file_free (kf);
    return;
  }

  if (!desktop_file_fixup (kf, filename)) {
    g_key_file_free (kf);
    g_set_error (err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE,
                 _("Failed to fix the content of the desktop file"));
    return;
  }

  if (copy_name_to_generic_name)
    dfu_key_file_copy_key (kf, GROUP_DESKTOP_ENTRY, "Name",
                               GROUP_DESKTOP_ENTRY, "GenericName");

  if (copy_generic_name_to_name)
    dfu_key_file_copy_key (kf, GROUP_DESKTOP_ENTRY, "GenericName",
                               GROUP_DESKTOP_ENTRY, "Name");
  
  /* Mark file as having been processed by us, so automated
   * tools can check that desktop files went through our
   * munging
   */
  g_key_file_set_string (kf, GROUP_DESKTOP_ENTRY,
                         "X-Desktop-File-Install-Version", VERSION);

#define PROCESS_LIST(key, added, removed)                       \
  /* Add to the list */                                         \
  tmp = added;                                                  \
  while (tmp != NULL)                                           \
    {                                                           \
      dfu_key_file_merge_list (kf, GROUP_DESKTOP_ENTRY,         \
                               key, tmp->data);                 \
      tmp = tmp->next;                                          \
    }                                                           \
                                                                \
  /* Remove from the list */                                    \
  tmp = removed;                                                \
  while (tmp != NULL)                                           \
    {                                                           \
      dfu_key_file_remove_list (kf, GROUP_DESKTOP_ENTRY,        \
                                key, tmp->data);                \
      tmp = tmp->next;                                          \
    }

  /* Add/remove categories */
  PROCESS_LIST ("Categories", added_categories, removed_categories);

  /* Add/remove onlyshowin */
  PROCESS_LIST ("OnlyShowIn", added_only_show_in, removed_only_show_in);

  /* Remove keys */
  tmp = removed_keys;
  while (tmp != NULL)
    {
      g_key_file_remove_key (kf, GROUP_DESKTOP_ENTRY, tmp->data, NULL);

      tmp = tmp->next;
    }

  /* Add/remove mime-types */
  PROCESS_LIST ("MimeType", added_mime_types, removed_mime_types);


  dirname = g_path_get_dirname (filename);
  basename = g_path_get_basename (filename);
  
  if (vendor_name && !g_str_has_prefix (basename, vendor_name))
    {
      char *new_base;
      new_base = g_strconcat (vendor_name, "-", basename, NULL);
      new_filename = g_build_filename (target_dir, new_base, NULL);
      g_free (new_base);
    }
  else
    {
      new_filename = g_build_filename (target_dir, basename, NULL);
    }

  g_free (dirname);
  g_free (basename);
  
  if (!dfu_key_file_to_file (kf, new_filename, err)) {
    g_key_file_free (kf);
    g_free (new_filename);
    return;
  }
  
  g_key_file_free (kf);

  /* Load and validate the file we just wrote */
  if (!desktop_file_validate (new_filename, FALSE, TRUE))
    {
      g_set_error (err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE,
                   _("Failed to validate the created desktop file"));
      g_unlink (new_filename);
      g_free (new_filename);
      return;
    }

  if (g_chmod (new_filename, permissions) < 0)
    {
      g_set_error (err, G_FILE_ERROR,
                   g_file_error_from_errno (errno),
                   _("Failed to set permissions %o on \"%s\": %s"),
                   permissions, new_filename, g_strerror (errno));

      g_unlink (new_filename);
      g_free (new_filename);
      return;
    }

  if (delete_original &&
      !files_are_the_same (filename, new_filename))
    {
      if (g_unlink (filename) < 0)
        g_printerr (_("Error removing original file \"%s\": %s\n"),
                    filename, g_strerror (errno));
    }

  if (rebuild_mime_info_cache)
    {
      rebuild_error = NULL;
      rebuild_cache (target_dir, &rebuild_error);

      if (rebuild_error != NULL)
        g_propagate_error (err, rebuild_error);
    }
  
  g_free (new_filename);
}
Esempio n. 7
0
void main_loop(int argc, char *argv[], int max_hosts)
{
	int i;
	int host;
	int *targeted_host;
	FileData local_file_data;
	FileData remote_file_data;
	Order *order;
	Direction direction;
	bool use_cache;
	char *top_dir;

	FtpInit();

	targeted_host = get_targeted_host(argc, argv, max_hosts);

	for (i = 0;; i++) {
		host = targeted_host[i];
		if (host == -1) {
			break;
		}

		message(PROCESS, cfgSectionNumberToName(host), 0, host);

		direction = get_direction(host);

		get_local_file_data(&local_file_data, host);

		if (command_line_option.rebuild_cache) { /* -r | --rebuild-cache */
			rebuild_cache(host);
			free_file_data(&local_file_data);
			continue;
		}
		if (command_line_option.catch_up) { /* -R | --catch-up */
			catch_up(&local_file_data, host);
			free_file_data(&local_file_data);
			continue;
		}

		if (direction == DOWNLOAD || !does_cache_exist(host)) {
			if (direction == DOWNLOAD) {
				printf(_("Rebuilding cache for downloading...\n"));
			} else {
				printf(_("Cache file is not found.\nCreating a new one...\n"));
			}
			if (connect_to_remote_host(host) != 0) {
				free_file_data(&local_file_data);
				continue;
			}
			get_remote_file_data(&remote_file_data, host);
			use_cache = FALSE;
		} else {
			load_cache(&remote_file_data, host);
			use_cache = TRUE;
		}

		order = compare_both_hosts_and_generate_order(&local_file_data, &remote_file_data, config.local_top_dir[host], config.remote_top_dir[host], direction, host);

		if (!does_need_update(order)) {
			disconnect_from_remote_host(host);
			if (direction == UPLOAD) {
				printf(_("The remote host doesn't need updating.\n"));
			} else {
				printf(_("The local host doesn't need updating.\n"));
			}
			if (!use_cache) {
				save_cache(&remote_file_data, host);
			}
			free_file_data(&remote_file_data);
			free_file_data(&local_file_data);
			continue;
		}

		if(command_line_option.list) {
			disconnect_from_remote_host(host);
			put_listing_of_updated_file(order, direction);
			free_file_data(&remote_file_data);
			free_file_data(&local_file_data);
			continue;
		}

		if(command_line_option.nlist) {
			disconnect_from_remote_host(host);
			put_the_number_of_updated_file(order, direction);
			free_file_data(&remote_file_data);
			free_file_data(&local_file_data);
			continue;
		}

		if (!use_cache) {
			printf("\n");
		}
		put_num_updated_file(order, direction);

		if (connect_to_remote_host(host) != 0) {
			free_file_data(&remote_file_data);
			free_file_data(&local_file_data);
			continue;
		}

		if (direction == UPLOAD) {
			top_dir = str_concat(config.remote_top_dir[host], "/", NULL);
		} else {
			top_dir = str_concat(config.local_top_dir[host], "/", NULL);
		}

		message(ENTER, top_dir, 0, host);
		execute_order(order, &remote_file_data, direction, host);
		message(LEAVE, top_dir, 0, host);

		free(top_dir);
		free_order(order);

		disconnect_from_remote_host(host);

		save_cache(&remote_file_data, host);

		free_file_data(&remote_file_data);
		free_file_data(&local_file_data);
	}
	free(targeted_host);
}
Esempio n. 8
0
static void
process_one_file (const char *filename,
                  GError    **err)
{
  char *new_filename;
  GKeyFile *kf = NULL;
  GError *rebuild_error;
  GSList *tmp;

  kf = g_key_file_new ();
  if (!g_key_file_load_from_file (kf, filename,
                                  G_KEY_FILE_KEEP_COMMENTS|
                                  G_KEY_FILE_KEEP_TRANSLATIONS,
                                  err)) {
    g_key_file_free (kf);
    return;
  }

  if (!desktop_file_fixup (kf, filename)) {
    g_key_file_free (kf);
    g_set_error (err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE,
                 _("Failed to fix the content of the desktop file"));
    return;
  }

  /* Mark file as having been processed by us, so automated
   * tools can check that desktop files went through our
   * munging
   */
  g_key_file_set_string (kf, GROUP_DESKTOP_ENTRY,
                         "X-Desktop-File-Install-Version", VERSION);

  tmp = edit_actions;
  while (tmp != NULL)
    {
      DfuEditAction *action = tmp->data;

      switch (action->type)
        {
          case DFU_SET_KEY:
            g_key_file_set_string (kf, GROUP_DESKTOP_ENTRY,
                                   action->key, action->action_value);
            dfu_key_file_drop_locale_strings (kf, GROUP_DESKTOP_ENTRY,
                                              action->key);
            break;
          case DFU_REMOVE_KEY:
            g_key_file_remove_key (kf, GROUP_DESKTOP_ENTRY,
                                   action->key, NULL);
            dfu_key_file_drop_locale_strings (kf, GROUP_DESKTOP_ENTRY,
                                              action->key);
            break;
          case DFU_ADD_TO_LIST:
            dfu_key_file_merge_list (kf, GROUP_DESKTOP_ENTRY,
                                     action->key, action->action_value);
            break;
          case DFU_REMOVE_FROM_LIST:
            dfu_key_file_remove_list (kf, GROUP_DESKTOP_ENTRY,
                                      action->key, action->action_value);
            break;
          case DFU_COPY_KEY:
            dfu_key_file_copy_key (kf, GROUP_DESKTOP_ENTRY, action->key,
                                   GROUP_DESKTOP_ENTRY, action->action_value);
            break;
          default:
            g_assert_not_reached ();
        }

      tmp = tmp->next;
    }

  if (edit_mode)
    {
      new_filename = g_strdup (filename);
    }
  else
    {
      char *basename = g_path_get_basename (filename);

      if (vendor_name && !g_str_has_prefix (basename, vendor_name))
        {
          char *new_base;
          new_base = g_strconcat (vendor_name, "-", basename, NULL);
          new_filename = g_build_filename (target_dir, new_base, NULL);
          g_free (new_base);
        }
      else
        {
          new_filename = g_build_filename (target_dir, basename, NULL);
        }

      g_free (basename);
    }

  if (!dfu_key_file_to_path (kf, new_filename, err)) {
    g_key_file_free (kf);
    g_free (new_filename);
    return;
  }

  g_key_file_free (kf);

  /* Load and validate the file we just wrote */
  if (!desktop_file_validate (new_filename, FALSE, TRUE, TRUE))
    {
      g_set_error (err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_PARSE,
                   _("Failed to validate the created desktop file"));

      if (!files_are_the_same (filename, new_filename))
        g_unlink (new_filename);

      g_free (new_filename);
      return;
    }

  if (!edit_mode)
    {
      if (g_chmod (new_filename, permissions) < 0)
        {
          g_set_error (err, G_FILE_ERROR,
                       g_file_error_from_errno (errno),
                       _("Failed to set permissions %o on \"%s\": %s"),
                       permissions, new_filename, g_strerror (errno));

          if (!files_are_the_same (filename, new_filename))
            g_unlink (new_filename);

          g_free (new_filename);
          return;
        }

      if (delete_original &&
          !files_are_the_same (filename, new_filename))
        {
          if (g_unlink (filename) < 0)
            g_printerr (_("Error removing original file \"%s\": %s\n"),
                        filename, g_strerror (errno));
        }
    }

  if (rebuild_mime_info_cache)
    {
      rebuild_error = NULL;
      rebuild_cache (target_dir, &rebuild_error);

      if (rebuild_error != NULL)
        g_propagate_error (err, rebuild_error);
    }

  g_free (new_filename);
}