2 * Copyright (C) 2013 Red Hat Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of Red Hat nor the names of its contributors may be
17 * used to endorse or promote products derived from this software without
18 * specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 #include "nbdkit-plugin.h"
49 nbdkit_absolute_path (const char *path
)
51 CLEANUP_FREE
char *pwd
= NULL
;
54 if (path
== NULL
|| *path
== '\0') {
55 nbdkit_error ("cannot convert null or empty path to an absolute path");
62 nbdkit_error ("strdup: %m");
68 pwd
= get_current_dir_name ();
70 nbdkit_error ("get_current_dir_name: %m");
74 if (asprintf (&ret
, "%s/%s", pwd
, path
) == -1) {
75 nbdkit_error ("asprintf: %m");
82 /* XXX Multiple problems with this function. Really we should use the
83 * 'human*' functions from gnulib.
86 nbdkit_parse_size (const char *str
)
91 if (sscanf (str
, "%" SCNu64
"%c", &size
, &t
) == 2) {
94 return (int64_t) size
;
96 return (int64_t) size
* 1024;
98 return (int64_t) size
* 1024 * 1024;
100 return (int64_t) size
* 1024 * 1024 * 1024;
102 return (int64_t) size
* 1024 * 1024 * 1024 * 1024;
104 return (int64_t) size
* 1024 * 1024 * 1024 * 1024 * 1024;
106 return (int64_t) size
* 1024 * 1024 * 1024 * 1024 * 1024 * 1024;
108 case 's': case 'S': /* "sectors", ie. units of 512 bytes,
109 * even if that's not the real sector size
111 return (int64_t) size
* 512;
114 nbdkit_error ("could not parse size: unknown specifier '%c'", t
);
120 if (sscanf (str
, "%" SCNu64
, &size
) == 1)
121 return (int64_t) size
;
123 nbdkit_error ("could not parse size string (%s)", str
);
127 /* Write buffer to socket and either succeed completely (returns 0)
128 * or fail (returns -1).
131 xwrite (int sock
, const void *vbuf
, size_t len
)
133 const char *buf
= vbuf
;
137 r
= write (sock
, buf
, len
);
139 if (errno
== EINTR
|| errno
== EAGAIN
)
150 /* Read buffer from socket and either succeed completely (returns > 0),
151 * read an EOF (returns 0), or fail (returns -1).
154 xread (int sock
, void *vbuf
, size_t len
)
158 bool first_read
= true;
161 r
= read (sock
, buf
, len
);
163 if (errno
== EINTR
|| errno
== EAGAIN
)
170 /* Partial record read. This is an error. */