filesdb.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. * dpkg - main program for package management
  3. * filesdb.c - management of database of files installed on system
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2000,2001 Wichert Akkerman <wakkerma@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #ifdef HAVE_LINUX_FIEMAP_H
  24. #include <linux/fiemap.h>
  25. #include <linux/fs.h>
  26. #include <sys/ioctl.h>
  27. #include <sys/vfs.h>
  28. #endif
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <assert.h>
  32. #include <errno.h>
  33. #include <string.h>
  34. #include <pwd.h>
  35. #include <grp.h>
  36. #include <fcntl.h>
  37. #include <unistd.h>
  38. #include <stdlib.h>
  39. #include <dpkg/i18n.h>
  40. #include <dpkg/dpkg.h>
  41. #include <dpkg/dpkg-db.h>
  42. #include <dpkg/path.h>
  43. #include <dpkg/dir.h>
  44. #include <dpkg/fdio.h>
  45. #include <dpkg/pkg-array.h>
  46. #include <dpkg/progress.h>
  47. #include "filesdb.h"
  48. #include "infodb.h"
  49. #include "main.h"
  50. /*** filepackages support for tracking packages owning a file. ***/
  51. struct filepackages_iterator {
  52. struct pkg_list *pkg_node;
  53. };
  54. struct filepackages_iterator *
  55. filepackages_iter_new(struct filenamenode *fnn)
  56. {
  57. struct filepackages_iterator *iter;
  58. iter = m_malloc(sizeof(*iter));
  59. iter->pkg_node = fnn->packages;
  60. return iter;
  61. }
  62. struct pkginfo *
  63. filepackages_iter_next(struct filepackages_iterator *iter)
  64. {
  65. struct pkg_list *pkg_node;
  66. if (iter->pkg_node == NULL)
  67. return NULL;
  68. pkg_node = iter->pkg_node;
  69. iter->pkg_node = pkg_node->next;
  70. return pkg_node->pkg;
  71. }
  72. void
  73. filepackages_iter_free(struct filepackages_iterator *iter)
  74. {
  75. free(iter);
  76. }
  77. /*** Generic data structures and routines. ***/
  78. static bool allpackagesdone = false;
  79. static int nfiles= 0;
  80. void
  81. ensure_package_clientdata(struct pkginfo *pkg)
  82. {
  83. if (pkg->clientdata)
  84. return;
  85. pkg->clientdata = nfmalloc(sizeof(struct perpackagestate));
  86. pkg->clientdata->istobe = itb_normal;
  87. pkg->clientdata->color = white;
  88. pkg->clientdata->fileslistvalid = false;
  89. pkg->clientdata->files = NULL;
  90. pkg->clientdata->replacingfilesandsaid = 0;
  91. pkg->clientdata->listfile_phys_offs = 0;
  92. pkg->clientdata->trigprocdeferred = NULL;
  93. }
  94. void note_must_reread_files_inpackage(struct pkginfo *pkg) {
  95. allpackagesdone = false;
  96. ensure_package_clientdata(pkg);
  97. pkg->clientdata->fileslistvalid = false;
  98. }
  99. enum filesdb_load_status {
  100. filesdb_load_none = 0,
  101. filesdb_load_inprogress = 1,
  102. filesdb_load_done = 2,
  103. };
  104. static enum filesdb_load_status saidread = filesdb_load_none;
  105. /**
  106. * Erase the files saved in pkg.
  107. */
  108. static void
  109. pkg_files_blank(struct pkginfo *pkg)
  110. {
  111. struct fileinlist *current;
  112. /* Anything to empty? */
  113. if (!pkg->clientdata)
  114. return;
  115. for (current= pkg->clientdata->files;
  116. current;
  117. current= current->next) {
  118. struct pkg_list *pkg_node, *pkg_prev = NULL;
  119. /* For each file that used to be in the package,
  120. * go through looking for this package's entry in the list
  121. * of packages containing this file, and blank it out. */
  122. for (pkg_node = current->namenode->packages;
  123. pkg_node;
  124. pkg_node = pkg_node->next) {
  125. if (pkg_node->pkg == pkg) {
  126. if (pkg_prev)
  127. pkg_prev->next = pkg_node->next;
  128. else
  129. current->namenode->packages = pkg_node->next;
  130. /* The actual filelist links were allocated using nfmalloc, so
  131. * we shouldn't free them. */
  132. break;
  133. }
  134. pkg_prev = pkg_node;
  135. }
  136. }
  137. pkg->clientdata->files = NULL;
  138. }
  139. static struct fileinlist **
  140. pkg_files_add_file(struct pkginfo *pkg, struct filenamenode *namenode,
  141. struct fileinlist **file_tail)
  142. {
  143. struct fileinlist *newent;
  144. struct pkg_list *pkg_node;
  145. ensure_package_clientdata(pkg);
  146. if (file_tail == NULL)
  147. file_tail = &pkg->clientdata->files;
  148. /* Make sure we're at the end. */
  149. while ((*file_tail) != NULL) {
  150. file_tail = &((*file_tail)->next);
  151. }
  152. /* Create a new node. */
  153. newent = nfmalloc(sizeof(struct fileinlist));
  154. newent->namenode = namenode;
  155. newent->next = NULL;
  156. *file_tail = newent;
  157. file_tail = &newent->next;
  158. /* Add pkg to newent's package list. */
  159. pkg_node = nfmalloc(sizeof(*pkg_node));
  160. pkg_node->pkg = pkg;
  161. pkg_node->next = newent->namenode->packages;
  162. newent->namenode->packages = pkg_node;
  163. /* Return the position for the next guy. */
  164. return file_tail;
  165. }
  166. /**
  167. * Load the list of files in this package into memory, or update the
  168. * list if it is there but stale.
  169. */
  170. void
  171. ensure_packagefiles_available(struct pkginfo *pkg)
  172. {
  173. static int fd;
  174. const char *filelistfile;
  175. struct fileinlist **lendp;
  176. struct stat stat_buf;
  177. char *loaded_list, *loaded_list_end, *thisline, *nextline, *ptr;
  178. if (pkg->clientdata && pkg->clientdata->fileslistvalid)
  179. return;
  180. ensure_package_clientdata(pkg);
  181. /* Throw away any stale data, if there was any. */
  182. pkg_files_blank(pkg);
  183. /* Packages which aren't installed don't have a files list. */
  184. if (pkg->status == stat_notinstalled) {
  185. pkg->clientdata->fileslistvalid = true;
  186. return;
  187. }
  188. filelistfile = pkg_infodb_get_file(pkg, &pkg->installed, LISTFILE);
  189. onerr_abort++;
  190. fd= open(filelistfile,O_RDONLY);
  191. if (fd==-1) {
  192. if (errno != ENOENT)
  193. ohshite(_("unable to open files list file for package `%.250s'"),
  194. pkg_name(pkg, pnaw_nonambig));
  195. onerr_abort--;
  196. if (pkg->status != stat_configfiles &&
  197. dpkg_version_is_informative(&pkg->configversion)) {
  198. warning(_("files list file for package '%.250s' missing; assuming "
  199. "package has no files currently installed"),
  200. pkg_name(pkg, pnaw_nonambig));
  201. }
  202. pkg->clientdata->files = NULL;
  203. pkg->clientdata->fileslistvalid = true;
  204. return;
  205. }
  206. push_cleanup(cu_closefd, ehflag_bombout, NULL, 0, 1, &fd);
  207. if (fstat(fd, &stat_buf))
  208. ohshite(_("unable to stat files list file for package '%.250s'"),
  209. pkg_name(pkg, pnaw_nonambig));
  210. if (!S_ISREG(stat_buf.st_mode))
  211. ohshit(_("files list for package '%.250s' is not a regular file"),
  212. pkg_name(pkg, pnaw_nonambig));
  213. if (stat_buf.st_size) {
  214. loaded_list = nfmalloc(stat_buf.st_size);
  215. loaded_list_end = loaded_list + stat_buf.st_size;
  216. if (fd_read(fd, loaded_list, stat_buf.st_size) < 0)
  217. ohshite(_("reading files list for package '%.250s'"),
  218. pkg_name(pkg, pnaw_nonambig));
  219. lendp= &pkg->clientdata->files;
  220. thisline = loaded_list;
  221. while (thisline < loaded_list_end) {
  222. struct filenamenode *namenode;
  223. ptr = memchr(thisline, '\n', loaded_list_end - thisline);
  224. if (ptr == NULL)
  225. ohshit(_("files list file for package '%.250s' is missing final newline"),
  226. pkg_name(pkg, pnaw_nonambig));
  227. /* Where to start next time around. */
  228. nextline = ptr + 1;
  229. /* Strip trailing ‘/’. */
  230. if (ptr > thisline && ptr[-1] == '/') ptr--;
  231. /* Add the file to the list. */
  232. if (ptr == thisline)
  233. ohshit(_("files list file for package `%.250s' contains empty filename"),
  234. pkg_name(pkg, pnaw_nonambig));
  235. *ptr = '\0';
  236. namenode = findnamenode(thisline, fnn_nocopy);
  237. lendp = pkg_files_add_file(pkg, namenode, lendp);
  238. thisline = nextline;
  239. }
  240. }
  241. pop_cleanup(ehflag_normaltidy); /* fd = open() */
  242. if (close(fd))
  243. ohshite(_("error closing files list file for package `%.250s'"),
  244. pkg_name(pkg, pnaw_nonambig));
  245. onerr_abort--;
  246. pkg->clientdata->fileslistvalid = true;
  247. }
  248. #if defined(HAVE_LINUX_FIEMAP_H)
  249. static int
  250. pkg_sorter_by_listfile_phys_offs(const void *a, const void *b)
  251. {
  252. const struct pkginfo *pa = *(const struct pkginfo **)a;
  253. const struct pkginfo *pb = *(const struct pkginfo **)b;
  254. /* We can't simply subtract, because the difference may be greater than
  255. * INT_MAX. */
  256. if (pa->clientdata->listfile_phys_offs < pb->clientdata->listfile_phys_offs)
  257. return -1;
  258. else
  259. return 1;
  260. }
  261. static void
  262. pkg_files_optimize_load(struct pkg_array *array)
  263. {
  264. struct statfs fs;
  265. int i;
  266. /* Get the filesystem block size. */
  267. if (statfs(pkg_infodb_get_dir(), &fs) < 0)
  268. return;
  269. /* Sort packages by the physical location of their list files, so that
  270. * scanning them later will minimize disk drive head movements. */
  271. for (i = 0; i < array->n_pkgs; i++) {
  272. struct pkginfo *pkg = array->pkgs[i];
  273. struct {
  274. struct fiemap fiemap;
  275. struct fiemap_extent extent;
  276. } fm;
  277. const char *listfile;
  278. int fd;
  279. ensure_package_clientdata(pkg);
  280. if (pkg->status == stat_notinstalled ||
  281. pkg->clientdata->listfile_phys_offs != 0)
  282. continue;
  283. pkg->clientdata->listfile_phys_offs = -1;
  284. listfile = pkg_infodb_get_file(pkg, &pkg->installed, LISTFILE);
  285. fd = open(listfile, O_RDONLY);
  286. if (fd < 0)
  287. continue;
  288. memset(&fm, 0, sizeof(fm));
  289. fm.fiemap.fm_start = 0;
  290. fm.fiemap.fm_length = fs.f_bsize;
  291. fm.fiemap.fm_flags = 0;
  292. fm.fiemap.fm_extent_count = 1;
  293. if (ioctl(fd, FS_IOC_FIEMAP, (unsigned long)&fm) == 0)
  294. pkg->clientdata->listfile_phys_offs = fm.fiemap.fm_extents[0].fe_physical;
  295. close(fd);
  296. }
  297. pkg_array_sort(array, pkg_sorter_by_listfile_phys_offs);
  298. }
  299. #elif defined(HAVE_POSIX_FADVISE)
  300. static void
  301. pkg_files_optimize_load(struct pkg_array *array)
  302. {
  303. int i;
  304. /* Ask the kernel to start preloading the list files, so as to get a
  305. * boost when later we actually load them. */
  306. for (i = 0; i < array->n_pkgs; i++) {
  307. struct pkginfo *pkg = array->pkgs[i];
  308. const char *listfile;
  309. int fd;
  310. listfile = pkg_infodb_get_file(pkg, &pkg->installed, LISTFILE);
  311. fd = open(listfile, O_RDONLY | O_NONBLOCK);
  312. if (fd != -1) {
  313. posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED);
  314. close(fd);
  315. }
  316. }
  317. }
  318. #else
  319. static void
  320. pkg_files_optimize_load(struct pkg_array *array)
  321. {
  322. }
  323. #endif
  324. void ensure_allinstfiles_available(void) {
  325. struct pkg_array array;
  326. struct pkginfo *pkg;
  327. struct progress progress;
  328. int i;
  329. if (allpackagesdone) return;
  330. if (saidread < filesdb_load_done) {
  331. int max = pkg_db_count_pkg();
  332. saidread = filesdb_load_inprogress;
  333. progress_init(&progress, _("(Reading database ... "), max);
  334. }
  335. pkg_array_init_from_db(&array);
  336. pkg_files_optimize_load(&array);
  337. for (i = 0; i < array.n_pkgs; i++) {
  338. pkg = array.pkgs[i];
  339. ensure_packagefiles_available(pkg);
  340. if (saidread == filesdb_load_inprogress)
  341. progress_step(&progress);
  342. }
  343. pkg_array_destroy(&array);
  344. allpackagesdone = true;
  345. if (saidread == filesdb_load_inprogress) {
  346. progress_done(&progress);
  347. printf(P_("%d file or directory currently installed.)\n",
  348. "%d files and directories currently installed.)\n", nfiles),
  349. nfiles);
  350. saidread = filesdb_load_done;
  351. }
  352. }
  353. void ensure_allinstfiles_available_quiet(void) {
  354. saidread = filesdb_load_done;
  355. ensure_allinstfiles_available();
  356. }
  357. /*
  358. * If mask is nonzero, will not write any file whose filenamenode
  359. * has any flag bits set in mask.
  360. */
  361. void
  362. write_filelist_except(struct pkginfo *pkg, struct pkgbin *pkgbin,
  363. struct fileinlist *list, enum fnnflags mask)
  364. {
  365. struct atomic_file *file;
  366. const char *listfile;
  367. listfile = pkg_infodb_get_file(pkg, pkgbin, LISTFILE);
  368. file = atomic_file_new(listfile, 0);
  369. atomic_file_open(file);
  370. while (list) {
  371. if (!(mask && (list->namenode->flags & mask))) {
  372. fputs(list->namenode->name, file->fp);
  373. putc('\n', file->fp);
  374. }
  375. list= list->next;
  376. }
  377. atomic_file_sync(file);
  378. atomic_file_close(file);
  379. atomic_file_commit(file);
  380. atomic_file_free(file);
  381. dir_sync_path(pkg_infodb_get_dir());
  382. note_must_reread_files_inpackage(pkg);
  383. }
  384. /*
  385. * Initializes an iterator that appears to go through the file
  386. * list ‘files’ in reverse order, returning the namenode from
  387. * each. What actually happens is that we walk the list here,
  388. * building up a reverse list, and then peel it apart one
  389. * entry at a time.
  390. */
  391. void
  392. reversefilelist_init(struct reversefilelistiter *iter, struct fileinlist *files)
  393. {
  394. struct fileinlist *newent;
  395. iter->todo = NULL;
  396. while (files) {
  397. newent= m_malloc(sizeof(struct fileinlist));
  398. newent->namenode= files->namenode;
  399. newent->next = iter->todo;
  400. iter->todo = newent;
  401. files= files->next;
  402. }
  403. }
  404. struct filenamenode *
  405. reversefilelist_next(struct reversefilelistiter *iter)
  406. {
  407. struct filenamenode *ret;
  408. struct fileinlist *todo;
  409. todo = iter->todo;
  410. if (!todo)
  411. return NULL;
  412. ret= todo->namenode;
  413. iter->todo = todo->next;
  414. free(todo);
  415. return ret;
  416. }
  417. /*
  418. * Clients must call this function to clean up the reversefilelistiter
  419. * if they wish to break out of the iteration before it is all done.
  420. * Calling this function is not necessary if reversefilelist_next has
  421. * been called until it returned 0.
  422. */
  423. void
  424. reversefilelist_abort(struct reversefilelistiter *iter)
  425. {
  426. while (reversefilelist_next(iter));
  427. }
  428. struct fileiterator {
  429. struct filenamenode *namenode;
  430. int nbinn;
  431. };
  432. /* This must always be a power of two. If you change it consider changing
  433. * the per-character hashing factor (currently 1785 = 137 * 13) too. */
  434. #define BINS (1 << 17)
  435. static struct filenamenode *bins[BINS];
  436. struct fileiterator *
  437. files_db_iter_new(void)
  438. {
  439. struct fileiterator *iter;
  440. iter = m_malloc(sizeof(struct fileiterator));
  441. iter->namenode = NULL;
  442. iter->nbinn = 0;
  443. return iter;
  444. }
  445. struct filenamenode *
  446. files_db_iter_next(struct fileiterator *iter)
  447. {
  448. struct filenamenode *r= NULL;
  449. while (!iter->namenode) {
  450. if (iter->nbinn >= BINS)
  451. return NULL;
  452. iter->namenode = bins[iter->nbinn++];
  453. }
  454. r = iter->namenode;
  455. iter->namenode = r->next;
  456. return r;
  457. }
  458. void
  459. files_db_iter_free(struct fileiterator *iter)
  460. {
  461. free(iter);
  462. }
  463. void filesdbinit(void) {
  464. struct filenamenode *fnn;
  465. int i;
  466. for (i=0; i<BINS; i++)
  467. for (fnn= bins[i]; fnn; fnn= fnn->next) {
  468. fnn->flags= 0;
  469. fnn->oldhash = NULL;
  470. fnn->newhash = EMPTYHASHFLAG;
  471. fnn->filestat = NULL;
  472. }
  473. }
  474. static int hash(const char *name) {
  475. int v= 0;
  476. while (*name) { v *= 1787; v += *name; name++; }
  477. return v;
  478. }
  479. struct filenamenode *findnamenode(const char *name, enum fnnflags flags) {
  480. struct filenamenode **pointerp, *newnode;
  481. const char *orig_name = name;
  482. /* We skip initial slashes and ‘./’ pairs, and add our own single
  483. * leading slash. */
  484. name = path_skip_slash_dotslash(name);
  485. pointerp= bins + (hash(name) & (BINS-1));
  486. while (*pointerp) {
  487. /* XXX: Why is the assert needed? It's checking already added entries. */
  488. assert((*pointerp)->name[0] == '/');
  489. if (strcmp((*pointerp)->name + 1, name) == 0)
  490. break;
  491. pointerp= &(*pointerp)->next;
  492. }
  493. if (*pointerp) return *pointerp;
  494. if (flags & fnn_nonew)
  495. return NULL;
  496. newnode= nfmalloc(sizeof(struct filenamenode));
  497. newnode->packages = NULL;
  498. if((flags & fnn_nocopy) && name > orig_name && name[-1] == '/')
  499. newnode->name = name - 1;
  500. else {
  501. char *newname= nfmalloc(strlen(name)+2);
  502. newname[0]= '/'; strcpy(newname+1,name);
  503. newnode->name= newname;
  504. }
  505. newnode->flags= 0;
  506. newnode->next = NULL;
  507. newnode->divert = NULL;
  508. newnode->statoverride = NULL;
  509. newnode->oldhash = NULL;
  510. newnode->newhash = EMPTYHASHFLAG;
  511. newnode->filestat = NULL;
  512. newnode->trig_interested = NULL;
  513. *pointerp= newnode;
  514. nfiles++;
  515. return newnode;
  516. }