Skip to content

Commit

Permalink
Add UpdateFilesystemServiceJob
Browse files Browse the repository at this point in the history
  • Loading branch information
alufers committed May 7, 2024
1 parent 1c50c13 commit cfde699
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/core/external/bell
4 changes: 3 additions & 1 deletion src/core/main/app/ServiceTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ using namespace euph;

void ServiceJob::reportProgress(std::shared_ptr<euph::Context> ctx,
float progress) {

EUPH_LOG(info, jobTypeName(), "Progress: %f", progress);
// TODO: broadcast event bus messages
}

Expand All @@ -20,7 +22,7 @@ void ServiceTask::runTask() {
try {
EUPH_LOG(info, TAG, "Running job %s",
currentJob->jobTypeName().c_str());
// currentJob->run(ctx);
currentJob->run(ctx.lock());
if (currentJob->nextJob) {
submitJob(std::move(currentJob->nextJob));
}
Expand Down
42 changes: 42 additions & 0 deletions src/core/main/app/UpdateFilesystemServiceJob.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "UpdateFilesystemServiceJob.h"
#include <fmt/core.h>
#include <filesystem>
#include <fstream>
#include "BellTar.h"
#include "ServiceTask.h"

using namespace euph;

UpdateFilesystemServiceJob::UpdateFilesystemServiceJob(std::string archivePath)
: archivePath(archivePath) {}

std::string UpdateFilesystemServiceJob::jobTypeName() {
return "UpdateFilesystemServiceJob";
}

void UpdateFilesystemServiceJob::run(std::shared_ptr<euph::Context> ctx) {
// First determine the size of the file
std::filesystem::path path(archivePath);
if (!std::filesystem::exists(path)) {
throw ServiceJobFailedException(
fmt::format("Archive at '{}' does not exist.", archivePath));
}

size_t fileSize = std::filesystem::file_size(path);
size_t progressReportStep = fileSize / 100; // Report progress every 1%

// Open the file
std::ifstream fileStream(archivePath, std::ios::in | std::ios::binary);

if (!fileStream.is_open()) {
throw ServiceJobFailedException(
fmt::format("Failed to open archive at '{}'.", archivePath));
}
bell::BellTar::reader tarArchive(fileStream);
// Remove directory if it exists
tarArchive.extract_all_files(ctx->rootPath, [&](size_t bytesRead) {
this->reportProgress(ctx, (float)bytesRead / (float)fileSize);
});

// We are done
}
2 changes: 1 addition & 1 deletion src/core/main/app/include/ServiceTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class ServiceTask : public bell::Task {

private:
const char* TAG = "ServiceTask";
std::shared_ptr<euph::Context> ctx;
std::weak_ptr<euph::Context> ctx;
std::unique_ptr<ServiceJob> currentJob;
std::mutex busyMutex;
std::binary_semaphore jobSemaphore;
Expand Down
22 changes: 22 additions & 0 deletions src/core/main/app/include/UpdateFilesystemServiceJob.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <string>
#include "ServiceTask.h"

namespace euph {

/**
* @brief Unpacks a tar archive with a filesystem OTA update.
*
*/
class UpdateFilesystemServiceJob : euph::ServiceJob {
public:
UpdateFilesystemServiceJob(std::string archivePath);
virtual std::string jobTypeName() override;
virtual void run(std::shared_ptr<euph::Context> ctx) override;

private:
std::string archivePath;
};

} // namespace euph

0 comments on commit cfde699

Please sign in to comment.