2 * The PCI Library -- ID to Name Cache
4 * Copyright (c) 2008--2009 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL v2+.
8 * SPDX-License-Identifier: GPL-2.0-or-later
20 #include <sys/types.h>
25 static const char cache_version[] = "#PCI-CACHE-1.0";
27 static char *get_cache_name(struct pci_access *a)
29 if (!a->id_cache_name)
31 char *name = pci_get_param(a, "net.cache_name");
32 if (!name || !name[0])
35 if (strncmp(name, "~/", 2))
36 a->id_cache_name = pci_strdup(a, name);
40 struct passwd *pw = getpwuid(uid);
44 a->id_cache_name = pci_malloc(a, strlen(pw->pw_dir) + strlen(name+1) + 1);
45 sprintf(a->id_cache_name, "%s%s", pw->pw_dir, name+1);
49 return a->id_cache_name;
52 static void create_parent_dirs(struct pci_access *a, char *name)
54 // Assumes that we have a private copy of the name we can modify
56 char *p = name + strlen(name);
57 while (p > name && *p != '/')
64 // We stand at a slash. Check if the current prefix exists.
67 int res = stat(name, &st);
72 // Does not exist yet, move up one directory
74 while (p > name && *p != '/')
78 // We now stand at the end of the longest existing prefix.
79 // Create all directories to the right of it.
83 while (*p && *p != '/')
89 int res = mkdir(name, 0777);
92 a->warning("Cannot create directory %s: %s", name, strerror(errno));
101 pci_id_cache_load(struct pci_access *a, int flags)
108 if (a->id_cache_status > 0)
110 a->id_cache_status = 1;
112 name = get_cache_name(a);
115 a->debug("Using cache %s\n", name);
117 if (flags & PCI_LOOKUP_REFRESH_CACHE)
119 a->debug("Not loading cache, will refresh everything\n");
120 a->id_cache_status = 2;
124 f = fopen(name, "rb");
127 a->debug("Cache file does not exist\n");
130 /* FIXME: Compare timestamp with the pci.ids file? */
133 while (fgets(line, sizeof(line), f))
135 char *p = strchr(line, '\n');
142 if (strcmp(line, cache_version))
144 a->debug("Unrecognized cache version %s, ignoring\n", line);
151 int cat, id1, id2, id3, id4, cnt;
152 if (sscanf(line, "%d%x%x%x%x%n", &cat, &id1, &id2, &id3, &id4, &cnt) >= 5)
155 while (*p && *p == ' ')
157 pci_id_insert(a, cat, id1, id2, id3, id4, p, SRC_CACHE);
162 a->warning("Malformed cache file %s (line %d), ignoring", name, lino);
167 a->warning("Error while reading %s", name);
173 pci_id_cache_flush(struct pci_access *a)
175 int orig_status = a->id_cache_status;
178 struct id_entry *e, *e2;
179 char hostname[256], *tmpname, *name;
182 a->id_cache_status = 0;
185 name = get_cache_name(a);
189 create_parent_dirs(a, name);
192 if (gethostname(hostname, sizeof(hostname)) < 0)
195 hostname[sizeof(hostname)-1] = 0;
196 tmpname = pci_malloc(a, strlen(name) + strlen(hostname) + 64);
197 sprintf(tmpname, "%s.tmp-%s-%d", name, hostname, this_pid);
199 f = fopen(tmpname, "wb");
202 a->warning("Cannot write to %s: %s", name, strerror(errno));
206 a->debug("Writing cache to %s\n", name);
207 fprintf(f, "%s\n", cache_version);
209 for (h=0; h<HASH_SIZE; h++)
210 for (e=a->id_hash[h]; e; e=e->next)
211 if (e->src == SRC_CACHE || e->src == SRC_NET)
213 /* Negative entries are not written */
217 /* Verify that every entry is written at most once */
218 for (e2=a->id_hash[h]; e2 != e; e2=e2->next)
219 if ((e2->src == SRC_CACHE || e2->src == SRC_NET) &&
221 e2->id12 == e->id12 && e2->id34 == e->id34)
224 fprintf(f, "%d %x %x %x %x %s\n",
226 pair_first(e->id12), pair_second(e->id12),
227 pair_first(e->id34), pair_second(e->id34),
233 a->warning("Error writing %s", name);
236 if (rename(tmpname, name) < 0)
238 a->warning("Cannot rename %s to %s: %s", tmpname, name, strerror(errno));
246 int pci_id_cache_load(struct pci_access *a UNUSED, int flags UNUSED)
248 a->id_cache_status = 1;
252 void pci_id_cache_flush(struct pci_access *a)
254 a->id_cache_status = 0;
255 pci_mfree(a->id_cache_name);
256 a->id_cache_name = NULL;
262 pci_id_cache_dirty(struct pci_access *a)
264 if (a->id_cache_status >= 1)
265 a->id_cache_status = 2;