+ char cachedir[len];
+ snprintf (cachedir, len, "%s/.guestfs-%d", tmpdir, uid);
+ char filename[len];
+ char filename2[len];
+ snprintf (filename, len, "%s/checksum", cachedir);
+
+ /* Open and acquire write lock on checksum file. The file might
+ * not exist, in which case we want to create it.
+ */
+ int fd = open (filename, O_WRONLY|O_CREAT, 0755);
+ if (fd == -1) {
+ perrorf (g, "open: %s", filename);
+ return -1;
+ }
+ struct flock fl;
+ fl.l_type = F_WRLCK;
+ fl.l_whence = SEEK_SET;
+ fl.l_start = 0;
+ fl.l_len = 1;
+ again:
+ if (fcntl (fd, F_SETLKW, &fl) == -1) {
+ if (errno == EINTR)
+ goto again;
+ perrorf (g, "fcntl: F_SETLKW: %s", filename);
+ close (fd);
+ return -1;
+ }
+
+ /* At this point we have acquired a write lock on the checksum
+ * file so we go ahead and replace it with the new checksum, and
+ * rename in appliance files into this directory.
+ */
+ size_t clen = strlen (checksum);
+ if (ftruncate (fd, clen) == -1) {
+ perrorf (g, "ftruncate: %s", filename);
+ close (fd);
+ return -1;
+ }
+
+ ssize_t rr = write (fd, checksum, clen);
+ if (rr == -1) {
+ perrorf (g, "write: %s", filename);
+ close (fd);
+ return -1;
+ }
+ if ((size_t) rr != clen) {
+ error (g, "partial write: %s", filename);
+ close (fd);
+ return -1;
+ }
+
+ 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");
+ /* Allocated in hard_link_to_cached_appliance above, must be
+ * freed along this error path.
+ */
+ free (*kernel);
+ free (*initrd);
+ free (*appliance);
+ return -1;
+ }