filesdb.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * 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
  19. * License along with dpkg; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <dpkg-i18n.h>
  25. #include <assert.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <string.h>
  30. #include <errno.h>
  31. #include <sys/stat.h>
  32. #include <pwd.h>
  33. #include <grp.h>
  34. #include <sys/types.h>
  35. #include <dpkg.h>
  36. #include <dpkg-db.h>
  37. #include <dpkg-priv.h>
  38. #include "filesdb.h"
  39. #include "main.h"
  40. /*** Generic data structures and routines ***/
  41. static int allpackagesdone= 0;
  42. static int nfiles= 0;
  43. static struct diversion *diversions = NULL;
  44. static FILE *diversionsfile = NULL;
  45. static FILE *statoverridefile = NULL;
  46. void
  47. ensure_package_clientdata(struct pkginfo *pkg)
  48. {
  49. if (pkg->clientdata)
  50. return;
  51. pkg->clientdata = nfmalloc(sizeof(struct perpackagestate));
  52. pkg->clientdata->istobe = itb_normal;
  53. pkg->clientdata->fileslistvalid = 0;
  54. pkg->clientdata->files = NULL;
  55. pkg->clientdata->trigprocdeferred = NULL;
  56. }
  57. void note_must_reread_files_inpackage(struct pkginfo *pkg) {
  58. allpackagesdone= 0;
  59. ensure_package_clientdata(pkg);
  60. pkg->clientdata->fileslistvalid= 0;
  61. }
  62. static int saidread=0;
  63. /* load the list of files in this package into memory, or update the
  64. * list if it is there but stale
  65. */
  66. void ensure_packagefiles_available(struct pkginfo *pkg) {
  67. int fd;
  68. const char *filelistfile;
  69. struct fileinlist **lendp, *newent, *current;
  70. struct filepackages *packageslump;
  71. int search, findlast, putat;
  72. struct stat stat_buf;
  73. char *loaded_list, *loaded_list_end, *thisline, *nextline, *ptr;
  74. if (pkg->clientdata && pkg->clientdata->fileslistvalid) return;
  75. ensure_package_clientdata(pkg);
  76. /* Throw away any the stale data, if there was any. */
  77. for (current= pkg->clientdata->files;
  78. current;
  79. current= current->next) {
  80. /* For each file that used to be in the package,
  81. * go through looking for this package's entry in the list
  82. * of packages containing this file, and blank it out.
  83. */
  84. for (packageslump= current->namenode->packages;
  85. packageslump;
  86. packageslump= packageslump->more)
  87. for (search= 0;
  88. search < PERFILEPACKAGESLUMP && packageslump->pkgs[search];
  89. search++)
  90. if (packageslump->pkgs[search] == pkg) {
  91. /* Hah! Found it. */
  92. for (findlast= search+1;
  93. findlast < PERFILEPACKAGESLUMP && packageslump->pkgs[findlast];
  94. findlast++);
  95. findlast--;
  96. /* findlast is now the last occupied entry, which may be the same as
  97. * search. We blank out the entry for this package. We also
  98. * have to copy the last entry into the empty slot, because
  99. * the list is null-pointer-terminated.
  100. */
  101. packageslump->pkgs[search]= packageslump->pkgs[findlast];
  102. packageslump->pkgs[findlast] = NULL;
  103. /* This may result in an empty link in the list. This is OK. */
  104. goto xit_search_to_delete_from_perfilenodelist;
  105. }
  106. xit_search_to_delete_from_perfilenodelist:
  107. ;
  108. /* The actual filelist links were allocated using nfmalloc, so
  109. * we shouldn't free them.
  110. */
  111. }
  112. pkg->clientdata->files = NULL;
  113. /* Packages which aren't installed don't have a files list. */
  114. if (pkg->status == stat_notinstalled) {
  115. pkg->clientdata->fileslistvalid= 1; return;
  116. }
  117. filelistfile= pkgadminfile(pkg,LISTFILE);
  118. onerr_abort++;
  119. fd= open(filelistfile,O_RDONLY);
  120. if (fd==-1) {
  121. if (errno != ENOENT)
  122. ohshite(_("unable to open files list file for package `%.250s'"),pkg->name);
  123. onerr_abort--;
  124. if (pkg->status != stat_configfiles) {
  125. if (saidread == 1) putc('\n',stderr);
  126. warning(_("files list file for package `%.250s' missing, assuming "
  127. "package has no files currently installed."), pkg->name);
  128. }
  129. pkg->clientdata->files = NULL;
  130. pkg->clientdata->fileslistvalid= 1;
  131. return;
  132. }
  133. push_cleanup(cu_closefd, ehflag_bombout, NULL, 0, 1, &fd);
  134. if(fstat(fd, &stat_buf))
  135. ohshite(_("unable to stat files list file for package '%.250s'"),
  136. pkg->name);
  137. if (stat_buf.st_size) {
  138. loaded_list = nfmalloc(stat_buf.st_size);
  139. loaded_list_end = loaded_list + stat_buf.st_size;
  140. fd_buf_copy(fd, loaded_list, stat_buf.st_size, _("files list for package `%.250s'"), pkg->name);
  141. lendp= &pkg->clientdata->files;
  142. thisline = loaded_list;
  143. while (thisline < loaded_list_end) {
  144. if (!(ptr = memchr(thisline, '\n', loaded_list_end - thisline)))
  145. ohshit(_("files list file for package '%.250s' is missing final newline"),
  146. pkg->name);
  147. /* where to start next time around */
  148. nextline = ptr + 1;
  149. /* strip trailing "/" */
  150. if (ptr > thisline && ptr[-1] == '/') ptr--;
  151. /* add the file to the list */
  152. if (ptr == thisline)
  153. ohshit(_("files list file for package `%.250s' contains empty filename"),pkg->name);
  154. *ptr = 0;
  155. newent= nfmalloc(sizeof(struct fileinlist));
  156. newent->namenode= findnamenode(thisline, fnn_nocopy);
  157. newent->next = NULL;
  158. *lendp= newent;
  159. lendp= &newent->next;
  160. thisline = nextline;
  161. }
  162. }
  163. pop_cleanup(ehflag_normaltidy); /* fd= open() */
  164. if (close(fd))
  165. ohshite(_("error closing files list file for package `%.250s'"),pkg->name);
  166. onerr_abort--;
  167. for (newent= pkg->clientdata->files; newent; newent= newent->next) {
  168. packageslump= newent->namenode->packages;
  169. putat= 0;
  170. if (packageslump) {
  171. for (; putat < PERFILEPACKAGESLUMP && packageslump->pkgs[putat];
  172. putat++);
  173. if (putat >= PERFILEPACKAGESLUMP)
  174. packageslump = NULL;
  175. }
  176. if (!packageslump) {
  177. packageslump= nfmalloc(sizeof(struct filepackages));
  178. packageslump->more= newent->namenode->packages;
  179. newent->namenode->packages= packageslump;
  180. putat= 0;
  181. }
  182. packageslump->pkgs[putat]= pkg;
  183. if (++putat < PERFILEPACKAGESLUMP)
  184. packageslump->pkgs[putat] = NULL;
  185. }
  186. pkg->clientdata->fileslistvalid= 1;
  187. }
  188. void ensure_allinstfiles_available(void) {
  189. struct pkgiterator *it;
  190. struct pkginfo *pkg;
  191. if (allpackagesdone) return;
  192. if (saidread<2) {
  193. saidread=1;
  194. printf(_("(Reading database ... "));
  195. }
  196. it= iterpkgstart();
  197. while ((pkg = iterpkgnext(it)) != NULL)
  198. ensure_packagefiles_available(pkg);
  199. iterpkgend(it);
  200. allpackagesdone= 1;
  201. if (saidread==1) {
  202. printf(_("%d files and directories currently installed.)\n"),nfiles);
  203. saidread=2;
  204. }
  205. }
  206. void ensure_allinstfiles_available_quiet(void) {
  207. saidread=2;
  208. ensure_allinstfiles_available();
  209. }
  210. void write_filelist_except(struct pkginfo *pkg, struct fileinlist *list, int leaveout) {
  211. /* If leaveout is nonzero, will not write any file whose filenamenode
  212. * has the fnnf_elide_other_lists flag set.
  213. */
  214. static struct varbuf vb, newvb;
  215. FILE *file;
  216. varbufreset(&vb);
  217. varbufaddstr(&vb,admindir);
  218. varbufaddstr(&vb,"/" INFODIR);
  219. varbufaddstr(&vb,pkg->name);
  220. varbufaddstr(&vb,"." LISTFILE);
  221. varbufaddc(&vb,0);
  222. varbufreset(&newvb);
  223. varbufaddstr(&newvb,vb.buf);
  224. varbufaddstr(&newvb,NEWDBEXT);
  225. varbufaddc(&newvb,0);
  226. file= fopen(newvb.buf,"w+");
  227. if (!file)
  228. ohshite(_("unable to create updated files list file for package %s"),pkg->name);
  229. push_cleanup(cu_closefile, ehflag_bombout, NULL, 0, 1, (void *)file);
  230. while (list) {
  231. if (!(leaveout && (list->namenode->flags & fnnf_elide_other_lists))) {
  232. fputs(list->namenode->name,file);
  233. putc('\n',file);
  234. }
  235. list= list->next;
  236. }
  237. if (ferror(file))
  238. ohshite(_("failed to write to updated files list file for package %s"),pkg->name);
  239. if (fflush(file))
  240. ohshite(_("failed to flush updated files list file for package %s"),pkg->name);
  241. if (fsync(fileno(file)))
  242. ohshite(_("failed to sync updated files list file for package %s"),pkg->name);
  243. pop_cleanup(ehflag_normaltidy); /* file= fopen() */
  244. if (fclose(file))
  245. ohshite(_("failed to close updated files list file for package %s"),pkg->name);
  246. if (rename(newvb.buf,vb.buf))
  247. ohshite(_("failed to install updated files list file for package %s"),pkg->name);
  248. note_must_reread_files_inpackage(pkg);
  249. }
  250. void reversefilelist_init(struct reversefilelistiter *iterptr,
  251. struct fileinlist *files) {
  252. /* Initialises an iterator that appears to go through the file
  253. * list `files' in reverse order, returning the namenode from
  254. * each. What actually happens is that we walk the list here,
  255. * building up a reverse list, and then peel it apart one
  256. * entry at a time.
  257. */
  258. struct fileinlist *newent;
  259. iterptr->todo = NULL;
  260. while (files) {
  261. newent= m_malloc(sizeof(struct fileinlist));
  262. newent->namenode= files->namenode;
  263. newent->next= iterptr->todo;
  264. iterptr->todo= newent;
  265. files= files->next;
  266. }
  267. }
  268. struct filenamenode *reversefilelist_next(struct reversefilelistiter *iterptr) {
  269. struct filenamenode *ret;
  270. struct fileinlist *todo;
  271. todo= iterptr->todo;
  272. if (!todo)
  273. return NULL;
  274. ret= todo->namenode;
  275. iterptr->todo= todo->next;
  276. free(todo);
  277. return ret;
  278. }
  279. void reversefilelist_abort(struct reversefilelistiter *iterptr) {
  280. /* Clients must call this function to clean up the reversefilelistiter
  281. * if they wish to break out of the iteration before it is all done.
  282. * Calling this function is not necessary if reversefilelist_next has
  283. * been called until it returned 0.
  284. */
  285. while (reversefilelist_next(iterptr));
  286. }
  287. void ensure_statoverrides(void) {
  288. static struct varbuf vb;
  289. struct stat stab1, stab2;
  290. FILE *file;
  291. char *loaded_list, *loaded_list_end, *thisline, *nextline, *ptr;
  292. struct filestatoverride *fso;
  293. struct filenamenode *fnn;
  294. varbufreset(&vb);
  295. varbufaddstr(&vb,admindir);
  296. varbufaddstr(&vb,"/" STATOVERRIDEFILE);
  297. varbufaddc(&vb,0);
  298. onerr_abort++;
  299. file= fopen(vb.buf,"r");
  300. if (!file) {
  301. if (errno != ENOENT) ohshite(_("failed to open statoverride file"));
  302. if (!statoverridefile) { onerr_abort--; return; }
  303. } else {
  304. if (fstat(fileno(file),&stab2))
  305. ohshite(_("failed to fstat statoverride file"));
  306. if (statoverridefile) {
  307. if (fstat(fileno(statoverridefile),&stab1))
  308. ohshite(_("failed to fstat previous statoverride file"));
  309. if (stab1.st_dev == stab2.st_dev && stab1.st_ino == stab2.st_ino) {
  310. fclose(file); onerr_abort--; return;
  311. }
  312. }
  313. }
  314. if (statoverridefile) fclose(statoverridefile);
  315. statoverridefile= file;
  316. setcloexec(fileno(statoverridefile), vb.buf);
  317. /* If the statoverride list is empty we don't need to bother reading it. */
  318. if (!stab2.st_size) {
  319. onerr_abort--;
  320. return;
  321. }
  322. loaded_list = nfmalloc(stab2.st_size);
  323. loaded_list_end = loaded_list + stab2.st_size;
  324. fd_buf_copy(fileno(file), loaded_list, stab2.st_size, _("statoverride file `%.250s'"), vb.buf);
  325. thisline = loaded_list;
  326. while (thisline<loaded_list_end) {
  327. char* endptr;
  328. fso= nfmalloc(sizeof(struct filestatoverride));
  329. if (!(ptr = memchr(thisline, '\n', loaded_list_end - thisline)))
  330. ohshit(_("statoverride file is missing final newline"));
  331. /* where to start next time around */
  332. nextline = ptr + 1;
  333. if (ptr == thisline)
  334. ohshit(_("statoverride file contains empty line"));
  335. *ptr = 0;
  336. /* Extract the uid */
  337. if (!(ptr=memchr(thisline, ' ', nextline-thisline)))
  338. ohshit(_("syntax error in statoverride file"));
  339. *ptr=0;
  340. if (thisline[0]=='#') {
  341. fso->uid=strtol(thisline + 1, &endptr, 10);
  342. if (*endptr!=0)
  343. ohshit(_("syntax error: invalid uid in statoverride file"));
  344. } else {
  345. struct passwd* pw = getpwnam(thisline);
  346. if (pw==NULL)
  347. ohshit(_("syntax error: unknown user '%s' in statoverride file"),
  348. thisline);
  349. fso->uid=pw->pw_uid;
  350. }
  351. /* Move to the next bit */
  352. thisline=ptr+1;
  353. if (thisline>=loaded_list_end)
  354. ohshit(_("unexpected end of line in statoverride file"));
  355. /* Extract the gid */
  356. if (!(ptr=memchr(thisline, ' ', nextline-thisline)))
  357. ohshit(_("syntax error in statoverride file"));
  358. *ptr=0;
  359. if (thisline[0]=='#') {
  360. fso->gid=strtol(thisline + 1, &endptr, 10);
  361. if (*endptr!=0)
  362. ohshit(_("syntax error: invalid gid in statoverride file"));
  363. } else {
  364. struct group* gr = getgrnam(thisline);
  365. if (gr==NULL)
  366. ohshit(_("syntax error: unknown group '%s' in statoverride file"),
  367. thisline);
  368. fso->gid=gr->gr_gid;
  369. }
  370. /* Move to the next bit */
  371. thisline=ptr+1;
  372. if (thisline>=loaded_list_end)
  373. ohshit(_("unexpected end of line in statoverride file"));
  374. /* Extract the mode */
  375. if (!(ptr=memchr(thisline, ' ', nextline-thisline)))
  376. ohshit(_("syntax error in statoverride file"));
  377. *ptr=0;
  378. fso->mode=strtol(thisline, &endptr, 8);
  379. if (*endptr!=0)
  380. ohshit(_("syntax error: invalid mode in statoverride file"));
  381. /* Move to the next bit */
  382. thisline=ptr+1;
  383. if (thisline>=loaded_list_end)
  384. ohshit(_("unexecpted end of line in statoverride file"));
  385. fnn= findnamenode(thisline, 0);
  386. if (fnn->statoverride)
  387. ohshit(_("multiple statusoverides present for file '%.250s'"), thisline);
  388. fnn->statoverride=fso;
  389. /* Moving on.. */
  390. thisline=nextline;
  391. }
  392. onerr_abort--;
  393. }
  394. void ensure_diversions(void) {
  395. static struct varbuf vb;
  396. struct stat stab1, stab2;
  397. char linebuf[MAXDIVERTFILENAME];
  398. FILE *file;
  399. struct diversion *ov, *oicontest, *oialtname;
  400. int l;
  401. varbufreset(&vb);
  402. varbufaddstr(&vb,admindir);
  403. varbufaddstr(&vb,"/" DIVERSIONSFILE);
  404. varbufaddc(&vb,0);
  405. onerr_abort++;
  406. file= fopen(vb.buf,"r");
  407. if (!file) {
  408. if (errno != ENOENT) ohshite(_("failed to open diversions file"));
  409. if (!diversionsfile) { onerr_abort--; return; }
  410. } else if (diversionsfile) {
  411. if (fstat(fileno(diversionsfile),&stab1))
  412. ohshite(_("failed to fstat previous diversions file"));
  413. if (fstat(fileno(file),&stab2))
  414. ohshite(_("failed to fstat diversions file"));
  415. if (stab1.st_dev == stab2.st_dev && stab1.st_ino == stab2.st_ino) {
  416. fclose(file); onerr_abort--; return;
  417. }
  418. }
  419. if (diversionsfile) fclose(diversionsfile);
  420. diversionsfile= file;
  421. setcloexec(fileno(diversionsfile), vb.buf);
  422. for (ov= diversions; ov; ov= ov->next) {
  423. ov->useinstead->divert->camefrom->divert = NULL;
  424. ov->useinstead->divert = NULL;
  425. }
  426. diversions = NULL;
  427. if (!file) { onerr_abort--; return; }
  428. while ((l = fgets_checked(linebuf, sizeof(linebuf), file, vb.buf)) >= 0) {
  429. oicontest= nfmalloc(sizeof(struct diversion));
  430. oialtname= nfmalloc(sizeof(struct diversion));
  431. oialtname->camefrom= findnamenode(linebuf, 0);
  432. oialtname->useinstead = NULL;
  433. fgets_must(linebuf, sizeof(linebuf), file, vb.buf);
  434. oicontest->useinstead= findnamenode(linebuf, 0);
  435. oicontest->camefrom = NULL;
  436. fgets_must(linebuf, sizeof(linebuf), file, vb.buf);
  437. oicontest->pkg= oialtname->pkg=
  438. strcmp(linebuf, ":") ? findpackage(linebuf) : NULL;
  439. if (oialtname->camefrom->divert || oicontest->useinstead->divert)
  440. ohshit(_("conflicting diversions involving `%.250s' or `%.250s'"),
  441. oialtname->camefrom->name, oicontest->useinstead->name);
  442. oialtname->camefrom->divert= oicontest;
  443. oicontest->useinstead->divert= oialtname;
  444. oicontest->next= diversions;
  445. diversions= oicontest;
  446. }
  447. if (ferror(file)) ohshite(_("read error in diversions [i]"));
  448. onerr_abort--;
  449. }
  450. struct fileiterator {
  451. struct filenamenode *namenode;
  452. int nbinn;
  453. };
  454. #define BINS (1 << 17)
  455. /* This must always be a power of two. If you change it
  456. * consider changing the per-character hashing factor (currently
  457. * 1785 = 137*13) too.
  458. */
  459. static struct filenamenode *bins[BINS];
  460. struct fileiterator *iterfilestart(void) {
  461. struct fileiterator *i;
  462. i= m_malloc(sizeof(struct fileiterator));
  463. i->namenode = NULL;
  464. i->nbinn= 0;
  465. return i;
  466. }
  467. struct filenamenode *iterfilenext(struct fileiterator *i) {
  468. struct filenamenode *r= NULL;
  469. while (!i->namenode) {
  470. if (i->nbinn >= BINS)
  471. return NULL;
  472. i->namenode= bins[i->nbinn++];
  473. }
  474. r= i->namenode;
  475. i->namenode= r->next;
  476. return r;
  477. }
  478. void iterfileend(struct fileiterator *i) {
  479. free(i);
  480. }
  481. void filesdbinit(void) {
  482. struct filenamenode *fnn;
  483. int i;
  484. for (i=0; i<BINS; i++)
  485. for (fnn= bins[i]; fnn; fnn= fnn->next) {
  486. fnn->flags= 0;
  487. fnn->oldhash = NULL;
  488. fnn->filestat = NULL;
  489. }
  490. }
  491. static int hash(const char *name) {
  492. int v= 0;
  493. while (*name) { v *= 1787; v += *name; name++; }
  494. return v;
  495. }
  496. struct filenamenode *findnamenode(const char *name, enum fnnflags flags) {
  497. struct filenamenode **pointerp, *newnode;
  498. const char *orig_name = name;
  499. /* We skip initial slashes and ./ pairs, and add our own single leading slash. */
  500. name= skip_slash_dotslash(name);
  501. pointerp= bins + (hash(name) & (BINS-1));
  502. while (*pointerp) {
  503. /* Why is this assert nescessary? It is checking already added entries. */
  504. assert((*pointerp)->name[0] == '/');
  505. if (!strcmp((*pointerp)->name+1,name)) break;
  506. pointerp= &(*pointerp)->next;
  507. }
  508. if (*pointerp) return *pointerp;
  509. if (flags & fnn_nonew)
  510. return NULL;
  511. newnode= nfmalloc(sizeof(struct filenamenode));
  512. newnode->packages = NULL;
  513. if((flags & fnn_nocopy) && name > orig_name && name[-1] == '/')
  514. newnode->name = name - 1;
  515. else {
  516. char *newname= nfmalloc(strlen(name)+2);
  517. newname[0]= '/'; strcpy(newname+1,name);
  518. newnode->name= newname;
  519. }
  520. newnode->flags= 0;
  521. newnode->next = NULL;
  522. newnode->divert = NULL;
  523. newnode->statoverride = NULL;
  524. newnode->filestat = NULL;
  525. newnode->trig_interested = NULL;
  526. *pointerp= newnode;
  527. nfiles++;
  528. return newnode;
  529. }
  530. /* vi: ts=8 sw=2
  531. */