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 |
_foreach (ext::String, atom, user) |
57 |
un[_index] = *atom; |
58 |
_foreach (ext::String, atom, password) |
59 |
pw[_index] = *atom; |
60 |
|
61 |
un[user.GetSize() < size_t(unlen) ? user.GetSize() : unlen - 1] = '\0'; |
62 |
pw[password.GetSize() < size_t(pwlen) ? password.GetSize() : pwlen - 1] = '\0'; |
63 |
} |
64 |
} |
65 |
|
66 |
Syncify::Syncify(const ext::String& local, const ext::String& remote, const ext::String& pattern) : pattern(pattern) |
67 |
{ |
68 |
CheckError(::smbc_init(authenticate, debug ? 2 : 0)); |
69 |
|
70 |
syncify(local, remote); |
71 |
} |
72 |
|
73 |
Syncify::~Syncify() |
74 |
{ |
75 |
::smbc_free_context(::smbc_set_context(NULL), 1); |
76 |
} |
77 |
|
78 |
ext::String Syncify::program(api::GetExecutablePath().GetName()); |
79 |
bool Syncify::debug(false); |
80 |
|
81 |
void Syncify::syncify(const ext::String& local, const ext::String& remote) |
82 |
{ |
83 |
try |
84 |
{ |
85 |
Matcher dots("^\\.{1,2}$"); |
86 |
ext::RedBlackSet<ext::String> directories; |
87 |
int directory(CheckError(::smbc_opendir(remote.NullTerminate()))); |
88 |
|
89 |
for (::smbc_dirent* entity(::smbc_readdir(directory)); entity != NULL; entity = ::smbc_readdir(directory)) switch (entity->smbc_type) |
90 |
{ |
91 |
case SMBC_FILE: |
92 |
if (entity->name == pattern) |
93 |
{ |
94 |
api::Cout << entity->name << ios::NewLine; |
95 |
} |
96 |
break; |
97 |
case SMBC_DIR: |
98 |
case SMBC_LINK: |
99 |
if (entity->name != dots) |
100 |
directories.Insert(entity->name); |
101 |
} |
102 |
|
103 |
::smbc_closedir(directory); |
104 |
|
105 |
_foreach (const ext::RedBlackSet<ext::String>, directory, directories) |
106 |
syncify(local + "/" + *directory, remote + "/" + *directory); |
107 |
} |
108 |
catch (const Error& error) |
109 |
{ |
110 |
api::Cerr << error.GetMessage() << remote << ios::NewLine; |
111 |
} |
112 |
} |