Sfoglia il codice sorgente

libdpkg: Encapsulate triggersdir handling in triglib

The only users of triggersdir and related files are the triglib and
trigdeferred modules, and dbmodify does not have any business in knowing
where the triggersdir is located (besides for conveninence when creating
the pathname). Create instead a new function trig_get_triggersdir() to
generate the triggersdir from an admindir, and use that on each module.
Guillem Jover 16 anni fa
parent
commit
a35f0e37a4
4 ha cambiato i file con 37 aggiunte e 5 eliminazioni
  1. 0 4
      lib/dpkg/dbmodify.c
  2. 2 1
      lib/dpkg/dpkg-db.h
  3. 3 0
      lib/dpkg/trigdeferred.l
  4. 32 0
      lib/dpkg/triglib.c

+ 0 - 4
lib/dpkg/dbmodify.c

@@ -44,7 +44,6 @@
 #include <dpkg/file.h>
 
 char *statusfile=NULL, *availablefile=NULL;
-char *triggersdir, *triggersfilefile, *triggersnewfilefile;
 
 static enum modstatdb_rw cstatus=-1, cflags=0;
 static char *importanttmpfile=NULL;
@@ -134,9 +133,6 @@ static const struct fni {
   {   STATUSFILE,                 &statusfile         },
   {   AVAILFILE,                  &availablefile      },
   {   UPDATESDIR IMPORTANTTMP,    &importanttmpfile   },
-  {   TRIGGERSDIR,                &triggersdir        },
-  {   TRIGGERSDIR "/File",        &triggersfilefile   },
-  {   TRIGGERSDIR "/File.new",    &triggersnewfilefile},
   {   NULL, NULL                                      }
 };
 

+ 2 - 1
lib/dpkg/dpkg-db.h

@@ -210,7 +210,6 @@ void modstatdb_shutdown(void);
 
 /* Initialised by modstatdb_init. */
 extern char *statusfile, *availablefile;
-extern char *triggersdir, *triggersfilefile, *triggersnewfilefile;
 
 const char *pkgadminfile(struct pkginfo *pkg, const char *whichfile);
 
@@ -286,6 +285,8 @@ void trig_override_hooks(const struct trig_hooks *hooks);
 
 /*** from triglib.c ***/
 
+char *trig_get_triggersdir(const char *admindir);
+
 void trig_file_activate_byname(const char *trig, struct pkginfo *aw);
 void trig_file_activate(struct filenamenode *trig, struct pkginfo *aw);
 

+ 3 - 0
lib/dpkg/trigdeferred.l

@@ -87,6 +87,7 @@ static const struct trigdefmeths *trigdef;
 
 /*---------- Deferred file handling ----------*/
 
+static const char *triggersdir;
 static int lock_fd = -1;
 static FILE *old_deferred;
 static FILE *trig_new_deferred;
@@ -107,6 +108,8 @@ trigdef_update_start(enum trigdef_updateflags uf, const char *admindir)
 	struct stat stab;
 	int r;
 
+	triggersdir = trig_get_triggersdir(admindir);
+
 	if (uf & tduf_write) {
 		constructfn(&fn, admindir, TRIGGERSLOCKFILE);
 		if (lock_fd == -1) {

+ 32 - 0
lib/dpkg/triglib.c

@@ -27,6 +27,7 @@
 
 #include <assert.h>
 #include <errno.h>
+#include <stdlib.h>
 #include <unistd.h>
 
 #include <dpkg/i18n.h>
@@ -53,6 +54,28 @@ illegal_triggername(const char *p)
 
 /*========== recording triggers ==========*/
 
+static char *triggersdir, *triggersfilefile, *triggersnewfilefile;
+
+char *
+trig_get_triggersdir(const char *admindir)
+{
+	struct varbuf path = VARBUF_INIT;
+
+	varbufprintf(&path, "%s/%s", admindir, TRIGGERSDIR);
+
+	return varbuf_detach(&path);
+}
+
+static char *
+trig_get_filename(const char *dir, const char *filename)
+{
+	struct varbuf path = VARBUF_INIT;
+
+	varbufprintf(&path, "%s/%s", dir, filename);
+
+	return varbuf_detach(&path);
+}
+
 static struct trig_hooks trigh;
 
 /*---------- noting trigger activation in memory ----------*/
@@ -735,6 +758,15 @@ trig_incorporate(enum modstatdb_rw cstatus, const char *admindir)
 	int ur;
 	enum trigdef_updateflags tduf;
 
+	free(triggersdir);
+	triggersdir = trig_get_triggersdir(admindir);
+
+	free(triggersfilefile);
+	triggersfilefile = trig_get_filename(triggersdir, "File");
+
+	free(triggersnewfilefile);
+	triggersnewfilefile = trig_get_filename(triggersdir, "File.new");
+
 	trigdef_set_methods(&tdm_incorp);
 	trig_file_interests_ensure();