Browse Source

Add a put_file function to the scheme modules

Add a new function to the scheme module interface, to write a
file's contents at a specified location.

Only the file module implemements this function for now.
develop
Damien Goutte-Gattat 5 years ago
parent
commit
da93cc4e8a
  1. 18
      src/scheme-file.c
  2. 7
      src/scheme-file.h
  3. 74
      src/scheme-module.c
  4. 7
      src/scheme-module.h

18
src/scheme-file.c

@ -42,3 +42,21 @@ gfsec_scheme_file_get_file(gfsec_scheme_t scheme,
else
return GFSEC_SCHEME_STATUS_ERROR;
}
int
gfsec_scheme_file_put_file(gfsec_scheme_t scheme,
const char *authority,
const char *path,
unsigned char *buffer,
size_t len)
{
(void) authority;
if ( scheme != GFSEC_SCHEME_FILE )
return GFSEC_SCHEME_STATUS_UNSUPPORTED_SCHEME;
if ( write_file(path, buffer, len) != -1 )
return GFSEC_SCHEME_STATUS_SUCCESS;
else
return GFSEC_SCHEME_STATUS_ERROR;
}

7
src/scheme-file.h

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

74
src/scheme-module.c

@ -35,8 +35,16 @@ typedef int (*fn_get_file)(gfsec_scheme_t,
unsigned char **,
size_t *);
typedef int (*fn_put_file)(gfsec_scheme_t,
const char *,
const char *,
unsigned char *,
size_t);
typedef struct module {
int present;
fn_get_file get_file;
fn_put_file put_file;
} module_t;
static module_t *modules;
@ -52,18 +60,24 @@ gfsec_scheme_module_init(void)
static module_t mods[4];
int n = 0;
mods[n++].get_file = gfsec_scheme_file_get_file;
mods[n].get_file = gfsec_scheme_file_get_file;
mods[n].put_file = gfsec_scheme_file_put_file;
mods[n++].present = 1;
#ifdef HAVE_LIBMTP
LIBMTP_Init();
mods[n++].get_file = gfsec_scheme_libmtp_get_file;
mods[n].get_file = gfsec_scheme_libmtp_get_file;
mods[n].put_file = NULL;
mods[n++].present = 1;
#endif
#ifdef HAVE_GIO
mods[n++].get_file = gfsec_scheme_gio_get_file;
mods[n].get_file = gfsec_scheme_gio_get_file;
mods[n].put_file = NULL;
mods[n++].present = 1;
#endif
mods[n++].get_file = NULL;
mods[n++].present = 0;
modules = mods;
}
@ -95,16 +109,52 @@ gfsec_scheme_module_get_file(gfsec_scheme_t scheme,
unsigned char **buffer,
size_t *len)
{
module_t *mod = modules;
int rc;
module_t *mod;
int rc = GFSEC_SCHEME_STATUS_UNSUPPORTED_SCHEME;
for ( mod = modules; mod->present; mod++ ) {
if ( mod->get_file ) {
rc = mod->get_file(scheme, authority, path, buffer, len);
if ( rc == GFSEC_SCHEME_STATUS_SUCCESS )
break;
}
}
while ( mod->get_file != NULL ) {
rc = mod->get_file(scheme, authority, path, buffer, len);
return rc;
}
if ( rc != GFSEC_SCHEME_STATUS_SUCCESS )
mod += 1;
else
break;
/**
* Attempts to write a file to the specified URI.
*
* @param scheme The URI scheme.
* @param authority The URI authority part.
* @param path The URI path.
* @param buffer The data to write.
* @param len The size of the data buffer.
*
* @return
* - GFSEC_SCHEME_STATUS_SUCCESS if successful;
* - GFSEC_SCHEME_STATUS_UNSUPPORTED_SCHEME if no module is
* available to handle the URI scheme;
* - GFSEC_SCHEME_STATUS_ERROR if a module supported the
* URI scheme but an error occured during the operation.
*/
int
gfsec_scheme_module_put_file(gfsec_scheme_t scheme,
const char *authority,
const char *path,
unsigned char *buffer,
size_t len)
{
module_t *mod;
int rc = GFSEC_SCHEME_STATUS_UNSUPPORTED_SCHEME;
for ( mod = modules; mod->present; mod++ ) {
if ( mod->put_file ) {
rc = mod->put_file(scheme, authority, path, buffer, len);
if ( rc == GFSEC_SCHEME_STATUS_SUCCESS )
break;
}
}
return rc;

7
src/scheme-module.h

@ -43,6 +43,13 @@ gfsec_scheme_module_get_file(gfsec_scheme_t,
unsigned char **,
size_t *);
int
gfsec_scheme_module_put_file(gfsec_scheme_t,
const char *,
const char *,
unsigned char *,
size_t);
#ifdef __cpluscplus
}
#endif

Loading…
Cancel
Save