-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathget-size.c
56 lines (47 loc) · 1.12 KB
/
get-size.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* This example shows how to connect to an NBD
* server and read the size of the disk.
*
* You can test it with nbdkit like this:
*
* nbdkit -U - memory 1M \
* --run './get-size $unixsocket'
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <libnbd.h>
int
main (int argc, char *argv[])
{
struct nbd_handle *nbd;
int64_t size;
if (argc != 2) {
fprintf (stderr, "%s socket\n", argv[0]);
exit (EXIT_FAILURE);
}
/* Create the libnbd handle. */
nbd = nbd_create ();
if (nbd == NULL) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
/* Connect to the NBD server over a
* Unix domain socket.
*/
if (nbd_connect_unix (nbd, argv[1]) == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
/* Read the size in bytes and print it. */
size = nbd_get_size (nbd);
if (size == -1) {
fprintf (stderr, "%s\n", nbd_get_error ());
exit (EXIT_FAILURE);
}
printf ("%s: size = %" PRIi64 " bytes\n",
argv[1], size);
/* Close the libnbd handle. */
nbd_close (nbd);
exit (EXIT_SUCCESS);
}