// Syncify // // Douglas Thrift // // $Id$ #include "Syncify.hpp" #include #include #include int Main(const app::Options& options) { ext::String local("."), remote, pattern("^.*$"); _foreach (const app::ArgumentList, arg, app::GetArguments()) { Matcher matcher; if (*arg == matcher("^-local=(.+)$")) local = matcher[1]; else if (*arg == matcher("^-remote=(.+)$")) remote = matcher[1]; else if (*arg == matcher("^-pattern=(.+)$")) pattern = matcher[1]; else if (*arg == "-D") Syncify::debug = true; else { api::Cerr << "Usage: " << Syncify::program << " [-local=local] -remote=remote [-pattern=pattern] [-D]" << ios::NewLine; return 1; } } if (remote.IsEmpty()) { api::Cerr << "Usage: " << Syncify::program << " [-local=local] -remote=remote [-pattern=pattern] [-D]" << ios::NewLine; return 1; } Syncify syncify(local, remote, pattern); return 0; } extern "C" { void authenticate(const char* srv, const char* shr, char* wg, int wglen, char* un, int unlen, char* pw, int pwlen) { static ext::String user("Douglas Thrift"), password(::getpass("Password:")); ::snprintf(un, unlen, "%s", user.NullTerminate()); ::snprintf(pw, pwlen, "%s", password.NullTerminate()); } } Syncify::Syncify(const ext::String& local, const ext::String& remote, const ext::String& pattern) : pattern(pattern) { CheckError(::smbc_init(authenticate, debug ? 2 : 0)); syncify(local, remote); } Syncify::~Syncify() { ::smbc_free_context(::smbc_set_context(NULL), 1); } ext::String Syncify::program(api::GetExecutablePath().GetName()); bool Syncify::debug(false); void Syncify::syncify(const ext::String& local, const ext::String& remote) { Matcher dots("^\\.{1,2}$"); ext::RedBlackSet directories; int directory(CheckError(::smbc_opendir(remote.NullTerminate()))); for (::smbc_dirent* entity(::smbc_readdir(directory)); entity != NULL; entity = ::smbc_readdir(directory)) switch (entity->smbc_type) { case SMBC_FILE: if (entity->name == pattern) { api::Cout << entity->name << ios::NewLine; } break; case SMBC_DIR: case SMBC_LINK: if (entity->name != dots) directories.Insert(entity->name); } ::smbc_closedir(directory); _foreach (const ext::RedBlackSet, directory, directories) syncify(local + "/" + *directory, remote + "/" + *directory); }