filesdb.c 13 KB

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