+ snprintf (filename, len, "%s/kernel", tmpcd);
+ snprintf (filename2, len, "%s/kernel", cachedir);
+ unlink (filename2);
+ if (rename (filename, filename2) == -1) {
+ perrorf (g, "rename: %s %s", filename, filename2);
+ close (fd);
+ return -1;
+ }
+
+ snprintf (filename, len, "%s/initrd", tmpcd);
+ snprintf (filename2, len, "%s/initrd", cachedir);
+ unlink (filename2);
+ if (rename (filename, filename2) == -1) {
+ perrorf (g, "rename: %s %s", filename, filename2);
+ close (fd);
+ return -1;
+ }
+
+ snprintf (filename, len, "%s/root", tmpcd);
+ snprintf (filename2, len, "%s/root", cachedir);
+ unlink (filename2);
+ if (rename (filename, filename2) == -1) {
+ perrorf (g, "rename: %s %s", filename, filename2);
+ close (fd);
+ return -1;
+ }
+
+ rmdir (tmpcd);
+
+ /* Now finish off by linking to the cached appliance and returning it. */
+ if (hard_link_to_cached_appliance (g, cachedir,
+ kernel, initrd, appliance) == -1) {
+ close (fd);
+ return -1;
+ }
+
+ /* Releases the lock on checksum. */
+ if (close (fd) == -1) {
+ perrorf (g, "close");
+ return -1;
+ }
+
+ return 0;
+}
+
+/* NB: lock on checksum file must be held when this is called. */
+static int
+hard_link_to_cached_appliance (guestfs_h *g,
+ const char *cachedir,
+ char **kernel, char **initrd, char **appliance)
+{
+ pid_t pid = getpid ();
+ size_t len = strlen (cachedir) + 32;
+
+ *kernel = safe_malloc (g, len);
+ *initrd = safe_malloc (g, len);
+ *appliance = safe_malloc (g, len);
+ snprintf (*kernel, len, "%s/kernel.%d", cachedir, pid);
+ snprintf (*initrd, len, "%s/initrd.%d", cachedir, pid);
+ snprintf (*appliance, len, "%s/root.%d", cachedir, pid);
+
+ char filename[len];
+ snprintf (filename, len, "%s/kernel", cachedir);
+ (void) unlink (*kernel);
+ if (link (filename, *kernel) == -1) {
+ perrorf (g, "link: %s %s", filename, *kernel);