Browse Source

Implement the put_file function in the GIO module

The GIO-based scheme module now supports the 'put_file' function.
develop
Damien Goutte-Gattat 5 years ago
parent
commit
2f1f6d5679
  1. 111
      src/scheme-gio.c
  2. 7
      src/scheme-gio.h
  3. 2
      src/scheme-module.c

111
src/scheme-gio.c

@ -181,6 +181,85 @@ get_file_contents(GVolume *volume,
return rc;
}
/**
* Create all the parent directories of the specified path.
*
* @param path Path to a file whose parents should be created.
*
* @return 0 if the parents were created (or already existed),
* or -1 if an error occured.
*/
static int
make_parent_directories(const char *path)
{
GFile *file, *parent;
int rc = 0;
file = g_file_new_for_path(path);
if ( (parent = g_file_get_parent(file)) ) {
char *ppath;
ppath = g_file_get_path(parent);
if ( g_mkdir_with_parents(ppath, 755) == -1 )
rc = -1;
g_free(ppath);
g_object_unref(parent);
}
g_object_unref(file);
return rc;
}
/**
* Write a file on the given volume.
*
* @param volume The volume on which the file should be written.
* @param path The pathname of the file to write.
* @param buffer The contents to write.
* @param len The size of the contents buffer.
*
* @return 0 if successful, -1 if an error occured.
*/
static int
put_file_contents(GVolume *volume,
const char *path,
unsigned char *buffer,
size_t len)
{
GMount *mount;
GFile *root;
char *root_path, *full_path;
int was_mounted, rc = -1;
if ( (mount = g_volume_get_mount(volume)) )
was_mounted = 1;
else if ( (mount = mount_volume(volume)) )
was_mounted = 0;
else
return -1;
root = g_mount_get_root(mount);
root_path = g_file_get_path(root);
full_path = g_strjoin("/", root_path, path, NULL);
if ( make_parent_directories(full_path) != -1 )
if ( write_file(full_path, buffer, len) != -1 )
rc = 0;
if ( was_mounted )
g_object_unref(mount);
else
unmount_volume(mount);
g_free(full_path);
g_free(root_path);
g_object_unref(root);
return rc;
}
int
gfsec_scheme_gio_get_file(gfsec_scheme_t scheme,
const char *authority,
@ -213,4 +292,36 @@ gfsec_scheme_gio_get_file(gfsec_scheme_t scheme,
return rc;
}
int
gfsec_scheme_gio_put_file(gfsec_scheme_t scheme,
const char *authority,
const char *path,
unsigned char *buffer,
size_t len)
{
GVolume *volume = NULL;
int rc = GFSEC_SCHEME_STATUS_ERROR;
switch ( scheme ) {
case GFSEC_SCHEME_UUID:
volume = find_volume(authority, 1);
break;
case GFSEC_SCHEME_LABEL:
volume = find_volume(authority, 0);
break;
default:
return GFSEC_SCHEME_STATUS_UNSUPPORTED_SCHEME;
}
if ( volume ) {
if ( put_file_contents(volume, path, buffer, len) == 0 )
rc = GFSEC_SCHEME_STATUS_SUCCESS;
g_object_unref(volume);
}
return rc;
}
#endif /* HAVE_GIO */

7
src/scheme-gio.h

@ -32,6 +32,13 @@ gfsec_scheme_gio_get_file(gfsec_scheme_t,
unsigned char **,
size_t *);
int
gfsec_scheme_gio_put_file(gfsec_scheme_t,
const char *,
const char *,
unsigned char *,
size_t);
#ifdef __cplusplus
}
#endif

2
src/scheme-module.c

@ -73,7 +73,7 @@ gfsec_scheme_module_init(void)
#ifdef HAVE_GIO
mods[n].get_file = gfsec_scheme_gio_get_file;
mods[n].put_file = NULL;
mods[n].put_file = gfsec_scheme_gio_put_file;
mods[n++].present = 1;
#endif

Loading…
Cancel
Save