Permalink
Browse files

plugin: add and use nbdkit_realpath

Introduce a new helper function to resolve a path name, calling
nbdkit_error on failure: other than doing what nbdkit_absolute_path
does, it also checks that the file exists (and thus avoids errors later
on).

Apply it where an existing path is required, both in nbdkit itself and
in plugins.

Related to: https://bugzilla.redhat.com/show_bug.cgi?id=1527334

Signed-off-by: Pino Toscano <ptoscano@redhat.com>
  • Loading branch information...
ptoscano committed Apr 12, 2018
1 parent da7733d commit 3ff967d614c3fe093c12028515373726d8658102
@@ -285,6 +285,23 @@ C<NULL>. Note that this function does not check that the file exists.
The returned string must be freed by the caller.
=head2 C<nbdkit_realpath>
char *nbdkit_realpath (const char *filename);
The utility function C<nbdkit_realpath> converts any path to an
absolute path, resolving symlinks. Under the hood it uses the
C<realpath> function, and thus it fails if the path does not exist,
or it is not possible to access to any of the components of the path.
Note that this function works I<only> when used in the C<.config>, and
C<.config_complete> callbacks.
If the path resolution was not possible, this calls C<nbdkit_error>
and returns C<NULL>.
The returned string must be freed by the caller.
=head1 CALLBACKS
=head2 C<.name>
@@ -67,6 +67,7 @@ extern void nbdkit_vdebug (const char *msg, va_list args);
extern char *nbdkit_absolute_path (const char *path);
extern int64_t nbdkit_parse_size (const char *str);
extern int nbdkit_read_password (const char *value, char **password);
extern char *nbdkit_realpath (const char *path);
#ifdef __cplusplus
}
@@ -78,7 +78,7 @@ example2_config (const char *key, const char *value)
{
if (strcmp (key, "file") == 0) {
/* See FILENAMES AND PATHS in nbdkit-plugin(3). */
filename = nbdkit_absolute_path (value);
filename = nbdkit_realpath (value);
if (!filename)
return -1;
}
@@ -65,7 +65,7 @@ file_config (const char *key, const char *value)
if (strcmp (key, "file") == 0) {
/* See FILENAMES AND PATHS in nbdkit-plugin(3). */
free (filename);
filename = nbdkit_absolute_path (value);
filename = nbdkit_realpath (value);
if (!filename)
return -1;
}
@@ -90,10 +90,6 @@ file_config_complete (void)
nbdkit_error ("you must supply the file=<FILENAME> parameter after the plugin name on the command line");
return -1;
}
if (access (filename, F_OK) < 0) {
nbdkit_error ("access '%s': %m", filename);
return -1;
}
return 0;
}
@@ -62,7 +62,7 @@ gzip_config (const char *key, const char *value)
{
if (strcmp (key, "file") == 0) {
/* See FILENAMES AND PATHS in nbdkit-plugin(3). */
filename = nbdkit_absolute_path (value);
filename = nbdkit_realpath (value);
if (!filename)
return -1;
}
@@ -76,7 +76,7 @@ split_config (const char *key, const char *value)
return -1;
}
filenames = new_filenames;
filenames[nr_files] = nbdkit_absolute_path (value);
filenames[nr_files] = nbdkit_realpath (value);
if (filenames[nr_files] == NULL)
return -1;
nr_files++;
@@ -153,7 +153,7 @@ vddk_config (const char *key, const char *value)
if (strcmp (key, "config") == 0) {
/* See FILENAMES AND PATHS in nbdkit-plugin(3). */
free (config);
config = nbdkit_absolute_path (value);
config = nbdkit_realpath (value);
if (!config)
return -1;
}
@@ -67,7 +67,7 @@ xz_config (const char *key, const char *value)
{
if (strcmp (key, "file") == 0) {
/* See FILENAMES AND PATHS in nbdkit-plugin(3). */
filename = nbdkit_absolute_path (value);
filename = nbdkit_realpath (value);
if (!filename)
return -1;
}
@@ -133,7 +133,7 @@ plugin_dump_fields (struct backend *b)
struct backend_plugin *p = container_of (b, struct backend_plugin, backend);
char *path;
path = nbdkit_absolute_path (p->filename);
path = nbdkit_realpath (p->filename);
printf ("path=%s\n", path);
free (path);
@@ -227,3 +227,22 @@ nbdkit_read_password (const char *value, char **password)
return 0;
}
char *
nbdkit_realpath (const char *path)
{
char *ret;
if (path == NULL || *path == '\0') {
nbdkit_error ("cannot resolve a null or empty path");
return NULL;
}
ret = realpath (path, NULL);
if (ret == NULL) {
nbdkit_error ("realpath(%s): %m", path);
return NULL;
}
return ret;
}

0 comments on commit 3ff967d

Please sign in to comment.