commit ca4768323365cf347c7c7be59dbb04c51ae0a6a1
Author: peterbud <peterbudai@hotmail.com>
Date:   Mon Aug 28 14:10:49 2017 +0200

    Close temp files before rename

diff --git a/gphoto2/actions.c b/gphoto2/actions.c
index f34088e..8c8b51f 100644
--- a/gphoto2/actions.c
+++ b/gphoto2/actions.c
@@ -978,6 +978,9 @@ _action_camera_capture_preview (GPParams *p, int viewasciiart)
 		return (r);
 	}
 
+	//close the temp file as you cannot rename it later
+	if (fd) close (fd);
+
 	/* name it file_%filename if --filename is set, otherwise capture_preview */
 	r = save_camera_file_to_file (NULL, "capture_preview", p->filename?GP_FILE_TYPE_PREVIEW:GP_FILE_TYPE_NORMAL, file, tmpfilename);
 	gp_file_unref (file);
@@ -1016,7 +1019,7 @@ action_camera_capture_movie (GPParams *p, const char *arg)
 		fd = dup(fileno(stdout));
 		xname = "stdout";
 	} else {
-		fd = open("movie.mjpg",O_WRONLY|O_CREAT,0660);
+		fd = open("movie.mjpg",O_WRONLY | O_CREAT | O_BINARY,0660);
 		if (fd == -1) {
 			cli_error_print(_("Could not open 'movie.mjpg'."));
 			return GP_ERROR;
@@ -1070,6 +1073,9 @@ action_camera_capture_movie (GPParams *p, const char *arg)
 	}
 	gp_file_unref (file);
 
+	//close the temp file as you cannot rename it later
+	if (fd) close (fd);
+
 	fprintf(stderr,_("Movie capture finished (%d frames)\n"), captured_frames);
 	return GP_OK;
 }
diff --git a/gphoto2/main.c b/gphoto2/main.c
index 01a045e..383ac28 100644
--- a/gphoto2/main.c
+++ b/gphoto2/main.c
@@ -688,9 +688,14 @@ save_file_to_file (Camera *camera, GPContext *context, Flags flags,
 		unlink (tmpname);
 		return (GP_OK);
 	}
-	res = save_camera_file_to_file (folder, filename, type, file, tmpfilename);
+
+	//close the temp file as you cannot rename it later
 	if (ps && ps->fd) close (ps->fd);
 	free (ps);
+	ps = NULL;
+	if (fd) close (fd);
+
+	res = save_camera_file_to_file (folder, filename, type, file, tmpfilename);
 	gp_file_unref (file);
 	if ((res!=GP_OK) && tmpfilename)
 		unlink (tmpfilename);
