filesdb.c 16 KB

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