Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
jackeri committed Dec 10, 2014
2 parents f201a6c + a779ecb commit 9b72b1a
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 105 deletions.
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ if (APPLE)
# of the updater binary
set(CMAKE_OSX_ARCHITECTURES i386;x86_64)

# Build the updater so that it works on OS X 10.5 and above.
set(MIN_OSX_VERSION 10.5)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mmacosx-version-min=${MIN_OSX_VERSION}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mmacosx-version-min=${MIN_OSX_VERSION}")
# Build the updater so that it works on OS X 10.6 and above.
if (NOT (DEFINED MIN_OSX_DEPLOYMENT_VERSION))
set(MIN_OSX_DEPLOYMENT_VERSION 10.6)
endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mmacosx-version-min=${MIN_OSX_DEPLOYMENT_VERSION}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mmacosx-version-min=${MIN_OSX_DEPLOYMENT_VERSION}")
endif()

add_subdirectory(src)
Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ if (APPLE)
set(SOURCES ${SOURCES}
MacBundle.cpp
StandardDirs.mm
StlSymbolsLeopard.cpp
UpdateDialogCocoa.mm
mac_dock_icon.cpp
mac_info_plist.cpp)
Expand Down
38 changes: 38 additions & 0 deletions src/FileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,3 +629,41 @@ std::string FileUtils::getcwd() throw (IOException)
#endif
}

bool FileUtils::removeEmptyDirs(const char* path)
{
DirIterator iter(path);
int fileCount = 0;
while (iter.next())
{
if (iter.fileName() == "." || iter.fileName() == "..")
{
continue;
}

if (!iter.isDir() || !removeEmptyDirs(iter.filePath().c_str()))
{
// entry is either not a directory or is a
// directory hierarchy which contains one or more non-directories
// once all empty dirs have been recursively removed
++fileCount;
}
}
if (fileCount == 0)
{
try
{
FileUtils::rmdir(path);
return true;
}
catch (const std::exception& ex)
{
LOG(Error,"Unable to remove empty directory " + std::string(ex.what()));
return false;
}
}
else
{
return false;
}
}

7 changes: 7 additions & 0 deletions src/FileUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,12 @@ class FileUtils

/** Returns the current working directory of the application. */
static std::string getcwd() throw (IOException);

/** Recursively remove all empty directories from the path rooted at
* @p path.
*
* Returns true if @p path was removed.
*/
static bool removeEmptyDirs(const char* path);
};

1 change: 1 addition & 0 deletions src/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

// platform-specific type aliases
#if defined(PLATFORM_UNIX)
#include <unistd.h>
#define PLATFORM_PID pid_t
#else
#define PLATFORM_PID DWORD
Expand Down
6 changes: 6 additions & 0 deletions src/ProcessUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ int ProcessUtils::runElevatedLinux(const std::string& executable,
#endif

#ifdef PLATFORM_MAC

// suppress warning about AuthorizationExecuteWithPriviledges
// being deprecated since OS X 10.7
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
int ProcessUtils::runElevatedMac(const std::string& executable,
const std::list<std::string>& args)
{
Expand Down Expand Up @@ -315,6 +320,7 @@ int ProcessUtils::runElevatedMac(const std::string& executable,
return RunElevatedFailed;
}
}
#pragma clang diagnostic pop
#endif

// convert a list of arguments in a space-separated string.
Expand Down
75 changes: 0 additions & 75 deletions src/StlSymbolsLeopard.cpp

This file was deleted.

41 changes: 25 additions & 16 deletions src/UpdateDialogGtkFactory.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "UpdateDialogGtkFactory.h"

#include "FileUtils.h"
#include "Log.h"
#include "UpdateDialog.h"
#include "StringUtils.h"
Expand All @@ -21,34 +22,41 @@ extern unsigned int libupdatergtk_so_len;
// pointers to helper functions in the GTK updater UI library
UpdateDialogGtk* (*update_dialog_gtk_new)() = 0;

#if __cplusplus >= 201103L
#define TYPEOF(x) decltype(x)
#else
#define TYPEOF(x) typeof(x)
#endif

#define BIND_FUNCTION(library,function) \
function = reinterpret_cast<typeof(function)>(dlsym(library,#function));
function = reinterpret_cast<TYPEOF(function)>(dlsym(library,#function));

#define MAX_FILE_PATH 4096

bool extractFileFromBinary(const char* path, const void* buffer, size_t length)
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;
return count >= length;
}

UpdateDialog* UpdateDialogGtkFactory::createDialog()
{
const char* libPath = "/tmp/libupdatergtk.so";
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)
{
LOG(Warn,"Failed to create temporary file - " + std::string(strerror(errno)));
return 0;
}

if (!extractFileFromBinary(libPath,libupdatergtk_so,libupdatergtk_so_len))
if (!extractFileFromBinary(libFd,libupdatergtk_so,libupdatergtk_so_len))
{
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)
Expand All @@ -58,6 +66,7 @@ UpdateDialog* UpdateDialogGtkFactory::createDialog()
}

BIND_FUNCTION(gtkLib,update_dialog_gtk_new);

FileUtils::removeFile(libPath);
return reinterpret_cast<UpdateDialog*>(update_dialog_gtk_new());
}

4 changes: 4 additions & 0 deletions src/UpdateInstaller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,10 @@ void UpdateInstaller::postInstallUpdate()
// Info.plist file.
FileUtils::touch(m_installDir.c_str());
#endif

// recursively remove any empty directories in the installation dir
// remove any empty directories in the installation dir
FileUtils::removeEmptyDirs(m_installDir.c_str());
}

void UpdateInstaller::setAutoClose(bool autoClose)
Expand Down
6 changes: 0 additions & 6 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/..")

if (APPLE)
set(HELPER_SHARED_SOURCES ../StlSymbolsLeopard.cpp)
endif()

# Create helper binaries for unit tests
add_executable(oldapp
old_app.cpp
${HELPER_SHARED_SOURCES}
)
add_executable(newapp
new_app.cpp
${HELPER_SHARED_SOURCES}
)

# Install data files required by unit tests
Expand Down
19 changes: 19 additions & 0 deletions src/tests/TestFileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ void TestFileUtils::testStandardDirs()
TEST_COMPARE(FileUtils::fileExists(tmpDir.data()), true);
}

void TestFileUtils::testRemoveEmptyDirs()
{
std::string tmpDir = FileUtils::tempPath();
std::string rootDir = tmpDir + "/TestFileUtils-testRemoveEmptyDirs";
std::string content = "non-empty-file-content";

FileUtils::mkpath((rootDir + "/nested/empty/dir").c_str());
FileUtils::mkpath((rootDir + "/nested/empty2/dir").c_str());
FileUtils::writeFile((rootDir + "/nonempty.txt").c_str(), content.c_str(), content.size());
FileUtils::removeEmptyDirs(rootDir.c_str());

// root dir and the regular file should still exist
TEST_COMPARE(FileUtils::fileExists(rootDir.c_str()), true);
TEST_COMPARE(FileUtils::fileExists((rootDir + "/nonempty.txt").c_str()), true);

// the empty nested directories should have been removed
TEST_COMPARE(FileUtils::fileExists((rootDir + "/nested").c_str()), true);
}

int main(int,char**)
{
TestList<TestFileUtils> tests;
Expand Down
1 change: 1 addition & 0 deletions src/tests/TestFileUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ class TestFileUtils
void testIsRelative();
void testSymlinkFileExists();
void testStandardDirs();
void testRemoveEmptyDirs();
};
2 changes: 1 addition & 1 deletion src/tests/file_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
</file>
</install>
<uninstall>
<!-- TODO - List some files to uninstall here !-->
<file>file-to-uninstall.txt</file>
<file>symlink-to-file-to-uninstall.txt</file>
<file>will-become-empty-after-update/nested/file-to-uninstall.txt</file>
</uninstall>
</update>
6 changes: 6 additions & 0 deletions src/tests/test-update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ def copy_file(src, dest)
create_test_file("#{INSTALL_DIR}/symlink-to-file-to-uninstall.txt", "dummy file. this is a symlink on Unix")
end

# Create a dummy file to uninstall in a directory
# which becomes empty after the update
empty_dir_path = "#{INSTALL_DIR}/will-become-empty-after-update/nested"
FileUtils.mkdir_p(empty_dir_path)
create_test_file("#{empty_dir_path}/file-to-uninstall.txt", "this file and its containing dir should be removed after the update")

# Populate package source dir with files to install
Dir.mkdir(PACKAGE_SRC_DIR)
nested_dir_path = "#{PACKAGE_SRC_DIR}/new-dir/new-dir2"
Expand Down
3 changes: 2 additions & 1 deletion src/tests/v2_file_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<name>test-dir/app-symlink</name>
<target>../app</target>
</file>
<!-- Test file in new directory !-->
<file>
<name>new-dir/new-dir2/new-file.txt</name>
<hash>$TEST_FILENAME</hash>
Expand All @@ -60,8 +61,8 @@
</file>
</install-v3>
<uninstall>
<!-- TODO - List some files to uninstall here !-->
<file>file-to-uninstall.txt</file>
<file>symlink-to-file-to-uninstall.txt</file>
<file>will-become-empty-after-update/nested/file-to-uninstall.txt</file>
</uninstall>
</update>
3 changes: 2 additions & 1 deletion tools/create-packages.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/ruby

require 'digest/sha1'
require 'fileutils'
require 'rubygems'
require 'find'
Expand Down Expand Up @@ -82,7 +83,7 @@ def strip_prefix(string,prefix)
end

def file_sha1(path)
return `sha1sum "#{path}"`.split(' ')[0]
Digest::SHA1.file(path).to_s
end

class UpdateScriptGenerator
Expand Down

0 comments on commit 9b72b1a

Please sign in to comment.