filesdb.c 17 KB

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