filesdb.c 18 KB

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