[Spice-devel] [PATCH linux vdagent v2] file-xfer: use better names for duplicate files

Jonathon Jongsma jjongsma at redhat.com
Fri Apr 28 20:53:30 UTC 2017


When transferring the same file multiple times, the vdagent tries to
avoid overwriting an existing file on the guest by appending an integer
to the filename. Instead of just appending the integer to the very end,
we now try to be smarter and append the integer before the file
extension. For example, if we transferred the file "file.doc" twice, the
second copy would have become "file.doc (1)". It is now "file (1).doc".
This matches more closely with the behavior of the Nautilus file manager
as well.

Signed-off-by: Jonathon Jongsma <jjongsma at redhat.com>
---
Changes since v1:
 - use '%.*s' instead of '%*s' in format string
 - use strlen instead of -1 for precision if there is no filename extension

 src/vdagent/file-xfers.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/vdagent/file-xfers.c b/src/vdagent/file-xfers.c
index b3937a4..fe762d7 100644
--- a/src/vdagent/file-xfers.c
+++ b/src/vdagent/file-xfers.c
@@ -202,7 +202,10 @@ void vdagent_file_xfers_start(struct vdagent_file_xfers *xfers,
     path = g_strdup(file_path);
     for (i = 0; i < 64 && (stat(path, &st) == 0 || errno != ENOENT); i++) {
         g_free(path);
-        path = g_strdup_printf("%s (%d)", file_path, i + 1);
+        char *extension = strrchr(file_path, '.');
+        int basename_len = extension != NULL ? extension - file_path : strlen(file_path);
+        path = g_strdup_printf("%.*s (%i)%s", basename_len, file_path,
+                               i + 1, extension ? extension : "");
     }
     g_free(task->file_name);
     task->file_name = path;
-- 
2.9.3



More information about the Spice-devel mailing list