[PATCH obexd] client: Fix not checking if destination exist while renaming file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Luiz Augusto von Dentz <luiz.von.dentz@xxxxxxxxx>

rename return an error if the destination directory doesn't exist, so
in case it doesn't exist it should be created before calling rename.
---
 client/transfer.c |   27 +++++++++++++++++++++++++--
 1 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/client/transfer.c b/client/transfer.c
index 3065c9c..772834f 100644
--- a/client/transfer.c
+++ b/client/transfer.c
@@ -715,11 +715,34 @@ int obc_transfer_set_filename(struct obc_transfer *transfer,
 					const char *filename)
 {
 	int err;
+	struct stat st;
+	char *dirname;
+	gboolean dir = FALSE;
+
+	dirname = g_path_get_dirname(filename);
+	if (stat(dirname, &st) < 0) {
+		if (errno != ENOENT) {
+			error("stat(): %s (%d)", strerror(errno), errno);
+			return -errno;
+		}
+
+		if (mkdir(dirname, 0755) < 0) {
+			error("mkdir(): %s (%d)", strerror(errno), errno);
+			return -errno;
+		}
+
+		dir = TRUE;
+	}
 
 	err = rename(transfer->filename, filename);
 	if (err < 0) {
-		error("rename(): %s (%d)", strerror(errno), errno);
-		return -errno;
+		err = -errno;
+
+		if (dir)
+			rmdir(dirname);
+
+		error("rename(): %s (%d)", strerror(-err), -err);
+		return -err;
 	}
 
 	g_free(transfer->filename);
-- 
1.7.7.6

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Bluez Devel]     [Linux Wireless Networking]     [Linux Wireless Personal Area Networking]     [Linux ATH6KL]     [Linux USB Devel]     [Linux Media Drivers]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux