ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Syncify/Syncify.cpp
Revision: 440
Committed: 2005-04-07T22:53:00-07:00 (20 years, 2 months ago) by douglas
File size: 2902 byte(s)
Log Message:
Worky!

File Contents

# User Rev Content
1 douglas 435 // Syncify
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7     #include "Syncify.hpp"
8 douglas 440 #include "SambaReader.hpp"
9 douglas 435
10     #include <menes-api/exename.hpp>
11     #include <menes-app/simple.hpp>
12    
13 douglas 436 #include <stdio.h>
14    
15 douglas 435 int Main(const app::Options& options)
16     {
17 douglas 436 ext::String local("."), remote, pattern("^.*$");
18 douglas 435
19     _foreach (const app::ArgumentList, arg, app::GetArguments())
20     {
21 douglas 436 Matcher matcher;
22    
23     if (*arg == matcher("^-local=(.+)$"))
24     local = matcher[1];
25     else if (*arg == matcher("^-remote=(.+)$"))
26     remote = matcher[1];
27     else if (*arg == matcher("^-pattern=(.+)$"))
28     pattern = matcher[1];
29     else if (*arg == "-D")
30 douglas 435 Syncify::debug = true;
31     else
32     {
33 douglas 436 api::Cerr << "Usage: " << Syncify::program << " [-local=local] -remote=remote [-pattern=pattern] [-D]" << ios::NewLine;
34 douglas 435
35     return 1;
36     }
37     }
38    
39 douglas 436 if (remote.IsEmpty())
40     {
41     api::Cerr << "Usage: " << Syncify::program << " [-local=local] -remote=remote [-pattern=pattern] [-D]" << ios::NewLine;
42    
43     return 1;
44     }
45    
46     Syncify syncify(local, remote, pattern);
47 douglas 435
48     return 0;
49     }
50    
51     extern "C"
52     {
53 douglas 436 void authenticate(const char* srv, const char* shr, char* wg, int wglen, char* un, int unlen, char* pw, int pwlen)
54     {
55 douglas 438 static ext::Buffer user("Douglas Thrift"), password(::getpass("Password:"));
56 douglas 436
57 douglas 438 _foreach (ext::Buffer, atom, user)
58 douglas 437 un[_index] = *atom;
59 douglas 438 _foreach (ext::Buffer, atom, password)
60 douglas 437 pw[_index] = *atom;
61    
62     un[user.GetSize() < size_t(unlen) ? user.GetSize() : unlen - 1] = '\0';
63     pw[password.GetSize() < size_t(pwlen) ? password.GetSize() : pwlen - 1] = '\0';
64 douglas 436 }
65 douglas 435 }
66    
67 douglas 436 Syncify::Syncify(const ext::String& local, const ext::String& remote, const ext::String& pattern) : pattern(pattern)
68     {
69     CheckError(::smbc_init(authenticate, debug ? 2 : 0));
70    
71     syncify(local, remote);
72     }
73    
74     Syncify::~Syncify()
75     {
76     ::smbc_free_context(::smbc_set_context(NULL), 1);
77     }
78    
79 douglas 435 ext::String Syncify::program(api::GetExecutablePath().GetName());
80     bool Syncify::debug(false);
81 douglas 436
82 douglas 440 void Syncify::syncify(const api::Path& local, const ext::String& remote)
83 douglas 436 {
84 douglas 437 try
85     {
86     Matcher dots("^\\.{1,2}$");
87     ext::RedBlackSet<ext::String> directories;
88     int directory(CheckError(::smbc_opendir(remote.NullTerminate())));
89 douglas 436
90 douglas 437 for (::smbc_dirent* entity(::smbc_readdir(directory)); entity != NULL; entity = ::smbc_readdir(directory)) switch (entity->smbc_type)
91 douglas 436 {
92 douglas 437 case SMBC_FILE:
93     if (entity->name == pattern)
94     {
95 douglas 440 api::Path path(local.GetChild(entity->name));
96    
97     if (!path.Exists())
98     {
99     SambaReader samba(remote + "/" + entity->name);
100     api::FileWriter file(path.GetPath());
101    
102     ios::ReadToWrite(samba, file);
103     }
104 douglas 437 }
105     break;
106     case SMBC_DIR:
107     case SMBC_LINK:
108     if (entity->name != dots)
109     directories.Insert(entity->name);
110 douglas 436 }
111    
112 douglas 437 ::smbc_closedir(directory);
113 douglas 436
114 douglas 437 _foreach (const ext::RedBlackSet<ext::String>, directory, directories)
115 douglas 440 syncify(local.GetChild(*directory), remote + "/" + *directory);
116 douglas 437 }
117     catch (const Error& error)
118     {
119     api::Cerr << error.GetMessage() << remote << ios::NewLine;
120     }
121 douglas 436 }

Properties

Name Value
svn:eol-style native
svn:keywords Id