filesdb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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/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. /* 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. warning(_("files list file for package `%.250s' missing, assuming "
  126. "package has no files currently installed."), pkg->name);
  127. }
  128. pkg->clientdata->files = NULL;
  129. pkg->clientdata->fileslistvalid= 1;
  130. return;
  131. }
  132. push_cleanup(cu_closefd, ehflag_bombout, NULL, 0, 1, &fd);
  133. if(fstat(fd, &stat_buf))
  134. ohshite(_("unable to stat files list file for package '%.250s'"),
  135. 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"),
  145. pkg->name);
  146. /* where to start next time around */
  147. nextline = ptr + 1;
  148. /* strip trailing "/" */
  149. if (ptr > thisline && ptr[-1] == '/') ptr--;
  150. /* add the file to the list */
  151. if (ptr == thisline)
  152. ohshit(_("files list file for package `%.250s' contains empty filename"),pkg->name);
  153. *ptr = '\0';
  154. newent= nfmalloc(sizeof(struct fileinlist));
  155. newent->namenode= findnamenode(thisline, fnn_nocopy);
  156. newent->next = NULL;
  157. *lendp= newent;
  158. lendp= &newent->next;
  159. thisline = nextline;
  160. }
  161. }
  162. pop_cleanup(ehflag_normaltidy); /* fd= open() */
  163. if (close(fd))
  164. ohshite(_("error closing files list file for package `%.250s'"),pkg->name);
  165. onerr_abort--;
  166. for (newent= pkg->clientdata->files; newent; newent= newent->next) {
  167. packageslump= newent->namenode->packages;
  168. putat= 0;
  169. if (packageslump) {
  170. for (; putat < PERFILEPACKAGESLUMP && packageslump->pkgs[putat];
  171. putat++);
  172. if (putat >= PERFILEPACKAGESLUMP)
  173. packageslump = NULL;
  174. }
  175. if (!packageslump) {
  176. packageslump= nfmalloc(sizeof(struct filepackages));
  177. packageslump->more= newent->namenode->packages;
  178. newent->namenode->packages= packageslump;
  179. putat= 0;
  180. }
  181. packageslump->pkgs[putat]= pkg;
  182. if (++putat < PERFILEPACKAGESLUMP)
  183. packageslump->pkgs[putat] = NULL;
  184. }
  185. pkg->clientdata->fileslistvalid= 1;
  186. }
  187. void ensure_allinstfiles_available(void) {
  188. struct pkgiterator *it;
  189. struct pkginfo *pkg;
  190. struct progress progress;
  191. if (allpackagesdone) return;
  192. if (saidread<2) {
  193. int max = countpackages();
  194. saidread=1;
  195. progress_init(&progress, _("(Reading database ... "), max);
  196. }
  197. it= iterpkgstart();
  198. while ((pkg = iterpkgnext(it)) != NULL) {
  199. ensure_packagefiles_available(pkg);
  200. if (saidread == 1)
  201. progress_step(&progress);
  202. }
  203. iterpkgend(it);
  204. allpackagesdone= 1;
  205. if (saidread==1) {
  206. progress_done(&progress);
  207. printf(_("%d files and directories currently installed.)\n"),nfiles);
  208. saidread=2;
  209. }
  210. }
  211. void ensure_allinstfiles_available_quiet(void) {
  212. saidread=2;
  213. ensure_allinstfiles_available();
  214. }
  215. void write_filelist_except(struct pkginfo *pkg, struct fileinlist *list, int leaveout) {
  216. /* If leaveout is nonzero, will not write any file whose filenamenode
  217. * has the fnnf_elide_other_lists flag set.
  218. */
  219. static struct varbuf vb, newvb;
  220. FILE *file;
  221. varbufreset(&vb);
  222. varbufaddstr(&vb,admindir);
  223. varbufaddstr(&vb,"/" INFODIR);
  224. varbufaddstr(&vb,pkg->name);
  225. varbufaddstr(&vb,"." LISTFILE);
  226. varbufaddc(&vb,0);
  227. varbufreset(&newvb);
  228. varbufaddstr(&newvb,vb.buf);
  229. varbufaddstr(&newvb,NEWDBEXT);
  230. varbufaddc(&newvb,0);
  231. file= fopen(newvb.buf,"w+");
  232. if (!file)
  233. ohshite(_("unable to create updated files list file for package %s"),pkg->name);
  234. push_cleanup(cu_closefile, ehflag_bombout, NULL, 0, 1, (void *)file);
  235. while (list) {
  236. if (!(leaveout && (list->namenode->flags & fnnf_elide_other_lists))) {
  237. fputs(list->namenode->name,file);
  238. putc('\n',file);
  239. }
  240. list= list->next;
  241. }
  242. if (ferror(file))
  243. ohshite(_("failed to write to updated files list file for package %s"),pkg->name);
  244. if (fflush(file))
  245. ohshite(_("failed to flush updated files list file for package %s"),pkg->name);
  246. if (fsync(fileno(file)))
  247. ohshite(_("failed to sync updated files list file for package %s"),pkg->name);
  248. pop_cleanup(ehflag_normaltidy); /* file= fopen() */
  249. if (fclose(file))
  250. ohshite(_("failed to close updated files list file for package %s"),pkg->name);
  251. if (rename(newvb.buf,vb.buf))
  252. ohshite(_("failed to install updated files list file for package %s"),pkg->name);
  253. note_must_reread_files_inpackage(pkg);
  254. }
  255. void reversefilelist_init(struct reversefilelistiter *iterptr,
  256. struct fileinlist *files) {
  257. /* Initialises an iterator that appears to go through the file
  258. * list `files' in reverse order, returning the namenode from
  259. * each. What actually happens is that we walk the list here,
  260. * building up a reverse list, and then peel it apart one
  261. * entry at a time.
  262. */
  263. struct fileinlist *newent;
  264. iterptr->todo = NULL;
  265. while (files) {
  266. newent= m_malloc(sizeof(struct fileinlist));
  267. newent->namenode= files->namenode;
  268. newent->next= iterptr->todo;
  269. iterptr->todo= newent;
  270. files= files->next;
  271. }
  272. }
  273. struct filenamenode *reversefilelist_next(struct reversefilelistiter *iterptr) {
  274. struct filenamenode *ret;
  275. struct fileinlist *todo;
  276. todo= iterptr->todo;
  277. if (!todo)
  278. return NULL;
  279. ret= todo->namenode;
  280. iterptr->todo= todo->next;
  281. free(todo);
  282. return ret;
  283. }
  284. void reversefilelist_abort(struct reversefilelistiter *iterptr) {
  285. /* Clients must call this function to clean up the reversefilelistiter
  286. * if they wish to break out of the iteration before it is all done.
  287. * Calling this function is not necessary if reversefilelist_next has
  288. * been called until it returned 0.
  289. */
  290. while (reversefilelist_next(iterptr));
  291. }
  292. struct fileiterator {
  293. struct filenamenode *namenode;
  294. int nbinn;
  295. };
  296. #define BINS (1 << 17)
  297. /* This must always be a power of two. If you change it
  298. * consider changing the per-character hashing factor (currently
  299. * 1785 = 137*13) too.
  300. */
  301. static struct filenamenode *bins[BINS];
  302. struct fileiterator *iterfilestart(void) {
  303. struct fileiterator *i;
  304. i= m_malloc(sizeof(struct fileiterator));
  305. i->namenode = NULL;
  306. i->nbinn= 0;
  307. return i;
  308. }
  309. struct filenamenode *iterfilenext(struct fileiterator *i) {
  310. struct filenamenode *r= NULL;
  311. while (!i->namenode) {
  312. if (i->nbinn >= BINS)
  313. return NULL;
  314. i->namenode= bins[i->nbinn++];
  315. }
  316. r= i->namenode;
  317. i->namenode= r->next;
  318. return r;
  319. }
  320. void iterfileend(struct fileiterator *i) {
  321. free(i);
  322. }
  323. void filesdbinit(void) {
  324. struct filenamenode *fnn;
  325. int i;
  326. for (i=0; i<BINS; i++)
  327. for (fnn= bins[i]; fnn; fnn= fnn->next) {
  328. fnn->flags= 0;
  329. fnn->oldhash = NULL;
  330. fnn->filestat = NULL;
  331. }
  332. }
  333. static int hash(const char *name) {
  334. int v= 0;
  335. while (*name) { v *= 1787; v += *name; name++; }
  336. return v;
  337. }
  338. struct filenamenode *findnamenode(const char *name, enum fnnflags flags) {
  339. struct filenamenode **pointerp, *newnode;
  340. const char *orig_name = name;
  341. /* We skip initial slashes and ./ pairs, and add our own single leading slash. */
  342. name = path_skip_slash_dotslash(name);
  343. pointerp= bins + (hash(name) & (BINS-1));
  344. while (*pointerp) {
  345. /* Why is this assert nescessary? It is checking already added entries. */
  346. assert((*pointerp)->name[0] == '/');
  347. if (!strcmp((*pointerp)->name+1,name)) break;
  348. pointerp= &(*pointerp)->next;
  349. }
  350. if (*pointerp) return *pointerp;
  351. if (flags & fnn_nonew)
  352. return NULL;
  353. newnode= nfmalloc(sizeof(struct filenamenode));
  354. newnode->packages = NULL;
  355. if((flags & fnn_nocopy) && name > orig_name && name[-1] == '/')
  356. newnode->name = name - 1;
  357. else {
  358. char *newname= nfmalloc(strlen(name)+2);
  359. newname[0]= '/'; strcpy(newname+1,name);
  360. newnode->name= newname;
  361. }
  362. newnode->flags= 0;
  363. newnode->next = NULL;
  364. newnode->divert = NULL;
  365. newnode->statoverride = NULL;
  366. newnode->filestat = NULL;
  367. newnode->trig_interested = NULL;
  368. *pointerp= newnode;
  369. nfiles++;
  370. return newnode;
  371. }
  372. /* vi: ts=8 sw=2
  373. */