File: journal.c

package info (click to toggle)
libguestfs 1%3A1.44.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,932 kB
  • sloc: ansic: 458,017; ml: 51,424; sh: 13,191; java: 9,578; makefile: 7,931; cs: 6,328; haskell: 5,674; python: 3,871; perl: 3,528; erlang: 2,446; xml: 1,347; ruby: 350; pascal: 257; javascript: 157; lex: 135; yacc: 128; cpp: 10
file content (161 lines) | stat: -rw-r--r-- 4,479 bytes parent folder | download | duplicates (4)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* libguestfs
 * Copyright (C) 2013 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <config.h>

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

#ifdef HAVE_ENDIAN_H
#include <endian.h>
#endif
#ifdef HAVE_SYS_ENDIAN_H
#include <sys/endian.h>
#endif

#if defined __APPLE__ && defined __MACH__
#include <libkern/OSByteOrder.h>
#define be64toh(x) OSSwapBigToHostInt64(x)
#endif

#include "full-read.h"

#include "guestfs.h"
#include "guestfs-internal.h"
#include "guestfs-internal-actions.h"

/* This is implemented library-side in order to get around potential
 * protocol limits.
 *
 * A journal record can contain an arbitrarily large amount of data
 * (stuff like core dumps in particular).  To save the user from
 * having to deal with it, the implementation uses an internal
 * function that downloads to a FileOut, and we reconstruct the
 * hashtable entries from that.
 */
struct guestfs_xattr_list *
guestfs_impl_journal_get (guestfs_h *g)
{
  CLEANUP_UNLINK_FREE char *tmpfile = NULL;
  CLEANUP_FREE char *buf = NULL;
  struct stat statbuf;
  struct guestfs_xattr_list *ret = NULL;
  char *p, *eofield, *eobuf;
  int fd = -1;
  size_t i, j, size;
  uint64_t len;

  tmpfile = guestfs_int_make_temp_path (g, "journal", NULL);
  if (tmpfile == NULL)
    goto err;

  if (guestfs_internal_journal_get (g, tmpfile) == -1)
    goto err;

  fd = open (tmpfile, O_RDONLY|O_CLOEXEC);
  if (fd == -1) {
    perrorf (g, "open: %s", tmpfile);
    goto err;
  }

  /* Read the whole file into memory. */
  if (fstat (fd, &statbuf) == -1) {
    perrorf (g, "stat: %s", tmpfile);
    goto err;
  }

  /* Don't use safe_malloc.  Want to return an errno to the caller. */
  size = statbuf.st_size;
  buf = malloc (size+1);
  if (!buf) {
    perrorf (g, "malloc: %zu bytes", size);
    goto err;
  }
  eobuf = &buf[size];
  *eobuf = '\0';                /* Makes strchr etc safe. */

  if (full_read (fd, buf, size) != size) {
    perrorf (g, "full-read: %s: %zu bytes", tmpfile, size);
    goto err;
  }

  if (close (fd) == -1) {
    perrorf (g, "close: %s", tmpfile);
    goto err;
  }
  fd = -1;

  j = 0;
  ret = safe_malloc (g, sizeof *ret);
  ret->len = 0;
  ret->val = NULL;

  /* There is a simple, private protocol employed here (note: it may
   * be changed at any time), where fields are sent using a big-endian
   * 64 bit length field followed by N bytes of 'field=data' binary
   * data.
   */
  for (i = 0; i < size; ) {
    if (i+8 > size) {
      error (g, "invalid data from guestfs_internal_journal_get: "
             "truncated: "
             "size=%zu, i=%zu", size, i);
      goto err;
    }
    memcpy(&len, &buf[i], sizeof(len));
    len = be64toh (len);
    i += 8;
    eofield = &buf[i+len];
    if (eofield > eobuf) {
      error (g, "invalid data from guestfs_internal_journal_get: "
             "length field is too large: "
             "size=%zu, i=%zu, len=%" PRIu64, size, i, len);
      goto err;
    }
    p = strchr (&buf[i], '=');
    if (!p || p >= eofield) {
      error (g, "invalid data from guestfs_internal_journal_get: "
             "no '=' found separating field name and data: "
             "size=%zu, i=%zu, p=%p", size, i, p);
      goto err;
    }
    *p = '\0';

    j++;
    ret->val = safe_realloc (g, ret->val, j * sizeof (struct guestfs_xattr));
    ret->val[j-1].attrname = safe_strdup (g, &buf[i]);
    ret->val[j-1].attrval_len = eofield - (p+1);
    ret->val[j-1].attrval = safe_memdup (g, p+1, eofield - (p+1));
    ret->len = j;
    i += len;
  }

  return ret;                   /* caller frees */

 err:
  guestfs_free_xattr_list (ret);
  if (fd >= 0)
    close (fd);
  return NULL;
}