filesdb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <assert.h>
  27. #include <errno.h>
  28. #include <string.h>
  29. #include <pwd.h>
  30. #include <grp.h>
  31. #include <fcntl.h>
  32. #include <unistd.h>
  33. #include <stdlib.h>
  34. #include <dpkg/i18n.h>
  35. #include <dpkg/dpkg.h>
  36. #include <dpkg/dpkg-db.h>
  37. #include <dpkg/path.h>
  38. #include <dpkg/buffer.h>
  39. #include <dpkg/progress.h>
  40. #include "filesdb.h"
  41. #include "main.h"
  42. /*** Generic data structures and routines ***/
  43. static int allpackagesdone= 0;
  44. static int nfiles= 0;
  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. /**
  63. * Erase the files saved in pkg.
  64. */
  65. static void
  66. pkg_files_blank(struct pkginfo *pkg)
  67. {
  68. struct fileinlist *current;
  69. struct filepackages *packageslump;
  70. int search, findlast;
  71. /* Anything to empty? */
  72. if (!pkg->clientdata)
  73. return;
  74. for (current= pkg->clientdata->files;
  75. current;
  76. current= current->next) {
  77. /* For each file that used to be in the package,
  78. * go through looking for this package's entry in the list
  79. * of packages containing this file, and blank it out.
  80. */
  81. for (packageslump= current->namenode->packages;
  82. packageslump;
  83. packageslump= packageslump->more)
  84. for (search= 0;
  85. search < PERFILEPACKAGESLUMP && packageslump->pkgs[search];
  86. search++)
  87. if (packageslump->pkgs[search] == pkg) {
  88. /* Hah! Found it. */
  89. for (findlast= search+1;
  90. findlast < PERFILEPACKAGESLUMP && packageslump->pkgs[findlast];
  91. findlast++);
  92. findlast--;
  93. /* findlast is now the last occupied entry, which may be the same as
  94. * search. We blank out the entry for this package. We also
  95. * have to copy the last entry into the empty slot, because
  96. * the list is null-pointer-terminated.
  97. */
  98. packageslump->pkgs[search]= packageslump->pkgs[findlast];
  99. packageslump->pkgs[findlast] = NULL;
  100. /* This may result in an empty link in the list. This is OK. */
  101. goto xit_search_to_delete_from_perfilenodelist;
  102. }
  103. xit_search_to_delete_from_perfilenodelist:
  104. ;
  105. /* The actual filelist links were allocated using nfmalloc, so
  106. * we shouldn't free them.
  107. */
  108. }
  109. pkg->clientdata->files = NULL;
  110. }
  111. struct fileinlist **
  112. pkg_files_add_file(struct pkginfo *pkg, const char *filename,
  113. enum fnnflags flags, struct fileinlist **file_tail)
  114. {
  115. struct fileinlist *newent;
  116. struct filepackages *packageslump;
  117. int putat = 0;
  118. ensure_package_clientdata(pkg);
  119. if (file_tail == NULL)
  120. file_tail = &pkg->clientdata->files;
  121. /* Make sure we're at the end. */
  122. while ((*file_tail) != NULL) {
  123. file_tail = &((*file_tail)->next);
  124. }
  125. /* Create a new node. */
  126. newent = nfmalloc(sizeof(struct fileinlist));
  127. newent->namenode = findnamenode(filename, flags);
  128. newent->next = NULL;
  129. *file_tail = newent;
  130. file_tail = &newent->next;
  131. /* Add pkg to newent's package list. */
  132. packageslump = newent->namenode->packages;
  133. putat = 0;
  134. if (packageslump) {
  135. while (putat < PERFILEPACKAGESLUMP && packageslump->pkgs[putat])
  136. putat++;
  137. if (putat >= PERFILEPACKAGESLUMP)
  138. packageslump = NULL;
  139. }
  140. if (!packageslump) {
  141. packageslump = nfmalloc(sizeof(struct filepackages));
  142. packageslump->more = newent->namenode->packages;
  143. newent->namenode->packages = packageslump;
  144. putat = 0;
  145. }
  146. packageslump->pkgs[putat]= pkg;
  147. if (++putat < PERFILEPACKAGESLUMP)
  148. packageslump->pkgs[putat] = NULL;
  149. /* Return the position for the next guy. */
  150. return file_tail;
  151. }
  152. /**
  153. * Load the list of files in this package into memory, or update the
  154. * list if it is there but stale.
  155. */
  156. void
  157. ensure_packagefiles_available(struct pkginfo *pkg)
  158. {
  159. int fd;
  160. const char *filelistfile;
  161. struct fileinlist **lendp;
  162. struct stat stat_buf;
  163. char *loaded_list, *loaded_list_end, *thisline, *nextline, *ptr;
  164. if (pkg->clientdata && pkg->clientdata->fileslistvalid)
  165. return;
  166. ensure_package_clientdata(pkg);
  167. /* Throw away any stale data, if there was any. */
  168. pkg_files_blank(pkg);
  169. /* Packages which aren't installed don't have a files list. */
  170. if (pkg->status == stat_notinstalled) {
  171. pkg->clientdata->fileslistvalid= 1; return;
  172. }
  173. filelistfile= pkgadminfile(pkg,LISTFILE);
  174. onerr_abort++;
  175. fd= open(filelistfile,O_RDONLY);
  176. if (fd==-1) {
  177. if (errno != ENOENT)
  178. ohshite(_("unable to open files list file for package `%.250s'"),pkg->name);
  179. onerr_abort--;
  180. if (pkg->status != stat_configfiles) {
  181. if (saidread == 1) putc('\n',stderr);
  182. warning(_("files list file for package `%.250s' missing, assuming "
  183. "package has no files currently installed."), pkg->name);
  184. }
  185. pkg->clientdata->files = NULL;
  186. pkg->clientdata->fileslistvalid= 1;
  187. return;
  188. }
  189. push_cleanup(cu_closefd, ehflag_bombout, NULL, 0, 1, &fd);
  190. if(fstat(fd, &stat_buf))
  191. ohshite(_("unable to stat files list file for package '%.250s'"),
  192. pkg->name);
  193. if (stat_buf.st_size) {
  194. loaded_list = nfmalloc(stat_buf.st_size);
  195. loaded_list_end = loaded_list + stat_buf.st_size;
  196. fd_buf_copy(fd, loaded_list, stat_buf.st_size, _("files list for package `%.250s'"), pkg->name);
  197. lendp= &pkg->clientdata->files;
  198. thisline = loaded_list;
  199. while (thisline < loaded_list_end) {
  200. if (!(ptr = memchr(thisline, '\n', loaded_list_end - thisline)))
  201. ohshit(_("files list file for package '%.250s' is missing final newline"),
  202. pkg->name);
  203. /* where to start next time around */
  204. nextline = ptr + 1;
  205. /* strip trailing "/" */
  206. if (ptr > thisline && ptr[-1] == '/') ptr--;
  207. /* add the file to the list */
  208. if (ptr == thisline)
  209. ohshit(_("files list file for package `%.250s' contains empty filename"),pkg->name);
  210. *ptr = '\0';
  211. lendp = pkg_files_add_file(pkg, thisline, fnn_nocopy, lendp);
  212. thisline = nextline;
  213. }
  214. }
  215. pop_cleanup(ehflag_normaltidy); /* fd= open() */
  216. if (close(fd))
  217. ohshite(_("error closing files list file for package `%.250s'"),pkg->name);
  218. onerr_abort--;
  219. pkg->clientdata->fileslistvalid= 1;
  220. }
  221. void ensure_allinstfiles_available(void) {
  222. struct pkgiterator *it;
  223. struct pkginfo *pkg;
  224. struct progress progress;
  225. if (allpackagesdone) return;
  226. if (saidread<2) {
  227. int max = countpackages();
  228. saidread=1;
  229. progress_init(&progress, _("(Reading database ... "), max);
  230. }
  231. it= iterpkgstart();
  232. while ((pkg = iterpkgnext(it)) != NULL) {
  233. ensure_packagefiles_available(pkg);
  234. if (saidread == 1)
  235. progress_step(&progress);
  236. }
  237. iterpkgend(it);
  238. allpackagesdone= 1;
  239. if (saidread==1) {
  240. progress_done(&progress);
  241. printf(_("%d files and directories currently installed.)\n"),nfiles);
  242. saidread=2;
  243. }
  244. }
  245. void ensure_allinstfiles_available_quiet(void) {
  246. saidread=2;
  247. ensure_allinstfiles_available();
  248. }
  249. void write_filelist_except(struct pkginfo *pkg, struct fileinlist *list, int leaveout) {
  250. /* If leaveout is nonzero, will not write any file whose filenamenode
  251. * has the fnnf_elide_other_lists flag set.
  252. */
  253. static struct varbuf vb, newvb;
  254. FILE *file;
  255. varbufreset(&vb);
  256. varbufaddstr(&vb,admindir);
  257. varbufaddstr(&vb,"/" INFODIR);
  258. varbufaddstr(&vb,pkg->name);
  259. varbufaddstr(&vb,"." LISTFILE);
  260. varbufaddc(&vb,0);
  261. varbufreset(&newvb);
  262. varbufaddstr(&newvb,vb.buf);
  263. varbufaddstr(&newvb,NEWDBEXT);
  264. varbufaddc(&newvb,0);
  265. file= fopen(newvb.buf,"w+");
  266. if (!file)
  267. ohshite(_("unable to create updated files list file for package %s"),pkg->name);
  268. push_cleanup(cu_closefile, ehflag_bombout, NULL, 0, 1, (void *)file);
  269. while (list) {
  270. if (!(leaveout && (list->namenode->flags & fnnf_elide_other_lists))) {
  271. fputs(list->namenode->name,file);
  272. putc('\n',file);
  273. }
  274. list= list->next;
  275. }
  276. if (ferror(file))
  277. ohshite(_("failed to write to updated files list file for package %s"),pkg->name);
  278. if (fflush(file))
  279. ohshite(_("failed to flush updated files list file for package %s"),pkg->name);
  280. if (fsync(fileno(file)))
  281. ohshite(_("failed to sync updated files list file for package %s"),pkg->name);
  282. pop_cleanup(ehflag_normaltidy); /* file= fopen() */
  283. if (fclose(file))
  284. ohshite(_("failed to close updated files list file for package %s"),pkg->name);
  285. if (rename(newvb.buf,vb.buf))
  286. ohshite(_("failed to install updated files list file for package %s"),pkg->name);
  287. note_must_reread_files_inpackage(pkg);
  288. }
  289. void reversefilelist_init(struct reversefilelistiter *iterptr,
  290. struct fileinlist *files) {
  291. /* Initialises an iterator that appears to go through the file
  292. * list `files' in reverse order, returning the namenode from
  293. * each. What actually happens is that we walk the list here,
  294. * building up a reverse list, and then peel it apart one
  295. * entry at a time.
  296. */
  297. struct fileinlist *newent;
  298. iterptr->todo = NULL;
  299. while (files) {
  300. newent= m_malloc(sizeof(struct fileinlist));
  301. newent->namenode= files->namenode;
  302. newent->next= iterptr->todo;
  303. iterptr->todo= newent;
  304. files= files->next;
  305. }
  306. }
  307. struct filenamenode *reversefilelist_next(struct reversefilelistiter *iterptr) {
  308. struct filenamenode *ret;
  309. struct fileinlist *todo;
  310. todo= iterptr->todo;
  311. if (!todo)
  312. return NULL;
  313. ret= todo->namenode;
  314. iterptr->todo= todo->next;
  315. free(todo);
  316. return ret;
  317. }
  318. void reversefilelist_abort(struct reversefilelistiter *iterptr) {
  319. /* Clients must call this function to clean up the reversefilelistiter
  320. * if they wish to break out of the iteration before it is all done.
  321. * Calling this function is not necessary if reversefilelist_next has
  322. * been called until it returned 0.
  323. */
  324. while (reversefilelist_next(iterptr));
  325. }
  326. struct fileiterator {
  327. struct filenamenode *namenode;
  328. int nbinn;
  329. };
  330. #define BINS (1 << 17)
  331. /* This must always be a power of two. If you change it
  332. * consider changing the per-character hashing factor (currently
  333. * 1785 = 137*13) too.
  334. */
  335. static struct filenamenode *bins[BINS];
  336. struct fileiterator *iterfilestart(void) {
  337. struct fileiterator *i;
  338. i= m_malloc(sizeof(struct fileiterator));
  339. i->namenode = NULL;
  340. i->nbinn= 0;
  341. return i;
  342. }
  343. struct filenamenode *iterfilenext(struct fileiterator *i) {
  344. struct filenamenode *r= NULL;
  345. while (!i->namenode) {
  346. if (i->nbinn >= BINS)
  347. return NULL;
  348. i->namenode= bins[i->nbinn++];
  349. }
  350. r= i->namenode;
  351. i->namenode= r->next;
  352. return r;
  353. }
  354. void iterfileend(struct fileiterator *i) {
  355. free(i);
  356. }
  357. void filesdbinit(void) {
  358. struct filenamenode *fnn;
  359. int i;
  360. for (i=0; i<BINS; i++)
  361. for (fnn= bins[i]; fnn; fnn= fnn->next) {
  362. fnn->flags= 0;
  363. fnn->oldhash = NULL;
  364. fnn->filestat = NULL;
  365. }
  366. }
  367. static int hash(const char *name) {
  368. int v= 0;
  369. while (*name) { v *= 1787; v += *name; name++; }
  370. return v;
  371. }
  372. struct filenamenode *findnamenode(const char *name, enum fnnflags flags) {
  373. struct filenamenode **pointerp, *newnode;
  374. const char *orig_name = name;
  375. /* We skip initial slashes and ./ pairs, and add our own single leading slash. */
  376. name = path_skip_slash_dotslash(name);
  377. pointerp= bins + (hash(name) & (BINS-1));
  378. while (*pointerp) {
  379. /* Why is this assert nescessary? It is checking already added entries. */
  380. assert((*pointerp)->name[0] == '/');
  381. if (!strcmp((*pointerp)->name+1,name)) break;
  382. pointerp= &(*pointerp)->next;
  383. }
  384. if (*pointerp) return *pointerp;
  385. if (flags & fnn_nonew)
  386. return NULL;
  387. newnode= nfmalloc(sizeof(struct filenamenode));
  388. newnode->packages = NULL;
  389. if((flags & fnn_nocopy) && name > orig_name && name[-1] == '/')
  390. newnode->name = name - 1;
  391. else {
  392. char *newname= nfmalloc(strlen(name)+2);
  393. newname[0]= '/'; strcpy(newname+1,name);
  394. newnode->name= newname;
  395. }
  396. newnode->flags= 0;
  397. newnode->next = NULL;
  398. newnode->divert = NULL;
  399. newnode->statoverride = NULL;
  400. newnode->filestat = NULL;
  401. newnode->trig_interested = NULL;
  402. *pointerp= newnode;
  403. nfiles++;
  404. return newnode;
  405. }
  406. /* vi: ts=8 sw=2
  407. */