dbmodify.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * dpkg - main program for package management
  3. * dbmodify.c - routines for managing dpkg database updates
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <iwj10@cus.cam.ac.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <signal.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <sys/wait.h>
  28. #include <errno.h>
  29. #include <unistd.h>
  30. #include <dirent.h>
  31. #include <limits.h>
  32. #include <ctype.h>
  33. #include <assert.h>
  34. #include <config.h>
  35. #include <dpkg.h>
  36. #include <dpkg-db.h>
  37. char *statusfile=0, *availablefile=0;
  38. static enum modstatdb_rw cstatus=-1, cflags=0;
  39. static char *importanttmpfile=0;
  40. static FILE *importanttmp;
  41. static int nextupdate;
  42. static int updateslength;
  43. static char *updatefnbuf, *updatefnrest;
  44. static const char *admindir;
  45. static struct varbuf uvb;
  46. static int ulist_select(const struct dirent *de) {
  47. const char *p;
  48. int l;
  49. for (p= de->d_name, l=0; *p; p++, l++)
  50. if (!isdigit(*p)) return 0;
  51. if (l > IMPORTANTMAXLEN)
  52. ohshit(_("updates directory contains file `%.250s' whose name is too long "
  53. "(length=%d, max=%d)"), de->d_name, l, IMPORTANTMAXLEN);
  54. if (updateslength == -1) updateslength= l;
  55. else if (l != updateslength)
  56. ohshit(_("updates directory contains files with different length names "
  57. "(both %d and %d)"), l, updateslength);
  58. return 1;
  59. }
  60. static void cleanupdates(void) {
  61. struct dirent **cdlist;
  62. int cdn, i;
  63. parsedb(statusfile, pdb_weakclassification, 0,0,0);
  64. *updatefnrest= 0;
  65. updateslength= -1;
  66. cdn= scandir(updatefnbuf, &cdlist, &ulist_select, alphasort);
  67. if (cdn == -1) ohshite(_("cannot scan updates directory `%.255s'"),updatefnbuf);
  68. if (cdn) {
  69. for (i=0; i<cdn; i++) {
  70. strcpy(updatefnrest, cdlist[i]->d_name);
  71. parsedb(updatefnbuf, pdb_weakclassification, 0,0,0);
  72. if (cstatus < msdbrw_write) free(cdlist[i]);
  73. }
  74. if (cstatus >= msdbrw_write) {
  75. writedb(statusfile,0,1);
  76. for (i=0; i<cdn; i++) {
  77. strcpy(updatefnrest, cdlist[i]->d_name);
  78. if (unlink(updatefnbuf))
  79. ohshite(_("failed to remove incorporated update file %.255s"),updatefnbuf);
  80. free(cdlist[i]);
  81. }
  82. }
  83. }
  84. free(cdlist);
  85. nextupdate= 0;
  86. }
  87. static void createimptmp(void) {
  88. int i;
  89. onerr_abort++;
  90. importanttmp= fopen(importanttmpfile,"w");
  91. if (!importanttmp) ohshite(_("unable to create %.250s"),importanttmpfile);
  92. for (i=0; i<512; i++) fputs("#padding\n",importanttmp);
  93. if (ferror(importanttmp))
  94. ohshite(_("unable to fill %.250s with padding"),importanttmpfile);
  95. if (fflush(importanttmp))
  96. ohshite(_("unable flush %.250s after padding"),importanttmpfile);
  97. if (fseek(importanttmp,0,SEEK_SET))
  98. ohshite(_("unable seek to start of %.250s after padding"),importanttmpfile);
  99. onerr_abort--;
  100. }
  101. enum modstatdb_rw modstatdb_init(const char *adir, enum modstatdb_rw readwritereq) {
  102. static const struct fni { const char *suffix; char **store; } fnis[]= {
  103. { STATUSFILE, &statusfile },
  104. { AVAILFILE, &availablefile },
  105. { UPDATESDIR IMPORTANTTMP, &importanttmpfile },
  106. { 0 }
  107. }, *fnip;
  108. static int obs_inited = 0;
  109. admindir= adir;
  110. if (!obs_inited) {
  111. obstack_init(&db_obs);
  112. obs_inited = 1;
  113. }
  114. for (fnip=fnis; fnip->suffix; fnip++) {
  115. free(*fnip->store);
  116. *fnip->store= m_malloc(strlen(adir)+strlen(fnip->suffix)+2);
  117. sprintf(*fnip->store, "%s/%s", adir, fnip->suffix);
  118. }
  119. cflags= readwritereq & msdbrw_flagsmask;
  120. readwritereq &= ~msdbrw_flagsmask;
  121. switch (readwritereq) {
  122. case msdbrw_needsuperuser:
  123. case msdbrw_needsuperuserlockonly:
  124. if (getuid() || geteuid())
  125. ohshit(_("requested operation requires superuser privilege"));
  126. /* fall through */
  127. case msdbrw_write: case msdbrw_writeifposs:
  128. if (access(adir,W_OK)) {
  129. if (errno != EACCES)
  130. ohshite(_("unable to access dpkg status area"));
  131. else if (readwritereq == msdbrw_write)
  132. ohshit(_("operation requires read/write access to dpkg status area"));
  133. cstatus= msdbrw_readonly;
  134. } else {
  135. lockdatabase(adir);
  136. cstatus= (readwritereq == msdbrw_needsuperuserlockonly ?
  137. msdbrw_needsuperuserlockonly :
  138. msdbrw_write);
  139. }
  140. break;
  141. case msdbrw_readonly:
  142. cstatus= msdbrw_readonly; break;
  143. default:
  144. internerr("unknown readwritereq");
  145. }
  146. updatefnbuf= m_malloc(strlen(adir)+sizeof(UPDATESDIR)+IMPORTANTMAXLEN+5);
  147. strcpy(updatefnbuf,adir);
  148. strcat(updatefnbuf,"/" UPDATESDIR);
  149. updatefnrest= updatefnbuf+strlen(updatefnbuf);
  150. if (cstatus != msdbrw_needsuperuserlockonly) {
  151. cleanupdates();
  152. if(!(cflags & msdbrw_noavail))
  153. parsedb(availablefile,
  154. pdb_recordavailable|pdb_rejectstatus,
  155. 0,0,0);
  156. }
  157. if (cstatus >= msdbrw_write) {
  158. createimptmp();
  159. uvb.used= 0;
  160. uvb.size= 10240;
  161. uvb.buf= m_malloc(uvb.size);
  162. }
  163. return cstatus;
  164. }
  165. static void checkpoint(void) {
  166. int i;
  167. assert(cstatus >= msdbrw_write);
  168. writedb(statusfile,0,1);
  169. for (i=0; i<nextupdate; i++) {
  170. sprintf(updatefnrest, IMPORTANTFMT, i);
  171. assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */
  172. if (unlink(updatefnbuf))
  173. ohshite(_("failed to remove my own update file %.255s"),updatefnbuf);
  174. }
  175. nextupdate= 0;
  176. }
  177. void modstatdb_shutdown(void) {
  178. switch (cstatus) {
  179. case msdbrw_write:
  180. checkpoint();
  181. writedb(availablefile,1,0);
  182. /* tidy up a bit, but don't worry too much about failure */
  183. fclose(importanttmp);
  184. strcpy(updatefnrest, IMPORTANTTMP); unlink(updatefnbuf);
  185. varbuffree(&uvb);
  186. /* fall through */
  187. case msdbrw_needsuperuserlockonly:
  188. unlockdatabase(admindir);
  189. default:
  190. break;
  191. }
  192. free(updatefnbuf);
  193. }
  194. struct pipef *status_pipes= NULL;
  195. void modstatdb_note(struct pkginfo *pkg) {
  196. assert(cstatus >= msdbrw_write);
  197. onerr_abort++;
  198. if (status_pipes) {
  199. static struct varbuf *status= NULL;
  200. struct pipef *pipef= status_pipes;
  201. int r;
  202. if (status == NULL) {
  203. status = nfmalloc(sizeof(struct varbuf));
  204. varbufinit(status);
  205. } else
  206. varbufreset(status);
  207. r= varbufprintf(status, "status: %s: %s\n", pkg->name, statusinfos[pkg->status]);
  208. while (pipef) {
  209. write(pipef->fd, status->buf, r);
  210. pipef= pipef->next;
  211. }
  212. }
  213. varbufreset(&uvb);
  214. varbufrecord(&uvb, pkg, &pkg->installed);
  215. if (fwrite(uvb.buf, 1, uvb.used, importanttmp) != uvb.used)
  216. ohshite(_("unable to write updated status of `%.250s'"), pkg->name);
  217. if (fflush(importanttmp))
  218. ohshite(_("unable to flush updated status of `%.250s'"), pkg->name);
  219. if (ftruncate(fileno(importanttmp), uvb.used))
  220. ohshite(_("unable to truncate for updated status of `%.250s'"), pkg->name);
  221. if (fsync(fileno(importanttmp)))
  222. ohshite(_("unable to fsync updated status of `%.250s'"), pkg->name);
  223. if (fclose(importanttmp))
  224. ohshite(_("unable to close updated status of `%.250s'"), pkg->name);
  225. sprintf(updatefnrest, IMPORTANTFMT, nextupdate);
  226. if (rename(importanttmpfile, updatefnbuf))
  227. ohshite(_("unable to install updated status of `%.250s'"), pkg->name);
  228. assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */
  229. nextupdate++;
  230. if (nextupdate > MAXUPDATES) {
  231. checkpoint();
  232. nextupdate= 0;
  233. }
  234. createimptmp();
  235. onerr_abort--;
  236. }