trigdeferred.l 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * dpkg - main program for package management
  3. * trigdeferred.l - parsing of triggers/Deferred
  4. *
  5. * Copyright (C) 2007 Canonical Ltd
  6. * written by Ian Jackson <ian@chiark.greenend.org.uk>
  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. %option prefix="trigdef_yy"
  23. /* Reset the name to the default value (instead of using "trigdeferred.c")
  24. * so that automake (ylwrap) can find it. */
  25. %option outfile="lex.yy.c"
  26. %option noyywrap
  27. %option batch
  28. %option nodefault
  29. %option perf-report
  30. %option warn
  31. %option nounput
  32. %x midline
  33. %{
  34. #include <config.h>
  35. #include <sys/stat.h>
  36. #include <sys/fcntl.h>
  37. #include <dpkg.h>
  38. #include <dpkg-db.h>
  39. static struct varbuf fn, newfn;
  40. %}
  41. %%
  42. [ \t\n] /* whitespace */
  43. #.*\n /* comments */
  44. [\x21-\x7e]+ {
  45. trigdef->trig_begin(trigdef_yytext);
  46. BEGIN(midline);
  47. }
  48. <midline>[ \t] /* whitespace */
  49. <midline>[-0-9a-z][-+.0-9a-z]* {
  50. if (trigdef_yytext[0] == '-' && trigdef_yytext[1])
  51. ohshit(_("invalid package name `%.250s' in triggers deferred "
  52. "file `%.250s'"), trigdef_yytext, fn.buf);
  53. trigdef->package(trigdef_yytext);
  54. }
  55. <midline>\n|#.*\n {
  56. trigdef->trig_end();
  57. BEGIN(0);
  58. }
  59. <midline><<EOF>> {
  60. ohshit(_("truncated triggers deferred file `%.250s'"), fn.buf);
  61. }
  62. <*>. {
  63. ohshit(_("syntax error in triggers deferred file `%.250s' at "
  64. "character `%s'%s"),
  65. fn.buf, yytext, YY_START == midline ? " midline": "");
  66. }
  67. %%
  68. /*---------- Deferred file handling ----------*/
  69. static int lock_fd = -1;
  70. static FILE *old_deferred;
  71. FILE *trig_new_deferred;
  72. const struct trigdefmeths *trigdef;
  73. static void
  74. constructfn(struct varbuf *vb, const char *admindir, const char *tail)
  75. {
  76. varbufreset(vb);
  77. varbufaddstr(vb, admindir);
  78. varbufaddstr(vb, "/" TRIGGERSDIR);
  79. varbufaddstr(vb, tail);
  80. varbufaddc(vb, 0);
  81. }
  82. int
  83. trigdef_update_start(enum trigdef_updateflags uf, const char *admindir)
  84. {
  85. struct stat stab;
  86. int r;
  87. if (uf & tduf_write) {
  88. constructfn(&fn, admindir, TRIGGERSLOCKFILE);
  89. if (lock_fd == -1) {
  90. lock_fd = open(fn.buf, O_RDWR | O_CREAT | O_TRUNC, 0600);
  91. if (lock_fd == -1) {
  92. if (!(errno == ENOENT && (uf & tduf_nolockok)))
  93. ohshite(_("unable to open/create "
  94. "triggers lockfile `%.250s'"),
  95. fn.buf);
  96. return -1;
  97. }
  98. }
  99. lock_file(&lock_fd, fn.buf, _("unable to lock triggers area"),
  100. NULL);
  101. } else {
  102. /* Dummy for pop_cleanups. */
  103. push_cleanup(NULL, 0, NULL, 0, 0);
  104. }
  105. constructfn(&fn, admindir, TRIGGERSDEFERREDFILE);
  106. r = stat(fn.buf, &stab);
  107. if (r) {
  108. if (errno != ENOENT)
  109. ohshite(_("unable to stat triggers deferred file `%.250s'"),
  110. fn.buf);
  111. } else if (!stab.st_size) {
  112. if (!(uf & tduf_writeifempty)) {
  113. pop_cleanup(ehflag_normaltidy);
  114. return -2;
  115. }
  116. }
  117. if (old_deferred)
  118. fclose(old_deferred);
  119. old_deferred = fopen(fn.buf, "r");
  120. if (!old_deferred) {
  121. if (errno != ENOENT)
  122. ohshite(_("unable to open triggers deferred file `%.250s'"),
  123. fn.buf);
  124. if (!(uf & tduf_writeifenoent)) {
  125. pop_cleanup(ehflag_normaltidy);
  126. return -3;
  127. }
  128. }
  129. if (uf & tduf_write) {
  130. constructfn(&newfn, admindir, TRIGGERSDEFERREDFILE ".new");
  131. if (trig_new_deferred)
  132. fclose(trig_new_deferred);
  133. trig_new_deferred = fopen(newfn.buf, "w");
  134. if (!trig_new_deferred)
  135. ohshite(_("unable to open/create new triggers deferred file `%.250s'"),
  136. newfn.buf);
  137. }
  138. if (!old_deferred)
  139. return 1;
  140. trigdef_yyrestart(old_deferred);
  141. BEGIN(0);
  142. return 2;
  143. }
  144. void
  145. trigdef_process_done(void)
  146. {
  147. int r;
  148. if (old_deferred) {
  149. if (ferror(old_deferred))
  150. ohshite(_("error reading triggers deferred file `%.250s'"),
  151. fn.buf);
  152. fclose(old_deferred);
  153. old_deferred = NULL;
  154. }
  155. if (trig_new_deferred) {
  156. if (ferror(trig_new_deferred))
  157. ohshite(_("unable to write new triggers deferred "
  158. "file `%.250s'"), newfn.buf);
  159. r = fclose(trig_new_deferred);
  160. trig_new_deferred = NULL;
  161. if (r)
  162. ohshite(_("unable to close new triggers deferred "
  163. "file `%.250s'"), newfn.buf);
  164. if (rename(newfn.buf, fn.buf))
  165. ohshite(_("unable to install new triggers deferred "
  166. "file `%.250s'"), fn.buf);
  167. }
  168. /* Unlock. */
  169. pop_cleanup(ehflag_normaltidy);
  170. }