<commit_before>/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: <NAME> <<EMAIL>>
 */

#include <config.h>

#include <string.h>
#include <stdlib.h>

#include <glib/gi18n-lib.h>

#include "gdbusutils.h"
#include "gmountspec.h"

static GHashTable *unique_hash = NULL;
G_LOCK_DEFINE_STATIC(unique_hash);

static int
item_compare (const void *_a, const void *_b)
{
  const GMountSpecItem *a = _a;
  const GMountSpecItem *b = _b;

  return strcmp (a->key, b->key);
}

GMountSpec *
g_mount_spec_new (const char *type)
{
  GMountSpec *spec;

  spec = g_new0 (GMountSpec, 1);
  spec->ref_count = 1;
  spec->items = g_array_new (FALSE, TRUE, sizeof (GMountSpecItem));
  spec->mount_prefix = g_strdup ("/");
  
  if (type != NULL)
    g_mount_spec_set (spec, "type", type);
  
  return spec;
}

/* Takes ownership of passed in data */
GMountSpec *
g_mount_spec_new_from_data (GArray *items,
			    char *mount_prefix)
{
  GMountSpec *spec;

  spec = g_new0 (GMountSpec, 1);
  spec->ref_count = 1;
  spec->items = items;
  if (mount_prefix == NULL)
    spec->mount_prefix = g_strdup ("/");
  else
    spec->mount_prefix = mount_prefix;

  g_array_sort (spec->items, item_compare);
  
  return spec;
}

GMountSpec *
g_mount_spec_get_unique_for (GMountSpec *spec)
{
  GMountSpec *unique_spec;

  if (spec->is_unique)
    return g_mount_spec_ref (spec);
  
  G_LOCK (unique_hash);
  
  if (unique_hash == NULL)
    unique_hash = g_hash_table_new (g_mount_spec_hash, (GEqualFunc)g_mount_spec_equal);

  unique_spec = g_hash_table_lookup (unique_hash, spec);

  if (unique_spec == NULL)
    {
      spec->is_unique = TRUE;
      g_hash_table_insert (unique_hash, spec, spec);
      unique_spec = spec;
    }
  
  g_mount_spec_ref (unique_spec);
  
  G_UNLOCK (unique_hash);

  return unique_spec;
}

void
g_mount_spec_set_mount_prefix  (GMountSpec      *spec,
				const char      *mount_prefix)
{
  g_free (spec->mount_prefix);
  spec->mount_prefix = g_strdup (mount_prefix);
}


static void 
add_item (GMountSpec *spec,
	  const char *key,
	  char *value)
{
  GMountSpecItem item;

  g_return_if_fail (key != NULL);
  g_return_if_fail (value != NULL);
<commit_msg>Canonicalize mount prefix to prevent mountspec matching issues<commit_after>/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: <NAME> <<EMAIL>>
 */

#include <config.h>

#include <string.h>
#include <stdlib.h>

#include <glib/gi18n-lib.h>

#include "gdbusutils.h"
#include "gmountspec.h"

static GHashTable *unique_hash = NULL;
G_LOCK_DEFINE_STATIC(unique_hash);

static int
item_compare (const void *_a, const void *_b)
{
  const GMountSpecItem *a = _a;
  const GMountSpecItem *b = _b;

  return strcmp (a->key, b->key);
}

GMountSpec *
g_mount_spec_new (const char *type)
{
  GMountSpec *spec;

  spec = g_new0 (GMountSpec, 1);
  spec->ref_count = 1;
  spec->items = g_array_new (FALSE, TRUE, sizeof (GMountSpecItem));
  spec->mount_prefix = g_strdup ("/");
  
  if (type != NULL)
    g_mount_spec_set (spec, "type", type);
  
  return spec;
}

/* Takes ownership of passed in data */
GMountSpec *
g_mount_spec_new_from_data (GArray *items,
			    char *mount_prefix)
{
  GMountSpec *spec;

  spec = g_new0 (GMountSpec, 1);
  spec->ref_count = 1;
  spec->items = items;
  if (mount_prefix == NULL)
    spec->mount_prefix = g_strdup ("/");
  else
    spec->mount_prefix = g_mount_spec_canonicalize_path (mount_prefix);

  g_array_sort (spec->items, item_compare);
  
  return spec;
}

GMountSpec *
g_mount_spec_get_unique_for (GMountSpec *spec)
{
  GMountSpec *unique_spec;

  if (spec->is_unique)
    return g_mount_spec_ref (spec);
  
  G_LOCK (unique_hash);
  
  if (unique_hash == NULL)
    unique_hash = g_hash_table_new (g_mount_spec_hash, (GEqualFunc)g_mount_spec_equal);

  unique_spec = g_hash_table_lookup (unique_hash, spec);

  if (unique_spec == NULL)
    {
      spec->is_unique = TRUE;
      g_hash_table_insert (unique_hash, spec, spec);
      unique_spec = spec;
    }
  
  g_mount_spec_ref (unique_spec);
  
  G_UNLOCK (unique_hash);

  return unique_spec;
}

void
g_mount_spec_set_mount_prefix  (GMountSpec      *spec,
				const char      *mount_prefix)
{
  g_free (spec->mount_prefix);
  spec->mount_prefix = g_mount_spec_canonicalize_path (mount_prefix);
}


static void 
add_item (GMountSpec *spec,
	  const char *key,
	  char *value)
{
  GMountSpecItem item;

  g_return_if_fail (key != NULL);
  g_return_if_fail (value != NULL);

{'fraction_non_alphanumeric': 0.10333333333333333, 'fraction_numerical': 0.010333333333333333, 'mean_word_length': 3.8669910786699107, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7177798', 'n_tokens_mistral': 2166, 'n_tokens_neox': 2067, 'n_words': 622}