dbmodify.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. admindir= adir;
  109. for (fnip=fnis; fnip->suffix; fnip++) {
  110. free(*fnip->store);
  111. *fnip->store= m_malloc(strlen(adir)+strlen(fnip->suffix)+2);
  112. sprintf(*fnip->store, "%s/%s", adir, fnip->suffix);
  113. }
  114. cflags= readwritereq & msdbrw_flagsmask;
  115. readwritereq &= ~msdbrw_flagsmask;
  116. switch (readwritereq) {
  117. case msdbrw_needsuperuser:
  118. case msdbrw_needsuperuserlockonly:
  119. if (getuid() || geteuid())
  120. ohshit(_("requested operation requires superuser privilege"));
  121. /* fall through */
  122. case msdbrw_write: case msdbrw_writeifposs:
  123. if (access(adir,W_OK)) {
  124. if (errno != EACCES)
  125. ohshite(_("unable to access dpkg status area"));
  126. else if (readwritereq == msdbrw_write)
  127. ohshit(_("operation requires read/write access to dpkg status area"));
  128. cstatus= msdbrw_readonly;
  129. } else {
  130. lockdatabase(adir);
  131. cstatus= (readwritereq == msdbrw_needsuperuserlockonly ?
  132. msdbrw_needsuperuserlockonly :
  133. msdbrw_write);
  134. }
  135. break;
  136. case msdbrw_readonly:
  137. cstatus= msdbrw_readonly; break;
  138. default:
  139. internerr("unknown readwritereq");
  140. }
  141. updatefnbuf= m_malloc(strlen(adir)+sizeof(UPDATESDIR)+IMPORTANTMAXLEN+5);
  142. strcpy(updatefnbuf,adir);
  143. strcat(updatefnbuf,"/" UPDATESDIR);
  144. updatefnrest= updatefnbuf+strlen(updatefnbuf);
  145. if (cstatus != msdbrw_needsuperuserlockonly) {
  146. cleanupdates();
  147. if(!(cflags & msdbrw_noavail))
  148. parsedb(availablefile,
  149. pdb_recordavailable|pdb_rejectstatus,
  150. 0,0,0);
  151. }
  152. if (cstatus >= msdbrw_write) {
  153. createimptmp();
  154. uvb.used= 0;
  155. uvb.size= 10240;
  156. uvb.buf= m_malloc(uvb.size);
  157. }
  158. return cstatus;
  159. }
  160. static void checkpoint(void) {
  161. int i;
  162. assert(cstatus >= msdbrw_write);
  163. writedb(statusfile,0,1);
  164. for (i=0; i<nextupdate; i++) {
  165. sprintf(updatefnrest, IMPORTANTFMT, i);
  166. assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */
  167. if (unlink(updatefnbuf))
  168. ohshite(_("failed to remove my own update file %.255s"),updatefnbuf);
  169. }
  170. nextupdate= 0;
  171. }
  172. void modstatdb_shutdown(void) {
  173. switch (cstatus) {
  174. case msdbrw_write:
  175. checkpoint();
  176. writedb(availablefile,1,0);
  177. /* tidy up a bit, but don't worry too much about failure */
  178. fclose(importanttmp);
  179. strcpy(updatefnrest, IMPORTANTTMP); unlink(updatefnbuf);
  180. varbuffree(&uvb);
  181. /* fall through */
  182. case msdbrw_needsuperuserlockonly:
  183. unlockdatabase(admindir);
  184. default:
  185. break;
  186. }
  187. free(updatefnbuf);
  188. }
  189. void modstatdb_note(struct pkginfo *pkg) {
  190. assert(cstatus >= msdbrw_write);
  191. onerr_abort++;
  192. varbufreset(&uvb);
  193. varbufrecord(&uvb, pkg, &pkg->installed);
  194. if (fwrite(uvb.buf, 1, uvb.used, importanttmp) != uvb.used)
  195. ohshite(_("unable to write updated status of `%.250s'"), pkg->name);
  196. if (fflush(importanttmp))
  197. ohshite(_("unable to flush updated status of `%.250s'"), pkg->name);
  198. if (ftruncate(fileno(importanttmp), uvb.used))
  199. ohshite(_("unable to truncate for updated status of `%.250s'"), pkg->name);
  200. if (fsync(fileno(importanttmp)))
  201. ohshite(_("unable to fsync updated status of `%.250s'"), pkg->name);
  202. if (fclose(importanttmp))
  203. ohshite(_("unable to close updated status of `%.250s'"), pkg->name);
  204. sprintf(updatefnrest, IMPORTANTFMT, nextupdate);
  205. if (rename(importanttmpfile, updatefnbuf))
  206. ohshite(_("unable to install updated status of `%.250s'"), pkg->name);
  207. assert(strlen(updatefnrest)<=IMPORTANTMAXLEN); /* or we've made a real mess */
  208. nextupdate++;
  209. if (nextupdate > MAXUPDATES) checkpoint();
  210. createimptmp();
  211. onerr_abort--;
  212. }