diff --git a/src/FileUtils.cpp b/src/FileUtils.cpp index 9028663..0e41856 100644 --- a/src/FileUtils.cpp +++ b/src/FileUtils.cpp @@ -419,6 +419,39 @@ bool FileUtils::isRelative(const char* path) #endif } +void FileUtils::copyFile(const char* src, const char* dest) throw (IOException) +{ +#ifdef PLATFORM_UNIX + std::ifstream inputFile(src,std::ios::binary); + std::ofstream outputFile(dest,std::ios::binary | std::ios::trunc); + + if (!inputFile.good()) + { + throw IOException("Failed to read file " + std::string(src)); + } + if (!outputFile.good()) + { + throw IOException("Failed to write file " + std::string(dest)); + } + + outputFile << inputFile.rdbuf(); + + if (inputFile.bad()) + { + throw IOException("Error reading file " + std::string(src)); + } + if (outputFile.bad()) + { + throw IOException("Error writing file " + std::string(dest)); + } +#else + if (!CopyFile(src,dest,FALSE)) + { + throw IOException("Failed to copy " + std::string(src) + " to " + std::string(dest)); + } +#endif +} + std::string FileUtils::makeAbsolute(const char* path, const char* basePath) { if (isRelative(path)) diff --git a/src/FileUtils.h b/src/FileUtils.h index fd2c74c..2ddad18 100644 --- a/src/FileUtils.h +++ b/src/FileUtils.h @@ -55,6 +55,7 @@ class FileUtils static void rmdir(const char* dir) throw (IOException); static void createSymLink(const char* link, const char* target) throw (IOException); static void touch(const char* path) throw (IOException); + static void copyFile(const char* src, const char* dest) throw (IOException); /** Create all the directories in @p path which do not yet exist. * @p path may be relative or absolute. diff --git a/src/UpdateInstaller.cpp b/src/UpdateInstaller.cpp index e7e45be..24c832a 100644 --- a/src/UpdateInstaller.cpp +++ b/src/UpdateInstaller.cpp @@ -224,15 +224,29 @@ void UpdateInstaller::installFile(const UpdateScriptFile& file) if (target.empty()) { // locate the package containing the file - std::string packageFile = m_packageDir + '/' + file.package + ".zip"; - if (!FileUtils::fileExists(packageFile.c_str())) + if (!file.package.empty()) { - throw "Package file does not exist: " + packageFile; - } + std::string packageFile = m_packageDir + '/' + file.package + ".zip"; + if (!FileUtils::fileExists(packageFile.c_str())) + { + throw "Package file does not exist: " + packageFile; + } - // extract the file from the package and copy it to - // the destination - FileUtils::extractFromZip(packageFile.c_str(),file.path.c_str(),destPath.c_str()); + // extract the file from the package and copy it to + // the destination + FileUtils::extractFromZip(packageFile.c_str(),file.path.c_str(),destPath.c_str()); + } + else + { + // if no package is specified, look for an uncompressed file in the + // root of the package directory + std::string sourceFile = m_packageDir + '/' + FileUtils::fileName(file.path.c_str()); + if (!FileUtils::fileExists(sourceFile.c_str())) + { + throw "Source file does not exist: " + sourceFile; + } + FileUtils::copyFile(sourceFile.c_str(),destPath.c_str()); + } // set the permissions on the newly extracted file FileUtils::chmod(destPath.c_str(),file.permissions); diff --git a/src/tests/file_list.xml b/src/tests/file_list.xml index 21b9ca7..256f3d7 100644 --- a/src/tests/file_list.xml +++ b/src/tests/file_list.xml @@ -24,6 +24,12 @@ app-pkg true + + $UPDATER_FILENAME + $UPDATER_HASH + $UPDATER_SIZE + 0755 + test-dir/app-symlink diff --git a/src/tests/test-update.rb b/src/tests/test-update.rb index db8b7d4..0472f21 100755 --- a/src/tests/test-update.rb +++ b/src/tests/test-update.rb @@ -24,7 +24,8 @@ else end file_list_vars = { - "APP_FILENAME" => APP_NAME + "APP_FILENAME" => APP_NAME, + "UPDATER_FILENAME" => UPDATER_NAME } def replace_vars(src_file,dest_file,vars) @@ -89,7 +90,7 @@ FileUtils.cp("../#{UPDATER_NAME}","#{PACKAGE_DIR}/#{UPDATER_NAME}") # make sure that it looks in the correct directory for # the file_list.xml file and packages # -install_path = File.absolute_path(INSTALL_DIR) +install_path = File.expand_path(INSTALL_DIR) Dir.chdir(INSTALL_DIR) do cmd = "#{PACKAGE_DIR}/#{UPDATER_NAME} --install-dir \"#{install_path}\" --package-dir \"#{PACKAGE_DIR}\" --script file_list.xml" puts "Running '#{cmd}'" @@ -101,7 +102,7 @@ sleep(1) # Check that the app was updated app_path = "#{INSTALL_DIR}/#{APP_NAME}" -output = `#{app_path}` +output = `"#{app_path}"` if (output.strip != "new app starting") throw "Updated app produced unexpected output: #{output}" end diff --git a/src/tests/v2_file_list.xml b/src/tests/v2_file_list.xml index ad40e89..128bd8e 100644 --- a/src/tests/v2_file_list.xml +++ b/src/tests/v2_file_list.xml @@ -40,6 +40,13 @@ app-pkg true + + $UPDATER_FILENAME + $UPDATER_HASH + $UPDATER_SIZE + 0755 + + test-dir/app-symlink