filesdb.c 16 KB

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