trigdeferred.l 4.7 KB

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