filesdb.c 16 KB

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