From abe12806e576b25f2c5d04898c00961c6d89c253 Mon Sep 17 00:00:00 2001 From: Carles Pina Date: Wed, 19 Nov 2014 16:09:50 +0000 Subject: [PATCH 1/8] Instead of always using the temporary file /tmp/libupdatergtk.so generates a new name. This solves a problem: if the file already existed and the current user couldn't truncate it the auto update couldn't use the Gtk auto-updater. In this commit it also deletes the file and avoids creating and closing the file and opening again. MD-20923 --- src/UpdateDialogGtkFactory.cpp | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/UpdateDialogGtkFactory.cpp b/src/UpdateDialogGtkFactory.cpp index 4ac2ec0..1e2ac58 100644 --- a/src/UpdateDialogGtkFactory.cpp +++ b/src/UpdateDialogGtkFactory.cpp @@ -30,27 +30,28 @@ UpdateDialogGtk* (*update_dialog_gtk_new)() = 0; #define BIND_FUNCTION(library,function) \ function = reinterpret_cast(dlsym(library,#function)); -bool extractFileFromBinary(const char* path, const void* buffer, size_t length) +#define MAX_FILE_PATH 4096 + +bool extractFileFromBinary(int fd, const void* buffer, size_t length) { - int fd = open(path,O_CREAT | O_WRONLY | O_TRUNC,0755); - size_t count = write(fd,buffer,length); - if (fd < 0 || count < length) - { - if (fd >= 0) - { - close(fd); - } - return false; - } - close(fd); - return true; + size_t count = write(fd,buffer,length); + close(fd); + return count >= length; } UpdateDialog* UpdateDialogGtkFactory::createDialog() { - const char* libPath = "/tmp/libupdatergtk.so"; + char* libPath = (char*)malloc(MAX_FILE_PATH); + snprintf(libPath, MAX_FILE_PATH, "/tmp/mendeley-libUpdaterGtk.so.XXXXXX"); - if (!extractFileFromBinary(libPath,libupdatergtk_so,libupdatergtk_so_len)) + int libFd = mkostemp(libPath, O_CREAT | O_WRONLY | O_TRUNC); + if (libFd == -1) + { + LOG(Warn,"Failed to create temporary file - " + std::string(strerror(errno))); + return 0; + } + + if (!extractFileFromBinary(libFd,libupdatergtk_so,libupdatergtk_so_len)) { LOG(Warn,"Failed to load the GTK UI library - " + std::string(strerror(errno))); return 0; @@ -64,6 +65,7 @@ UpdateDialog* UpdateDialogGtkFactory::createDialog() } BIND_FUNCTION(gtkLib,update_dialog_gtk_new); + + unlink(libPath); return reinterpret_cast(update_dialog_gtk_new()); } - From 8a68ba2844e29c9de66e48889b0985839fd57234 Mon Sep 17 00:00:00 2001 From: Carles Pina Date: Wed, 19 Nov 2014 16:37:34 +0000 Subject: [PATCH 2/8] Uses FileUtils::removeFile() instead of unlink() MD-20923 --- src/UpdateDialogGtkFactory.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/UpdateDialogGtkFactory.cpp b/src/UpdateDialogGtkFactory.cpp index 1e2ac58..3daaf0e 100644 --- a/src/UpdateDialogGtkFactory.cpp +++ b/src/UpdateDialogGtkFactory.cpp @@ -1,5 +1,6 @@ #include "UpdateDialogGtkFactory.h" +#include "FileUtils.h" #include "Log.h" #include "UpdateDialog.h" #include "StringUtils.h" @@ -66,6 +67,6 @@ UpdateDialog* UpdateDialogGtkFactory::createDialog() BIND_FUNCTION(gtkLib,update_dialog_gtk_new); - unlink(libPath); + FileUtils::removeFile(libPath); return reinterpret_cast(update_dialog_gtk_new()); } From ac93a8b06f5b683d3afada1b932394dd2e0a1976 Mon Sep 17 00:00:00 2001 From: Carles Pina Date: Wed, 19 Nov 2014 16:39:32 +0000 Subject: [PATCH 3/8] Uses tabs for indentation. MD-20923 --- src/UpdateDialogGtkFactory.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/UpdateDialogGtkFactory.cpp b/src/UpdateDialogGtkFactory.cpp index 3daaf0e..8bcc56c 100644 --- a/src/UpdateDialogGtkFactory.cpp +++ b/src/UpdateDialogGtkFactory.cpp @@ -35,22 +35,22 @@ UpdateDialogGtk* (*update_dialog_gtk_new)() = 0; bool extractFileFromBinary(int fd, const void* buffer, size_t length) { - size_t count = write(fd,buffer,length); - close(fd); - return count >= length; + size_t count = write(fd,buffer,length); + close(fd); + return count >= length; } UpdateDialog* UpdateDialogGtkFactory::createDialog() { - char* libPath = (char*)malloc(MAX_FILE_PATH); - snprintf(libPath, MAX_FILE_PATH, "/tmp/mendeley-libUpdaterGtk.so.XXXXXX"); + char* libPath = (char*)malloc(MAX_FILE_PATH); + snprintf(libPath, MAX_FILE_PATH, "/tmp/mendeley-libUpdaterGtk.so.XXXXXX"); - int libFd = mkostemp(libPath, O_CREAT | O_WRONLY | O_TRUNC); - if (libFd == -1) - { - LOG(Warn,"Failed to create temporary file - " + std::string(strerror(errno))); - return 0; - } + int libFd = mkostemp(libPath, O_CREAT | O_WRONLY | O_TRUNC); + if (libFd == -1) + { + LOG(Warn,"Failed to create temporary file - " + std::string(strerror(errno))); + return 0; + } if (!extractFileFromBinary(libFd,libupdatergtk_so,libupdatergtk_so_len)) { @@ -67,6 +67,6 @@ UpdateDialog* UpdateDialogGtkFactory::createDialog() BIND_FUNCTION(gtkLib,update_dialog_gtk_new); - FileUtils::removeFile(libPath); + FileUtils::removeFile(libPath); return reinterpret_cast(update_dialog_gtk_new()); } From 0ef971a251ee4a5f9fc2effdd994d4597a968482 Mon Sep 17 00:00:00 2001 From: Carles Pina Date: Thu, 20 Nov 2014 12:00:01 +0000 Subject: [PATCH 4/8] Uses std::string MD-20923 --- src/UpdateDialogGtkFactory.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/UpdateDialogGtkFactory.cpp b/src/UpdateDialogGtkFactory.cpp index 8bcc56c..2680bfd 100644 --- a/src/UpdateDialogGtkFactory.cpp +++ b/src/UpdateDialogGtkFactory.cpp @@ -31,8 +31,6 @@ UpdateDialogGtk* (*update_dialog_gtk_new)() = 0; #define BIND_FUNCTION(library,function) \ function = reinterpret_cast(dlsym(library,#function)); -#define MAX_FILE_PATH 4096 - bool extractFileFromBinary(int fd, const void* buffer, size_t length) { size_t count = write(fd,buffer,length); @@ -42,8 +40,7 @@ bool extractFileFromBinary(int fd, const void* buffer, size_t length) UpdateDialog* UpdateDialogGtkFactory::createDialog() { - char* libPath = (char*)malloc(MAX_FILE_PATH); - snprintf(libPath, MAX_FILE_PATH, "/tmp/mendeley-libUpdaterGtk.so.XXXXXX"); + char* libPath = strdup(std::string("/tmp/mendeley-libUpdaterGtk.so.XXXXXX").c_str()); int libFd = mkostemp(libPath, O_CREAT | O_WRONLY | O_TRUNC); if (libFd == -1) From 3904349a065aef4e450e0f3f7f45bfdea6708299 Mon Sep 17 00:00:00 2001 From: Carles Pina Date: Thu, 20 Nov 2014 12:03:38 +0000 Subject: [PATCH 5/8] Simplifies creating the char*, avoids using useless std::string MD-20923 --- src/UpdateDialogGtkFactory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/UpdateDialogGtkFactory.cpp b/src/UpdateDialogGtkFactory.cpp index 2680bfd..f3a4d6c 100644 --- a/src/UpdateDialogGtkFactory.cpp +++ b/src/UpdateDialogGtkFactory.cpp @@ -40,7 +40,7 @@ bool extractFileFromBinary(int fd, const void* buffer, size_t length) UpdateDialog* UpdateDialogGtkFactory::createDialog() { - char* libPath = strdup(std::string("/tmp/mendeley-libUpdaterGtk.so.XXXXXX").c_str()); + char* libPath = strdup("/tmp/mendeley-libUpdaterGtk.so.XXXXXX"); int libFd = mkostemp(libPath, O_CREAT | O_WRONLY | O_TRUNC); if (libFd == -1) From 55f8918641e825a08fa32053965d77739a27cf54 Mon Sep 17 00:00:00 2001 From: Carles Pina Date: Thu, 20 Nov 2014 12:45:04 +0000 Subject: [PATCH 6/8] Uses stack-allocated array. MD-20923 --- src/UpdateDialogGtkFactory.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/UpdateDialogGtkFactory.cpp b/src/UpdateDialogGtkFactory.cpp index f3a4d6c..b56745d 100644 --- a/src/UpdateDialogGtkFactory.cpp +++ b/src/UpdateDialogGtkFactory.cpp @@ -11,6 +11,7 @@ #include #include #include +#include class UpdateDialogGtk; @@ -31,6 +32,8 @@ UpdateDialogGtk* (*update_dialog_gtk_new)() = 0; #define BIND_FUNCTION(library,function) \ function = reinterpret_cast(dlsym(library,#function)); +#define MAX_FILE_PATH 4096 + bool extractFileFromBinary(int fd, const void* buffer, size_t length) { size_t count = write(fd,buffer,length); @@ -40,7 +43,8 @@ bool extractFileFromBinary(int fd, const void* buffer, size_t length) UpdateDialog* UpdateDialogGtkFactory::createDialog() { - char* libPath = strdup("/tmp/mendeley-libUpdaterGtk.so.XXXXXX"); + char libPath[MAX_FILE_PATH]; + strncpy(libPath, "/tmp/mendeley-libUpdaterGtk.so.XXXXXX", MAX_FILE_PATH); int libFd = mkostemp(libPath, O_CREAT | O_WRONLY | O_TRUNC); if (libFd == -1) From c24408d2556ae1ec4c9676e4e57825978bb5dfcb Mon Sep 17 00:00:00 2001 From: Carles Pina Date: Thu, 20 Nov 2014 15:36:14 +0000 Subject: [PATCH 7/8] Close the file descriptor in the same method that had been opened. MD-20923 --- src/UpdateDialogGtkFactory.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/UpdateDialogGtkFactory.cpp b/src/UpdateDialogGtkFactory.cpp index b56745d..63612e8 100644 --- a/src/UpdateDialogGtkFactory.cpp +++ b/src/UpdateDialogGtkFactory.cpp @@ -37,7 +37,6 @@ UpdateDialogGtk* (*update_dialog_gtk_new)() = 0; bool extractFileFromBinary(int fd, const void* buffer, size_t length) { size_t count = write(fd,buffer,length); - close(fd); return count >= length; } @@ -58,6 +57,7 @@ UpdateDialog* UpdateDialogGtkFactory::createDialog() LOG(Warn,"Failed to load the GTK UI library - " + std::string(strerror(errno))); return 0; } + close(libFd); void* gtkLib = dlopen(libPath,RTLD_LAZY); if (!gtkLib) From 8e4df618c3da74b27254b6f677202c76ffe93dc0 Mon Sep 17 00:00:00 2001 From: Carles Pina Date: Thu, 20 Nov 2014 16:30:26 +0000 Subject: [PATCH 8/8] Deletes non-needed include. MD-20923 --- src/UpdateDialogGtkFactory.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/UpdateDialogGtkFactory.cpp b/src/UpdateDialogGtkFactory.cpp index 63612e8..c62e129 100644 --- a/src/UpdateDialogGtkFactory.cpp +++ b/src/UpdateDialogGtkFactory.cpp @@ -11,7 +11,6 @@ #include #include #include -#include class UpdateDialogGtk;