Skip to content

Commit

Permalink
streaming: Miscellaneous code cleanups to this plugin.
Browse files Browse the repository at this point in the history
- Rearrange headers.

- Use boolean to store errorstate flag.

- Move unload function to top, next to the variables it uses.

- Use NBDKIT_HANDLE_NOT_NEEDED.

- Remove close callback as it is empty and unneeded.

- Added comments.
  • Loading branch information
rwmjones committed Jul 24, 2020
1 parent 54af6ce commit 97d3cb4
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions plugins/streaming/streaming.c
Expand Up @@ -34,15 +34,17 @@

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

#include <nbdkit-plugin.h>

/* The pipe. */
static char *filename = NULL;
static int fd = -1;

Expand All @@ -54,11 +56,19 @@ static int64_t size = INT64_C(9223372036854775296);
/* Flag if we have entered the unrecoverable error state because of
* a seek backwards.
*/
static int errorstate = 0;
static bool errorstate = 0;

/* Highest byte (+1) that has been written in the data stream. */
static uint64_t highestwrite = 0;

static void
streaming_unload (void)
{
if (fd >= 0)
close (fd);
free (filename);
}

/* Called for each key=value passed on the command line. */
static int
streaming_config (const char *key, const char *value)
Expand Down Expand Up @@ -119,15 +129,6 @@ streaming_get_ready (void)
return 0;
}

/* nbdkit is shutting down. */
static void
streaming_unload (void)
{
if (fd >= 0)
close (fd);
free (filename);
}

#define streaming_config_help \
"write=<FILENAME> (required) The filename to serve.\n" \
"size=<SIZE> (optional) Stream size."
Expand All @@ -136,8 +137,6 @@ streaming_unload (void)
static void *
streaming_open (int readonly)
{
void *h;

if (readonly) {
nbdkit_error ("you cannot use the -r option with the streaming plugin");
return NULL;
Expand All @@ -149,16 +148,7 @@ streaming_open (int readonly)
return NULL;
}

/* There is no handle, so return an arbitrary non-NULL pointer. */
h = &fd;

return h;
}

/* Free up the per-connection handle. */
static void
streaming_close (void *handle)
{
return NBDKIT_HANDLE_NOT_NEEDED;
}

#define THREAD_MODEL NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS
Expand Down Expand Up @@ -187,7 +177,7 @@ streaming_pwrite (void *handle, const void *buf,
if (offset < highestwrite) {
nbdkit_error ("client tried to seek backwards and write: "
"the streaming plugin does not currently support this");
errorstate = 1;
errorstate = true;
errno = EIO;
return -1;
}
Expand All @@ -202,7 +192,7 @@ streaming_pwrite (void *handle, const void *buf,
r = write (fd, zerobuf, n);
if (r == -1) {
nbdkit_error ("write: %m");
errorstate = 1;
errorstate = true;
return -1;
}
highestwrite += r;
Expand All @@ -215,7 +205,7 @@ streaming_pwrite (void *handle, const void *buf,
r = write (fd, buf, count);
if (r == -1) {
nbdkit_error ("write: %m");
errorstate = 1;
errorstate = true;
return -1;
}
buf += r;
Expand Down Expand Up @@ -259,7 +249,6 @@ static struct nbdkit_plugin plugin = {
.config_help = streaming_config_help,
.get_ready = streaming_get_ready,
.open = streaming_open,
.close = streaming_close,
.get_size = streaming_get_size,
.pwrite = streaming_pwrite,
.pread = streaming_pread,
Expand Down

0 comments on commit 97d3cb4

Please sign in to comment.