filesdb.c 18 KB

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