filesdb.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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->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
  263. return 1;
  264. }
  265. static void
  266. pkg_files_optimize_load(struct pkg_array *array)
  267. {
  268. struct statfs fs;
  269. int i;
  270. /* Get the filesystem block size. */
  271. if (statfs(pkg_infodb_get_dir(), &fs) < 0)
  272. return;
  273. /* Sort packages by the physical location of their list files, so that
  274. * scanning them later will minimize disk drive head movements. */
  275. for (i = 0; i < array->n_pkgs; i++) {
  276. struct pkginfo *pkg = array->pkgs[i];
  277. struct {
  278. struct fiemap fiemap;
  279. struct fiemap_extent extent;
  280. } fm;
  281. const char *listfile;
  282. int fd;
  283. ensure_package_clientdata(pkg);
  284. if (pkg->status == PKG_STAT_NOTINSTALLED ||
  285. pkg->clientdata->listfile_phys_offs != 0)
  286. continue;
  287. pkg->clientdata->listfile_phys_offs = -1;
  288. listfile = pkg_infodb_get_file(pkg, &pkg->installed, LISTFILE);
  289. fd = open(listfile, O_RDONLY);
  290. if (fd < 0)
  291. continue;
  292. memset(&fm, 0, sizeof(fm));
  293. fm.fiemap.fm_start = 0;
  294. fm.fiemap.fm_length = fs.f_bsize;
  295. fm.fiemap.fm_flags = 0;
  296. fm.fiemap.fm_extent_count = 1;
  297. if (ioctl(fd, FS_IOC_FIEMAP, (unsigned long)&fm) == 0)
  298. pkg->clientdata->listfile_phys_offs = fm.fiemap.fm_extents[0].fe_physical;
  299. close(fd);
  300. }
  301. pkg_array_sort(array, pkg_sorter_by_listfile_phys_offs);
  302. }
  303. #elif defined(HAVE_POSIX_FADVISE)
  304. static void
  305. pkg_files_optimize_load(struct pkg_array *array)
  306. {
  307. int i;
  308. /* Ask the kernel to start preloading the list files, so as to get a
  309. * boost when later we actually load them. */
  310. for (i = 0; i < array->n_pkgs; i++) {
  311. struct pkginfo *pkg = array->pkgs[i];
  312. const char *listfile;
  313. int fd;
  314. listfile = pkg_infodb_get_file(pkg, &pkg->installed, LISTFILE);
  315. fd = open(listfile, O_RDONLY | O_NONBLOCK);
  316. if (fd != -1) {
  317. posix_fadvise(fd, 0, 0, POSIX_FADV_WILLNEED);
  318. close(fd);
  319. }
  320. }
  321. }
  322. #else
  323. static void
  324. pkg_files_optimize_load(struct pkg_array *array)
  325. {
  326. }
  327. #endif
  328. void ensure_allinstfiles_available(void) {
  329. struct pkg_array array;
  330. struct pkginfo *pkg;
  331. struct progress progress;
  332. int i;
  333. if (allpackagesdone) return;
  334. if (saidread < PKG_FILESDB_LOAD_DONE) {
  335. int max = pkg_db_count_pkg();
  336. saidread = PKG_FILESDB_LOAD_INPROGRESS;
  337. progress_init(&progress, _("(Reading database ... "), max);
  338. }
  339. pkg_array_init_from_db(&array);
  340. pkg_files_optimize_load(&array);
  341. for (i = 0; i < array.n_pkgs; i++) {
  342. pkg = array.pkgs[i];
  343. ensure_packagefiles_available(pkg);
  344. if (saidread == PKG_FILESDB_LOAD_INPROGRESS)
  345. progress_step(&progress);
  346. }
  347. pkg_array_destroy(&array);
  348. allpackagesdone = true;
  349. if (saidread == PKG_FILESDB_LOAD_INPROGRESS) {
  350. progress_done(&progress);
  351. printf(P_("%d file or directory currently installed.)\n",
  352. "%d files and directories currently installed.)\n", nfiles),
  353. nfiles);
  354. saidread = PKG_FILESDB_LOAD_DONE;
  355. }
  356. }
  357. void ensure_allinstfiles_available_quiet(void) {
  358. saidread = PKG_FILESDB_LOAD_DONE;
  359. ensure_allinstfiles_available();
  360. }
  361. /*
  362. * If mask is nonzero, will not write any file whose filenamenode
  363. * has any flag bits set in mask.
  364. */
  365. void
  366. write_filelist_except(struct pkginfo *pkg, struct pkgbin *pkgbin,
  367. struct fileinlist *list, enum filenamenode_flags mask)
  368. {
  369. struct atomic_file *file;
  370. const char *listfile;
  371. listfile = pkg_infodb_get_file(pkg, pkgbin, LISTFILE);
  372. file = atomic_file_new(listfile, 0);
  373. atomic_file_open(file);
  374. while (list) {
  375. if (!(mask && (list->namenode->flags & mask))) {
  376. fputs(list->namenode->name, file->fp);
  377. putc('\n', file->fp);
  378. }
  379. list= list->next;
  380. }
  381. atomic_file_sync(file);
  382. atomic_file_close(file);
  383. atomic_file_commit(file);
  384. atomic_file_free(file);
  385. dir_sync_path(pkg_infodb_get_dir());
  386. note_must_reread_files_inpackage(pkg);
  387. }
  388. /*
  389. * Initializes an iterator that appears to go through the file
  390. * list ‘files’ in reverse order, returning the namenode from
  391. * each. What actually happens is that we walk the list here,
  392. * building up a reverse list, and then peel it apart one
  393. * entry at a time.
  394. */
  395. void
  396. reversefilelist_init(struct reversefilelistiter *iter, struct fileinlist *files)
  397. {
  398. struct fileinlist *newent;
  399. iter->todo = NULL;
  400. while (files) {
  401. newent= m_malloc(sizeof(struct fileinlist));
  402. newent->namenode= files->namenode;
  403. newent->next = iter->todo;
  404. iter->todo = newent;
  405. files= files->next;
  406. }
  407. }
  408. struct filenamenode *
  409. reversefilelist_next(struct reversefilelistiter *iter)
  410. {
  411. struct filenamenode *ret;
  412. struct fileinlist *todo;
  413. todo = iter->todo;
  414. if (!todo)
  415. return NULL;
  416. ret= todo->namenode;
  417. iter->todo = todo->next;
  418. free(todo);
  419. return ret;
  420. }
  421. /*
  422. * Clients must call this function to clean up the reversefilelistiter
  423. * if they wish to break out of the iteration before it is all done.
  424. * Calling this function is not necessary if reversefilelist_next has
  425. * been called until it returned 0.
  426. */
  427. void
  428. reversefilelist_abort(struct reversefilelistiter *iter)
  429. {
  430. while (reversefilelist_next(iter));
  431. }
  432. struct fileiterator {
  433. struct filenamenode *namenode;
  434. int nbinn;
  435. };
  436. /* This must always be a prime for optimal performance.
  437. * This is the closest one to 2^18 (262144). */
  438. #define BINS 262139
  439. static struct filenamenode *bins[BINS];
  440. struct fileiterator *
  441. files_db_iter_new(void)
  442. {
  443. struct fileiterator *iter;
  444. iter = m_malloc(sizeof(struct fileiterator));
  445. iter->namenode = NULL;
  446. iter->nbinn = 0;
  447. return iter;
  448. }
  449. struct filenamenode *
  450. files_db_iter_next(struct fileiterator *iter)
  451. {
  452. struct filenamenode *r= NULL;
  453. while (!iter->namenode) {
  454. if (iter->nbinn >= BINS)
  455. return NULL;
  456. iter->namenode = bins[iter->nbinn++];
  457. }
  458. r = iter->namenode;
  459. iter->namenode = r->next;
  460. return r;
  461. }
  462. void
  463. files_db_iter_free(struct fileiterator *iter)
  464. {
  465. free(iter);
  466. }
  467. void filesdbinit(void) {
  468. struct filenamenode *fnn;
  469. int i;
  470. for (i=0; i<BINS; i++)
  471. for (fnn= bins[i]; fnn; fnn= fnn->next) {
  472. fnn->flags= 0;
  473. fnn->oldhash = NULL;
  474. fnn->newhash = EMPTYHASHFLAG;
  475. fnn->filestat = NULL;
  476. }
  477. }
  478. struct filenamenode *findnamenode(const char *name, enum fnnflags flags) {
  479. struct filenamenode **pointerp, *newnode;
  480. const char *orig_name = name;
  481. /* We skip initial slashes and ‘./’ pairs, and add our own single
  482. * leading slash. */
  483. name = path_skip_slash_dotslash(name);
  484. pointerp = bins + (str_fnv_hash(name) % (BINS));
  485. while (*pointerp) {
  486. /* XXX: Why is the assert needed? It's checking already added entries. */
  487. assert((*pointerp)->name[0] == '/');
  488. if (strcmp((*pointerp)->name + 1, name) == 0)
  489. break;
  490. pointerp= &(*pointerp)->next;
  491. }
  492. if (*pointerp) return *pointerp;
  493. if (flags & fnn_nonew)
  494. return NULL;
  495. newnode= nfmalloc(sizeof(struct filenamenode));
  496. newnode->packages = NULL;
  497. if((flags & fnn_nocopy) && name > orig_name && name[-1] == '/')
  498. newnode->name = name - 1;
  499. else {
  500. char *newname= nfmalloc(strlen(name)+2);
  501. newname[0]= '/'; strcpy(newname+1,name);
  502. newnode->name= newname;
  503. }
  504. newnode->flags= 0;
  505. newnode->next = NULL;
  506. newnode->divert = NULL;
  507. newnode->statoverride = NULL;
  508. newnode->oldhash = NULL;
  509. newnode->newhash = EMPTYHASHFLAG;
  510. newnode->filestat = NULL;
  511. newnode->trig_interested = NULL;
  512. *pointerp= newnode;
  513. nfiles++;
  514. return newnode;
  515. }