Qore SftpPoller Module Reference  1.1
SftpPoller.qm.dox.h
1 // -*- mode: c++; indent-tabs-mode: nil -*-
3 
4 /* SftpPoller.qm Copyright (C) 2014 - 2016 David Nichols
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23 */
24 
25 // make sure we have the required qore version
26 
27 // requires ssh2 functionality
28 
29 // assume local vars and do not allow $ chars
30 
31 
32 /* Version History
33  * 2017-02-02 v1.1: Pavel Kveton <pavel.kveton@qoretechnologies.org>
34  * 2014-03-01 v1.0: David Nichols <david@qore.org>: initial version
35 */
36 
100 namespace SftpPoller {
103  class SftpPoller {
104 
105 public:
106  public :
108  const OrderAsc = 0;
110  const OrderDesc = 1;
111 
113  const SortNone = 0;
115  const SortName = 1;
116  //const SortDate = 2;
117 
119  const RequiredKeys = (
120  "sleep",
121  "start_thread",
122  );
123 
125  const RequiredKeysWithHost = RequiredKeys + "host";
126 
128  const Defaults = (
129  "port": 22,
130  "mask": "*",
131  "poll_interval": 10,
132  "reopts": 0,
133  "timeout": 30s,
134  "binary": False,
135  "writable": False,
136  "check_file": "qore-sftp-check-file",
137  );
138 
140  const OptionalKeys = (
141  "path", "user", "pass", "keyfile", "regex_mask", "minage",
142  "encoding", "log_info", "log_detail", "log_debug", "binary",
143  "sleep",
144  "start_thread",
145  "skip_file_content_retrieval",
146  );
147 
149  const AllKeys = RequiredKeysWithHost + Defaults.keys() + OptionalKeys;
150 
152  const ErrorDelay = 1m;
153 
154 public:
155 
156  private :
158  string host;
159 
161  int port;
162 
164  string user;
165 
167  string url;
168 
170  *string pass;
171 
173  *string keyfile;
174 
176  softlist path = ".";
177 
179  string rootSftpPath;
180 
182  *string mask;
183 
186 
188  bool runflag = False;
189 
191  bool get_files;
192 
194  bool fatal = False;
195 
197  int pollcnt = 0;
198 
200  Mutex m();
201 
203  int tid;
204 
206  timeout timeout;
207 
209  Counter sc();
210 
213 
215  int reopts = 0;
216 
218  *softint minage;
219 
221  *string encoding;
222 
224  *code log_info;
225 
227  *code log_detail;
228 
230  *code log_debug;
231 
234 
236  *code sleep;
237 
239  bool binary;
240 
242  bool writable;
243 
245  string check_file;
246 
249 
250 public:
251 
253 
280  constructor(Qore::SSH2::SFTPClient n_sftp, hash nconf);
281 
282 
284 
315  constructor(hash nconf);
316 
317 
319  destructor();
320 
321 
323  int getPollCount();
324 
325 
327 
328 private:
329  logInfo(string fmt);
330 public:
331 
332 
334 
335 private:
336  logDetail(string fmt);
337 public:
338 
339 
341 
342 private:
343  logDebug(string fmt);
344 public:
345 
346 
348 
349 private:
350  setMask();
351 public:
352 
353 
355 
361  int getStoreFile(string remote_path, string local_path, *timeout n_timeout);
362 
363 
365  string getTextFile(string path, *timeout n_timeout, *string n_encoding);
366 
367 
369  binary getFile(string path, *timeout n_timeout);
370 
371 
373  rename(string old, string nnew, *timeout n_timeout);
374 
375 
377  removeFile(string fn, *timeout n_timeout);
378 
379 
381  *hash stat(string path, *timeout n_timeout);
382 
383 
385 
388  checkRemotePath(string path, bool write = False, *timeout n_timeout);
389 
390 
392 
406  list getFiles(int sort = SftpPoller::SortNone, int order = SftpPoller::OrderAsc);
407 
408 
410 
413  int start();
414 
415 
417 
419  stopNoWait();
420 
421 
423 
429  stop();
430 
431 
433 
436  waitStop();
437 
438 
440 
443  startInline();
444 
445 
447  bool runOnce();
448 
449 
451 
452 private:
453  sftpSleep(softint secs);
454 public:
455 
456 
458 
459 private:
460  run();
461 public:
462 
463 
465  *bool fileEvent(list l);
466 
467 
469 
482  abstract singleFileEvent(hash fih);
483 
485 
502  abstract postSingleFileEvent(hash fih);
503 
505 
508  static checkPath(string path, string type, bool write = False);
509  };
510 };
*string pass
password; one of "pass" or "keyfile" *must* be set
Definition: SftpPoller.qm.dox.h:170
int port
port
Definition: SftpPoller.qm.dox.h:161
string rootSftpPath
path after connect to SFTP server
Definition: SftpPoller.qm.dox.h:179
*code log_info
optional info log closure
Definition: SftpPoller.qm.dox.h:224
*code log_detail
optional detail log closure
Definition: SftpPoller.qm.dox.h:227
main SftpPoller namespace
Definition: SftpPoller.qm.dox.h:101
*code sleep
optional sleep closure
Definition: SftpPoller.qm.dox.h:236
string host
host or address name
Definition: SftpPoller.qm.dox.h:158
*softint minage
minimum file age
Definition: SftpPoller.qm.dox.h:218
*string encoding
file encoding for text files
Definition: SftpPoller.qm.dox.h:221
Qore::SSH2::SFTPClient sftp
SFTPClient object.
Definition: SftpPoller.qm.dox.h:212
string check_file
name of check writable file
Definition: SftpPoller.qm.dox.h:245
string url
url
Definition: SftpPoller.qm.dox.h:167
*string keyfile
path to the ssh private key in PEM format; one of "pass" or "keyfile" *must* be set ...
Definition: SftpPoller.qm.dox.h:173
*bool skip_file_content_retrieval
whether or not to skip the file content retrieval in runOnce()
Definition: SftpPoller.qm.dox.h:248
string user
user
Definition: SftpPoller.qm.dox.h:164
bool get_files
internal "get files" flag
Definition: SftpPoller.qm.dox.h:191
*code start_thread
optional start thread closure
Definition: SftpPoller.qm.dox.h:233
*string mask
file glob name mask (ignored if "regex_mask" also set)
Definition: SftpPoller.qm.dox.h:182
int tid
polling tid
Definition: SftpPoller.qm.dox.h:203
bool writable
chech if path is writable for others in constructor
Definition: SftpPoller.qm.dox.h:242
*code log_debug
optional debug log closure
Definition: SftpPoller.qm.dox.h:230
int poll_interval
poll interval in seconds
Definition: SftpPoller.qm.dox.h:185
bool binary
binary transfer flag (for singleFileEvent())
Definition: SftpPoller.qm.dox.h:239
timeout timeout
timeout in ms
Definition: SftpPoller.qm.dox.h:206