xref: /netbsd-src/external/gpl3/gdb/dist/libctf/testsuite/libctf-regression/gzrewrite.c (revision c9055873d0546e63388f027d3d7f85381cde0545)
1*c9055873Schristos /* Make sure that you can modify then ctf_gzwrite() a dict
2*c9055873Schristos    and it changes after modification.  */
3*c9055873Schristos 
4*c9055873Schristos #include <ctf-api.h>
5*c9055873Schristos #include <errno.h>
6*c9055873Schristos #include <stdio.h>
7*c9055873Schristos #include <stdlib.h>
8*c9055873Schristos #include <string.h>
9*c9055873Schristos #include <unistd.h>
10*c9055873Schristos #include <zlib.h>
11*c9055873Schristos 
12*c9055873Schristos char *read_gz(const char *path, size_t *len)
13*c9055873Schristos {
14*c9055873Schristos   char *in = NULL;
15*c9055873Schristos   char buf[4096];
16*c9055873Schristos   gzFile foo;
17*c9055873Schristos   size_t ret;
18*c9055873Schristos 
19*c9055873Schristos   if ((foo = gzopen (path, "rb")) == NULL)
20*c9055873Schristos     return NULL;
21*c9055873Schristos 
22*c9055873Schristos   *len = 0;
23*c9055873Schristos   while ((ret = gzread (foo, buf, 4096)) > 0)
24*c9055873Schristos     {
25*c9055873Schristos       if ((in = realloc (in, *len + ret)) == NULL)
26*c9055873Schristos 	{
27*c9055873Schristos 	  fprintf (stderr, "Out of memory\n");
28*c9055873Schristos 	  exit (1);
29*c9055873Schristos 	}
30*c9055873Schristos 
31*c9055873Schristos       memcpy (&in[*len], buf, ret);
32*c9055873Schristos       *len += ret;
33*c9055873Schristos     }
34*c9055873Schristos   if (ret < 0)
35*c9055873Schristos     {
36*c9055873Schristos       int errnum;
37*c9055873Schristos       const char *err;
38*c9055873Schristos       err = gzerror (foo, &errnum);
39*c9055873Schristos       if (errnum != Z_ERRNO)
40*c9055873Schristos 	fprintf (stderr, "error reading %s: %s\n", path, err);
41*c9055873Schristos       else
42*c9055873Schristos 	fprintf (stderr, "error reading %s: %s\n", path, strerror(errno));
43*c9055873Schristos       exit (1);
44*c9055873Schristos     }
45*c9055873Schristos   gzclose (foo);
46*c9055873Schristos   return in;
47*c9055873Schristos }
48*c9055873Schristos 
49*c9055873Schristos int
50*c9055873Schristos main (int argc, char *argv[])
51*c9055873Schristos {
52*c9055873Schristos   ctf_dict_t *fp, *fp_b;
53*c9055873Schristos   ctf_archive_t *ctf;
54*c9055873Schristos   gzFile foo;
55*c9055873Schristos   char *a, *b;
56*c9055873Schristos   size_t a_len, b_len;
57*c9055873Schristos   ctf_id_t type, ptrtype;
58*c9055873Schristos   int err;
59*c9055873Schristos 
60*c9055873Schristos   if (argc != 2)
61*c9055873Schristos     {
62*c9055873Schristos       fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]);
63*c9055873Schristos       exit(1);
64*c9055873Schristos     }
65*c9055873Schristos 
66*c9055873Schristos   if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
67*c9055873Schristos     goto open_err;
68*c9055873Schristos   if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
69*c9055873Schristos     goto open_err;
70*c9055873Schristos 
71*c9055873Schristos   if ((foo = gzopen ("tmpdir/one.gz", "wb")) == NULL)
72*c9055873Schristos     goto write_gzerr;
73*c9055873Schristos   if (ctf_gzwrite (fp, foo) < 0)
74*c9055873Schristos     goto write_err;
75*c9055873Schristos   gzclose (foo);
76*c9055873Schristos 
77*c9055873Schristos   if ((foo = gzopen ("tmpdir/two.gz", "wb")) == NULL)
78*c9055873Schristos     goto write_gzerr;
79*c9055873Schristos   if (ctf_gzwrite (fp, foo) < 0)
80*c9055873Schristos     goto write_err;
81*c9055873Schristos   gzclose (foo);
82*c9055873Schristos 
83*c9055873Schristos   if ((a = read_gz ("tmpdir/one.gz", &a_len)) == NULL)
84*c9055873Schristos     goto read_err;
85*c9055873Schristos 
86*c9055873Schristos   if ((b = read_gz ("tmpdir/two.gz", &b_len)) == NULL)
87*c9055873Schristos     goto read_err;
88*c9055873Schristos 
89*c9055873Schristos   if (a_len != b_len || memcmp (a, b, a_len) != 0)
90*c9055873Schristos     {
91*c9055873Schristos       fprintf (stderr, "consecutive gzwrites are different: lengths %zu and %zu\n", a_len, b_len);
92*c9055873Schristos       return 1;
93*c9055873Schristos     }
94*c9055873Schristos 
95*c9055873Schristos   free (b);
96*c9055873Schristos 
97*c9055873Schristos   /* Add some new types to the dict and write it out, then read it back in and
98*c9055873Schristos      make sure they're still there, and that at least some of the
99*c9055873Schristos      originally-present data objects are still there too.  */
100*c9055873Schristos 
101*c9055873Schristos   if ((type = ctf_lookup_by_name (fp, "struct a_struct")) == CTF_ERR)
102*c9055873Schristos     fprintf (stderr, "Lookup of struct a_struct failed: %s\n", ctf_errmsg (ctf_errno (fp)));
103*c9055873Schristos 
104*c9055873Schristos   if ((ptrtype = ctf_add_pointer (fp, CTF_ADD_ROOT, type)) == CTF_ERR)
105*c9055873Schristos     fprintf (stderr, "Cannot add pointer to ctf_opened dict: %s\n", ctf_errmsg (ctf_errno (fp)));
106*c9055873Schristos 
107*c9055873Schristos   unlink ("tmpdir/two.gz");
108*c9055873Schristos   if ((foo = gzopen ("tmpdir/two.gz", "wb")) == NULL)
109*c9055873Schristos     goto write_gzerr;
110*c9055873Schristos   if (ctf_gzwrite (fp, foo) < 0)
111*c9055873Schristos     goto write_err;
112*c9055873Schristos   gzclose (foo);
113*c9055873Schristos 
114*c9055873Schristos   if ((b = read_gz ("tmpdir/two.gz", &b_len)) == NULL)
115*c9055873Schristos     goto read_err;
116*c9055873Schristos 
117*c9055873Schristos   if (a_len == b_len && memcmp (a, b, b_len) == 0)
118*c9055873Schristos     {
119*c9055873Schristos       fprintf (stderr, "gzwrites after adding types does not change the dict\n");
120*c9055873Schristos       return 1;
121*c9055873Schristos     }
122*c9055873Schristos 
123*c9055873Schristos   free (a);
124*c9055873Schristos   if ((fp_b = ctf_simple_open (b, b_len, NULL, 0, 0, NULL, 0, &err)) == NULL)
125*c9055873Schristos     goto open_err;
126*c9055873Schristos 
127*c9055873Schristos   if (ctf_type_reference (fp_b, ptrtype) == CTF_ERR)
128*c9055873Schristos     fprintf (stderr, "Lookup of pointer preserved across writeout failed: %s\n", ctf_errmsg (ctf_errno (fp_b)));
129*c9055873Schristos 
130*c9055873Schristos   if (ctf_type_reference (fp_b, ptrtype) != type)
131*c9055873Schristos     fprintf (stderr, "Look up of newly-added type in serialized dict yields ID %lx, expected %lx\n", ctf_type_reference (fp_b, ptrtype), type);
132*c9055873Schristos 
133*c9055873Schristos   if (ctf_lookup_by_symbol_name (fp_b, "an_int") == CTF_ERR)
134*c9055873Schristos     fprintf (stderr, "Lookup of symbol an_int failed: %s\n", ctf_errmsg (ctf_errno (fp_b)));
135*c9055873Schristos 
136*c9055873Schristos   free (b);
137*c9055873Schristos   ctf_dict_close (fp);
138*c9055873Schristos   ctf_dict_close (fp_b);
139*c9055873Schristos   ctf_close (ctf);
140*c9055873Schristos 
141*c9055873Schristos   printf ("All done.\n");
142*c9055873Schristos   return 0;
143*c9055873Schristos 
144*c9055873Schristos  open_err:
145*c9055873Schristos   fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
146*c9055873Schristos   return 1;
147*c9055873Schristos  write_err:
148*c9055873Schristos   fprintf (stderr, "%s: cannot write: %s\n", argv[0], ctf_errmsg (ctf_errno (fp)));
149*c9055873Schristos   return 1;
150*c9055873Schristos  write_gzerr:
151*c9055873Schristos   {
152*c9055873Schristos     int errnum;
153*c9055873Schristos     const char *err;
154*c9055873Schristos 
155*c9055873Schristos     err = gzerror (foo, &errnum);
156*c9055873Schristos     if (errnum != Z_ERRNO)
157*c9055873Schristos       fprintf (stderr, "error gzwriting: %s\n", err);
158*c9055873Schristos     else
159*c9055873Schristos       fprintf (stderr, "error gzwriting: %s\n", strerror(errno));
160*c9055873Schristos     return 1;
161*c9055873Schristos   }
162*c9055873Schristos  read_err:
163*c9055873Schristos   fprintf (stderr, "%s: cannot read\n", argv[0]);
164*c9055873Schristos   return 1;
165*c9055873Schristos }
166