filesdb.c 18 KB

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