Skip to content

Commit

Permalink
feat(detect_modifications): quick test based on last write time of files
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Jan 18, 2018
1 parent 97cd661 commit 285fbcc
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
54 changes: 54 additions & 0 deletions src/rime/lever/deployment_tasks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// 2011-12-10 GONG Chen <chen.sst@gmail.com>
//
#include <algorithm>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/uuid/random_generator.hpp>
Expand All @@ -30,6 +31,51 @@ namespace fs = boost::filesystem;

namespace rime {

DetectModifications::DetectModifications(TaskInitializer arg) {
try {
data_dirs_ = boost::any_cast<vector<string>>(arg);
}
catch (const boost::bad_any_cast&) {
LOG(ERROR) << "DetectModifications: invalid arguments.";
}
}

bool DetectModifications::Run(Deployer* deployer) {
time_t last_modified = 0;
try {
for (auto dir : data_dirs_) {
fs::path p = fs::canonical(dir);
last_modified = (std::max)(last_modified, fs::last_write_time(p));
if (fs::is_directory(p)) {
for (fs::directory_iterator iter(p), end; iter != end; ++iter) {
fs::path entry(iter->path());
if (fs::is_regular_file(fs::canonical(entry)) &&
entry.extension().string() == ".yaml" &&
entry.filename().string() != "user.yaml") {
last_modified =
(std::max)(last_modified, fs::last_write_time(entry));
}
}
}
}
} catch(const fs::filesystem_error& ex) {
LOG(ERROR) << "Error reading file information: " << ex.what();
return true;
}

// TODO: store as 64-bit number to avoid the year 2038 problem
int last_build_time = 0;
{
the<Config> user_config(Config::Require("config")->Create("user"));
user_config->GetInt("var/last_build_time", &last_build_time);
}
if (last_modified > (time_t)last_build_time) {
LOG(INFO) << "modifications detected. workspace needs update.";
return true;
}
return false;
}

bool InstallationUpdate::Run(Deployer* deployer) {
LOG(INFO) << "updating rime installation info.";
fs::path shared_data_path(deployer->shared_data_dir);
Expand Down Expand Up @@ -188,6 +234,11 @@ bool WorkspaceUpdate::Run(Deployer* deployer) {
}
LOG(INFO) << "finished updating schemas: "
<< success << " success, " << failure << " failure.";

the<Config> user_config(Config::Require("config")->Create("user"));
// TODO: store as 64-bit number to avoid the year 2038 problem
user_config->SetInt("var/last_build_time", (int)time(NULL));

return failure == 0;
}

Expand Down Expand Up @@ -263,6 +314,7 @@ bool SchemaUpdate::Run(Deployer* deployer) {
LOG(INFO) << "patched copy of schema '" << schema_id
<< "' is moved to trash";
}
// TODO: compile the config file if needs update

string dict_name;
if (!config->GetString("translator/dictionary", &dict_name)) {
Expand All @@ -281,6 +333,7 @@ bool SchemaUpdate::Run(Deployer* deployer) {
if (verbose_) {
dict_compiler.set_options(DictCompiler::kRebuild | DictCompiler::kDump);
}
// TODO: use compiled schema instead of the YAML file alone
if (!dict_compiler.Compile(schema_file_)) {
LOG(ERROR) << "dictionary '" << dict_name << "' failed to compile.";
return false;
Expand Down Expand Up @@ -317,6 +370,7 @@ bool ConfigFileUpdate::Run(Deployer* deployer) {
trash)) {
LOG(INFO) << "patched copy of '" << file_name_ << "' is moved to trash.";
}
// TODO: compile the config file if needs update
return true;
}

Expand Down
14 changes: 13 additions & 1 deletion src/rime/lever/deployment_tasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@
#ifndef RIME_DEPLOYMENT_TASKS_H_
#define RIME_DEPLOYMENT_TASKS_H_

#include <rime/common.h>
#include <rime/deployer.h>

namespace rime {

// initialize/update installation.yaml, default.yaml
// detects changes in either user configuration or upgraded shared data
class DetectModifications : public DeploymentTask {
public:
DetectModifications(TaskInitializer arg = TaskInitializer());
// Unlike other tasks, its return value indicates whether modifications
// has been detected and workspace needs update.
bool Run(Deployer* deployer);
protected:
vector<string> data_dirs_;
};

// initialize/update installation.yaml
class InstallationUpdate : public DeploymentTask {
public:
InstallationUpdate(TaskInitializer arg = TaskInitializer()) {}
Expand Down
1 change: 1 addition & 0 deletions src/rime/lever/levers_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ static void rime_levers_initialize() {
Registry& r = Registry::instance();

// deployment tools
r.Register("detect_modifications", new Component<DetectModifications>);
r.Register("installation_update", new Component<InstallationUpdate>);
r.Register("workspace_update", new Component<WorkspaceUpdate>);
r.Register("schema_update", new Component<SchemaUpdate>);
Expand Down
5 changes: 3 additions & 2 deletions src/rime_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ RIME_API Bool RimeStartMaintenance(Bool full_check) {
}
if (!full_check) {
TaskInitializer args{
make_pair<string, string>("default.yaml", "config_version")};
if (!deployer.RunTask("config_file_update", args)) {
vector<string>{deployer.user_data_dir, deployer.shared_data_dir}
};
if (!deployer.RunTask("detect_modifications", args)) {
return False;
}
LOG(INFO) << "changes detected; starting maintenance.";
Expand Down

0 comments on commit 285fbcc

Please sign in to comment.