filesdb.c 17 KB

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