Browse Source

Create a list of available supports

Add structures to hold a representation of all the available
supports and the functions to fill them.
develop
Damien Goutte-Gattat 5 years ago
parent
commit
cf1eea65e1
  1. 3
      src/Makefile.am
  2. 10
      src/scheme-file.c
  3. 4
      src/scheme-file.h
  4. 38
      src/scheme-gio.c
  5. 4
      src/scheme-gio.h
  6. 25
      src/scheme-libmtp.c
  7. 4
      src/scheme-libmtp.h
  8. 26
      src/scheme-module.c
  9. 4
      src/scheme-module.h
  10. 89
      src/support.c
  11. 54
      src/support.h

3
src/Makefile.am

@ -5,7 +5,8 @@ common_sources = util.c util.h secret.c secret.h \
schemes.h scheme-module.c scheme-module.h \
scheme-libmtp.c scheme-libmtp.h \
scheme-gio.c scheme-gio.h \
scheme-file.c scheme-file.h
scheme-file.c scheme-file.h \
support.c support.h
gfsec_use_SOURCES = gfsec-use.c $(common_sources)

10
src/scheme-file.c

@ -26,6 +26,8 @@
#include "scheme-module.h"
#include "util.h"
#include <xmem.h>
int
gfsec_scheme_file_get_file(gfsec_scheme_t scheme,
const char *authority,
@ -74,3 +76,11 @@ gfsec_scheme_file_lst_auth(FILE *f)
{
fprintf(f, "file:// Local filesystem\n");
}
int
gfsec_scheme_file_get_supports(gfsec_supports_list_t *list)
{
gfsec_support_add(list, GFSEC_SCHEME_FILE, xstrdup(""), xstrdup("Local filesystem"));
return 1;
}

4
src/scheme-file.h

@ -21,6 +21,7 @@
#include <stdio.h>
#include "schemes.h"
#include "support.h"
#ifdef __cpluscplus
extern "C" {
@ -43,6 +44,9 @@ gfsec_scheme_file_put_file(gfsec_scheme_t,
void
gfsec_scheme_file_lst_auth(FILE *);
int
gfsec_scheme_file_get_supports(gfsec_supports_list_t *);
#ifdef __cplusplus
}
#endif

38
src/scheme-gio.c

@ -27,6 +27,7 @@
#include <string.h>
#include <gio/gio.h>
#include <xmem.h>
#include "util.h"
typedef struct mount_cb_data {
@ -326,4 +327,41 @@ gfsec_scheme_gio_lst_auth(FILE *f)
g_object_unref(volmon);
}
int
gfsec_scheme_gio_get_supports(gfsec_supports_list_t *list)
{
GVolumeMonitor *volmon;
GList *volumes, *l;
int n;
volmon = g_volume_monitor_get();
volumes = g_volume_monitor_get_volumes(volmon);
for ( l = volumes, n = 0; l != NULL; l = l->next ) {
char *label, *uuid;
GVolume *volume;
volume = G_VOLUME(l->data);
label = g_volume_get_name(volume);
uuid = g_volume_get_uuid(volume);
if ( label ) {
gfsec_support_add(list, GFSEC_SCHEME_LABEL, label, xstrdup("GIO volume"));
n += 1;
}
if ( uuid ) {
gfsec_support_add(list, GFSEC_SCHEME_UUID, uuid, xstrdup("GIO volume"));
n += 1;
}
g_object_unref(l->data);
}
g_list_free(volumes);
g_object_unref(volmon);
return n;
}
#endif /* HAVE_GIO */

4
src/scheme-gio.h

@ -21,6 +21,7 @@
#include <stdio.h>
#include "schemes.h"
#include "support.h"
#ifdef __cpluscplus
extern "C" {
@ -43,6 +44,9 @@ gfsec_scheme_gio_put_file(gfsec_scheme_t,
void
gfsec_scheme_gio_lst_auth(FILE *);
int
gfsec_scheme_gio_get_supports(gfsec_supports_list_t *);
#ifdef __cplusplus
}
#endif

25
src/scheme-libmtp.c

@ -412,4 +412,29 @@ gfsec_scheme_libmtp_lst_auth(FILE *f)
}
}
int
gfsec_scheme_libmtp_get_supports(gfsec_supports_list_t *list)
{
LIBMTP_raw_device_t *raw_devices;
LIBMTP_mtpdevice_t *device;
int i, n;
if ( LIBMTP_Detect_Raw_Devices(&raw_devices, &n) != 0 )
return 0;
for ( i = 0, device = NULL; i < n && device == NULL; i++ ) {
char *serial, *name;
device = LIBMTP_Open_Raw_Device_Uncached(&(raw_devices[i]));
serial = LIBMTP_Get_Serialnumber(device);
name = LIBMTP_Get_Friendlyname(device);
gfsec_support_add(list, GFSEC_SCHEME_MTP, serial, name);
LIBMTP_Release_Device(device);
}
return n;
}
#endif /* HAVE_LIBMTP */

4
src/scheme-libmtp.h

@ -21,6 +21,7 @@
#include <stdio.h>
#include "schemes.h"
#include "support.h"
#ifdef __cpluscplus
extern "C" {
@ -43,6 +44,9 @@ gfsec_scheme_libmtp_put_file(gfsec_scheme_t,
void
gfsec_scheme_libmtp_lst_auth(FILE *);
int
gfsec_scheme_libmtp_get_supports(gfsec_supports_list_t *);
#ifdef __cplusplus
}
#endif

26
src/scheme-module.c

@ -43,11 +43,14 @@ typedef int (*fn_put_file)(gfsec_scheme_t,
typedef void (*fn_lst_auth)(FILE *);
typedef int (*fn_get_supp)(gfsec_supports_list_t *);
typedef struct module {
int present;
fn_get_file get_file;
fn_put_file put_file;
fn_lst_auth lst_auth;
fn_get_supp get_supp;
} module_t;
static module_t *modules;
@ -66,6 +69,7 @@ gfsec_scheme_module_init(void)
mods[n].get_file = gfsec_scheme_file_get_file;
mods[n].put_file = gfsec_scheme_file_put_file;
mods[n].lst_auth = gfsec_scheme_file_lst_auth;
mods[n].get_supp = gfsec_scheme_file_get_supports;
mods[n++].present = 1;
#ifdef HAVE_LIBMTP
@ -73,6 +77,7 @@ gfsec_scheme_module_init(void)
mods[n].get_file = gfsec_scheme_libmtp_get_file;
mods[n].put_file = gfsec_scheme_libmtp_put_file;
mods[n].lst_auth = gfsec_scheme_libmtp_lst_auth;
mods[n].get_supp = gfsec_scheme_libmtp_get_supports;
mods[n++].present = 1;
#endif
@ -80,6 +85,7 @@ gfsec_scheme_module_init(void)
mods[n].get_file = gfsec_scheme_gio_get_file;
mods[n].put_file = gfsec_scheme_gio_put_file;
mods[n].lst_auth = gfsec_scheme_gio_lst_auth;
mods[n].get_supp = gfsec_scheme_gio_get_supports;
mods[n++].present = 1;
#endif
@ -181,3 +187,23 @@ gfsec_scheme_module_list_authorities(FILE *f)
mod->lst_auth(f);
}
}
/**
* Get a list of currently available supports.
*
* @return A gfsec_supports_list_t object containing all available
* supports.
*/
gfsec_supports_list_t *
gfsec_scheme_module_get_supports(void)
{
gfsec_supports_list_t *list = gfsec_support_new_list();
module_t *mod;
for ( mod = modules; mod->present; mod++ ) {
if ( mod->get_supp )
mod->get_supp(list);
}
return list;
}

4
src/scheme-module.h

@ -23,6 +23,7 @@
#include <stdio.h>
#include "schemes.h"
#include "support.h"
typedef enum gfsec_scheme_status {
GFSEC_SCHEME_STATUS_SUCCESS = 0,
@ -54,6 +55,9 @@ gfsec_scheme_module_put_file(gfsec_scheme_t,
void
gfsec_scheme_module_list_authorities(FILE *);
gfsec_supports_list_t *
gfsec_scheme_module_get_supports(void);
#ifdef __cpluscplus
}
#endif

89
src/support.c

@ -0,0 +1,89 @@
/*
* gfsecret - Secret sharing tools
* Copyright (C) 2016 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "support.h"
#include <xmem.h>
/**
* Create a new list of supports.
*/
gfsec_supports_list_t *
gfsec_support_new_list(void)
{
gfsec_supports_list_t *list;
list = xmalloc(sizeof(*list));
list->supports = xmalloc(10 * sizeof(gfsec_support_t));
list->size = 10;
list->count = 0;
return list;
}
/**
* Destroy a list of supports and release all associated memory.
*
* @param list The list to destroy.
*/
void
gfsec_support_destroy_list(gfsec_supports_list_t *list)
{
unsigned n;
for ( n = 0; n < list->count; n++ ) {
free(list->supports[n].authority);
free(list->supports[n].description);
}
free(list->supports);
free(list);
}
/**
* Add a new support to a list of supports.
*
* @param list The list to modify.
* @param scheme The scheme associated to the support.
* @param authority The authority refering to the support.
* @param description A free-form description of the support.
*
* @note The string parameters will be owned by the list and freed
* when the gfsec_support_destroy_list function is called.
*/
void
gfsec_support_add(gfsec_supports_list_t *list,
gfsec_scheme_t scheme,
char *authority,
char *description)
{
if ( list->count >= list->size ) {
list->size += 10;
list->supports = xrealloc(list->supports, list->size * sizeof(gfsec_support_t));
}
list->supports[list->count].scheme = scheme;
list->supports[list->count].authority = authority;
list->supports[list->count].description = description;
list->count += 1;
}

54
src/support.h

@ -0,0 +1,54 @@
/*
* gfsecret - Secret sharing tools
* Copyright (C) 2016 Damien Goutte-Gattat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ICP20161015_SUPPORT_H
#define ICP20161015_SUPPORT_H
#include "schemes.h"
typedef struct gfsec_support {
gfsec_scheme_t scheme;
char *authority;
char *description;
} gfsec_support_t;
typedef struct gfsec_supports_list {
gfsec_support_t *supports;
size_t size;
size_t count;
} gfsec_supports_list_t;
#ifdef __cpluscplus
extern "C" {
#endif
gfsec_supports_list_t *
gfsec_support_new_list(void);
void
gfsec_support_destroy_list(gfsec_supports_list_t *);
void
gfsec_support_add(gfsec_supports_list_t *, gfsec_scheme_t, char *, char *);
#ifdef __cplusplus
}
#endif
#endif /* !ICP20161015_SUPPORT_H */
Loading…
Cancel
Save