filesdb.c 16 KB

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