Coverage Report

Created: 2026-09-13 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/util-linux/libblkid/src/verify.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2008 Karel Zak <kzak@redhat.com>
3
 *
4
 * This file may be redistributed under the terms of the
5
 * GNU Lesser General Public License.
6
 */
7
8
#include <stdio.h>
9
#include <string.h>
10
#include <stdlib.h>
11
#include <unistd.h>
12
#include <fcntl.h>
13
#include <time.h>
14
#include <sys/time.h>
15
#include <sys/types.h>
16
#ifdef HAVE_SYS_STAT_H
17
#include <sys/stat.h>
18
#endif
19
#ifdef HAVE_ERRNO_H
20
#include <errno.h>
21
#endif
22
23
#include "blkidP.h"
24
#include "sysfs.h"
25
#include "timeutils.h"
26
27
static void blkid_probe_to_tags(blkid_probe pr, blkid_dev dev)
28
0
{
29
0
  const char *data;
30
0
  const char *name;
31
0
  int nvals, n;
32
0
  size_t len;
33
34
0
  nvals = blkid_probe_numof_values(pr);
35
36
0
  for (n = 0; n < nvals; n++) {
37
0
    if (blkid_probe_get_value(pr, n, &name, &data, &len) != 0)
38
0
      continue;
39
0
    if (strncmp(name, "PART_ENTRY_", 11) == 0) {
40
0
      if (strcmp(name, "PART_ENTRY_UUID") == 0)
41
0
        blkid_set_tag(dev, "PARTUUID", data, len);
42
0
      else if (strcmp(name, "PART_ENTRY_NAME") == 0)
43
0
        blkid_set_tag(dev, "PARTLABEL", data, len);
44
45
0
    } else if (!strstr(name, "_ID")) {
46
      /* superblock UUID, LABEL, ...
47
       * but not {SYSTEM,APPLICATION,..._ID} */
48
0
      blkid_set_tag(dev, name, data, len);
49
0
    }
50
0
  }
51
0
}
52
53
/*
54
 * Verify that the data in dev is consistent with what is on the actual
55
 * block device (using the devname field only).  Normally this will be
56
 * called when finding items in the cache, but for long running processes
57
 * is also desirable to revalidate an item before use.
58
 *
59
 * If we are unable to revalidate the data, we return the old data and
60
 * do not set the BLKID_BID_FL_VERIFIED flag on it.
61
 */
62
blkid_dev blkid_verify(blkid_cache cache, blkid_dev dev)
63
0
{
64
0
  blkid_tag_iterate iter;
65
0
  const char *type, *value;
66
0
  struct stat st;
67
0
  time_t diff, now;
68
69
0
  if (!dev || !cache)
70
0
    return NULL;
71
72
0
  now = time(NULL);
73
0
  diff = (uintmax_t)now - dev->bid_time;
74
75
0
  if (stat(dev->bid_name, &st) < 0) {
76
0
    DBG(PROBE, ul_debug("blkid_verify: error %m (%d) while "
77
0
         "trying to stat %s", errno,
78
0
         dev->bid_name));
79
0
    goto dev_err;
80
0
  }
81
82
0
  if (now >= dev->bid_time &&
83
0
#ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
84
0
      (st.st_mtime < dev->bid_time ||
85
0
          (st.st_mtime == dev->bid_time &&
86
0
     (suseconds_t) (st.st_mtim.tv_nsec / NSEC_PER_USEC) <= dev->bid_utime)) &&
87
#else
88
      st.st_mtime <= dev->bid_time &&
89
#endif
90
0
      diff >= 0 && diff < BLKID_PROBE_MIN) {
91
0
    dev->bid_flags |= BLKID_BID_FL_VERIFIED;
92
0
    return dev;
93
0
  }
94
95
#ifndef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
96
  DBG(PROBE, ul_debug("need to revalidate %s (cache time %lld, stat time %lld,\t"
97
       "time since last check %lld)",
98
       dev->bid_name, (long long)dev->bid_time,
99
       (long long)st.st_mtime, (long long)diff));
100
#else
101
0
  DBG(PROBE, ul_debug("need to revalidate %s (cache time %lld.%lld, stat time %lld.%lld,\t"
102
0
       "time since last check %lld)",
103
0
       dev->bid_name,
104
0
       (long long)dev->bid_time, (long long)dev->bid_utime,
105
0
       (long long)st.st_mtime, (long long) (st.st_mtim.tv_nsec / NSEC_PER_USEC),
106
0
       (long long)diff));
107
0
#endif
108
109
0
  if (sysfs_devno_is_dm_private(st.st_rdev, NULL, NULL))
110
0
    goto dev_free;
111
0
  if (!cache->probe) {
112
0
    cache->probe = blkid_new_probe();
113
0
    if (!cache->probe)
114
0
      goto dev_free;
115
0
  }
116
117
0
  if (blkid_probe_open_device(cache->probe, dev->bid_name, 0)) {
118
0
    DBG(PROBE, ul_debug("blkid_verify: error %m (%d) while "
119
0
          "opening %s", errno,
120
0
          dev->bid_name));
121
0
    goto dev_err;
122
0
  }
123
124
  /* remove old cache info */
125
0
  iter = blkid_tag_iterate_begin(dev);
126
0
  while (blkid_tag_next(iter, &type, &value) == 0)
127
0
    blkid_set_tag(dev, type, NULL, 0);
128
0
  blkid_tag_iterate_end(iter);
129
130
  /* enable superblocks probing */
131
0
  blkid_probe_enable_superblocks(cache->probe, TRUE);
132
0
  blkid_probe_set_superblocks_flags(cache->probe,
133
0
    BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
134
0
    BLKID_SUBLKS_TYPE | BLKID_SUBLKS_SECTYPE);
135
136
  /* enable partitions probing */
137
0
  blkid_probe_enable_partitions(cache->probe, TRUE);
138
0
  blkid_probe_set_partitions_flags(cache->probe, BLKID_PARTS_ENTRY_DETAILS);
139
140
  /* probe */
141
0
  if (blkid_do_safeprobe(cache->probe)) {
142
    /* found nothing or error */
143
0
    blkid_free_dev(dev);
144
0
    dev = NULL;
145
0
  }
146
147
0
  if (dev) {
148
0
#ifdef HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
149
0
    struct timeval tv;
150
0
    if (!gettimeofday(&tv, NULL)) {
151
0
      dev->bid_time = tv.tv_sec;
152
0
      dev->bid_utime = tv.tv_usec;
153
0
    } else
154
0
#endif
155
0
      dev->bid_time = time(NULL);
156
157
0
    dev->bid_devno = st.st_rdev;
158
0
    dev->bid_flags |= BLKID_BID_FL_VERIFIED;
159
0
    cache->bic_flags |= BLKID_BIC_FL_CHANGED;
160
161
0
    blkid_probe_to_tags(cache->probe, dev);
162
163
0
    DBG(PROBE, ul_debug("%s: devno 0x%04llx, type %s",
164
0
         dev->bid_name, (long long)st.st_rdev, dev->bid_type));
165
0
  }
166
167
  /* reset prober */
168
0
  blkid_probe_reset_superblocks_filter(cache->probe);
169
0
  blkid_probe_set_device(cache->probe, -1, 0, 0);
170
171
0
  return dev;
172
173
0
dev_err:
174
0
  if ((errno == EPERM) || (errno == EACCES) || (errno == ENOENT)) {
175
0
    DBG(PROBE, ul_debug("returning unverified data for %s",
176
0
          dev->bid_name));
177
0
    return dev;
178
0
  }
179
0
dev_free:
180
0
  blkid_free_dev(dev);
181
  return NULL;
182
0
}
183
184
#ifdef TEST_PROGRAM
185
int main(int argc, char **argv)
186
{
187
  blkid_dev dev;
188
  blkid_cache cache;
189
  int ret;
190
191
  if (argc != 2) {
192
    fprintf(stderr, "Usage: %s device\n"
193
      "Probe a single device to determine type\n", argv[0]);
194
    exit(1);
195
  }
196
  if ((ret = blkid_get_cache(&cache, "/dev/null")) != 0) {
197
    fprintf(stderr, "%s: error creating cache (%d)\n",
198
      argv[0], ret);
199
    exit(1);
200
  }
201
  dev = blkid_get_dev(cache, argv[1], BLKID_DEV_NORMAL);
202
  if (!dev) {
203
    printf("%s: %s has an unsupported type\n", argv[0], argv[1]);
204
    return (1);
205
  }
206
  printf("TYPE='%s'\n", dev->bid_type ? dev->bid_type : "(null)");
207
  if (dev->bid_label)
208
    printf("LABEL='%s'\n", dev->bid_label);
209
  if (dev->bid_uuid)
210
    printf("UUID='%s'\n", dev->bid_uuid);
211
212
  blkid_free_dev(dev);
213
  return (0);
214
}
215
#endif