From: Mikko Rasa Date: Mon, 9 Jul 2012 14:45:46 +0000 (+0300) Subject: Add an example that does some directory operations X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=f1e97209d74290a88639cd2895a7b4a86ffee52e;p=libs%2Fcore.git Add an example that does some directory operations --- diff --git a/Build b/Build index fc7b33a..8478fc5 100644 --- a/Build +++ b/Build @@ -102,6 +102,15 @@ package "mspcore" }; }; + program "syncdir" + { + source "examples/syncdir.cpp"; + build_info + { + library "mspcore"; + }; + }; + tarball "@src" { source "License.txt"; diff --git a/examples/syncdir.cpp b/examples/syncdir.cpp new file mode 100644 index 0000000..7d4789a --- /dev/null +++ b/examples/syncdir.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace Msp; + +class SyncDir: public RegisteredApplication +{ +private: + FS::Path source; + FS::Path destination; + +public: + SyncDir(int, char **); + + int main(); +private: + void sync_directory(const FS::Path &, const FS::Path &); + void copy_file(const FS::Path &, const FS::Path &); +}; + +SyncDir::SyncDir(int argc, char **argv) +{ + if(argc<3) + throw usage_error("Missing arguments", format("Usage: %s ", argv[0])); + + source = argv[1]; + destination = argv[2]; +} + +int SyncDir::main() +{ + sync_directory(source, destination); + return 0; +} + +void SyncDir::sync_directory(const FS::Path &src, const FS::Path &dest) +{ + IO::print("Syncing %s to %s\n", src, dest); + + FS::Stat st = FS::stat(dest); + if(!st) + FS::mkpath(dest, 0755); + else if(!st.is_directory()) + { + FS::unlink(dest); + FS::mkdir(dest, 0755); + } + + list src_files = FS::list_files(src); + for(list::const_iterator i=src_files.begin(); i!=src_files.end(); ++i) + { + const string &fn = *i; + FS::Stat ss = FS::stat(src/fn); + if(ss.is_directory()) + sync_directory(src/fn, dest/fn); + else + { + FS::Stat ds = FS::stat(dest/fn); + if(!ds || ds.get_size()!=ss.get_size()) + copy_file(src/fn, dest/fn); + } + } + + list dest_files = FS::list_files(dest); + for(list::const_iterator i=dest_files.begin(); i!=dest_files.end(); ++i) + { + if(find(src_files.begin(), src_files.end(), *i)==src_files.end()) + { + const string &fn = *i; + IO::print("Removing obsolete %s\n", dest/fn); + if(FS::is_dir(dest/fn)) + FS::rmpath(dest/fn); + else + FS::unlink(dest/fn); + } + } +} + +void SyncDir::copy_file(const FS::Path &src, const FS::Path &dest) +{ + IO::File in(src.str(), IO::M_READ); + IO::File out(dest.str(), IO::M_WRITE); + char buf[16384]; + while(!in.eof()) + { + unsigned len = in.read(buf, sizeof(buf)); + out.write(buf, len); + } +}