filesdb.c 18 KB

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