1 |
// Syncify |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include "Syncify.hpp" |
8 |
|
9 |
#include <menes-api/exename.hpp> |
10 |
#include <menes-app/simple.hpp> |
11 |
|
12 |
#include <stdio.h> |
13 |
|
14 |
int Main(const app::Options& options) |
15 |
{ |
16 |
ext::String local("."), remote, pattern("^.*$"); |
17 |
|
18 |
_foreach (const app::ArgumentList, arg, app::GetArguments()) |
19 |
{ |
20 |
Matcher matcher; |
21 |
|
22 |
if (*arg == matcher("^-local=(.+)$")) |
23 |
local = matcher[1]; |
24 |
else if (*arg == matcher("^-remote=(.+)$")) |
25 |
remote = matcher[1]; |
26 |
else if (*arg == matcher("^-pattern=(.+)$")) |
27 |
pattern = matcher[1]; |
28 |
else if (*arg == "-D") |
29 |
Syncify::debug = true; |
30 |
else |
31 |
{ |
32 |
api::Cerr << "Usage: " << Syncify::program << " [-local=local] -remote=remote [-pattern=pattern] [-D]" << ios::NewLine; |
33 |
|
34 |
return 1; |
35 |
} |
36 |
} |
37 |
|
38 |
if (remote.IsEmpty()) |
39 |
{ |
40 |
api::Cerr << "Usage: " << Syncify::program << " [-local=local] -remote=remote [-pattern=pattern] [-D]" << ios::NewLine; |
41 |
|
42 |
return 1; |
43 |
} |
44 |
|
45 |
Syncify syncify(local, remote, pattern); |
46 |
|
47 |
return 0; |
48 |
} |
49 |
|
50 |
extern "C" |
51 |
{ |
52 |
void authenticate(const char* srv, const char* shr, char* wg, int wglen, char* un, int unlen, char* pw, int pwlen) |
53 |
{ |
54 |
static ext::String user("Douglas Thrift"), password(::getpass("Password:")); |
55 |
|
56 |
::snprintf(un, unlen, "%s", user.NullTerminate()); |
57 |
::snprintf(pw, pwlen, "%s", password.NullTerminate()); |
58 |
} |
59 |
} |
60 |
|
61 |
Syncify::Syncify(const ext::String& local, const ext::String& remote, const ext::String& pattern) : pattern(pattern) |
62 |
{ |
63 |
CheckError(::smbc_init(authenticate, debug ? 2 : 0)); |
64 |
|
65 |
syncify(local, remote); |
66 |
} |
67 |
|
68 |
Syncify::~Syncify() |
69 |
{ |
70 |
::smbc_free_context(::smbc_set_context(NULL), 1); |
71 |
} |
72 |
|
73 |
ext::String Syncify::program(api::GetExecutablePath().GetName()); |
74 |
bool Syncify::debug(false); |
75 |
|
76 |
void Syncify::syncify(const ext::String& local, const ext::String& remote) |
77 |
{ |
78 |
Matcher dots("^\\.{1,2}$"); |
79 |
ext::RedBlackSet<ext::String> directories; |
80 |
int directory(CheckError(::smbc_opendir(remote.NullTerminate()))); |
81 |
|
82 |
for (::smbc_dirent* entity(::smbc_readdir(directory)); entity != NULL; entity = ::smbc_readdir(directory)) switch (entity->smbc_type) |
83 |
{ |
84 |
case SMBC_FILE: |
85 |
if (entity->name == pattern) |
86 |
{ |
87 |
api::Cout << entity->name << ios::NewLine; |
88 |
} |
89 |
break; |
90 |
case SMBC_DIR: |
91 |
case SMBC_LINK: |
92 |
if (entity->name != dots) |
93 |
directories.Insert(entity->name); |
94 |
} |
95 |
|
96 |
::smbc_closedir(directory); |
97 |
|
98 |
_foreach (const ext::RedBlackSet<ext::String>, directory, directories) |
99 |
syncify(local + "/" + *directory, remote + "/" + *directory); |
100 |
} |