|
Packit Service |
b23acc |
// SPDX-License-Identifier: GPL-2.0+
|
|
Packit Service |
b23acc |
/*
|
|
Packit Service |
b23acc |
* Copyright (C) 2019 Red Hat, Inc.
|
|
Packit Service |
b23acc |
*/
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
#include "nm-default.h"
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
#include "nm-settings-utils.h"
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
#include <sys/stat.h>
|
|
Packit Service |
b23acc |
#include <sys/time.h>
|
|
Packit Service |
b23acc |
#include <sys/types.h>
|
|
Packit Service |
b23acc |
#include <unistd.h>
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
#include "nm-settings-plugin.h"
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/*****************************************************************************/
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
const struct timespec *
|
|
Packit Service |
b23acc |
nm_sett_util_stat_mtime (const char *filename,
|
|
Packit Service |
b23acc |
gboolean do_lstat,
|
|
Packit Service |
b23acc |
struct timespec *out_val)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
struct stat st;
|
|
Packit Service |
b23acc |
struct timeval now_tv;
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
if (filename) {
|
|
Packit Service |
b23acc |
if (do_lstat) {
|
|
Packit Service |
b23acc |
if (lstat (filename, &st) == 0) {
|
|
Packit Service |
b23acc |
*out_val = st.st_mtim;
|
|
Packit Service |
b23acc |
return out_val;
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
} else {
|
|
Packit Service |
b23acc |
if (stat (filename, &st) == 0) {
|
|
Packit Service |
b23acc |
*out_val = st.st_mtim;
|
|
Packit Service |
b23acc |
return out_val;
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
if (gettimeofday (&now_tv, NULL) == 0) {
|
|
Packit Service |
b23acc |
*out_val = (struct timespec) {
|
|
Packit Service |
b23acc |
.tv_sec = now_tv.tv_sec,
|
|
Packit Service |
b23acc |
.tv_nsec = now_tv.tv_usec * 1000u,
|
|
Packit Service |
b23acc |
};
|
|
Packit Service |
b23acc |
return out_val;
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
*out_val = (struct timespec) { };
|
|
Packit Service |
b23acc |
return out_val;
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/*****************************************************************************/
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
gboolean
|
|
Packit Service |
b23acc |
nm_sett_util_allow_filename_cb (const char *filename,
|
|
Packit Service |
b23acc |
gpointer user_data)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
const NMSettUtilAllowFilenameData *allow_filename_data = user_data;
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
if ( allow_filename_data->allowed_filename
|
|
Packit Service |
b23acc |
&& nm_streq (allow_filename_data->allowed_filename, filename))
|
|
Packit Service |
b23acc |
return TRUE;
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
return !g_hash_table_contains (allow_filename_data->idx_by_filename, filename);
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/*****************************************************************************/
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
void
|
|
Packit Service |
b23acc |
nm_sett_util_storage_by_uuid_head_destroy (NMSettUtilStorageByUuidHead *sbuh)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
CList *iter;
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
while ((iter = c_list_first (&sbuh->_storage_by_uuid_lst_head)))
|
|
Packit Service |
b23acc |
c_list_unlink (iter);
|
|
Packit Service |
b23acc |
g_free (sbuh);
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
/*****************************************************************************/
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
void
|
|
Packit Service |
b23acc |
nm_sett_util_storages_clear (NMSettUtilStorages *storages)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
nm_clear_pointer (&storages->idx_by_uuid, g_hash_table_destroy);
|
|
Packit Service |
b23acc |
nm_clear_pointer (&storages->idx_by_filename, g_hash_table_destroy);
|
|
Packit Service |
b23acc |
nm_assert (c_list_is_empty (&storages->_storage_lst_head));
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
void
|
|
Packit Service |
b23acc |
nm_sett_util_storages_add_take (NMSettUtilStorages *storages,
|
|
Packit Service |
b23acc |
gpointer storage_take_p /* NMSettingsStorage *, take reference */)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
NMSettingsStorage *storage_take = storage_take_p;
|
|
Packit Service |
b23acc |
NMSettUtilStorageByUuidHead *sbuh;
|
|
Packit Service |
b23acc |
const char *uuid;
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
nm_assert (storage_take);
|
|
Packit Service |
b23acc |
nm_assert (c_list_is_empty (&storage_take->_storage_lst));
|
|
Packit Service |
b23acc |
nm_assert (c_list_is_empty (&storage_take->_storage_by_uuid_lst));
|
|
Packit Service |
b23acc |
nm_assert (nm_settings_storage_get_filename (storage_take));
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
if (!g_hash_table_replace (storages->idx_by_filename,
|
|
Packit Service |
b23acc |
(char *) nm_settings_storage_get_filename (storage_take),
|
|
Packit Service |
b23acc |
storage_take /* takes ownership of reference. */))
|
|
Packit Service |
b23acc |
nm_assert_not_reached ();
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
uuid = nm_settings_storage_get_uuid_opt (storage_take);
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
if (uuid) {
|
|
Packit Service |
b23acc |
sbuh = nm_sett_util_storages_lookup_by_uuid (storages, uuid);
|
|
Packit Service |
b23acc |
if (!sbuh) {
|
|
Packit Service |
b23acc |
gsize l = strlen (uuid) + 1;
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
sbuh = g_malloc (sizeof (NMSettUtilStorageByUuidHead) + l);
|
|
Packit Service |
b23acc |
sbuh->uuid = sbuh->uuid_data;
|
|
Packit Service |
b23acc |
c_list_init (&sbuh->_storage_by_uuid_lst_head);
|
|
Packit Service |
b23acc |
memcpy (sbuh->uuid_data, uuid, l);
|
|
Packit Service |
b23acc |
g_hash_table_add (storages->idx_by_uuid, sbuh);
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
c_list_link_tail (&sbuh->_storage_by_uuid_lst_head, &storage_take->_storage_by_uuid_lst);
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
c_list_link_tail (&storages->_storage_lst_head, &storage_take->_storage_lst);
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
gpointer /* NMSettingsStorage * */
|
|
Packit Service |
b23acc |
nm_sett_util_storages_steal (NMSettUtilStorages *storages,
|
|
Packit Service |
b23acc |
gpointer storage_p /* NMSettingsStorage **/)
|
|
Packit Service |
b23acc |
{
|
|
Packit Service |
b23acc |
NMSettingsStorage *storage = storage_p;
|
|
Packit Service |
b23acc |
NMSettUtilStorageByUuidHead *sbuh;
|
|
Packit Service |
b23acc |
const char *uuid;
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
nm_assert (storage);
|
|
Packit Service |
b23acc |
nm_assert (nm_sett_util_storages_lookup_by_filename (storages, nm_settings_storage_get_filename (storage)) == storage);
|
|
Packit Service |
b23acc |
nm_assert (c_list_contains (&storages->_storage_lst_head, &storage->_storage_lst));
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
uuid = nm_settings_storage_get_uuid_opt (storage);
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
if (!uuid) {
|
|
Packit Service |
b23acc |
nm_assert (c_list_is_empty (&storage->_storage_by_uuid_lst));
|
|
Packit Service |
b23acc |
} else {
|
|
Packit Service |
b23acc |
nm_assert (!c_list_is_empty (&storage->_storage_by_uuid_lst));
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
sbuh = nm_sett_util_storages_lookup_by_uuid (storages, uuid);
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
nm_assert (sbuh);
|
|
Packit Service |
b23acc |
nm_assert (c_list_contains (&sbuh->_storage_by_uuid_lst_head, &storage->_storage_by_uuid_lst));
|
|
Packit Service |
b23acc |
c_list_unlink (&storage->_storage_by_uuid_lst);
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
if (c_list_is_empty (&sbuh->_storage_by_uuid_lst_head))
|
|
Packit Service |
b23acc |
g_hash_table_remove (storages->idx_by_uuid, sbuh);
|
|
Packit Service |
b23acc |
}
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
c_list_unlink (&storage->_storage_lst);
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
g_hash_table_steal (storages->idx_by_filename, nm_settings_storage_get_filename (storage));
|
|
Packit Service |
b23acc |
|
|
Packit Service |
b23acc |
return storage;
|
|
Packit Service |
b23acc |
}
|