From: Mikko Rasa Date: Sat, 21 Jul 2012 13:49:12 +0000 (+0300) Subject: Handle directory creation and unlinking in Task X-Git-Url: http://git.tdb.fi/?p=builder.git;a=commitdiff_plain;h=33d74db3e99f35a8984d4ad9b703f709d07d44c5 Handle directory creation and unlinking in Task --- diff --git a/source/externaltask.cpp b/source/externaltask.cpp index 5f5d394..643831e 100644 --- a/source/externaltask.cpp +++ b/source/externaltask.cpp @@ -50,6 +50,8 @@ void ExternalTask::start() if(stdout_dest==CAPTURE || stderr_dest==CAPTURE) capture_pipe = new IO::Pipe; + prepare(); + if((pid = fork())) { if(pid==-1) diff --git a/source/filetarget.cpp b/source/filetarget.cpp index 3d80147..fca8cea 100644 --- a/source/filetarget.cpp +++ b/source/filetarget.cpp @@ -6,6 +6,7 @@ #include "builder.h" #include "filetarget.h" #include "sourcepackage.h" +#include "task.h" #include "tool.h" using namespace std; @@ -110,10 +111,10 @@ string FileTarget::create_build_signature() const Task *FileTarget::build() { - if(tool && !builder.get_dry_run() && mtime) - FS::unlink(path); - - return Target::build(); + Task *task = Target::build(); + task->set_file(path); + task->set_unlink(true); + return task; } void FileTarget::build_finished(bool success) diff --git a/source/gnuarchiver.cpp b/source/gnuarchiver.cpp index 168a163..3499204 100644 --- a/source/gnuarchiver.cpp +++ b/source/gnuarchiver.cpp @@ -63,12 +63,5 @@ Task *GnuArchiver::run(const Target &target) const if(ObjectFile *obj = dynamic_cast(*i)) argv.push_back(relative(obj->get_path(), work_dir).str()); - if(!builder.get_dry_run()) - { - FS::mkpath(FS::dirname(lib.get_path()), 0755); - if(FS::exists(lib.get_path())) - FS::unlink(lib.get_path()); - } - return new ExternalTask(argv, work_dir); } diff --git a/source/gnucompiler.cpp b/source/gnucompiler.cpp index a73fad7..43e8e30 100644 --- a/source/gnucompiler.cpp +++ b/source/gnucompiler.cpp @@ -136,8 +136,5 @@ Task *GnuCompiler::run(const Target &target) const argv.push_back(relative(obj_path, work_dir).str()); argv.push_back(relative(src_path, work_dir).str()); - if(!builder.get_dry_run()) - FS::mkpath(FS::dirname(obj_path), 0755); - return new ExternalTask(argv, work_dir); } diff --git a/source/gnulinker.cpp b/source/gnulinker.cpp index 6ea8871..7c3f872 100644 --- a/source/gnulinker.cpp +++ b/source/gnulinker.cpp @@ -176,8 +176,5 @@ Task *GnuLinker::Linker::run(const Target &target) const if(static_link_ok) argv.push_back("-static"); - if(!builder.get_dry_run()) - FS::mkpath(FS::dirname(bin.get_path()), 0755); - return new ExternalTask(argv, work_dir); } diff --git a/source/internaltask.cpp b/source/internaltask.cpp index bcea591..aa10649 100644 --- a/source/internaltask.cpp +++ b/source/internaltask.cpp @@ -12,6 +12,7 @@ InternalTask::~InternalTask() void InternalTask::start() { + prepare(); worker->launch(); } diff --git a/source/task.cpp b/source/task.cpp new file mode 100644 index 0000000..f6fa7b2 --- /dev/null +++ b/source/task.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include "task.h" + +using namespace Msp; + +Task::Task(): + unlink(false) +{ } + +void Task::set_file(const FS::Path &f) +{ + file = f; +} + +void Task::set_unlink(bool u) +{ + unlink = u; +} + +void Task::prepare() +{ + if(!file.empty()) + { + if(FS::exists(file)) + { + // If the file exists, the directory it's in must exist too + FS::unlink(file); + } + else + { + FS::Path dir = FS::dirname(file); + if(!FS::exists(dir)) + FS::mkpath(dir, 0755); + } + } +} diff --git a/source/task.h b/source/task.h index e7e37e3..0732453 100644 --- a/source/task.h +++ b/source/task.h @@ -3,6 +3,7 @@ #include #include +#include /** Tasks are used to manage other programs and worker threads involved in the @@ -21,11 +22,21 @@ public: sigc::signal signal_finished; + Msp::FS::Path file; + bool unlink; + protected: - Task() { } + Task(); public: virtual ~Task() { } + /** Associate the task with a file. */ + void set_file(const Msp::FS::Path &); + + /** If set to true, the associated file is removed before the task is + started. */ + void set_unlink(bool = true); + /** Returns the command being executed for this task. Only makes sense if an external command is involved. */ virtual std::string get_command() const = 0; @@ -33,6 +44,12 @@ public: /// Starts the task. virtual void start() = 0; +protected: + /** Ensures that the output directory exists and removes the file if + necessary. */ + void prepare(); + +public: /// Checks the status of the task and immediately returns. virtual Status check() = 0;