[nbdkit PATCH] file: Diagnose a missing file earlier
If a user typos the filename for the file plugin, they do not find out about it until later when the first client gets an abrupt hangup, when file_open() fails to open things. Better is to avoid starting a server at all if the file doesn't exist yet. While at it, avoid a memory leak if the caller passes a file= argument more than once. Signed-off-by: Eric Blake <eblake@redhat.com> --- plugins/file/file.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugins/file/file.c b/plugins/file/file.c index ef5da3d..4a91251 100644 --- a/plugins/file/file.c +++ b/plugins/file/file.c @@ -117,6 +117,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); if (!filename) return -1; @@ -147,6 +148,10 @@ 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; } -- 2.13.6
On Tue, Nov 21, 2017 at 04:45:14PM -0600, Eric Blake wrote:
If a user typos the filename for the file plugin, they do not find out about it until later when the first client gets an abrupt hangup, when file_open() fails to open things. Better is to avoid starting a server at all if the file doesn't exist yet.
While at it, avoid a memory leak if the caller passes a file= argument more than once.
Signed-off-by: Eric Blake <eblake@redhat.com> --- plugins/file/file.c | 5 +++++ 1 file changed, 5 insertions(+)
diff --git a/plugins/file/file.c b/plugins/file/file.c index ef5da3d..4a91251 100644 --- a/plugins/file/file.c +++ b/plugins/file/file.c @@ -117,6 +117,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); if (!filename) return -1; @@ -147,6 +148,10 @@ 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; + }
ACK. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top
participants (2)
-
Eric Blake -
Richard W.M. Jones