-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpp_nvpair.c
60 lines (51 loc) · 1.34 KB
/
pp_nvpair.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
57
58
59
60
/*
gcc -std=c18 -Wall -pie -I/usr/include/libzfs/ pp_nvpair.c -lnvpair -luutil -o pp_nvpair.exe
*/
#define _POSIX_SOURCE 1
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <stdarg.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
typedef unsigned int uint_t;
typedef int boolean_t;
typedef unsigned char uchar_t;
typedef int hrtime_t; /* TODO wrong */
#include <libzfs/sys/nvpair.h>
#include <libzfs/libnvpair.h>
int
main(int argc, char **argv)
{
size_t nv_sz = 0;
FILE *fd = NULL;
struct stat statbuf;
if (argc != 2) return (10);
fd = fopen(argv[1], "r");
if (!fd) return (1);
if(fstat(fileno(fd), &statbuf)) return (2);
nv_sz = statbuf.st_size;
char *outbuf = malloc(nv_sz);
if (!outbuf) return (4);
if (fread(outbuf, nv_sz, 1, fd) != 1) return (3);
nvlist_t *check = fnvlist_unpack(outbuf, nv_sz);
printf("file: %zd\n", nv_sz);
nvlist_print(stdout, check);
/* commented out because we hit an infinite loop bug:
* nvlist_print_json(stdout, check);
*/
printf("\n");
free(outbuf); outbuf = NULL;
outbuf = fnvlist_pack(check, &nv_sz);
printf("repacked %zd\n", nv_sz);
fnvlist_free(check);
nvlist_t *reunpacked = fnvlist_unpack(outbuf, nv_sz);
fnvlist_free(reunpacked);
free(outbuf);
fflush(stdout);
return 0;
}