[cowdancer] 01/11: qemubuilder.c: Run hooks from mountpoint and use input/ for copying

James Clarke jrtc27-guest at moszumanska.debian.org
Mon May 2 01:19:58 UTC 2016


This is an automated email from the git hooks/post-receive script.

jrtc27-guest pushed a commit to branch master
in repository cowdancer.

commit ef98fc74e733c7830879982effa1206a0fb5c587
Author: James Clarke <jrtc27 at jrtc27.com>
Date:   Sun May 1 22:34:43 2016 +0100

    qemubuilder.c: Run hooks from mountpoint and use input/ for copying
---
 qemubuilder.c | 162 +++++++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 131 insertions(+), 31 deletions(-)

diff --git a/qemubuilder.c b/qemubuilder.c
index 65266e5..a53d4fe 100755
--- a/qemubuilder.c
+++ b/qemubuilder.c
@@ -41,8 +41,8 @@
 #include "file.h"
 
 #define BUILDDIR "/tmp/buildd"
-
-#define CHROOT_HOOKDIR BUILDDIR"/hooks"
+#define MOUNTPOINT "/var/cache/pbuilder/pbuilder-mnt"
+#define CHROOT_HOOKDIR MOUNTPOINT"/hooks"
 
 #define EXECUTE_HOOKS_INDENT(indent, prefix) \
 indent"if [ -d \""CHROOT_HOOKDIR"\" ]; then\n" \
@@ -166,6 +166,23 @@ static FILE* create_script(const char* mountpoint, const char* relative_path)
   FILE *ret = NULL;
 
   asprintf(&s, "%s/%s", mountpoint, relative_path);
+
+  /* Create directories in relative_path */
+  for (char *c = s + strlen(mountpoint) + 1; *c; ++c)
+    {
+      if (*c != '/') continue;
+      *c = 0;
+      if (mkdir(s, 0777) && errno != EEXIST)
+	{
+	  fprintf(stderr,
+		  "E: Could not create directory '%s': %s\n",
+		  s,
+		  strerror(errno));
+	  goto fail;
+	}
+      *c = '/';
+    }
+
   if(!(f=fopen(s, "w")))
     goto fail;
   if(chmod(s, 0700))
@@ -192,6 +209,28 @@ static void fix_terminal(void)
     }
 }
 
+static int copy_file_contents_to_temp(const char* orig,
+				      const char* tempdir,
+				      const char* tempname)
+{
+  char* temppath;
+  int ret;
+  if (tempname == NULL)
+    tempname = basename(orig);
+
+  asprintf(&temppath, "%s/%s", tempdir, tempname);
+  ret=copy_file(orig, temppath);
+  if (ret == -1)
+    {
+      fprintf(stderr, "E: Copy file error in %s to %s\n",
+	      orig, temppath);
+      goto out;
+    }
+out:
+  free(temppath);
+  return ret;
+}
+
 static int copy_file_contents_in_temp(FILE *f,
 				      const char* tempname,
 				      const char* targetdir,
@@ -215,32 +254,88 @@ static int copy_file_contents_through_temp(FILE* f,
 					   const char* tempdir,
 					   const char* targetdir)
 {
-  char* temppath;
   int ret;
   char* file_basename = basename(orig);
-  char* tempname = file_basename;
-  asprintf(&temppath, "%s/%s", tempdir, tempname);
-  ret=copy_file(orig, temppath);
-  if (ret == -1)
+  char* tempname = NULL;
+  char* tempinput = NULL;
+  if (0>asprintf(&tempinput,
+		 "%s/input",
+		 tempdir))
     {
-      fprintf(f, "E: Copy file error in %s to %s\n",
-	      orig, temppath);
+      fprintf(stderr,
+	      "E: failed to allocate string for '%s/input': %s\n",
+	      tempdir,
+	      strerror(errno));
+      ret = 1;
       goto out;
     }
 
+  if (0>asprintf(&tempname,
+		 "input/%s",
+		 file_basename))
+    {
+      fprintf(stderr,
+	      "E: failed to allocate string for 'input/%s': %s\n",
+	      file_basename,
+	      strerror(errno));
+      ret = 1;
+      goto out;
+    }
 
-  copy_file_contents_in_temp(f, tempname, targetdir, file_basename);
+  if (mkdir(tempinput, 0777) && errno != EEXIST)
+    {
+      fprintf(stderr,
+	      "E: failed to create directory '%s': %s\n",
+	      tempinput,
+	      strerror(errno));
+      ret = 1;
+      goto out;
+    }
+
+  ret = copy_file_contents_to_temp(orig, tempinput, file_basename);
+  if (ret != 0)
+    goto out;
+
+  ret = copy_file_contents_in_temp(f, tempname, targetdir, file_basename);
 
 out:
-  free(temppath);
+  if (tempname != NULL)
+    {
+      free(tempname);
+    }
+  if (tempinput != NULL)
+    {
+      free(tempinput);
+    }
   return ret;
 }
 
-static int copy_hookdir(FILE *f, const char *hookdir, const char *tmp)
+static int copy_hookdir(const char *hookdir, const char *tmp)
 {
   int ret = 0;
   struct dirent *dirp;
   DIR *hd = opendir(hookdir);
+  char *hookstmp = NULL;
+  if (0>asprintf(&hookstmp,
+		 "%s/hooks",
+		 tmp))
+    {
+      fprintf(stderr,
+	      "Error allocating string for '%s/hooks': %s\n",
+	      tmp,
+	      strerror(errno));
+      ret = 1;
+      goto out;
+    }
+
+  if (mkdir(hookstmp,0777))
+    {
+      fprintf(stderr,
+	      "Error creating directory for hooks: %s\n",
+	      strerror(errno));
+      ret = 1;
+      goto out;
+    }
 
   if (hd == NULL)
     {
@@ -288,14 +383,15 @@ static int copy_hookdir(FILE *f, const char *hookdir, const char *tmp)
 	  continue;
 	}
 
-      copy_file_contents_through_temp(f, src, tmp, CHROOT_HOOKDIR);
-      fprintf(f,
-	      "chmod 0755 \""CHROOT_HOOKDIR"/%s\"\n",
-	      dirp->d_name);
+      copy_file_contents_to_temp(src, hookstmp, basename(src));
       free(src);
     }
 
 out:
+  if (hookstmp != NULL)
+    {
+      free(hookstmp);
+    }
   if (hd != NULL)
     {
       closedir(hd);
@@ -585,10 +681,10 @@ static void write_first_stage(FILE *f, const struct pbuilderconfig* pc)
 	  "chmod 01777 /dev/shm\n"
 	  "mount -t tmpfs tmpfs /dev/shm\n"
 	  "ln -s /proc/mounts /etc/mtab\n"
-	  "export PBUILDER_MOUNTPOINT=/var/cache/pbuilder/pbuilder-mnt\n"
+	  "export PBUILDER_MOUNTPOINT="MOUNTPOINT"\n"
 	  "mkdir -p $PBUILDER_MOUNTPOINT\n"
 	  "mount -n -t ext3 /dev/%sb $PBUILDER_MOUNTPOINT \n"
-	  "$PBUILDER_MOUNTPOINT/pbuilder-run \n",
+	  "$PBUILDER_MOUNTPOINT/input/pbuilder-run \n",
 	  qemu_arch_diskdevice(pc)
 	  );
 }
@@ -636,7 +732,7 @@ static int run_second_stage_script
   ret=create_ext3_block_device(workblockdevicepath, 1);
   loop_mount(workblockdevicepath, pc->buildplace);
 
-  f = create_script(pc->buildplace, "pbuilder-run");
+  f = create_script(pc->buildplace, "input/pbuilder-run");
   fprintf(f,
 	  "#!/bin/bash\n"
 
@@ -664,7 +760,7 @@ static int run_second_stage_script
 	  "dhclient $IFNAME\n"
 	  "mkdir -p \""BUILDDIR"\"\n"
 	  "%s\n"
-	  "$PBUILDER_MOUNTPOINT/run-copyfiles\n"
+	  "$PBUILDER_MOUNTPOINT/input/run-copyfiles\n"
 	  "hostname pbuilder-$(cat /etc/hostname)\n"
 	  "%s\n"
 	  //TODO: I can mount /var/cache/apt/archives from some scratch space to not need this:
@@ -677,24 +773,24 @@ static int run_second_stage_script
   fclose(f);
 
   /* copy files script */
-  f = create_script(pc->buildplace, "run-copyfiles");
+  f = create_script(pc->buildplace, "input/run-copyfiles");
   if (update_first_stage)
     {
-      FILE *g = create_script(pc->buildplace, "pbuilder-run-first-stage");
+      FILE *g = create_script(pc->buildplace, "input/pbuilder-run-first-stage");
       write_first_stage(g, pc);
       fclose(g);
-      copy_file_contents_in_temp(f, "pbuilder-run-first-stage", "/", "pbuilder-run");
+      copy_file_contents_in_temp(f, "input/pbuilder-run-first-stage", "/", "pbuilder-run");
     }
   copy_file_contents_through_temp(f, "/etc/hosts", pc->buildplace, "/etc");
   copy_file_contents_through_temp(f, "/etc/hostname", pc->buildplace, "/etc");
   /* copy inputfile */
   for (i=0; pc->inputfile[i]; ++i)
     {
-      copy_file_contents_through_temp(f, pc->inputfile[i], pc->buildplace, BUILDDIR);
+      copy_file_contents_to_temp(pc->inputfile[i], pc->buildplace, NULL);
     }
   if (pc->hookdir != NULL && pc->hookdir[0])
     {
-      copy_hookdir(f, pc->hookdir, pc->buildplace);
+      copy_hookdir(pc->hookdir, pc->buildplace);
     }
   fclose(f);
 
@@ -921,7 +1017,7 @@ int cpbuilder_create(const struct pbuilderconfig* pc)
 
   timestring=get_current_time_string();
 
-  f = create_script(pc->buildplace, "pbuilder-run");
+  f = create_script(pc->buildplace, "input/pbuilder-run");
   fprintf(f,
 	  "#!/bin/bash\n"
 	  /* define function to terminate qemu */
@@ -960,7 +1056,7 @@ int cpbuilder_create(const struct pbuilderconfig* pc)
 	  "mount -n devpts /dev/pts -t devpts\n"
 	  "dhclient eth0\n"
 	  "%s\n"
-	  "$PBUILDER_MOUNTPOINT/run-copyfiles\n"
+	  "$PBUILDER_MOUNTPOINT/input/run-copyfiles\n"
 	  "hostname pbuilder-$(cat /etc/hostname)\n"
 	  //TODO: installaptlines
 	  "echo '%s' > /etc/apt/sources.list.d/other.list\n"
@@ -993,12 +1089,12 @@ int cpbuilder_create(const struct pbuilderconfig* pc)
   /* TODO: can I do 'date --set' from output of 'LC_ALL=C date' */
 
   /* copy files script */
-  f = create_script(pc->buildplace, "run-copyfiles");
+  f = create_script(pc->buildplace, "input/run-copyfiles");
   copy_file_contents_through_temp(f, "/etc/hosts", pc->buildplace, "/etc");
   copy_file_contents_through_temp(f, "/etc/hostname", pc->buildplace, "/etc");
   if (pc->hookdir != NULL && pc->hookdir[0])
     {
-      copy_hookdir(f, pc->hookdir, pc->buildplace);
+      copy_hookdir(pc->hookdir, pc->buildplace);
     }
   fclose(f);
 
@@ -1135,12 +1231,16 @@ int cpbuilder_execute(const struct pbuilderconfig* pc, char** av)
   char* runcommandline;
   int ret;
 
-  asprintf(&hostcommand, "cp %s %s/runscript\n", av[0], pc->buildplace);
+  asprintf(&hostcommand,
+	   "[ -d %s/input ] || mkdir %s/input\n || echo \"E: Failed to create directory '%s/input'\"\n"
+	   "cp %s %s/input/runscript\n",
+	   pc->buildplace, pc->buildplace, pc->buildplace,
+	   av[0], pc->buildplace);
   /* TODO: add options too */
   asprintf(&runcommandline,
 	   EXECUTE_HOOKS("H")
 	   EXECUTE_HOOKS("F")
-	   "sh $PBUILDER_MOUNTPOINT/runscript");
+	   "sh $PBUILDER_MOUNTPOINT/input/runscript");
   ret=run_second_stage_script(pc->save_after_login,
 			      0,
 			      runcommandline,

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pbuilder/cowdancer.git



More information about the Pbuilder-maint mailing list